@bconnorwhite/bob
Version:
Bob is a toolkit for TypeScript projects
216 lines (182 loc) • 11.7 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.initPackageJSON = initPackageJSON;
exports.initPackageJSONAction = initPackageJSONAction;
exports["default"] = void 0;
var _inquirer = require("inquirer");
var _configstore = _interopRequireDefault(require("configstore"));
var _commanderVersion = require("commander-version");
var _structure = require("../../structure");
var _utils = require("../../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function except(a, b) {
if (a === void 0) {
a = {};
}
if (b === void 0) {
b = {};
}
return Object.keys(a).reduce(function (retval, key) {
if (!Object.keys(b).includes(key)) {
var _extends2;
return _extends({}, retval, (_extends2 = {}, _extends2[key] = a[key], _extends2));
} else {
return retval;
}
}, {});
}
function sort(object) {
return Object.keys(object).sort().reduce(function (retval, key) {
retval[key] = object[key];
return retval;
}, {});
}
function initPackageJSON(_x) {
return _initPackageJSON.apply(this, arguments);
}
function _initPackageJSON() {
_initPackageJSON = _asyncToGenerator(function* (args) {
if (args === void 0) {
args = {};
}
return (0, _utils.getModuleName)(args.configPackageName).then( /*#__PURE__*/function () {
var _ref = _asyncToGenerator(function* (moduleName) {
var config = moduleName ? new _configstore["default"](moduleName) : undefined;
return (0, _structure.getPackageJSON)().read().then( /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(function* (pkgJSON) {
var questions = [];
if ((pkgJSON == null ? void 0 : pkgJSON.name) === undefined) {
questions.push({
type: "input",
name: "name"
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.description) === undefined) {
questions.push({
type: "input",
name: "description"
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.author) === undefined || typeof pkgJSON.author === "object" && pkgJSON.author.name === undefined) {
var configAuthorName = config == null ? void 0 : config.get("authorName");
questions.push({
type: "input",
name: "author.name",
message: "author name:",
"default": configAuthorName
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.author) === undefined || typeof pkgJSON.author === "object" && pkgJSON.author.email === undefined) {
var configAuthorEmail = config == null ? void 0 : config.get("authorEmail");
questions.push({
type: "input",
name: "author.email",
message: "author email:",
"default": configAuthorEmail
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.author) === undefined || typeof (pkgJSON == null ? void 0 : pkgJSON.author) === "object" && (pkgJSON == null ? void 0 : pkgJSON.author.url) === undefined) {
var configAuthorURL = config == null ? void 0 : config.get("authorURL");
questions.push({
type: "input",
name: "author.url",
message: "author url:",
"default": configAuthorURL
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.homepage) === undefined) {
questions.push({
type: "input",
name: "homepage"
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.repository) === undefined) {
var configGithubUsername = config == null ? void 0 : config.get("githubUsername");
questions.push({
type: "input",
name: "githubUsername",
message: "GitHub username:",
"default": configGithubUsername
});
}
if ((pkgJSON == null ? void 0 : pkgJSON.license) === undefined) {
questions.push({
type: "input",
name: "license"
});
}
return (0, _inquirer.prompt)(questions).then( /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator(function* (answers) {
var _answers$author, _answers$author2, _answers$author3, _answers$name, _pkgJSON$repository, _pkgJSON$version, _answers$description, _pkgJSON$license, _pkgJSON$author, _answers$author4, _answers$homepage, _pkgJSON$keywords, _pkgJSON$files, _pkgJSON$main, _pkgJSON$scripts, _pkgJSON$scripts$buil, _pkgJSON$scripts2, _pkgJSON$scripts$comm, _pkgJSON$scripts3, _pkgJSON$scripts$lint, _pkgJSON$scripts4, _pkgJSON$scripts$rele, _pkgJSON$scripts5, _pkgJSON$scripts$test, _pkgJSON$scripts6, _pkgJSON$eslintConfig, _pkgJSON$husky, _pkgJSON$npmpackagejs;
config == null ? void 0 : config.set("authorName", (_answers$author = answers.author) == null ? void 0 : _answers$author.name);
config == null ? void 0 : config.set("authorEmail", (_answers$author2 = answers.author) == null ? void 0 : _answers$author2.email);
config == null ? void 0 : config.set("authorURL", (_answers$author3 = answers.author) == null ? void 0 : _answers$author3.url);
config == null ? void 0 : config.set("githubUsername", answers.githubUsername);
var name = (_answers$name = answers.name) != null ? _answers$name : pkgJSON == null ? void 0 : pkgJSON.name;
var repositoryURL = name && answers.githubUsername ? "git+https://github.com/" + answers.githubUsername + "/" + name + ".git" : typeof (pkgJSON == null ? void 0 : pkgJSON.repository) === "object" ? pkgJSON == null ? void 0 : (_pkgJSON$repository = pkgJSON.repository) == null ? void 0 : _pkgJSON$repository.url : undefined;
var ordered = {
name: name,
version: (_pkgJSON$version = pkgJSON == null ? void 0 : pkgJSON.version) != null ? _pkgJSON$version : "1.0.0",
description: (_answers$description = answers.description) != null ? _answers$description : pkgJSON == null ? void 0 : pkgJSON.description,
license: (_pkgJSON$license = pkgJSON == null ? void 0 : pkgJSON.license) != null ? _pkgJSON$license : answers.license,
"private": pkgJSON == null ? void 0 : pkgJSON["private"],
author: typeof (pkgJSON == null ? void 0 : pkgJSON.author) === "string" ? pkgJSON.author : _extends({}, (_pkgJSON$author = pkgJSON == null ? void 0 : pkgJSON.author) != null ? _pkgJSON$author : {}, (_answers$author4 = answers.author) != null ? _answers$author4 : {}),
homepage: (_answers$homepage = answers.homepage) != null ? _answers$homepage : pkgJSON == null ? void 0 : pkgJSON.homepage,
repository: repositoryURL ? {
type: "git",
url: repositoryURL
} : pkgJSON == null ? void 0 : pkgJSON.repository,
contributors: pkgJSON == null ? void 0 : pkgJSON.contributors,
keywords: (_pkgJSON$keywords = pkgJSON == null ? void 0 : pkgJSON.keywords) != null ? _pkgJSON$keywords : [],
files: (_pkgJSON$files = pkgJSON == null ? void 0 : pkgJSON.files) != null ? _pkgJSON$files : [(0, _structure.getBuildDir)().relative],
main: (_pkgJSON$main = pkgJSON == null ? void 0 : pkgJSON.main) != null ? _pkgJSON$main : (0, _structure.getBuildIndex)().relative,
bin: pkgJSON == null ? void 0 : pkgJSON.bin,
scripts: sort(_extends({}, (_pkgJSON$scripts = pkgJSON == null ? void 0 : pkgJSON.scripts) != null ? _pkgJSON$scripts : {}, {
build: (_pkgJSON$scripts$buil = pkgJSON == null ? void 0 : (_pkgJSON$scripts2 = pkgJSON.scripts) == null ? void 0 : _pkgJSON$scripts2.build) != null ? _pkgJSON$scripts$buil : "bob build",
commit: (_pkgJSON$scripts$comm = pkgJSON == null ? void 0 : (_pkgJSON$scripts3 = pkgJSON.scripts) == null ? void 0 : _pkgJSON$scripts3.commit) != null ? _pkgJSON$scripts$comm : "bob commit",
lint: (_pkgJSON$scripts$lint = pkgJSON == null ? void 0 : (_pkgJSON$scripts4 = pkgJSON.scripts) == null ? void 0 : _pkgJSON$scripts4.lint) != null ? _pkgJSON$scripts$lint : "bob lint",
release: (_pkgJSON$scripts$rele = pkgJSON == null ? void 0 : (_pkgJSON$scripts5 = pkgJSON.scripts) == null ? void 0 : _pkgJSON$scripts5.release) != null ? _pkgJSON$scripts$rele : "bob publish",
test: (_pkgJSON$scripts$test = pkgJSON == null ? void 0 : (_pkgJSON$scripts6 = pkgJSON.scripts) == null ? void 0 : _pkgJSON$scripts6.test) != null ? _pkgJSON$scripts$test : "bob test"
})),
dependencies: pkgJSON == null ? void 0 : pkgJSON.dependencies,
peerDependencies: pkgJSON == null ? void 0 : pkgJSON.peerDependencies,
devDependencies: pkgJSON == null ? void 0 : pkgJSON.devDependencies,
eslintConfig: (_pkgJSON$eslintConfig = pkgJSON == null ? void 0 : pkgJSON.eslintConfig) != null ? _pkgJSON$eslintConfig : {
"extends": "eslint-config-bob"
},
husky: (_pkgJSON$husky = pkgJSON == null ? void 0 : pkgJSON.husky) != null ? _pkgJSON$husky : {
hooks: {
"commit-msg": "bob lint commit"
}
},
npmpackagejsonlint: (_pkgJSON$npmpackagejs = pkgJSON == null ? void 0 : pkgJSON.npmpackagejsonlint) != null ? _pkgJSON$npmpackagejs : {
"extends": "npm-package-json-lint-config-bob"
}
};
return (0, _structure.getPackageJSON)().write(_extends({}, ordered, except(pkgJSON, ordered)));
});
return function (_x4) {
return _ref3.apply(this, arguments);
};
}());
});
return function (_x3) {
return _ref2.apply(this, arguments);
};
}());
});
return function (_x2) {
return _ref.apply(this, arguments);
};
}());
});
return _initPackageJSON.apply(this, arguments);
}
function initPackageJSONAction() {
initPackageJSON();
}
var _default = (0, _commanderVersion.createCommand)("package-json").description("initialize package.json").action(initPackageJSONAction);
exports["default"] = _default;