Skip to content

Commit

Permalink
feat(nx): improve the dev ergonomics of create-nx-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jul 27, 2019
1 parent 6d6cbf8 commit 6fd181f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 39 deletions.
6 changes: 6 additions & 0 deletions docs/api-workspace/schematics/ng-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ ng generate ng-new ...

## Options

### appName

Type: `string`

Application name.

### commit

Default: `true`
Expand Down
6 changes: 6 additions & 0 deletions docs/api-workspace/schematics/tao-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ ng generate tao-new ...

## Options

### appName

Type: `string`

Application name.

### commit

Default: `true`
Expand Down
130 changes: 94 additions & 36 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,28 @@ const nxVersion = 'NX_VERSION';
const angularCliVersion = 'ANGULAR_CLI_VERSION';

const parsedArgs = yargsParser(process.argv, {
string: ['cli', 'preset'],
string: ['cli', 'preset', 'appName'],
alias: {
appName: 'app-name'
},
boolean: ['help']
});

if (parsedArgs.help) {
showHelp();
process.exit(0);
}
validateInput(parsedArgs);
const packageManager = determinePackageManager();
determinePreset(parsedArgs).then(preset => {
return determineCli(preset, parsedArgs).then(cli => {
const tmpDir = createSandbox(packageManager, cli);
createApp(tmpDir, cli, parsedArgs, preset);
showNxWarning();
showCliWarning(preset, parsedArgs);
determineWorkspaceName(parsedArgs).then(name => {
determinePreset(parsedArgs).then(preset => {
return determineAppName(preset, parsedArgs).then(appName => {
return determineCli(preset, parsedArgs).then(cli => {
const tmpDir = createSandbox(packageManager, cli);
createApp(tmpDir, cli, parsedArgs, name, preset, appName);
showNxWarning();
showCliWarning(preset, parsedArgs);
});
});
});
});

Expand All @@ -73,6 +79,8 @@ function showHelp() {
.map(o => '"' + o.value + '"')
.join(', ')})
appName the name of the application created by some presets
cli CLI to power the Nx workspace (options: "nx", "angular")
[new workspace options] any 'new workspace' options
Expand Down Expand Up @@ -103,32 +111,46 @@ function determinePackageManager() {
return packageManager;
}

function validateInput(parsedArgs: any) {
const projectName = parsedArgs._[2];
function determineWorkspaceName(parsedArgs: any) {
const workspaceName = parsedArgs._[2];

if (!projectName) {
output.error({
title: 'A project name is required when creating a new workspace',
bodyLines: [
output.colors.gray('For example:'),
'',
`${output.colors.gray('>')} create-nx-workspace my-new-workspace`
]
});
process.exit(1);
if (workspaceName) {
return Promise.resolve(workspaceName);
} else {
return inquirer
.prompt([
{
name: 'WorkspaceName',
message: `Workspace name (e.g., org name) `,
type: 'string'
}
])
.then(a => {
if (!a.WorkspaceName) {
output.error({
title: 'Invalid workspace name',
bodyLines: [`Workspace name cannot be empty`]
});
process.exit(1);
}
return a.WorkspaceName;
});
}

return projectName;
return workspaceName;
}

function determinePreset(parsedArgs: any): Promise<string> {
if (parsedArgs.preset) {
if (presetOptions.map(o => o.value).indexOf(parsedArgs.preset) === -1) {
console.error(
`Invalid preset. It must be one of the following: ${presetOptions
.map(o => '"' + o.value + '"')
.join(', ')}.`
);
output.error({
title: 'Invalid preset',
bodyLines: [
`It must be one of the following:`,
'',
...presetOptions.map(o => o.value)
]
});
process.exit(1);
} else {
return Promise.resolve(parsedArgs.preset);
Expand All @@ -148,6 +170,35 @@ function determinePreset(parsedArgs: any): Promise<string> {
}
}

function determineAppName(preset: string, parsedArgs: any): Promise<string> {
if (preset === 'empty') {
return Promise.resolve('');
}

if (parsedArgs.appName) {
return Promise.resolve(parsedArgs.appName);
} else {
return inquirer
.prompt([
{
name: 'AppName',
message: `Application name `,
type: 'string'
}
])
.then(a => {
if (!a.AppName) {
output.error({
title: 'Invalid name',
bodyLines: [`Name cannot be empty`]
});
process.exit(1);
}
return a.AppName;
});
}
}

function determineCli(preset: string, parsedArgs: any) {
const angular = {
package: '@angular/cli',
Expand All @@ -163,9 +214,10 @@ function determineCli(preset: string, parsedArgs: any) {

if (parsedArgs.cli) {
if (['nx', 'angular'].indexOf(parsedArgs.cli) === -1) {
console.error(
`Invalid cli. It must be one of the following: "nx", "angular".`
);
output.error({
title: 'Invalid cli',
bodyLines: [`It must be one of the following:`, '', 'nx', 'angular']
});
process.exit(1);
}
return Promise.resolve(parsedArgs.cli === 'angular' ? angular : nx);
Expand Down Expand Up @@ -231,16 +283,22 @@ function createApp(
tmpDir: string,
cli: { command: string },
parsedArgs: any,
preset: string
name: string,
preset: string,
appName: string
) {
// creating the app itself
const args = process.argv
.slice(2)
.filter(a => !a.startsWith('--cli')) // not used by the new command
.map(a => `"${a}"`)
.join(' ');
const args = [
name,
...process.argv
.slice(parsedArgs._[2] ? 3 : 2)
.filter(a => !a.startsWith('--cli')) // not used by the new command
.map(a => `"${a}"`)
].join(' ');

const presetArg = parsedArgs.preset ? '' : ` --preset=${preset}`;
const presetArg = parsedArgs.preset
? ''
: ` --preset=${preset} --appName=${appName}`;

console.log(`new ${args}${presetArg} --collection=@nrwl/workspace`);
execSync(
Expand Down
4 changes: 4 additions & 0 deletions packages/workspace/src/schematics/ng-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
}
]
}
},
"appName": {
"type": "string",
"description": "Application name."
}
}
}
2 changes: 1 addition & 1 deletion packages/workspace/src/schematics/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function connectFrontendAndApi(options: Schema) {
)
]);

const scope = options.npmScope ? options.npmScope : options.name;
const scope = options.npmScope;
const style = options.style ? options.style : 'css';
host.overwrite(
`apps/${options.name}/src/app/app.component.ts`,
Expand Down
9 changes: 7 additions & 2 deletions packages/workspace/src/schematics/shared-new/shared-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { platform } from 'os';
export interface Schema {
directory: string;
name: string;
appName: string;
npmScope?: string;
skipInstall?: boolean;
skipGit?: boolean;
Expand Down Expand Up @@ -62,11 +63,15 @@ function createPresetTaskExecutor(cli: string, opts: Schema) {
const args = [
`g`,
`@nrwl/workspace:preset`,
`--name=${opts.name}`,
`--name=${opts.appName}`,
opts.style ? `--style=${opts.style}` : null,
opts.npmScope ? `--npmScope=${opts.npmScope}` : null,
opts.npmScope
? `--npmScope=${opts.npmScope}`
: `--npmScope=${opts.name}`,
opts.preset ? `--preset=${opts.preset}` : null
].filter(e => !!e);

console.log('here', path.join(process.cwd(), opts.directory));
return new Observable(obs => {
spawn(executable, args, spawnOptions).on('close', (code: number) => {
if (code === 0) {
Expand Down
4 changes: 4 additions & 0 deletions packages/workspace/src/schematics/tao-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
}
]
}
},
"appName": {
"type": "string",
"description": "Application name."
}
}
}

0 comments on commit 6fd181f

Please sign in to comment.