Simplify new binary tool
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent a9d9123 · Browse files · 6 files·+114 added−148 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+114 added−148 removed
- tests:+70 added−74 removed
- other:+44 added−74 removed
Provenance
- Commit
27eaa90- Tree
9419929- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
a9d9123
6 files changed+114 added−148 removed
deleted:bin/utils/index.js-44 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344let cleanArgName = (arg) => arg.startsWith('--') ? arg.slice(2) : arg.slice(1)
let parseArgs = (argv) => { argv.splice(0, 2)
let parsedArgs = {}
let currentArg = null argv.forEach((arg) => { if (arg.includes('=')) { if (currentArg) { parsedArgs[currentArg] = true currentArg = null } let argSplit = arg.split('=') parsedArgs[cleanArgName(argSplit[0])] = argSplit[1]
return }
if (arg.startsWith('-') || arg.startsWith('--')) { if (currentArg) { parsedArgs[currentArg] = true currentArg = null }
currentArg = cleanArgName(arg) return }
if (currentArg) { parsedArgs[currentArg] = arg currentArg = null } })
if (currentArg) { parsedArgs[currentArg] = true }
return parsedArgs}
module.exports = { parseArgs }modified:bin/nanoid.cjs+40 added-28 removed
123446567889101191011121313141516141516171818192019202122232223242425262728293031322829303132333334353637383940414243444546474849505138395253415455564357584546#!/usr/bin/env node
let { nanoid, customAlphabet } = require('..')let { parseArgs } = require('./utils')
let parsedArgs = parseArgs(process.argv)function print(msg) { process.stdout.write(msg + '\n')}
if (parsedArgs.help) { process.stdout.write(` Usage $ nanoid [options]function error(msg) { process.stderr.write(msg + '\n') process.exit(1)}
Options -s, --size Generated ID size -a, --alphabet Alphabet to use -h, --help Show this helpif (process.argv.includes('--help') || process.argv.includes('-h')) { print(` Usage $ nanoid [options]
Examples $ nano --s=15 S9sBF77U6sDB8Yg Options -s, --size Generated ID size -a, --alphabet Alphabet to use -h, --help Show this help
$ nano --size=10 --alphabet=abc bcabababca`) Examples $ nano --s 15 S9sBF77U6sDB8Yg
$ nano --size 10 --alphabet abc bcabababca`) process.exit()}
let alphabet = parsedArgs.alphabet || parsedArgs.alet size = parsedArgs.size || parsedArgs.s ? Number(parsedArgs.size || parsedArgs.s) : undefined
if (typeof size !== 'undefined' && (Number.isNaN(size) || size <= 0)) { process.stderr.write('Size must be positive integer\n') process.exit(1)let alphabet, sizefor (let i = 2; i < process.argv.length; i++) { let arg = process.argv[i] if (arg === '--size' || arg === '-s') { size = Number(process.argv[i + 1]) i += 1 if (Number.isNaN(size) || size <= 0) { error('Size must be positive integer') } } else if (arg === '--alphabet' || arg === '-a') { alphabet = process.argv[i + 1] i += 1 } else { error('Unknown argument ' + arg) }}
if (alphabet) { if (typeof size === 'undefined') { process.stderr.write('You must also specify size option, when using custom alphabet\n') process.exit(1) error('You must also specify size option, when using custom alphabet') } process.stdout.write(customAlphabet(alphabet, size)()) let customNanoid = customAlphabet(alphabet, size) print(customNanoid())} else { process.stdout.write(nanoid(size)) print(nanoid(size))}
process.stdout.write('\n')deleted:bin/nanoid.test.js-74 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374let { suite } = require('uvu')let { is, match, equal } = require('uvu/assert')let { promisify } = require('util')let { join } = require('path')let child = require('child_process')
let { parseArgs } = require('./utils')
let exec = promisify(child.exec)
const nanoIdSuit = suite('nanoid')
nanoIdSuit('prints unique ID', async () => { let { stdout, stderr } = await exec('node ' + join(__dirname, 'nanoid.cjs')) is(stderr, '') match(stdout, /^[\w-]{21}\n$/)})
nanoIdSuit('uses size', async () => { let { stdout, stderr } = await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --size=10') is(stderr, '') match(stdout, /^[\w-]{10}\n$/)})
nanoIdSuit('uses alphabet', async () => { let { stdout, stderr } = await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --alphabet=abc --size=15') is(stderr, '') match(stdout, /^[abc]{15}\n$/)})
nanoIdSuit('show an error if size is not a number', async () => { try { await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --s abc') } catch (e) { match(e, /Size must be positive integer/) }})
nanoIdSuit('shows an error if size is not provided when using custom alphabet', async () => { try { await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --alphabet abc') } catch (e) { match(e, /You must also specify size option, when using custom alphabet/) }})
nanoIdSuit('requires error if size is a negative number', async () => { try { await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --size "-1"') } catch (e) { match(e, /Size must be positive integer/) }})
nanoIdSuit('displays help', async () => { let { stdout, stderr } = await exec('node ' + join(__dirname, 'nanoid.cjs') + ' --help') is(stderr, '') match(stdout, /Usage/) match(stdout, /\$ nanoid \[options]/)})
nanoIdSuit.run()
const parseArgsSuite = suite('parseArgs')
parseArgsSuite('parses args', () => { equal(parseArgs(['node', 'nanoid.cjs', '--help']), { help: true }) equal(parseArgs(['node', 'nanoid.cjs', '--help', '--size=30']), { help: true, size: '30' }) equal(parseArgs(['node', 'nanoid.cjs', '--help', '-s', '30']), { help: true, s: '30' }) equal(parseArgs(['node', 'nanoid.cjs', '--help', '-size', '30']), { help: true, size: '30' }) equal(parseArgs(['node', 'nanoid.cjs', '--help', '-size', '30', '-alphabet', 'abc']), { help: true, size: '30', alphabet: 'abc' })})
parseArgsSuite.run()added:test/bin.test.js+70 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970let { is, match } = require('uvu/assert')let { promisify } = require('util')let { test } = require('uvu')let { join } = require('path')let child = require('child_process')
let exec = promisify(child.exec)
const BIN = join(__dirname, '..', 'bin', 'nanoid.cjs')
test('prints unique ID', async () => { let { stdout, stderr } = await exec('node ' + BIN) is(stderr, '') match(stdout, /^[\w-]{21}\n$/)})
test('uses size', async () => { let { stdout, stderr } = await exec('node ' + BIN + ' --size 10') is(stderr, '') match(stdout, /^[\w-]{10}\n$/)})
test('uses alphabet', async () => { let { stdout, stderr } = await exec( 'node ' + BIN + ' --alphabet abc --size 15' ) is(stderr, '') match(stdout, /^[abc]{15}\n$/)})
test('shows an error on unknown argument', async () => { try { await exec('node ' + BIN + ' -test') } catch (e) { match(e, /Unknown argument -test/) }})
test('shows an error if size is not a number', async () => { try { await exec('node ' + BIN + ' -s abc') } catch (e) { match(e, /Size must be positive integer/) }})
test('shows an error on no size with custom alphabet', async () => { try { await exec('node ' + BIN + ' --alphabet abc') } catch (e) { match(e, /You must also specify size option, when using custom alphabet/) }})
test('requires error if size is a negative number', async () => { try { await exec('node ' + BIN + ' --size "-1"') } catch (e) { match(e, /Size must be positive integer/) }})
test('displays help', async () => { let { stdout, stderr } = await exec('node ' + BIN + ' --help') is(stderr, '') match(stdout, /Usage/) match(stdout, /\$ nanoid \[options]/)})
test.run()modified:README.md+2 added-1 removed
473 unmodified lines474 $ npx nanoid --size 10475 L3til0JS4z476 ```477 478-Custom alphabet can be specified with `--alphabet` (or `-a`) option (note that in this case `--size` is required):478+Custom alphabet can be specified with `--alphabet` (or `-a`) option479+(note that in this case `--size` is required):480 481 ```sh482 $ npx nanoid --alphabet abc --size 15483 bccbcabaabaccab47 unmodified linesmodified:README.ru.md+2 added-1 removed
461 unmodified lines462 $ npx nanoid --size 10463 L3til0JS4z464 ```465 466-Изменить алфавит можно при помощи аргумента `--alphabet` (ли `-a`) (обратите внимание, что в этом случае `--size` обязателен):466+Изменить алфавит можно при помощи аргумента `--alphabet` (ли `-a`)467+(в этом случае `--size` обязателен):468 469 ```sh470 $ npx nanoid --alphabet abc --size 15471 bccbcabaabaccab46 unmodified lines