Skip to content

Commit

Permalink
fix: add _from, _resolved back to package.json
Browse files Browse the repository at this point in the history
closes #42
  • Loading branch information
fengmk2 committed Mar 6, 2016
1 parent a988a72 commit 2f8b73c
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 18 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,30 @@ Two rules:

e.g.:

- app: `{ "dependencies": { "debug": "2.2.0", "ms": "0.5.1" } }` (root)
- app: `{ "dependencies": { "debug": "2.2.0" } }` (root)
- [email protected]: `{ "dependencies": { "ms": "0.7.1" } }`

```bash
app/
├── package.json
└── node_modules/
├── .npminstall/
│   ├── debug/
│   │   └── 2.2.0/
│   │   └── debug/
│   │   └── node_modules/
│   │   └── debug
│ │ ├── package.json
│   │   └── node_modules/
│   │      └── ms -> ../../../../ms/0.7.1/ms
│   ├── ms/
│   │   ├── 0.5.1/
│   │   │   └── ms/
│   │   └── 0.7.1/
│   │   └── ms/
│   │   └── ms
│   │   └── package.json
│   └── node_modules/
│   └── ms -> ../ms/0.7.1/ms
├── debug -> .npminstall/debug/2.2.0/debug
└── ms -> .npminstall/ms/0.5.1/ms
└── debug -> .npminstall/debug/2.2.0/debug
```

`debug@1.0.0` is root package, won't create link at `app/node_modules/.npminstall/node_modules/debug@`.
`debug@2.2.0` is root package, won't create link at `app/node_modules/.npminstall/node_modules/debug@`.

## Benchmarks

Expand Down
36 changes: 28 additions & 8 deletions lib/download/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
const path = require('path');
const uuid = require('node-uuid');
const chalk = require('chalk');
const utils = require('../utils');
const runscript = require('runscript');
const utils = require('../utils');

module.exports = function* (pkg, options) {
options.gitPackages++;
Expand All @@ -26,6 +26,12 @@ module.exports = function* (pkg, options) {
yield utils.mkdirp(cloneDir);
try {
yield clone(gitInfo.url, gitInfo.branch, cloneDir, options);
const commit = yield getLastCommitHash(cloneDir);
const resolved = gitInfo.url + '#' + commit;
yield utils.addMetaToJSONFile(path.join(cloneDir, 'package.json'), {
_from: pkg.name ? `${pkg.name}@${gitInfo.fullUrl}` : gitInfo.fullUrl,
_resolved: resolved,
});
return yield utils.copyInstall(cloneDir, options);
} catch (err) {
throw new Error(`[${pkg.name}@${pkg.rawSpec}]: ${err.message}`);
Expand All @@ -36,26 +42,27 @@ module.exports = function* (pkg, options) {
};

function parseGitInfo(pkg) {
let url;
let fullUrl;
if (pkg.type === 'git') {
url = pkg.spec;
fullUrl = pkg.spec;
} else {
// try https git url first
if (pkg.hosted.httpsUrl) {
// git+https://github.com/node-modules/pedding.git#0.0.3
url = pkg.hosted.httpsUrl.replace('git+', '');
fullUrl = pkg.hosted.httpsUrl.replace('git+', '');
} else if (pkg.hosted.gitUrl) {
// git://github.com/node-modules/pedding.git#0.0.3
url = pkg.hosted.gitUrl;
fullUrl = pkg.hosted.gitUrl;
} else {
url = pkg.hosted.ssh;
fullUrl = pkg.hosted.ssh;
}
}
const branchReg = /#(.*)$/;
const r = branchReg.exec(url);
const r = branchReg.exec(fullUrl);
const branch = r && r[1] ? r[1] : 'master';
url = url.replace(branchReg, '');
const url = fullUrl.replace(branchReg, '');
return {
fullUrl,
url,
branch,
};
Expand All @@ -72,3 +79,16 @@ function* clone(url, branch, target, options) {
],
});
}

function* getLastCommitHash(target) {
const script = `git log -1 --format="%H"`;
const stdio = yield runscript(script, {
cwd: target,
stdio: [
'ignore',
'pipe',
'inherit',
],
});
return stdio.stdout && stdio.stdout.toString().trim() || '';
}
6 changes: 6 additions & 0 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ function* download(pkg, options) {
const stream = yield getTarballStream(pkg, options);
yield checkShasumAndUngzip(ungzipDir, stream, pkg);
yield fs.writeFile(donefile, Date());

yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), {
_from: `${pkg.name}@${pkg.version}`,
_resolved: pkg.dist.tarball,
});

options.cache[key].done = true;
options.events.emit(key);
options.registryPackages++;
Expand Down
4 changes: 4 additions & 0 deletions lib/download/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = function* (pkg, options) {
yield utils.mkdirp(ungzipDir);
try {
yield utils.unpack(readstream, ungzipDir);
yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), {
_from: pkg.name ? `${pkg.name}@${remoteUrl}` : remoteUrl,
_resolved: remoteUrl,
});
return yield utils.copyInstall(ungzipDir, options);
} catch (err) {
throw new Error(`[${pkg.name}@${pkg.rawSpec}]: ${err.message}`);
Expand Down
8 changes: 8 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ exports.readJSON = function* readJSON(filepath) {
return JSON.parse(content);
};

exports.addMetaToJSONFile = function*(filepath, meta) {
const pkg = yield exports.readJSON(filepath);
for (const key in meta) {
pkg[key] = meta[key];
}
yield fs.writeFile(filepath, JSON.stringify(pkg, null, 2));
};

exports.mkdirp = function mkdirp(dir, mod) {
return new Promise((resolve, reject) => {
_mkdirp(dir, mod, err => err ? reject(err) : resolve());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"node-uuid": "~1.4.7",
"npm-package-arg": "~4.1.0",
"rimraf": "~2.5.1",
"runscript": "~1.0.0",
"runscript": "~1.1.0",
"semver": "~5.1.0",
"tar": "~2.2.1",
"urllib": "~2.7.1",
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/packageMeta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "packageMeta",
"dependencies": {
"debug": "2.2.0",
"a": "*",
"pedding": "http://registry.cnpmjs.org/pedding/download/pedding-1.0.0.tgz",
"bytes": "https://github.com/visionmedia/bytes.js.git"
}
}
39 changes: 39 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,43 @@ describe('test/index.test.js', function() {
const v1 = yield readJSON(path.join(tmp, 'node_modules', 'pedding', 'package.json'));
assert.equal(v1.version[0], '1');
});

describe('_from, _resolved in package.json', function() {
const root = path.join(__dirname, 'fixtures', 'packageMeta');

function cleanup() {
rimraf.sync(path.join(root, 'node_modules'));
}

beforeEach(cleanup);
afterEach(cleanup);

it('should add _from, _resolved to package.json', function*() {
yield npminstall({
root: root,
});
// node_modules/.npminstall/node_modules/ms should exists
assert(yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'ms')));
// node_modules/.npminstall/node_modules/debug should not exists
assert(!(yield fs.exists(path.join(root, 'node_modules', '.npminstall', 'node_modules', 'debug'))));
assert(yield fs.exists(path.join(root, 'node_modules', 'pedding')));

const debugPkg = yield readJSON(path.join(root, 'node_modules', 'debug', 'package.json'));
assert.equal(debugPkg._from, '[email protected]');
assert(debugPkg._resolved);

const aPkg = yield readJSON(path.join(root, 'node_modules', 'a', 'package.json'));
assert(/^a@\d+\.\d+\.\d+$/.test(aPkg._from), aPkg._from);
assert(aPkg._resolved);

const peddingPkg = yield readJSON(path.join(root, 'node_modules', 'pedding', 'package.json'));
assert.equal(peddingPkg._from, 'pedding@http://registry.cnpmjs.org/pedding/download/pedding-1.0.0.tgz');
assert.equal(peddingPkg._resolved, 'http://registry.cnpmjs.org/pedding/download/pedding-1.0.0.tgz');

const bytesPkg = yield 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{40}$/.test(bytesPkg._resolved), bytesPkg._resolved);
});
});

});

0 comments on commit 2f8b73c

Please sign in to comment.