Skip to content

Commit

Permalink
fix node not honoring NODE_PRESERVE_SYMLINKS
Browse files Browse the repository at this point in the history
node 10 has --preserve-symlinks-main flag, but we are still supporting 8
  • Loading branch information
jchip committed Jun 4, 2018
1 parent 7f7fc07 commit 7628ad9
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const _ = require("lodash");
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require("fs-extra"));
Expand Down Expand Up @@ -46,12 +48,23 @@ function getAllDefaultMessages(messageFilesPathPattern) {

const writeRawMessages = _.partial(writeFileAsJSON, RAW_MESSAGES_DIR, RAW_MESSAGES_NAME);

Promise.all([getAllDefaultMessages(MESSAGES_PATTERN), fs.ensureDirAsync(RAW_MESSAGES_DIR)])
.then(_.first)
.then(writeRawMessages)
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
});
const isMain = require.main === module;

function flattenMessagesL10n() {
return Promise.all([getAllDefaultMessages(MESSAGES_PATTERN), fs.ensureDirAsync(RAW_MESSAGES_DIR)])
.then(_.first)
.then(writeRawMessages)
.then(() => {
if (isMain) process.exit(0);
})
.catch(err => {
console.log("flatten messages failed", err);
if (isMain) process.exit(1);
});
}

module.exports = flattenMessagesL10n;

if (isMain) {
flattenMessagesL10n();
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
"use strict";

let appDir = process.cwd();
/* eslint-disable max-statements */

const logger = require("electrode-archetype-react-app/lib/logger");
const fs = require("fs");
const Path = require("path");
const isMain = require.main === module;

if (process.argv[2]) {
appDir = process.argv[2];
function exit(code) {
if (isMain) process.exit(code);
}

const fs = require("fs");
const Path = require("path");
const isoConfigFile = Path.join(appDir, ".isomorphic-loader-config.json");
const cdnAssetsFile = Path.join(appDir, "config/assets.json");
const isoConfig = require(isoConfigFile);
const cdnAssets = require(cdnAssetsFile);

const uniques = Object.keys(cdnAssets).reduce((acc, k) => {
const base = Path.dirname(cdnAssets[k]);
acc[base] = acc[base] ? acc[base]++ : 1;
return acc;
}, {});

const paths = Object.keys(uniques);

if (paths.length > 1) {
logger.error("CDN upload files has different base paths");
paths.forEach(p => {
logger.info(` - "${p}"`);
});
process.exit(1);
function mapIsomorphicCdn(appDir) {
appDir = appDir || process.cwd();
const isoConfigFile = Path.join(appDir, ".isomorphic-loader-config.json");
const cdnAssetsFile = Path.join(appDir, "config/assets.json");
const isoConfig = require(isoConfigFile);
const cdnAssets = require(cdnAssetsFile);

const uniques = Object.keys(cdnAssets).reduce((acc, k) => {
const base = Path.dirname(cdnAssets[k]);
acc[base] = acc[base] ? acc[base]++ : 1;
return acc;
}, {});

const paths = Object.keys(uniques);

if (paths.length > 1) {
logger.error("CDN upload files has different base paths");
paths.forEach(p => {
logger.info(` - "${p}"`);
});
exit(1);
}

if (paths.length === 1) {
let pp = paths[0].trim();
pp += pp.endsWith("/") ? "" : "/";
isoConfig.output.publicPath = pp;
fs.writeFileSync(isoConfigFile, JSON.stringify(isoConfig, null, 2));
logger.info(`\n===\nISOMORPHIC loader config publicPath updated to CDN path ${pp}\n`);
} else {
logger.info(`No CDN path found from CDN assets file ${cdnAssetsFile}`);
}

exit(0);
}

if (paths.length === 1) {
let pp = paths[0].trim();
pp += pp.endsWith("/") ? "" : "/";
isoConfig.output.publicPath = pp;
fs.writeFileSync(isoConfigFile, JSON.stringify(isoConfig, null, 2));
logger.info(`\n===\nISOMORPHIC loader config publicPath updated to CDN path ${pp}\n`);
} else {
logger.info(`No CDN path found from CDN assets file ${cdnAssetsFile}`);
if (isMain) {
mapIsomorphicCdn(process.argv[2]);
}

process.exit(0);
module.exports = mapIsomorphicCdn;
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@ const path = require("path");

const assetsPattern = path.resolve("@(dist|dll)/isomorphic-assets.*");
const assetsPath = path.resolve("dist/isomorphic-assets.json");
const isMain = require.main === module;

glob(assetsPattern, (readErr, filenames) => {
const assets = filenames.reduce(
(result, filename) => {
const fileAssets = require(filename);

Object.assign(result.marked, fileAssets.marked);
Object.assign(result.chunks, fileAssets.chunks);

return result;
},
{
chunks: {},
marked: {}
}
);

fs.writeFile(assetsPath, JSON.stringify(assets, null, 2), err => {
if (err) {
throw err;
}
function mergeIsomorphicAssets() {
glob(assetsPattern, (readErr, filenames) => {
const assets = filenames.reduce(
(result, filename) => {
const fileAssets = require(filename);

Object.assign(result.marked, fileAssets.marked);
Object.assign(result.chunks, fileAssets.chunks);

return result;
},
{
chunks: {},
marked: {}
}
);

fs.writeFile(assetsPath, JSON.stringify(assets, null, 2), err => {
if (err) {
throw err;
}
if (isMain) process.exit(0);
});
});
});
}

module.exports = mergeIsomorphicAssets;

if (isMain) {
mergeIsomorphicAssets();
}
17 changes: 7 additions & 10 deletions packages/electrode-archetype-react-app/arch-clap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ if (process.argv[1].indexOf("gulp") >= 0) {
process.exit(1);
}

const mergeIsomorphicAssets = require(`${archetype.devDir}/scripts/merge-isomorphic-assets.js`);
const flattenMessagesL10n = require(`${archetype.devDir}/scripts/l10n/flatten-messages.js`);
const mapIsomorphicCdn = require(`${archetype.devDir}/scripts/map-isomorphic-cdn.js`);

const config = archetype.config;
const mkdirp = devRequire("mkdirp");
const xsh = devRequire("xsh");
Expand Down Expand Up @@ -408,16 +412,9 @@ function makeTasks() {
task: () => shell.rm("-rf", "./tmp")
},

"build-dist:flatten-l10n": {
desc: false,
task: `node ${archetype.devDir}/scripts/l10n/flatten-messages.js`
},

"build-dist:merge-isomorphic-assets": {
desc: false,
task: `node ${archetype.devDir}/scripts/merge-isomorphic-assets.js`
},
"build-dist:flatten-l10n": flattenMessagesL10n,

"build-dist:merge-isomorphic-assets": mergeIsomorphicAssets,
".build-lib": () => undefined,

".check.top.level.babelrc": () => {
Expand Down Expand Up @@ -568,7 +565,7 @@ Individual .babelrc files were generated for you in src/client and src/server
},

"npm:test": ["check"],
"npm:release": `node ${quote(Path.join(archetype.devDir, `scripts/map-isomorphic-cdn.js`))}`,
"npm:release": mapIsomorphicCdn,

server: ["app-server"], // keep old server name for backward compat

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var archDevRequire = require("electrode-archetype-react-component-dev/require");
var _ = archDevRequire("lodash");
var Promise = archDevRequire("bluebird");
Expand All @@ -16,17 +18,19 @@ var RAW_MESSAGES_NAME = "raw-messages.json";
* @return {Promise} A promise that resolves when the file has been written
*/
var writeFileAsJSON = function writeFileAsJSON(filePath, name, contents) {
return Promise.try(function () { return JSON.stringify(contents, null, 2); })
.then(function (result) { return fs.writeFileAsync(filePath + name, result); });
return Promise.try(function() {
return JSON.stringify(contents, null, 2);
}).then(function(result) {
return fs.writeFileAsync(filePath + name, result);
});
};

/**
* @param {String} filePath The file of a file to read
* @return {Promise} A promise that resolves to a POJO with the file's contents
*/
var readFileAsJSON = function readFileAsJSON(filePath) {
return fs.readFileAsync(filePath, "utf8")
.then(JSON.parse);
return fs.readFileAsync(filePath, "utf8").then(JSON.parse);
};

/**
Expand All @@ -39,19 +43,34 @@ var readFileAsJSON = function readFileAsJSON(filePath) {
var getAllDefaultMessages = function getAllDefaultMessages(messageFilesPathPattern) {
return getFilePaths(messageFilesPathPattern)
.map(readFileAsJSON)
.reduce(function (previousValue, defaultMessageDescriptors) {
defaultMessageDescriptors.forEach(function (descriptor) { previousValue[descriptor.id] = descriptor; });
.reduce(function(previousValue, defaultMessageDescriptors) {
defaultMessageDescriptors.forEach(function(descriptor) {
previousValue[descriptor.id] = descriptor;
});
return previousValue;
}, {});
};

var writeRawMessages = _.partial(writeFileAsJSON, RAW_MESSAGES_DIR, RAW_MESSAGES_NAME);

Promise.all([
getAllDefaultMessages(MESSAGES_PATTERN),
fs.ensureDirAsync(RAW_MESSAGES_DIR)
])
.then(_.first)
.then(writeRawMessages)
.then(function () { process.exit(0); })
.catch(function () { process.exit(1); });
const isMain = require.main === module;

function flattenMessagesL10n() {
return Promise.all([getAllDefaultMessages(MESSAGES_PATTERN), fs.ensureDirAsync(RAW_MESSAGES_DIR)])
.then(_.first)
.then(writeRawMessages)
.then(function() {
if (isMain) process.exit(0);
})
.catch(function(err) {
console.log("flatten messages failed", err);

if (isMain) process.exit(1);
});
}

module.exports = flattenMessagesL10n;

if (isMain) {
flattenMessagesL10n();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const chalk = devRequire("chalk");
const Fs = require("fs");
const glob = devRequire("glob");

const flattenMessagesL10n = require(`${archetype.devPath}/scripts/l10n/flatten-messages.js`);

if (process.argv[1].indexOf("gulp") >= 0) {
const cmd = chalk.magenta(`clap ${process.argv.slice(2).join(" ")}`);
console.log(`\nPlease use ${chalk.magenta("clap")} to run archetype commands.`);
Expand Down Expand Up @@ -109,14 +111,16 @@ const tasks = {
"clean-lib",
".tmp-to-lib",
"build-lib:flatten-l10n",
// TODO: fix the badly written and messy copy-as-flow-declaration.js so it can
// deterministically await async complete (and exit if isMain).
// "build-lib:copy-flow",
"build-lib:clean-tmp"
]
},
"babel-src-step": `babel -D src -d .tmplib`,
"build-lib:clean-tmp": () => $$.rm("-rf", "./tmp"),
"build-lib:copy-flow": `node -r ${archetype.devPath}/scripts/copy-as-flow-declaration.js`,
"build-lib:flatten-l10n": `node -r ${archetype.devPath}/scripts/l10n/flatten-messages.js`,
"build-lib:flatten-l10n": flattenMessagesL10n,

"archetype:check": [
"archetype:test-dev-pkg",
Expand Down

0 comments on commit 7628ad9

Please sign in to comment.