Skip to content

Commit

Permalink
fix: use pacote to install git repo deps (#370)
Browse files Browse the repository at this point in the history
Co-authored-by: killa <killa123@126.com>
  • Loading branch information
gemwuu and killagu authored Nov 22, 2021
1 parent 5e633bd commit 7859046
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
63 changes: 3 additions & 60 deletions lib/download/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,23 @@
const path = require('path');
const uuid = require('uuid');
const chalk = require('chalk');
const runscript = require('runscript');
const pacote = require('pacote');
const utils = require('../utils');

module.exports = async (pkg, options) => {
const {
name,
raw,
hosted,
displayName,
} = pkg;

options.gitPackages++;
options.console.warn(chalk.yellow(`[${displayName}] install ${name || ''} from git ${raw}, may be very slow, please keep patience`));
const url = hosted.https();
const branchOrCommit = hosted.committish || 'master';
const cloneDir = path.join(options.storeDir, '.tmp', uuid());
await utils.mkdirp(cloneDir);
try {
let commit;

if (hosted.type === 'github') {
try {
// try download tarball first
// github.com can download tarball via branch, too
await downloadAndExtract(url, branchOrCommit, cloneDir, options);
commit = branchOrCommit;
} catch (err) {
options.console.warn(chalk.yellow(`download tarball failed: ${err.message}, try to clone repository now.`));
await clone(url, branchOrCommit, cloneDir, options);
commit = await getLastCommitHash(cloneDir);
}
} else {
await clone(url, branchOrCommit, cloneDir, options);
commit = await getLastCommitHash(cloneDir);
}

const resolved = `${url}#${commit}`;
const resolveResult = await pacote.extract(raw, cloneDir);
const resolved = resolveResult.resolved;
await utils.addMetaToJSONFile(path.join(cloneDir, 'package.json'), {
_from: raw,
_resolved: resolved,
Expand All @@ -63,40 +43,3 @@ module.exports = async (pkg, options) => {
}
}
};

async function clone(url, branch, target, options) {
const cloneScript = `git clone ${url} ${target}`;
options.console.warn(chalk.yellow(cloneScript));
await runscript(cloneScript, {
stdio: [
'ignore',
'inherit',
'inherit',
],
});

const coScript = `git checkout ${branch}`;
options.console.warn(chalk.yellow(coScript));
await utils.runScript(target, coScript, options);

}

async function downloadAndExtract(url, branch, target, options) {
const downloadUrl = `${url.replace(/\.git$/, '')}/tarball/${branch}`;
const readstream = await utils.getTarballStream(downloadUrl, options);
await utils.mkdirp(target);
await utils.unpack(readstream, target, url);
}

async function getLastCommitHash(target) {
const script = 'git log -1 --format="%H"';
const stdio = await runscript(script, {
cwd: target,
stdio: [
'ignore',
'pipe',
'inherit',
],
});
return stdio.stdout && stdio.stdout.toString().trim() || '';
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"npm-package-arg": "^8.1.5",
"ora": "^3.4.0",
"p-map": "^2.1.0",
"pacote": "^12.0.2",
"runscript": "^1.3.0",
"semver": "^6.0.0",
"tar": "^4.4.8",
Expand All @@ -60,6 +61,7 @@
"egg-bin": "^4.13.1",
"eslint": "7",
"eslint-config-egg": "7",
"eslint-plugin-jsdoc": "^4.1.1",
"git-contributor": "^1.0.10",
"http-proxy": "1",
"mm": "^2.5.0"
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('test/index.test.js', () => {

const bytesPkg = await readJSON(path.join(root, 'node_modules', 'bytes', 'package.json'));
assert.equal(bytesPkg._from, 'bytes@https://github.com/visionmedia/bytes.js.git');
assert(/^https:\/\/github\.com\/visionmedia\/bytes\.js\.git#\w+$/.test(bytesPkg._resolved), bytesPkg._resolved);
assert(/git\+ssh\:\/\/git@github.com\/visionmedia\/bytes\.js\.git#a66d1b578f3e6fceb518f7e6a83827f7b2f17ac1/.test(bytesPkg._resolved));
});
});
});
40 changes: 39 additions & 1 deletion test/installGit.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 coffee = require('coffee');
const fs = require('fs');
const npminstall = require('./npminstall');
const helper = require('./helper');

Expand Down Expand Up @@ -129,7 +130,7 @@ describe('test/installGit.test.js', () => {
],
});
} catch (err) {
assert(/checkout wtf\?\?\?!!!fail-here,hahaa" error/.test(err.message), err.message);
assert(/\[@git\+https\:\/\/github.com\/mozilla\/nunjucks.git#wtf\?\?\?\!\!\!fail-here\,hahaa\] The git reference could not be found/.test(err.message));
}

});
Expand All @@ -148,4 +149,41 @@ describe('test/installGit.test.js', () => {
done(err);
});
});
it('should install success', done => {
coffee.fork(helper.npminstall, [
'a@git+ssh://git@bitbucket.org/saibotsivad/demo-npm-git-semver.git#semver:1.0.3',
], {
cwd: tmp,
})
.debug()
.expect('code', 0)
.end(() => {
const nodeModulesDir = path.join(tmp, 'node_modules');
// check package installed and linked
const symlink = fs.readlinkSync(path.join(nodeModulesDir, 'a'));
assert.strictEqual(require(path.join(nodeModulesDir, 'a/package.json')).name, 'demo-npm-git-semver');
// check package real package existed
assert.strictEqual(require(path.join(nodeModulesDir, `${symlink}/package.json`)).name, 'demo-npm-git-semver');
done();
});
});
it('should install with https success', done => {
coffee.fork(helper.npminstall, [
'a@git+https://git@bitbucket.org/saibotsivad/demo-npm-git-semver.git#semver:1.0.3',
], {
cwd: tmp,
})
.debug()
.expect('code', 0)
.end(() => {
const nodeModulesDir = path.join(tmp, 'node_modules');
// check package installed and linked
const symlink = fs.readlinkSync(path.join(nodeModulesDir, 'a'));
assert.strictEqual(require(path.join(nodeModulesDir, 'a/package.json')).name, 'demo-npm-git-semver');
// check package real package existed
assert.strictEqual(require(path.join(nodeModulesDir, `${symlink}/package.json`)).name, 'demo-npm-git-semver');
done();
});
});

});

0 comments on commit 7859046

Please sign in to comment.