Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
dabroek committed Jun 20, 2017
1 parent 3b4cef8 commit 15f20a2
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# shrinkwrap-to-lockfile
Migrate npm shrinkwrap to yarn lockfile
# NPM Shrinkwrap to Yarn lockfile

Migrate your `npm-shrinkwrap.json` to a yarn lockfile without a headache!

There is three steps we perform with this migration:
* We extract the dependencies from the `npm-shrinkwrap.json`;
* We save and lock down these dependencies in the `package.json` (removing the ~ or ^);
* We generate a new `yarn.lock` file from the updated `package.json`.

## Installation

```bash
yarn global add shrinkwrap-to-lockfile
```

## How to use

```bash
shrinkwrap-to-lockfile [npm-shrinkwrap-file] [package-file]
```

That's it!
3 changes: 3 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('..')(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;

const _ = require('lodash');

function parseJsonFile(filename) {
let obj = {};

try {
const file = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf8');
obj = JSON.parse(file);
} catch (error) {
throw new Error(`${filename} could not be parsed.`);
}

return obj;
}

function getDependencyVersions(dependencies) {
return _.reduce(dependencies, (result, pkg, name) => {
const version = pkg.version || pkg;
result[name] = _.head(version.match(/(\d+\.\d+\.\d+(?:-.+)?)/));

return result;
}, {});
}

function objectMergeLeft(a, b) {
return _.reduce(a, (result, value, key) => {
result[key] = value;

if (!_.isEqual(value, b[key])) {
result[key] = b[key];
}

return result;
}, {});
}

function updatePackageJson(shrinkwrapFile, packageFile) {
const shrinkwrapJson = parseJsonFile(shrinkwrapFile);
const packageJson = parseJsonFile(packageFile);

const shrinkwrapVersions = getDependencyVersions(shrinkwrapJson.dependencies);

if (packageJson.dependencies) {
const dependencyVersions = getDependencyVersions(packageJson.dependencies);
packageJson.dependencies = objectMergeLeft(dependencyVersions, shrinkwrapVersions);
}

if (packageJson.devDependencies) {
const devDependencyVersions = getDependencyVersions(packageJson.devDependencies);
packageJson.devDependencies = objectMergeLeft(devDependencyVersions, shrinkwrapVersions);
}

fs.writeFileSync(path.resolve(__dirname, 'package.json'), JSON.stringify(packageJson, null, 2));
}

function createLockFile() {
exec('yarn install');
}

function shrinkwrapToLockfile(shrinkwrap = 'npm-shrinkwrap.json', packageJson = 'package.json') {
updatePackageJson(shrinkwrap, packageJson);
createLockFile();
}

module.exports = shrinkwrapToLockfile;
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "shrinkwrap-to-lockfile",
"version": "1.0.0",
"description": "Migrate your npm shrinkwrap to a yarn lockfile.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"shrinkwrap-to-lockfile": "bin/cli.js"
},
"author": "Matthijs Dabroek <dabroek@gmail.com>",
"license": "MIT",
"dependencies": {
"lodash": "^4.17.4"
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


lodash@4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

0 comments on commit 15f20a2

Please sign in to comment.