@pega/custom-dx-components
Version:
Utility for building custom UI components
125 lines (88 loc) • 4.14 kB
JavaScript
// import { exec } from 'child_process';
const path = require('path');
const fs = require('fs');
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const tasksConfigDir = path.resolve('../../..', 'tasks.config.json');
const tasksConfigData = fs.readFileSync(tasksConfigDir, { encoding: 'utf8' });
let tasksConfigJSON = JSON.parse(tasksConfigData);
const packageJSONDir = path.resolve('../../..', 'package.json');
const packageJSONData = fs.readFileSync(packageJSONDir, { encoding: 'utf8'});
let packageJSON = JSON.parse(packageJSONData);
let isLibraryBased = tasksConfigJSON['libraryMode'];
const savedLibraryBased = isLibraryBased;
const savedOrganization = packageJSON.organization;
const serverType = tasksConfigJSON['server-config'].serverType;
const serverDir = serverType === 'launchpad' ? 'Launchpad' : 'Constellation';
let compType = isLibraryBased ? "library" : "original";
describe(`promote revert webpack: ${serverDir} - ${compType}`, () => {
beforeAll( async() => {
// make non libraryBased
isLibraryBased = false;
tasksConfigJSON['libraryMode'] = isLibraryBased;
compType = isLibraryBased ? "library" : "original";
fs.writeFileSync(tasksConfigDir, JSON.stringify(tasksConfigJSON, null, 4), { encoding: 'utf8' });
// make organzation Pega
packageJSON.organization = "Pega";
fs.writeFileSync(packageJSONDir, JSON.stringify(packageJSON, null, 4), { encoding: 'utf8' });
const componentsDir = path.join(path.resolve('../../..'), 'src/components');
if (fs.existsSync(componentsDir)) {
fs.rmSync(componentsDir, { recursive: true, force: true, maxRetries: 2 });
fs.mkdirSync(componentsDir, { recursive: true });
}
await new Promise((r) => setTimeout(r, 2000));
});
afterAll( async() => {
//restore
isLibraryBased = savedLibraryBased
tasksConfigJSON['libraryMode'] = isLibraryBased;
fs.writeFileSync(tasksConfigDir, JSON.stringify(tasksConfigJSON, null, 4), { encoding: 'utf8' });
// return organzation
packageJSON.organization = savedOrganization;
fs.writeFileSync(packageJSONDir, JSON.stringify(packageJSON, null, 4), { encoding: 'utf8' });
const componentsDir = path.join(path.resolve('../../..'), 'src/components');
if (fs.existsSync(componentsDir)) {
fs.rmSync(componentsDir, { recursive: true, force: true, maxRetries: 2 });
fs.mkdirSync(componentsDir, { recursive: true });
}
await new Promise((r) => setTimeout(r, 2000));
});
it('npm run promoteWebPack', async () => {
const script = `npm run promoteWebPack --prefix ../../..`;
const { stdout, stderr } = await exec(script);
// console.log(stdout);
expect(stdout).not.toBeNull();
expect(stderr).not.toBeNull();
// want message to contain component name
expect(stdout).toContain('Bundler now using');
expect(stdout).toContain('webpack.config.js');
// utilize
expect(stdout).toContain('Utilize this file for webpack modifications.');
// verify new file exists
const webPackFile = path.resolve('../../..', 'webpack.config.js');
expect(fs.existsSync(webPackFile)).toBeTruthy();
// if (doesExist) {
// // remove it
// fs.rmdirSync(fileDir, { recursive: true });
// }
}, 10000);
it('npm run revertWebPack', async () => {
const script = `npm run revertWebPack --prefix ../../..`;
const { stdout, stderr } = await exec(script);
// console.log(stdout);
expect(stdout).not.toBeNull();
expect(stderr).not.toBeNull();
// want message to contain component name
expect(stdout).toContain('marked as not used.');
expect(stdout).toContain('webpack.config.js');
// message with bundler
expect(stdout).toContain('Bundler reverted to using internal webpack.config.js.');
// verify file marked as not used
const webPackFile = path.resolve('../../..', 'webpack.config.js.notused');
expect(fs.existsSync(webPackFile)).toBeTruthy();
// if (doesExist) {
// // remove it
// fs.rmdirSync(fileDir, { recursive: true });
// }
}, 10000);
});