Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions tools/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,41 @@ function remove(files) {
});
}

// Add/update shebang (#!) line
function shebang(line, file) {
var content = fs.readFileSync(file, 'utf8');
var firstLine = content.split(/\n/, 1)[0];
var newContent;
if (firstLine.slice(0, 2) === '#!') {
newContent = line + content.slice(firstLine.length);
} else {
newContent = line + '\n' + content;
}
if (content !== newContent) {
fs.writeFileSync(file, newContent, 'utf8');
}
}

// Run every command in queue, one-by-one
function run() {
var cmd = queue.shift();
if (!cmd) return;

console.log(cmd);
exec(cmd, function(err, stdout, stderr) {
if (stderr) console.error(stderr);
if (err) process.exit(1);

if (Array.isArray(cmd) && cmd[0] instanceof Function) {
var func = cmd[0];
var args = cmd.slice(1);
console.log.apply(null, [func.name].concat(args));
func.apply(null, args);
run();
});
} else {
console.log(cmd);
exec(cmd, function(err, stdout, stderr) {
if (stderr) console.error(stderr);
if (err) process.exit(1);

run();
});
}
}

if (cmd === 'install') {
Expand Down Expand Up @@ -109,6 +132,8 @@ if (cmd === 'install') {
copy('deps/npm', 'lib/node_modules/npm');
queue.push('ln -sf ../lib/node_modules/npm/bin/npm-cli.js ' +
path.join(node_prefix, 'bin/npm'));
queue.push([shebang, '#!' + path.join(node_prefix, 'bin/node'),
path.join(node_prefix, 'lib/node_modules/npm/bin/npm-cli.js')]);
}
} else {
remove([
Expand Down