Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support windows shebang #366

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来这就是之前不支持 windows 的关键原因了。。。

}
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
24 changes: 24 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,27 @@ 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');
/* 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'
);
});
});
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"
}
}