create-nx-workspace
Version:
71 lines (70 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEmptyWorkspace = createEmptyWorkspace;
const ora = require("ora");
const path_1 = require("path");
const child_process_utils_1 = require("./utils/child-process-utils");
const error_utils_1 = require("./utils/error-utils");
const output_1 = require("./utils/output");
const package_manager_1 = require("./utils/package-manager");
const unparse_1 = require("./utils/unparse");
/**
* Create a new Nx workspace
* @param tmpDir temporary directory to invoke nx cli
* @param name name of new nx workspace
* @param packageManager current package manager
* @param options options to pass to nx cli
* @returns
*/
async function createEmptyWorkspace(tmpDir, name, packageManager, options) {
// Ensure to use packageManager for args
// if it's not already passed in from previous process
if (!options.packageManager) {
options.packageManager = packageManager;
}
const directory = options.name;
// Cannot skip install for create-nx-workspace or else it'll fail.
// Even though --skipInstall is not an option to create-nx-workspace, we pass through extra options to presets.
// See: https://github.com/nrwl/nx/issues/31834
delete options.skipInstall;
const args = (0, unparse_1.unparse)({
...options,
}).join(' ');
const pmc = (0, package_manager_1.getPackageManagerCommand)(packageManager);
const command = `new ${args}`;
const workingDir = process.cwd().replace(/\\/g, '/');
let nxWorkspaceRoot = `"${workingDir}"`;
// If path contains spaces there is a problem in Windows for npm@6.
// In this case we have to escape the wrapping quotes.
if (process.platform === 'win32' &&
/\s/.test(nxWorkspaceRoot) &&
packageManager === 'npm') {
const pmVersion = +(0, package_manager_1.getPackageManagerVersion)(packageManager, tmpDir).split('.')[0];
if (pmVersion < 7) {
nxWorkspaceRoot = `\\"${nxWorkspaceRoot.slice(1, -1)}\\"`;
}
}
let workspaceSetupSpinner = ora(`Creating your workspace in ${directory}`).start();
try {
const fullCommand = `${pmc.exec} nx ${command} --nxWorkspaceRoot=${nxWorkspaceRoot}`;
await (0, child_process_utils_1.execAndWait)(fullCommand, tmpDir);
workspaceSetupSpinner.succeed(`Successfully created the workspace: ${directory}`);
}
catch (e) {
workspaceSetupSpinner.fail();
if (e instanceof Error) {
output_1.output.error({
title: `Failed to create a workspace.`,
bodyLines: (0, error_utils_1.mapErrorToBodyLines)(e),
});
}
else {
console.error(e);
}
process.exit(1);
}
finally {
workspaceSetupSpinner.stop();
}
return (0, path_1.join)(workingDir, directory);
}