Skip to content

Commit

Permalink
feat: migrate from yargs to sywac
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew committed Aug 2, 2017
1 parent cbc0eeb commit b755458
Show file tree
Hide file tree
Showing 5 changed files with 8,693 additions and 110 deletions.
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,45 @@ View help content:

```console
$ rewrite-shrinkwrap-urls --help
Usage: rewrite-shrinkwrap-urls [npm-shrinkwrap.json] -r <registry> [opts]
Usage: rewrite-shrinkwrap-urls [npm-shrinkwrap.json] -r <registry> [options]

Arguments:
[npm-shrinkwrap.json] The input shrinkwrap file containing urls to
rewrite. You can omit this if --stdin is used.
[file] [default: npm-shrinkwrap.json]

Required:
-r, --registry Base URL of the registry to point URLs at [string] [required]
-r, --registry <registry> Base URL of the registry to point URLs at
[required] [string]

Options:
-f, --file Path of file to write modified shrinkwrap to, defaults to input
file [string]
-i, --stdin Read shrinkwrap file contents from stdin [boolean]
-o, --stdout Write modified shrinkwrap content to stdout instead of file
[boolean]
-p, --public Use public registry style URLs. Omit this flag when rewriting
to npm Enterprise. [boolean]
-s, --spaces Number of spaces per JSON indent of output[number] [default: 2]
-m, --from Sync the "from" field with the "resolved" field (both will be
the rewritten URL) [boolean]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
-f, --file <output> Path of file to write modified shrinkwrap to,
defaults to input file
[file]

-i, --stdin Read shrinkwrap file contents from stdin
[boolean]

-o, --stdout Write modified shrinkwrap content to stdout instead
of file
[boolean]

-p, --public Use public registry style URLs. Omit this flag when
rewriting to npm Enterprise.
[boolean]

-s, --spaces <num> Number of spaces per JSON indent of output
[number] [default: 2]

-m, --from Sync the "from" field with the "resolved" field
(both will be the rewritten URL)
[boolean]

-h, --help Show help
[commands: help] [boolean]

-v, --version Show version number
[commands: version] [boolean]
```

Rewrite all URLs in the current directory's `npm-shrinkwrap.json`, pointing to the private registry at `https://private-registry`:
Expand Down
187 changes: 93 additions & 94 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,116 @@ const jsonfile = require('jsonfile')
const rewriteShrinkwrapUrls = require('./')

const defaultFile = 'npm-shrinkwrap.json'
let argv
let outfile
let shrinkwrap

const argv = require('yargs')
.usage('Usage: $0 [npm-shrinkwrap.json] -r <registry> [opts]')
.option('r', {
alias: 'registry',
describe: 'Base URL of the registry to point URLs at',
type: 'string',
require('sywac')
.positional(`[${defaultFile}] -r <registry>`, {
ignore: '-r <registry>',
params: [{
desc: 'The input shrinkwrap file containing urls to rewrite. You can omit this if --stdin is used.',
type: 'file',
defaultValue: defaultFile
}]
})
.string('-r, --registry <registry>', {
desc: 'Base URL of the registry to point URLs at',
required: true,
group: 'Required:'
})
.option('f', {
alias: 'file',
describe: 'Path of file to write modified shrinkwrap to, defaults to input file',
type: 'string'
.file('-f, --file <output>', {
desc: 'Path of file to write modified shrinkwrap to, defaults to input file'
})
.option('i', {
alias: 'stdin',
describe: 'Read shrinkwrap file contents from stdin',
type: 'boolean'
.boolean('-i, --stdin', {
desc: 'Read shrinkwrap file contents from stdin'
})
.option('o', {
alias: 'stdout',
describe: 'Write modified shrinkwrap content to stdout instead of file',
type: 'boolean'
.boolean('-o, --stdout', {
desc: 'Write modified shrinkwrap content to stdout instead of file'
})
.option('p', {
alias: 'public',
describe: 'Use public registry style URLs. Omit this flag when rewriting to npm Enterprise.',
type: 'boolean'
.boolean('-p, --public', {
desc: 'Use public registry style URLs. Omit this flag when rewriting to npm Enterprise.'
})
.option('s', {
alias: 'spaces',
describe: 'Number of spaces per JSON indent of output',
default: 2,
type: 'number'
.number('-s, --spaces <num>', {
desc: 'Number of spaces per JSON indent of output',
defaultValue: 2
})
.option('m', {
alias: 'from',
describe: 'Sync the "from" field with the "resolved" field (both will be the rewritten URL)',
type: 'boolean'
.boolean('-m, --from', {
desc: 'Sync the "from" field with the "resolved" field (both will be the rewritten URL)'
})
.help('-h, --help')
.version('-v, --version')
.outputSettings({ maxWidth: 76 })
.check(argv => {
if (argv._.length === 0) argv._.push(defaultFile)
else if (argv._[0] === '-') argv.stdin = true
if (argv[defaultFile] === '-') argv.stdin = true

if (argv.stdin && !argv.file) argv.stdout = true

if (typeof argv.spaces === 'undefined' || isNaN(argv.spaces)) argv.spaces = 2
return true
})
.help().alias('h', 'help')
.version().alias('v', 'version')
.argv

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', chunk => {
shrinkwrap += chunk
})
process.stdin.on('end', () => {
try {
shrinkwrap = JSON.parse(shrinkwrap)
} catch (e) {
console.error('Invalid JSON')
process.exit(1)
.parseAndExit()
.then(parsedArgv => {
argv = parsedArgv
outfile = argv.file || defaultFile
if (argv.stdin) {
return new Promise(resolve => {
// read from stdin
shrinkwrap = ''
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', chunk => {
shrinkwrap += chunk
})
process.stdin.on('end', () => {
try {
shrinkwrap = JSON.parse(shrinkwrap)
} catch (e) {
console.error('Invalid JSON')
process.exit(1)
}
resolve()
})
})
} else {
return new Promise(resolve => {
// look for file
const infile = argv[defaultFile]
outfile = argv.file || infile
jsonfile.readFile(infile, (e, content) => {
if (!e) {
shrinkwrap = content
return resolve()
}
if (infile === defaultFile) {
console.error('No %s file found', defaultFile)
process.exit(1)
}
// see if actual file contents were passed
try {
shrinkwrap = JSON.parse(infile)
outfile = argv.file || defaultFile
} catch (er) {
console.error('Invalid file or JSON content:', infile)
process.exit(1)
}
resolve()
})
})
}
rewrite()
})
} else {
// look for file
const infile = argv._[0]
outfile = argv.file || infile
try {
shrinkwrap = jsonfile.readFileSync(infile)
} catch (e) {
if (infile === defaultFile) {
console.error('No %s file found', defaultFile)
process.exit(1)
}
// see if actual file contents were passed
try {
shrinkwrap = JSON.parse(infile)
outfile = argv.file || defaultFile
} catch (er) {
console.error('Invalid file or JSON content:', infile)
process.exit(1)
}
}

rewrite()
}
.then(whenDone => {
// rewrite urls (modifies shrinkwrap object)
rewriteShrinkwrapUrls(shrinkwrap, {
newBaseUrl: argv.registry,
public: argv.public,
syncFrom: argv.from
})

function rewrite () {
// rewrite urls (modifies shrinkwrap object)
rewriteShrinkwrapUrls(shrinkwrap, {
newBaseUrl: argv.registry,
public: argv.public,
syncFrom: argv.from
// output to file or stdout
if (argv.stdout) {
console.log(JSON.stringify(shrinkwrap, null, argv.spaces))
} else {
jsonfile.writeFileSync(outfile, shrinkwrap, { spaces: argv.spaces })
console.log('Modified shrinkwrap content successfullly written to:', outfile)
}
})

// output to file or stdout
if (argv.stdout) {
console.log(JSON.stringify(shrinkwrap, null, argv.spaces))
} else {
jsonfile.writeFileSync(outfile, shrinkwrap, { spaces: argv.spaces })
console.log('Modified shrinkwrap content successfullly written to:', outfile)
}
}
Loading

0 comments on commit b755458

Please sign in to comment.