Skip to content

Commit

Permalink
Replace setup.sh with npm-rebuild.js, and run it on npm install.
Browse files Browse the repository at this point in the history
Implements meteor#6537 (comment)

The setup.sh script was only sometimes written previously, so no existing
deployment logic should rely on it existing.

On the other hand, all apps built by `meteor build` require running
`npm install` in the programs/server/ directory, so the install hook I
added to programs/server/package.json will ensure npm-rebuild.js is
invoked reliably.

Using a pure Node script means this code will work just as well on Windows
as on Linux or Darwin, though Linux is by far the most common deployment
platform for Meteor apps.

TODO Remember to rebuild the dev bundle before the next release!
  • Loading branch information
Ben Newman committed Apr 7, 2016
1 parent 2dd511f commit c18c1f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
3 changes: 3 additions & 0 deletions scripts/dev-bundle-server-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var packageJson = {
name: "meteor-dev-bundle",
// Version is not important but is needed to prevent warnings.
version: "0.0.0",
scripts: {
install: "node npm-rebuild.js"
},
dependencies: {
fibers: fibersVersion,
"meteor-promise": "0.5.1",
Expand Down
20 changes: 9 additions & 11 deletions tools/isobuild/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1817,8 +1817,8 @@ class JsImage {
load.push(loadItem);
});

const setupScriptPieces = [];
const rebuildDirs = Object.create(null);

// node_modules resources from the packages. Due to appropriate
// builder configuration, 'meteor bundle' and 'meteor deploy' copy
// them, and 'meteor run' symlinks them. If these contain
Expand All @@ -1842,18 +1842,15 @@ class JsImage {
}
});

_.each(rebuildDirs, dir => {
setupScriptPieces.push("(cd ", dir, " && npm rebuild)\n");
// This JSON file will be read by npm-rebuild.js, which is executed to
// trigger rebuilds for all non-portable npm packages.
builder.write("npm-rebuilds.json", {
data: new Buffer(
JSON.stringify(Object.keys(rebuildDirs), null, 2) + "\n",
"utf8"
)
});

if (setupScriptPieces.length) {
setupScriptPieces.unshift('#!/usr/bin/env bash\n', 'set -e\n\n');
builder.write('setup.sh', {
data: new Buffer(setupScriptPieces.join(''), 'utf8'),
executable: true
});
}

// Control file
builder.writeJson('program.json', {
format: "javascript-image-pre1",
Expand Down Expand Up @@ -2062,6 +2059,7 @@ class ServerTarget extends JsImageTarget {
"server-json.js",
"mini-files.js",
"npm-require.js",
"npm-rebuild.js",
], function (filename) {
builder.write(filename, {
file: files.pathJoin(
Expand Down
47 changes: 47 additions & 0 deletions tools/static-assets/server/npm-rebuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var path = require("path");
var spawn = require("child_process").spawn;

try {
// This JSON file gets written in meteor/tools/isobuild/bundler.js.
var rebuilds = require("./npm-rebuilds.json");
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
throw e;
}
// If npm-rebuilds.json was not written, assume there is nothing that
// needs to be rebuilt.
}

// Make sure the npm finds this exact version of node in its $PATH.
var PATH = path.dirname(process.execPath) + ":" + process.env.PATH;
var env = Object.create(process.env, {
PATH: { value: PATH }
});

function rebuild(i) {
var dir = rebuilds && rebuilds[i];

if (! dir) {
// Print Node/V8/etc. versions for diagnostic purposes.
spawn("npm", ["version", "--json"], {
stdio: "inherit",
env: env
});

return;
}

spawn("npm", ["rebuild"], {
cwd: path.join(__dirname, dir),
stdio: "inherit",
env: env
}).on("exit", function (code) {
if (code !== 0) {
process.exit(code);
} else {
rebuild(i + 1);
}
});
}

rebuild(0);

0 comments on commit c18c1f5

Please sign in to comment.