npm-pkg-kit
Version:
CLI tool to simplify NPM package creation by generating boilerplate setup
79 lines (78 loc) • 3.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const shelljs_1 = require("shelljs");
const files_1 = require("../utils/files");
const npm_1 = require("../utils/npm");
const rollup_1 = require("../utils/rollup");
const babel_1 = __importDefault(require("./opt/babel"));
const tsc_1 = __importDefault(require("./opt/tsc"));
const bbMain = __importStar(require("./tpl/mod/bb.main"));
const bb_rollup_1 = __importDefault(require("./tpl/mod/bb.rollup"));
const tsMain = __importStar(require("./tpl/mod/ts.main"));
const ts_rollup_1 = __importDefault(require("./tpl/mod/ts.rollup"));
const tsconfig_1 = __importDefault(require("./tpl/mod/tsconfig"));
function default_1(name, cmd) {
const dir = npm_1.gen(name);
const opt = cmd.opts();
if (opt.ts) {
tsc_1.default(dir);
}
else {
babel_1.default(dir);
}
template(dir, opt.ts);
mod(dir, opt.ts);
}
exports.default = default_1;
function template(dir, ts) {
const src = path_1.join(dir, 'src');
const opt = ts ? tsMain : bbMain;
const ext = ts ? 'ts' : 'js';
shelljs_1.mkdir('-p', src);
files_1.writeContent(src, `module.${ext}`, opt.module);
files_1.writeContent(src, `umd.${ext}`, opt.umd);
}
function mod(dir, ts) {
const pack = files_1.readJSON(dir, 'package.json');
const deps = ['rollup-plugin-esmin'];
pack.main = 'dist/umd.js';
pack.esnext = 'dist/module.js';
pack.module = 'dist/module.js';
pack.scripts.dist = 'run-s dist:*';
pack.scripts['dist:lint'] = 'npm run lint';
pack.scripts['dist:clean'] = 'npm run clean';
pack.scripts['dist:build'] = 'rollup -c';
if (ts) {
pack.types = 'dist/module.d.ts';
pack.scripts['dist:types'] = 'tsc --removeComments --emitDeclarationOnly';
deps.push('@rollup/plugin-typescript', 'tslib');
files_1.writeJSON(dir, 'tsconfig.json', tsconfig_1.default, 2);
}
const install = rollup_1.rolldeps(dir, ...deps);
files_1.writeContent(dir, 'rollup.config.js', ts ? ts_rollup_1.default : bb_rollup_1.default);
files_1.writeJSON(dir, 'package.json', pack, 2);
shelljs_1.exec(install);
}