-
-
Notifications
You must be signed in to change notification settings - Fork 793
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
114 additions
and
148 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
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
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,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') |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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() |