@nx/plugin
Version:
72 lines (71 loc) • 2.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchPackageJsonForPlugin = patchPackageJsonForPlugin;
exports.uniq = uniq;
exports.runPackageManagerInstall = runPackageManagerInstall;
exports.newNxProject = newNxProject;
exports.ensureNxProject = ensureNxProject;
const devkit_1 = require("@nx/devkit");
const devkit_2 = require("@nx/devkit");
const child_process_1 = require("child_process");
const node_fs_1 = require("node:fs");
const path_1 = require("path");
const paths_1 = require("./paths");
const utils_1 = require("./utils");
function runNxNewCommand(args, silent) {
const localTmpDir = (0, path_1.dirname)((0, paths_1.tmpProjPath)());
return (0, child_process_1.execSync)(`node ${require.resolve('nx')} new proj --nx-workspace-root=${localTmpDir} --no-interactive --skip-install --collection=@nx/workspace --npmScope=proj --preset=apps ${args || ''}`, {
cwd: localTmpDir,
...(silent && false ? { stdio: ['ignore', 'ignore', 'ignore'] } : {}),
windowsHide: false,
});
}
function patchPackageJsonForPlugin(npmPackageName, distPath) {
const path = (0, paths_1.tmpProjPath)('package.json');
const json = (0, devkit_2.readJsonFile)(path);
json.devDependencies[npmPackageName] = `file:${devkit_1.workspaceRoot}/${distPath}`;
(0, devkit_2.writeJsonFile)(path, json);
}
/**
* Generate a unique name for running CLI commands
* @param prefix
*
* @returns `'<prefix><random number>'`
*/
function uniq(prefix) {
return `${prefix}${Math.floor(Math.random() * 10000000)}`;
}
/**
* Run the appropriate package manager install command in the e2e directory
* @param silent silent output from the install
*/
function runPackageManagerInstall(silent = true) {
const cwd = (0, paths_1.tmpProjPath)();
const pmc = (0, devkit_2.getPackageManagerCommand)((0, devkit_1.detectPackageManager)(cwd));
const install = (0, child_process_1.execSync)(pmc.install, {
cwd,
...(silent ? { stdio: ['ignore', 'ignore', 'ignore'] } : {}),
windowsHide: false,
});
return install ? install.toString() : '';
}
/**
* Creates a new nx project in the e2e directory
*
* @param npmPackageName package name to test
* @param pluginDistPath dist path where the plugin was outputted to
*/
function newNxProject(npmPackageName, pluginDistPath) {
(0, utils_1.cleanup)();
runNxNewCommand('', true);
patchPackageJsonForPlugin(npmPackageName, pluginDistPath);
runPackageManagerInstall();
}
/**
* Ensures that a project has been setup in the e2e directory
* It will also copy `@nx` packages to the e2e directory
*/
function ensureNxProject(npmPackageName, pluginDistPath) {
(0, node_fs_1.mkdirSync)((0, paths_1.tmpProjPath)(), { recursive: true });
newNxProject(npmPackageName, pluginDistPath);
}
;