UNPKG

md-generator

Version:

NPM Package which bootstraps Development by creating all required .md files to meet community standards.

210 lines (209 loc) 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var inquirer_1 = tslib_1.__importDefault(require("inquirer")); var index_1 = require("../../questions/setupQuestions/index"); var index_2 = require("../../../common/index"); var askQuestions_1 = require("../../questions/askQuestions"); var fileWriter_1 = require("../../../templates/fileWriter"); var actionsUtils_1 = require("../actionsUtils"); var lodash_1 = require("lodash"); exports.shouldOverride = function (existingFiles, noneExistingFiles) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var files; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, inquirer_1.default .prompt(index_1.overrideFiles(existingFiles)) .then(function (item) { var override = item.override; if (!override && lodash_1.isEmpty(noneExistingFiles[0])) return process.exit(1); if (override) return noneExistingFiles.concat(existingFiles); return noneExistingFiles; })]; case 1: files = _a.sent(); return [2, files]; } }); }); }; exports.getValidFiles = function (values) { var list = typeof values[0] === 'object' ? index_2.getFullFileNames(values) : values; var _a = actionsUtils_1.queryFilesExistence(list), foundFiles = _a.foundFiles, filesNotFound = _a.filesNotFound; var validFileNames = filesNotFound.filter(function (key) { return Object.keys(actionsUtils_1.allFiles).includes(key); }); var inValidFileNames = filesNotFound.filter(function (key) { return !Object.keys(actionsUtils_1.allFiles).includes(key); }); return { validFileNames: validFileNames, inValidFileNames: inValidFileNames, foundFiles: foundFiles, }; }; exports.handleOverride = function (values) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var validFileNames, _a, inValidFileNames, foundFiles; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: validFileNames = exports.getValidFiles(values).validFileNames; _a = exports.getValidFiles(values), inValidFileNames = _a.inValidFileNames, foundFiles = _a.foundFiles; if (!(foundFiles.length > 0)) return [3, 2]; return [4, exports.shouldOverride(foundFiles, validFileNames)]; case 1: validFileNames = _b.sent(); _b.label = 2; case 2: return [2, { validFileNames: validFileNames, inValidFileNames: inValidFileNames }]; } }); }); }; exports.createMdFiles = function (USE_DEFAULT, filesToBeCreated, isEmpty) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var _a, validFileNames, inValidFileNames, files; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: return [4, exports.handleOverride(filesToBeCreated)]; case 1: _a = _b.sent(), validFileNames = _a.validFileNames, inValidFileNames = _a.inValidFileNames; if (validFileNames.length === 0 && inValidFileNames.length === 0) { index_2.useHelpAlert({ errorText: 'Error: No option selected, Press <space> to select, <a> to toggle all, <i> to invert selection', override: true }); process.exit(1); } if (validFileNames.length === 0 && inValidFileNames.length > 0) { index_2.useHelpAlert({ errorText: inValidFileNames + " not supported,", override: false }); process.exit(1); } if (inValidFileNames.length > 0 && validFileNames.length > 0) { index_2.useHelpAlert({ errorText: "The following files are not supported and will not be created " + inValidFileNames, override: true }); } files = validFileNames; if (validFileNames.includes('README') || validFileNames.includes('LICENSE')) USE_DEFAULT = false; return [4, askQuestions_1.getInfos(USE_DEFAULT, files).then(function (projectInfos) { files.forEach(function (file) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var pathToTemplate, _a, path, templatePath, fileContent, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: _a = actionsUtils_1.getItemFromFileName(file), path = _a.path, templatePath = _a.templatePath; if (file === 'LICENSE') { pathToTemplate = projectInfos.licenseName.path; } else { pathToTemplate = templatePath || projectInfos.templatePath; } if (!isEmpty) return [3, 1]; _b = ''; return [3, 3]; case 1: return [4, fileWriter_1.buildFileContent(projectInfos, pathToTemplate)]; case 2: _b = _c.sent(); _c.label = 3; case 3: fileContent = _b; return [4, fileWriter_1.writeFile(fileContent, path)]; case 4: _c.sent(); return [2]; } }); }); }); })]; case 2: _b.sent(); index_2.spinner().succeed('File(s) created Successfully\n'); index_2.showEndMessage(); return [2]; } }); }); }; var processCreation = function (allItems, mode, IS_EMPTY_FILE) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var USE_DEFAULT_VALUES, CREATE_EMPTY_FILE, filesToBeCreated; return tslib_1.__generator(this, function (_a) { USE_DEFAULT_VALUES = true; CREATE_EMPTY_FILE = false; return [2, inquirer_1.default.prompt(mode(actionsUtils_1.getArrayOfValues(allItems))) .then(function (answer) { var createFiles = answer.createFiles; if (createFiles === false) return process.exit(1); inquirer_1.default.prompt(index_1.createEmptyFiles()).then(function (res) { CREATE_EMPTY_FILE = IS_EMPTY_FILE || res.empty; if (typeof createFiles === 'object') { filesToBeCreated = Object.values(allItems).filter(function (file) { return createFiles.includes(file.name); }); if (createFiles.includes('README.md') || createFiles.includes('CODE_OF_CONDUCT.md') || createFiles.includes('LICENSE')) { return exports.createMdFiles(!USE_DEFAULT_VALUES, filesToBeCreated, CREATE_EMPTY_FILE); } } if (createFiles === true) { return exports.createMdFiles(!USE_DEFAULT_VALUES, allItems, CREATE_EMPTY_FILE); } return exports.createMdFiles(USE_DEFAULT_VALUES, createFiles, CREATE_EMPTY_FILE); }); })]; }); }); }; exports.checkCreatableFiles = function (values) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var _a, validFileNames, inValidFileNames, foundFiles, validFiles; return tslib_1.__generator(this, function (_b) { _a = exports.getValidFiles(values), validFileNames = _a.validFileNames, inValidFileNames = _a.inValidFileNames, foundFiles = _a.foundFiles; validFiles = validFileNames.concat(foundFiles); if (inValidFileNames.length > 0) { index_2.unrecognizedFileAlert(inValidFileNames); } if (validFiles.length > 0) { validFiles = Object.values(actionsUtils_1.allFiles).filter(function (item) { return validFiles.includes(item.name.split('.')[0]); }); } else { index_2.fileNotDetectedAlert(); process.exit(1); } return [2, validFiles]; }); }); }; var createNonSpecificFiles = function () { processCreation(actionsUtils_1.allFiles, index_1.selectFileToCreate); }; exports.createSpecificFiles = function (isEmpty, values) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var files; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, exports.checkCreatableFiles(values)]; case 1: files = _a.sent(); processCreation(files, index_1.createFiles, isEmpty); return [2]; } }); }); }; var createRequiredFiles = function (isEmpty) { processCreation(actionsUtils_1.requiredFiles, index_1.createRequired, isEmpty); }; var createOptionalFiles = function (isEmpty) { processCreation(actionsUtils_1.optionalFiles, index_1.createOptional, isEmpty); }; var createHandler = function (values) { var file = values.file, required = values.required, optional = values.optional, isEmpty = values.isEmpty, resp = values.resp; if (file) return exports.createSpecificFiles(isEmpty, resp); if (required) return createRequiredFiles(isEmpty); if (optional) return createOptionalFiles(isEmpty); createNonSpecificFiles(); }; exports.default = createHandler;