12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
/**
* @title pkg-name-check
* @version 0.0.1
*
* @fileoverview A utiliy script to help identify whether your chosen package
* name already exists with a similar name in the npm registry. See:
* https://blog.npmjs.org/post/168978377570/new-package-moniker-rules)
*
* Prior to usage you'll need to:
*
* 1. Download npm registry (approx 124Mb of JSON) by running:
*
* curl -X GET https://replicate.npmjs.com/_all_docs > /path/to/npm-registry.json
*
* @example
* node path/to/pkg-name-check.js "<pkg-name>"
*/
const tick = process.platform === 'win32' ? '√' : '✔';
const cross = process.platform === 'win32' ? '×' : '✖';
const npmRegistry = require('./npm-registry.json');
const pkgName = process.argv[2];
const CHARSET = '[_\\.\\-]*?';
if (!pkgName) {
console.log('\x1b[31mERR!\x1b[0m %s', 'pkg name argument must be provided.');
process.exit(1);
}
// Construct the regex pattern.
const regex = RegExp(pkgName
.split('')
.map(function (char, i, arr) {
// If last letter is `s` then add `?` qualifier (i.e. zero or one)
// to test for non-plural instances.
if (arr.length - 1 === i && char === 's') {
char = char + '?'
}
// If character equals `-`, `_`, or `.` then add `*?` qualifier
// (i.e. zero to unlimited) to test for names without `-`, `_`, or `.`
return (char === '-' || char === '_' || char === '.') ? char + '*?' : char
})
// Insert $CHARSET between each letter
.join(CHARSET)
// Prefix string with $CHARSET
.replace(/^/, CHARSET)
// Suffix string with $CHARSET
.replace(/$/, CHARSET)
// Prefix string with ^
.replace(/^/, '^')
// Suffix string with `s` and `?` qualifier
// (i.e. zero or one) to test for plural names.
.concat('[s]?$')
, 'g');
const result = npmRegistry.rows
.filter(function (pkg) {
return regex.test(pkg.id);
})
.map(function (pkg) {
return pkg.id;
});
if (result.length) {
console.log('\x1b[31m%s\x1b[0m "%s" is unavailable:', cross, pkgName);
console.log(result.join('\n'));
} else {
console.log('\x1b[32m%s\x1b[0m "%s" is available.', tick, pkgName);
}