Skip to content

Commit a0d25db

Browse files
committed
build(npm): add tools/npm/reshrinkwrap script and update docs
1 parent 625474c commit a0d25db

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

npm-shrinkwrap.readme.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,23 @@ All of our npm dependencies are locked via the `npm-shrinkwrap.json` file for th
33
- our project has lots of dependencies which update at unpredictable times, so it's important that
44
we update them explicitly once in a while rather than implicitly when any of us runs npm install
55
- locked dependencies allow us to do reuse npm cache on travis, significantly speeding up our builds
6-
(by 5min or more)
6+
(by 5 minutes or more)
77
- locked dependencies allow us to detect when node_modules folder is out of date after a branch switch
88
which allows us to build the project with the correct dependencies every time
99

10-
However npm's shrinkwrap is known to be buggy, so we need to take some extra steps to deal with this.
11-
The most important step is generating the npm-shrinkwrap.clean.js which is used during code reviews
12-
or debugging to easily review what has actually changed.
13-
See https://github.com/npm/npm/issues/3581 for related npm issue. A common symptom is that the `from`
14-
property of various dependencies in `npm-shrinkwrap.json` "arbitrarily" changes depending on when and
15-
where the shrinkwrap command was run.
10+
We also generate `npm-shrinkwrap.clean.js` file which is used during code reviews or debugging to easily review what has actually changed without extra noise.
1611

1712
To add a new dependency do the following:
1813

1914
1. if you are on linux or windows, then use MacOS or ask someone with MacOS to perform the
2015
installation. This is due to an optional `fsevents` dependency that is really required on MacOS
2116
to get good performance from file watching.
2217
2. make sure you are in sync with `upstream/master`
23-
3. ensure that your `node_modules` directory is not stale or poisoned by doing a clean install with
24-
`rm -rf node_modules && npm install`
18+
3. ensure that your `node_modules` directory is not stale by running `npm install`
2519
4. add a new dependency via `npm install --save-dev <packagename>`
26-
5. update npm-shrinkwrap.json with `npm shrinkwrap --dev`
27-
6. run `./tools/npm/clean-shrinkwrap.js`
28-
7. these steps should change 3 files: `package.json`, `npm-shrinkwrap.json` and
29-
`npm-shrinkwrap.clean.json`
30-
8. commit changes to these three files and you are done
20+
5. run `./tools/npm/reshrinkwrap`
21+
6. these steps should change 3 files: `package.json`, `npm-shrinkwrap.json` and `npm-shrinkwrap.clean.json`
22+
7. commit changes to these three files and you are done
3123

3224

3325
To update existing dependency do the following:
@@ -36,16 +28,14 @@ To update existing dependency do the following:
3628
installation. This is due to an optional `fsevents` dependency that is really required on MacOS
3729
to get good performance from file watching.
3830
2. make sure you are in sync with `upstream/master`: `git fetch upstream && git rebase upstream/master`
39-
3. ensure that your `node_modules` directory is not stale or poisoned by doing a clean install with
40-
`rm -rf node_modules && npm install`
31+
3. ensure that your `node_modules` directory is not stale by running `npm install`
4132
4. run `npm install --save-dev <packagename>@<version|latest>` or `npm update <packagename>` to
4233
update to the latest version that matches version constraint in `package.json`
43-
5. relock the dependencies with `npm shrinkwrap --dev`
44-
6. clean up the shrinkwrap file for review with `./tools/npm/clean-shrinkwrap.js`
45-
7. these steps should change 2 files: `npm-shrinkwrap.json` and `npm-shrinkwrap.clean.json`.
34+
5. run `./tools/npm/reshrinkwrap`
35+
6. these steps should change 2 files: `npm-shrinkwrap.json` and `npm-shrinkwrap.clean.json`.
4636
Optionally if you used `npm install ...` in the first step, `package.json` might be modified as
4737
well.
48-
8. commit changes to these three files and you are done
38+
7. commit changes to these three files and you are done
4939

5040

5141
To Remove an existing dependency do the following:
@@ -54,10 +44,8 @@ To Remove an existing dependency do the following:
5444
installation. This is due to an optional `fsevents` dependency that is really required on MacOS
5545
to get good performance from file watching.
5646
2. make sure you are in sync with `upstream/master`: `git fetch upstream && git rebase upstream/master`
57-
3. ensure that your `node_modules` directory is not stale or poisoned by doing a clean install with
58-
`rm -rf node_modules && npm install`
47+
3. ensure that your `node_modules` directory is not stale by running `npm install`
5948
4. run `npm uninstall --save-dev <packagename>@<version|latest>`
60-
5. relock the dependencies with `npm shrinkwrap --dev`
61-
6. clean up the shrinkwrap file for review with `./tools/npm/clean-shrinkwrap.js`
62-
7. these steps should change 3 files: `npm-shrinkwrap.json` and `npm-shrinkwrap.clean.json`.
63-
8. commit changes to these three files and you are done
49+
5. run `./tools/npm/reshrinkwrap`
50+
6. these steps should change 3 files: `npm-shrinkwrap.json` and `npm-shrinkwrap.clean.json`.
51+
7. commit changes to these three files and you are done

tools/npm/clean-shrinkwrap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ function cleanModule(moduleRecord, name) {
2727
}
2828

2929

30-
console.log('Reading npm-shrinkwrap.json');
30+
//console.log('Reading npm-shrinkwrap.json');
3131
var shrinkwrap = require('../../npm-shrinkwrap.json');
3232

33-
console.log('Cleaning shrinkwrap object');
33+
//console.log('Cleaning shrinkwrap object');
3434
cleanModule(shrinkwrap, shrinkwrap.name);
3535

3636
var cleanShrinkwrapPath = path.join(__dirname, '..', '..', 'npm-shrinkwrap.clean.json');
37-
console.log('Writing cleaned to', cleanShrinkwrapPath);
37+
console.log('writing npm-shrinkwrap.clean.json');
3838
fs.writeFileSync(cleanShrinkwrapPath, JSON.stringify(sorted(shrinkwrap), null, 2) + "\n");

tools/npm/copy-npm-shrinkwrap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var PROJECT_ROOT = path.join(__dirname, '../../');
1111
process.chdir(PROJECT_ROOT);
1212

1313
if (fs.existsSync(NPM_SHRINKWRAP_FILE)) {
14+
console.log('copying shrinkwrap fingerprint to', NPM_SHRINKWRAP_CACHED_FILE);
1415
fse.copySync(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE);
1516
} else {
1617
console.warn(`${NPM_SHRINKWRAP_FILE} not found. Copy operation will be skipped.`);

tools/npm/reshrinkwrap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
let childProcess = require('child_process');
6+
7+
childProcess.spawn('npm', ['shrinkwrap', '--dev'], {stdio: 'inherit'}).on('exit', (exitCode) => {
8+
if (exitCode !== 0) return;
9+
10+
childProcess.fork('./tools/npm/clean-shrinkwrap.js').on('exit', (exitCode) => {
11+
if (exitCode !== 0) return;
12+
13+
childProcess.fork('./tools/npm/copy-npm-shrinkwrap');
14+
});
15+
});

0 commit comments

Comments
 (0)