@pega/custom-dx-components
Version:
Utility for building custom UI components
82 lines (64 loc) • 3.09 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { join } from 'path';
import { URL, fileURLToPath } from 'url';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { checkPathAccess, showVersion, updateUseWebPackPromotion, getLibraryBased, addDebugLog } from '../../util.js';
import { WEBPACK_CONFIG_JS_FILENAME, WEBPACK_PATHS_JS_FILENAME, PACKAGE_JSON_FILENAME, BUILDER_DIR_FILE_PATH } from '../../constants.js';
export const copyWebPack = async () => {
addDebugLog('copyWebPack', '', '');
// first check to see if already have the file
const newWebPackConfigDir = path.resolve(WEBPACK_CONFIG_JS_FILENAME);
const newWebPackPathsDir = path.resolve(WEBPACK_PATHS_JS_FILENAME);
let override = true;
if (fs.existsSync(newWebPackConfigDir)) {
console.log(`Promoted file ${chalk.bold.yellow(`${newWebPackConfigDir}`)} already exists.`);
const questions = [
{
name: 'override',
type: 'confirm',
message: `Overwrite`,
default: false
}
];
await inquirer.prompt(questions).then(async answers => {
override = answers.override;
});
}
if (override) {
const currentDirectory = process.cwd();
const packageJsonPath = join(currentDirectory, PACKAGE_JSON_FILENAME);
await checkPathAccess(packageJsonPath);
let data = fs.readFileSync(packageJsonPath, { encoding: 'utf8' });
data = JSON.parse(data);
// if top level package.json is of type=module, then promote standard webpack.config.js that is being
// used in builder (along with paths.js file), otherwise promote the local copy in promote-webpack directory, which is a version
// modified to support "require" instead of "import" (and paths code is inside the webpack.config.js file)
if (data['type'] && data['type'] === 'module') {
const builderIndexDir = fileURLToPath(new URL(BUILDER_DIR_FILE_PATH, import.meta.url));
const promoteWebPackConfigDir = path.resolve(path.join(builderIndexDir, WEBPACK_CONFIG_JS_FILENAME));
const promoteWebPackPathsDir = path.resolve(path.join(builderIndexDir, WEBPACK_PATHS_JS_FILENAME));
fs.copyFileSync(promoteWebPackConfigDir, newWebPackConfigDir);
fs.copyFileSync(promoteWebPackPathsDir, newWebPackPathsDir);
} else {
const newWebPackDir = path.resolve(WEBPACK_CONFIG_JS_FILENAME);
const promoteWebPackDir = fileURLToPath(new URL(WEBPACK_CONFIG_JS_FILENAME, import.meta.url));
fs.copyFileSync(promoteWebPackDir, newWebPackDir);
}
console.log(chalk.bold.green(`Bundler now using ${newWebPackConfigDir}.`));
console.log(chalk.bold.green(`Utilize this file for webpack modifications.`));
}
};
export default async options => {
const isLibraryBased = getLibraryBased();
if (isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('non library mode')} components.`);
process.exit();
}
await showVersion();
addDebugLog('promoteWebPack', '', '+');
await copyWebPack();
await updateUseWebPackPromotion(true);
addDebugLog('promoteWebPack', 'END', '-');
};