Skip to content

Commit

Permalink
feat: support windows shebang (#366)
Browse files Browse the repository at this point in the history
* feat: support windows shebang

* test: handle shebang in windows
  • Loading branch information
vagusX authored Oct 18, 2021
1 parent 43fb55a commit 202c9fe
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const chalk = require('chalk');
const debug = require('debug')('npminstall:bin');
const path = require('path');
const fs = require('mz/fs');
const cmdShim = require('cmd-shim-hotfix');
const normalize = require('npm-normalize-package-bin');
const fixBin = require('bin-links/lib/fix-bin');
const utils = require('./utils');

module.exports = bin;
Expand Down Expand Up @@ -39,7 +39,6 @@ async function bin(parentDir, pkg, pkgDir, options) {
for (const name of names) {
const srcBin = path.join(pkgDir, bins[name]);
const destBin = path.join(binDir, name);
await fs.chmod(srcBin, 0o755);
await linkBin(srcBin, destBin);
if (showBinLog) {
options.console.info('[%s] link %s@ -> %s',
Expand All @@ -61,5 +60,8 @@ async function linkBin(src, dest) {
});
}

return await utils.forceSymlink(src, dest);
await utils.forceSymlink(src, dest);
// ensure that bin are executable and not containing
// windows line-endings(CRLF) on the hashbang line
await fixBin(src, 0o755);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"agentkeepalive": "^4.0.2",
"await-event": "^2.1.0",
"bin-links": "^2.3.0",
"binary-mirror-config": "^1.19.0",
"bytes": "^3.1.0",
"chalk": "^2.4.2",
Expand Down
36 changes: 36 additions & 0 deletions test/bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const umask = process.umask();
const readJSON = require('../lib/utils').readJSON;
const npminstall = require('./npminstall');
const helper = require('./helper');
Expand Down Expand Up @@ -35,4 +36,39 @@ describe('test/bin.test.js', () => {
assert(pkg.name === '@bigfunger/decompress-zip');
assert(fs.existsSync(path.join(root, 'node_modules', '.bin', 'decompress-zip')));
});

it('fix windows hashbang', async () => {
const pkgs = [{ version: '../windows-shebang', type: 'local' }];
await npminstall({
root,
pkgs,
});
const pkg = await readJSON(path.join(root, 'node_modules', 'windows-shebang', 'package.json'));
assert.equal(pkg.name, 'windows-shebang');
assert.equal(pkg.version, '1.0.0');
if (process.platform !== 'win32') {
/* eslint-disable no-bitwise */
assert.equal(fs.statSync(path.join(root, 'node_modules/.bin/crlf')).mode & 0o755, 0o755 & (~umask));
assert.equal(
fs.readFileSync(path.join(root, 'node_modules/.bin/crlf'), 'utf-8'),
'#!/usr/bin/env node\nconsole.log(\'crlf\');\r\n'
);
/* eslint-disable no-bitwise */
assert.equal(fs.statSync(path.join(root, 'node_modules/.bin/lf')).mode & 0o755, 0o755 & (~umask));
assert.equal(
fs.readFileSync(path.join(root, 'node_modules/.bin/lf'), 'utf-8'),
'#!/usr/bin/env node\nconsole.log(\'lf\');\n'
);
} else {
// don't change shebang file line
assert.equal(
fs.readFileSync(path.join(root, 'node_modules/.bin/crlf'), 'utf-8'),
'#!/usr/bin/env node\r\nconsole.log(\'crlf\');\r\n'
);
assert.equal(
fs.readFileSync(path.join(root, 'node_modules/.bin/lf'), 'utf-8'),
'#!/usr/bin/env node\r\nconsole.log(\'lf\');\n'
);
}
});
});
2 changes: 2 additions & 0 deletions test/fixtures/windows-shebang/bin/crlf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('crlf');
2 changes: 2 additions & 0 deletions test/fixtures/windows-shebang/bin/lf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('lf');
8 changes: 8 additions & 0 deletions test/fixtures/windows-shebang/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "windows-shebang",
"version": "1.0.0",
"bin": {
"crlf": "bin/crlf.js",
"lf": "bin/lf.js"
}
}

0 comments on commit 202c9fe

Please sign in to comment.