"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const yeoman_generator_1 = __importDefault(require("yeoman-generator")); const copy_utils_1 = require("./utils/copy-utils"); const webpack_cli_1 = require("webpack-cli"); const { logger, getPackageManager } = webpack_cli_1.utils; /** * Creates a Yeoman Generator that generates a project conforming * to webpack-defaults. * * @param {Generator.Questions} prompts An array of Yeoman prompt objects * * @param {string} templateDir Absolute path to template directory * * @param {string[]} copyFiles An array of file paths (relative to `./templates`) * of files to be copied to the generated project. File paths should be of the * form `path/to/file.js.tpl`. * * @param {string[]} copyTemplateFiles An array of file paths (relative to * `./templates`) of files to be copied to the generated project. Template * file paths should be of the form `path/to/_file.js.tpl`. * * @param {Function} templateFn A function that is passed a generator instance and * returns an object containing data to be supplied to the template files. * * @returns {Generator} A class extending Generator */ const addonGenerator = (prompts, templateDir, copyFiles, copyTemplateFiles, templateFn) => { return class extends yeoman_generator_1.default { prompting() { return this.prompt(prompts).then((props) => { this.props = props; }); } default() { const currentDirName = path_1.default.basename(this.destinationPath()); if (currentDirName !== this.props.name) { this.log(` Your project must be inside a folder named ${this.props.name} I will create this folder for you. `); const pathToProjectDir = this.destinationPath(this.props.name); try { fs_1.default.mkdirSync(pathToProjectDir, { recursive: true }); } catch (error) { logger.error('Failed to create directory'); logger.error(error); } this.destinationRoot(pathToProjectDir); } } writing() { const packageJsonTemplatePath = '../addon-template/package.json.js'; // eslint-disable-next-line @typescript-eslint/no-var-requires this.fs.extendJSON(this.destinationPath('package.json'), require(packageJsonTemplatePath)(this.props.name)); this.copy = copy_utils_1.generatorCopy(this, templateDir); this.copyTpl = copy_utils_1.generatorCopyTpl(this, templateDir, templateFn(this)); copyFiles.forEach(this.copy); copyTemplateFiles.forEach(this.copyTpl); } install() { const packager = getPackageManager(); const opts = packager === 'yarn' ? { dev: true } : { 'save-dev': true }; this.scheduleInstallTask(packager, ['webpack-defaults', 'bluebird'], opts); } }; }; exports.default = addonGenerator;