nx/packages/create-nx-workspace/src/create-empty-workspace.ts
Leosvel Pérez Espinosa 4cc3dc6960
fix(misc): create workspaces and default app with the name as provided (#23196)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #19253
2024-05-08 17:33:47 +04:00

88 lines
2.4 KiB
TypeScript

import * as ora from 'ora';
import { join } from 'path';
import { CreateWorkspaceOptions } from './create-workspace-options';
import { execAndWait } from './utils/child-process-utils';
import { mapErrorToBodyLines } from './utils/error-utils';
import { output } from './utils/output';
import {
getPackageManagerCommand,
getPackageManagerVersion,
PackageManager,
} from './utils/package-manager';
import { unparse } from './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
*/
export async function createEmptyWorkspace<T extends CreateWorkspaceOptions>(
tmpDir: string,
name: string,
packageManager: PackageManager,
options: T
): Promise<string> {
// 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;
const args = unparse({
...options,
}).join(' ');
const pmc = 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 = +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 execAndWait(fullCommand, tmpDir);
workspaceSetupSpinner.succeed(
`Successfully created the workspace: ${directory}.`
);
} catch (e) {
workspaceSetupSpinner.fail();
if (e instanceof Error) {
output.error({
title: `Failed to create a workspace.`,
bodyLines: mapErrorToBodyLines(e),
});
} else {
console.error(e);
}
process.exit(1);
} finally {
workspaceSetupSpinner.stop();
}
return join(workingDir, directory);
}