@almaclaine/npm-utils
Version:
Set of utilities for working with npm and npm packages.
59 lines (52 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fsUtils = require('@almaclaine/fs-utils');
var validPackage = require('validate-npm-package-name');
var fs = require('fs');
var readline = require('readline-promise');
var child_process = require('child_process');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var validPackage__default = /*#__PURE__*/_interopDefaultLegacy(validPackage);
var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);
const execSyncToString = (cmd) => child_process.execSync(cmd).toString('utf-8').trim();
async function initPackage(folderName) {
const rl = readline__default['default'].createInterface({
input: process.stdin,
output: process.stdout
});
if (!validPackage__default['default'](folderName)) {
throw Error('Package name must be in the form of word(-word)*.');
}
const dirPath = fsUtils.makeCwdRelPath(folderName);
execSyncToString(`npx degit almaclaine/typescript-package-template ${dirPath}`);
if (!fsUtils.existsSync(dirPath)) {
throw Error(`Failed to create directory: ${folderName}.\n${fsUtils.alreadyExistsError(dirPath)}.`);
}
fs.unlinkSync(fsUtils.join(dirPath, 'src', 'postgres-utils.ts'));
fsUtils.writeFileAsStringSync(fsUtils.join(dirPath, 'src', `${folderName}.ts`), '');
fs.unlinkSync(fsUtils.join(dirPath, 'tests', 'typescript-package-template.test.ts'));
fsUtils.writeFileAsStringSync(fsUtils.join(dirPath, 'tests', `${folderName}.test.ts`), '');
const desc = await rl.questionAsync('Enter a description for the package: ');
const author = await rl.questionAsync('Package Author: ');
const keywords = await rl.questionAsync('Enter package keywords separated by space: ');
const git = await rl.questionAsync('Github url (no .git, optional): ');
const pack = require(fsUtils.join(dirPath, 'package.json'));
pack.name = `${folderName}`;
pack.description = desc;
pack.author = `${author.trim()}`;
if (git) {
pack.repository.url = git + '.git';
pack.bugs.url = git + '/issues';
pack.homepage = git;
child_process.execSync(`cd ${dirPath}; git init; git remote add origin ${git}.git`);
}
// Capitalize first letter of each Keyword, Don't Allow Duplicates
pack.keywords = [...new Set(keywords.trim()
.replace(/\s\s+/g, ' ')
.split(' ')
.map(e => e.toLowerCase())
.map(e => e[0].toUpperCase() + e.slice(1)))];
fsUtils.writeFileAsStringSync(fsUtils.join(dirPath, 'package.json'), JSON.stringify(pack, null, 2));
rl.close();
}
exports.initPackage = initPackage;