You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

98 lines
4.1 KiB

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifyHelperUtil = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const yeoman_environment_1 = __importDefault(require("yeoman-environment"));
const yeoman_generator_1 = __importDefault(require("yeoman-generator"));
const scaffold_1 = require("./scaffold");
const webpack_cli_1 = require("webpack-cli");
const { logger } = webpack_cli_1.utils;
const DEFAULT_WEBPACK_CONFIG_FILENAME = 'webpack.config.js';
function modifyHelperUtil(generator, configFile = DEFAULT_WEBPACK_CONFIG_FILENAME, packages, autoSetDefaults = false, generationPath = '.') {
const configPath = null;
const env = yeoman_environment_1.default.createEnv('webpack', { cwd: generationPath });
const generatorName = 'webpack-init-generator';
if (!generator) {
generator = class extends yeoman_generator_1.default {
initializing() {
packages.forEach((pkgPath) => {
return this.composeWith(require.resolve(pkgPath), {});
});
}
};
}
// this is the default name that the yeoman generator uses when writing
// to .yo-rc.json
// see: https://github.com/yeoman/generator/blob/v4.5.0/lib/index.js#L773
let packageName = '*';
try {
const packagePath = path_1.default.resolve(generationPath, 'package.json');
if (fs_1.default.existsSync(packagePath)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageData = require(packagePath);
if (packageData && packageData.name) {
packageName = packageData.name;
}
}
}
catch (err) {
logger.error('Your package.json was incorrectly formatted.');
Error.stackTraceLimit = 0;
process.exitCode = 2;
}
env.registerStub(generator, generatorName);
env.run(generatorName, {
configFile,
autoSetDefaults,
generationPath,
}, () => {
let configModule;
let finalConfig = {
config: {},
};
try {
const confPath = path_1.default.resolve(generationPath, '.yo-rc.json');
configModule = require(confPath);
}
catch (err) {
logger.error('Could not find a yeoman configuration file (.yo-rc.json).');
logger.error("Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.");
Error.stackTraceLimit = 0;
process.exitCode = 2;
}
try {
// the configuration stored in .yo-rc.json should already be in the correct
// WebpackScaffoldObject format
// it is labeled with the name property from the user's package.json, meaning
// we should simply access that value, rather than iterating through all
// the configs that are stored in .yo-rc.json
if (configModule[packageName] && configModule[packageName].configuration) {
finalConfig = configModule[packageName].configuration;
}
}
catch (err) {
logger.error(err);
logger.error(`${err.stack}\n`);
logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n');
Error.stackTraceLimit = 0;
process.exitCode = 2;
}
try {
const transformConfig = Object.assign({
configFile: !configPath ? null : fs_1.default.readFileSync(configPath, 'utf8'),
configPath,
}, finalConfig);
// scaffold webpack config file from using .yo-rc.json
return scaffold_1.runTransform(transformConfig, 'init', generationPath);
}
catch (error) {
logger.error(error);
process.exitCode = 2;
}
});
}
exports.modifyHelperUtil = modifyHelperUtil;