Skip to content

Commit

Permalink
feat(core): add standalone api prompt to CNW
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 10, 2023
1 parent 4d21915 commit 63bb705
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/generated/cli/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ Default: `false`

Skip initializing a git repository.

### standaloneApi

Type: `string`

Use Standalone APIs if generating an Angular app

### style

Type: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ Default: `false`

Skip initializing a git repository.

### standaloneApi

Type: `string`

Use Standalone APIs if generating an Angular app

### style

Type: `string`
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/workspace/generators/new.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"type": "string",
"description": "Npm scope for importing libs."
},
"standaloneApi": {
"description": "Use the Standalone APIs if generating an Angular application.",
"type": "boolean",
"default": false
},
"defaultBase": {
"type": "string",
"description": "Default base branch for affected."
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/workspace/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
]
}
},
"standaloneApi": {
"description": "Use the Standalone APIs if generating an Angular application.",
"type": "boolean",
"default": false
},
"standaloneConfig": {
"description": "Split the project configurations into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
Expand Down
6 changes: 6 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function runCreateWorkspace(
useDetectedPm = false,
cwd = e2eCwd,
bundler,
standaloneApi,
}: {
preset: string;
appName?: string;
Expand All @@ -155,6 +156,7 @@ export function runCreateWorkspace(
useDetectedPm?: boolean;
cwd?: string;
bundler?: 'webpack' | 'vite';
standaloneApi?: boolean;
}
) {
projName = name;
Expand All @@ -178,6 +180,10 @@ export function runCreateWorkspace(
command += ` --bundler=${bundler}`;
}

if (standaloneApi !== undefined) {
command += ` --standaloneApi=${standaloneApi}`;
}

if (base) {
command += ` --defaultBase="${base}"`;
}
Expand Down
20 changes: 20 additions & 0 deletions e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ describe('create-nx-workspace', () => {

checkFilesExist('package.json');
checkFilesExist('project.json');
checkFilesExist('src/app/app.module.ts');
});

it('should create a workspace with a single angular app at the root using standalone APIs', () => {
const wsName = uniq('angular');

runCreateWorkspace(wsName, {
preset: 'angular-standalone',
appName: wsName,
style: 'css',
packageManager,
standaloneApi: true,
});

checkFilesExist('package.json');
checkFilesExist('project.json');
checkFilesDoNotExist('src/app/app.module.ts');
});

it('should create a workspace with a single react app with vite at the root', () => {
Expand Down Expand Up @@ -112,6 +129,7 @@ describe('create-nx-workspace', () => {
style: 'css',
appName,
packageManager,
standaloneApi: false,
});
});

Expand All @@ -127,6 +145,7 @@ describe('create-nx-workspace', () => {
style: 'css',
appName,
packageManager,
standaloneApi: false,
});
} catch (e) {
expect(e).toBeTruthy();
Expand Down Expand Up @@ -294,6 +313,7 @@ describe('create-nx-workspace', () => {
style: 'css',
packageManager: 'npm',
cli: 'angular',
standaloneApi: false,
});

checkFilesDoNotExist('yarn.lock');
Expand Down
48 changes: 47 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Arguments = {
cli: string;
style: string;
framework: string;
standaloneApi: string;
docker: boolean;
nxCloud: boolean;
allPrompts: boolean;
Expand Down Expand Up @@ -149,6 +150,10 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
describe: chalk.dim`Style option to be used when a preset with pregenerated app is selected`,
type: 'string',
})
.option('standaloneApi', {
describe: chalk.dim`Use Standalone APIs if generating an Angular app`,
type: 'string',
})
.option('bundler', {
describe: chalk.dim`Bundler to be used to build the application`,
type: 'string',
Expand Down Expand Up @@ -234,6 +239,7 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
preset,
appName,
style,
standaloneApi,
nxCloud,
packageManager,
defaultBase,
Expand Down Expand Up @@ -265,6 +271,7 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
preset,
appName,
style,
standaloneApi,
nxCloud,
defaultBase,
framework,
Expand Down Expand Up @@ -322,7 +329,7 @@ async function getConfiguration(
argv: yargs.Arguments<Arguments>
): Promise<void> {
try {
let name, appName, style, preset, framework, bundler, docker;
let name, appName, style, preset, framework, bundler, docker, standaloneApi;

output.log({
title:
Expand Down Expand Up @@ -374,12 +381,20 @@ async function getConfiguration(
if (preset === Preset.ReactStandalone) {
bundler = await determineBundler(argv);
}

if (preset === Preset.AngularStandalone) {
standaloneApi = await determineStandaloneApi(argv);
}
} else {
name = await determineRepoName(argv);
appName = await determineAppName(preset, argv);
if (preset === Preset.ReactMonorepo) {
bundler = await determineBundler(argv);
}

if (preset === Preset.AngularMonorepo) {
standaloneApi = await determineStandaloneApi(argv);
}
}
style = await determineStyle(preset, argv);
}
Expand All @@ -395,6 +410,7 @@ async function getConfiguration(
preset,
appName,
style,
standaloneApi,
framework,
cli,
nxCloud,
Expand Down Expand Up @@ -739,6 +755,36 @@ async function determineFramework(
return Promise.resolve(parsedArgs.framework);
}

async function determineStandaloneApi(
parsedArgs: yargs.Arguments<Arguments>
): Promise<string> {
if (parsedArgs.standaloneApi === undefined) {
return enquirer
.prompt([
{
name: 'standaloneApi',
message: 'Would you like to use Standalone APIs in your application?',
type: 'autocomplete',
choices: [
{
name: 'Yes',
},

{
name: 'No',
},
],
initial: 'No' as any,
},
])
.then((a: { standaloneApi: 'Yes' | 'No' }) =>
a.standaloneApi === 'Yes' ? 'true' : 'false'
);
}

return parsedArgs.standaloneApi;
}

async function determineDockerfile(
parsedArgs: yargs.Arguments<Arguments>
): Promise<boolean> {
Expand Down
3 changes: 3 additions & 0 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
opts.framework ? `--framework=${opts.framework}` : null,
opts.docker ? `--docker=${opts.docker}` : null,
opts.packageManager ? `--packageManager=${opts.packageManager}` : null,
opts.standaloneApi !== undefined
? `--standaloneApi=${opts.standaloneApi}`
: null,
parsedArgs.interactive ? '--interactive=true' : '--interactive=false',
].filter((e) => !!e);
}
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Schema {
docker?: boolean;
linter?: Linter;
bundler?: 'vite' | 'webpack';
standaloneApi?: boolean;
packageManager?: PackageManager;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/workspace/src/generators/new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"type": "string",
"description": "Npm scope for importing libs."
},
"standaloneApi": {
"description": "Use the Standalone APIs if generating an Angular application.",
"type": "boolean",
"default": false
},
"defaultBase": {
"type": "string",
"description": "Default base branch for affected."
Expand Down
2 changes: 2 additions & 0 deletions packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function createPreset(tree: Tree, options: Schema) {
name: options.name,
style: options.style,
linter: options.linter,
standalone: options.standaloneApi,
});
} else if (options.preset === Preset.AngularStandalone) {
const {
Expand All @@ -43,6 +44,7 @@ async function createPreset(tree: Tree, options: Schema) {
style: options.style,
linter: options.linter,
rootProject: true,
standalone: options.standaloneApi,
});
} else if (options.preset === Preset.ReactMonorepo) {
const {
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/preset/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Schema {
packageManager?: PackageManager;
bundler?: 'vite' | 'webpack';
docker?: boolean;
standaloneApi?: boolean;
}
5 changes: 5 additions & 0 deletions packages/workspace/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
]
}
},
"standaloneApi": {
"description": "Use the Standalone APIs if generating an Angular application.",
"type": "boolean",
"default": false
},
"standaloneConfig": {
"description": "Split the project configurations into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
Expand Down

0 comments on commit 63bb705

Please sign in to comment.