Skip to content

Commit

Permalink
Simplify new binary tool
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jan 16, 2022
1 parent a9d9123 commit 27eaa90
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 148 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ $ npx nanoid --size 10
L3til0JS4z
```

Custom alphabet can be specified with `--alphabet` (or `-a`) option (note that in this case `--size` is required):
Custom alphabet can be specified with `--alphabet` (or `-a`) option
(note that in this case `--size` is required):

```sh
$ npx nanoid --alphabet abc --size 15
Expand Down
3 changes: 2 additions & 1 deletion README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ $ npx nanoid --size 10
L3til0JS4z
```

Изменить алфавит можно при помощи аргумента `--alphabet` (ли `-a`) (обратите внимание, что в этом случае `--size` обязателен):
Изменить алфавит можно при помощи аргумента `--alphabet` (ли `-a`)
(в этом случае `--size` обязателен):

```sh
$ npx nanoid --alphabet abc --size 15
Expand Down
68 changes: 40 additions & 28 deletions bin/nanoid.cjs
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
#!/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')
}

function error(msg) {
process.stderr.write(msg + '\n')
process.exit(1)
}

if (parsedArgs.help) {
process.stdout.write(`
Usage
$ nanoid [options]
if (process.argv.includes('--help') || process.argv.includes('-h')) {
print(`
Usage
$ nanoid [options]
Options
-s, --size Generated ID size
-a, --alphabet Alphabet to use
-h, --help Show this help
Options
-s, --size Generated ID size
-a, --alphabet Alphabet to use
-h, --help Show this help
Examples
$ nano --s=15
S9sBF77U6sDB8Yg
Examples
$ nano --s 15
S9sBF77U6sDB8Yg
$ nano --size=10 --alphabet=abc
bcabababca
`)
$ nano --size 10 --alphabet abc
bcabababca`)
process.exit()
}

let alphabet = parsedArgs.alphabet || parsedArgs.a
let 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, size
for (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')
74 changes: 0 additions & 74 deletions bin/nanoid.test.js

This file was deleted.

44 changes: 0 additions & 44 deletions bin/utils/index.js

This file was deleted.

70 changes: 70 additions & 0 deletions test/bin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
let { 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()

0 comments on commit 27eaa90

Please sign in to comment.