Skip to content

Commit 15f20a2

Browse files
committed
Initial release
1 parent 3b4cef8 commit 15f20a2

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.idea/

README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
# shrinkwrap-to-lockfile
2-
Migrate npm shrinkwrap to yarn lockfile
1+
# NPM Shrinkwrap to Yarn lockfile
2+
3+
Migrate your `npm-shrinkwrap.json` to a yarn lockfile without a headache!
4+
5+
There is three steps we perform with this migration:
6+
* We extract the dependencies from the `npm-shrinkwrap.json`;
7+
* We save and lock down these dependencies in the `package.json` (removing the ~ or ^);
8+
* We generate a new `yarn.lock` file from the updated `package.json`.
9+
10+
## Installation
11+
12+
```bash
13+
yarn global add shrinkwrap-to-lockfile
14+
```
15+
16+
## How to use
17+
18+
```bash
19+
shrinkwrap-to-lockfile [npm-shrinkwrap-file] [package-file]
20+
```
21+
22+
That's it!

bin/cli.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('..')(...process.argv.slice(2));

index.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const exec = require('child_process').exec;
4+
5+
const _ = require('lodash');
6+
7+
function parseJsonFile(filename) {
8+
let obj = {};
9+
10+
try {
11+
const file = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf8');
12+
obj = JSON.parse(file);
13+
} catch (error) {
14+
throw new Error(`${filename} could not be parsed.`);
15+
}
16+
17+
return obj;
18+
}
19+
20+
function getDependencyVersions(dependencies) {
21+
return _.reduce(dependencies, (result, pkg, name) => {
22+
const version = pkg.version || pkg;
23+
result[name] = _.head(version.match(/(\d+\.\d+\.\d+(?:-.+)?)/));
24+
25+
return result;
26+
}, {});
27+
}
28+
29+
function objectMergeLeft(a, b) {
30+
return _.reduce(a, (result, value, key) => {
31+
result[key] = value;
32+
33+
if (!_.isEqual(value, b[key])) {
34+
result[key] = b[key];
35+
}
36+
37+
return result;
38+
}, {});
39+
}
40+
41+
function updatePackageJson(shrinkwrapFile, packageFile) {
42+
const shrinkwrapJson = parseJsonFile(shrinkwrapFile);
43+
const packageJson = parseJsonFile(packageFile);
44+
45+
const shrinkwrapVersions = getDependencyVersions(shrinkwrapJson.dependencies);
46+
47+
if (packageJson.dependencies) {
48+
const dependencyVersions = getDependencyVersions(packageJson.dependencies);
49+
packageJson.dependencies = objectMergeLeft(dependencyVersions, shrinkwrapVersions);
50+
}
51+
52+
if (packageJson.devDependencies) {
53+
const devDependencyVersions = getDependencyVersions(packageJson.devDependencies);
54+
packageJson.devDependencies = objectMergeLeft(devDependencyVersions, shrinkwrapVersions);
55+
}
56+
57+
fs.writeFileSync(path.resolve(__dirname, 'package.json'), JSON.stringify(packageJson, null, 2));
58+
}
59+
60+
function createLockFile() {
61+
exec('yarn install');
62+
}
63+
64+
function shrinkwrapToLockfile(shrinkwrap = 'npm-shrinkwrap.json', packageJson = 'package.json') {
65+
updatePackageJson(shrinkwrap, packageJson);
66+
createLockFile();
67+
}
68+
69+
module.exports = shrinkwrapToLockfile;

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "shrinkwrap-to-lockfile",
3+
"version": "1.0.0",
4+
"description": "Migrate your npm shrinkwrap to a yarn lockfile.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": {
10+
"shrinkwrap-to-lockfile": "bin/cli.js"
11+
},
12+
"author": "Matthijs Dabroek <dabroek@gmail.com>",
13+
"license": "MIT",
14+
"dependencies": {
15+
"lodash": "^4.17.4"
16+
}
17+
}

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
lodash@4.17.4:
6+
version "4.17.4"
7+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

0 commit comments

Comments
 (0)