Skip to content

Commit

Permalink
chore: ES2015ify and require Node 4 (#10)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: support for older Node versions has been dropped, please upgrade to Node 4+
  • Loading branch information
nexdrew authored Jun 19, 2017
1 parent 3923e63 commit c7d8772
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
21 changes: 11 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
'use strict'

var jsonfile = require('jsonfile')
var rewriteShrinkwrapUrls = require('./')
const jsonfile = require('jsonfile')
const rewriteShrinkwrapUrls = require('./')

var defaultFile = 'npm-shrinkwrap.json'
const defaultFile = 'npm-shrinkwrap.json'

var argv = require('yargs')
const argv = require('yargs')
.usage('Usage: $0 [npm-shrinkwrap.json] -r <registry> [opts]')
.option('r', {
alias: 'registry',
Expand Down Expand Up @@ -45,7 +46,7 @@ var argv = require('yargs')
describe: 'Sync the "from" field with the "resolved" field (both will be the rewritten URL)',
type: 'boolean'
})
.check(function (argv) {
.check(argv => {
if (argv._.length === 0) argv._.push(defaultFile)
else if (argv._[0] === '-') argv.stdin = true

Expand All @@ -58,18 +59,18 @@ var argv = require('yargs')
.version().alias('v', 'version')
.argv

var shrinkwrap
var outfile = argv.file || defaultFile
let shrinkwrap
let outfile = argv.file || defaultFile

if (argv.stdin) {
// read from stdin
shrinkwrap = ''
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) {
process.stdin.on('data', chunk => {
shrinkwrap += chunk
})
process.stdin.on('end', function () {
process.stdin.on('end', () => {
try {
shrinkwrap = JSON.parse(shrinkwrap)
} catch (e) {
Expand All @@ -80,7 +81,7 @@ if (argv.stdin) {
})
} else {
// look for file
var infile = argv._[0]
const infile = argv._[0]
outfile = argv.file || infile
try {
shrinkwrap = jsonfile.readFileSync(infile)
Expand Down
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
var npmUrls = require('./lib/npm-urls')
var url = require('url')
var visit = require('./lib/visit')
'use strict'

const npmUrls = require('./lib/npm-urls')
const url = require('url')
const visit = require('./lib/visit')

module.exports = function rewriteShrinkwrapUrls (shrinkwrap, opts) {
if (!shrinkwrap) return

opts = opts || {}

var transformer = typeof opts.transformer === 'function'
const transformer = typeof opts.transformer === 'function'
? opts.transformer
: function (after, before, name, version) { return after }
var npmUrl = opts.public ? npmUrls.oldTarballUrlToRegistry2 : npmUrls.oldTarballUrlToNew
var baseUrl = parseBaseUrl(opts.newBaseUrl || 'http://localhost:8080')
: (after, before, name, version) => after
const npmUrl = opts.public ? npmUrls.oldTarballUrlToRegistry2 : npmUrls.oldTarballUrlToNew
const baseUrl = parseBaseUrl(opts.newBaseUrl || 'http://localhost:8080')

var before
var after
let before
let after

visit(shrinkwrap, function (value, key, parent) {
visit(shrinkwrap, (value, key, parent) => {
if (typeof value === 'object' && 'resolved' in value) {
before = value.resolved
after = baseUrl + npmUrl(key, before)
Expand Down
2 changes: 1 addition & 1 deletion test/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('rewrites to public https://skimdb.npmjs.com/registry', (t) => {
test('calls transformer for each url', (t) => {
let count = 0
rewriteShrinkwrapUrls(shrinkwrap, {
transformer: function (newUrl, oldUrl, packageName, version) {
transformer: (newUrl, oldUrl, packageName, version) => {
count++
return 'https://something/' + packageName + '-' + version + '.tgz'
}
Expand Down

0 comments on commit c7d8772

Please sign in to comment.