-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('..')(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |