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 5, 2016
1 parent a988a72 commit 7951454
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,27 @@ 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
```

`[email protected]` is root package, won't create link at `app/node_modules/.npminstall/node_modules/debug@`.
Expand Down
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
8 changes: 8 additions & 0 deletions test/fixtures/packageMeta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "packageMeta",
"dependencies": {
"debug": "2.2.0",
"a": "*",
"pedding": "http://registry.cnpmjs.org/pedding/download/pedding-1.0.0.tgz"
}
}
35 changes: 35 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,39 @@ 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');
});
});

});

0 comments on commit 7951454

Please sign in to comment.