Skip to content

Commit

Permalink
feat(angular): add routing option to CNW
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 10, 2023
1 parent 762b9f3 commit a2dd9f0
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 2 deletions.
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 @@ -125,6 +125,12 @@ Type: `string`

Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "react-native", "expo", "next", "nest", "express", "react", "angular", "node-server"]. To build your own see https://nx.dev/packages/nx-plugin#preset

### routing

Type: `string`

Add a routing setup when a preset with pregenerated app is selected

### skipGit

Type: `boolean`
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 @@ -125,6 +125,12 @@ Type: `string`

Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "react-native", "expo", "next", "nest", "express", "react", "angular", "node-server"]. To build your own see https://nx.dev/packages/nx-plugin#preset

### routing

Type: `string`

Add a routing setup when a preset with pregenerated app is selected

### skipGit

Type: `boolean`
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 @@ -20,6 +20,11 @@
"type": "string",
"default": "css"
},
"routing": {
"description": "Add routing to the generated application.",
"type": "boolean",
"default": true
},
"npmScope": {
"type": "string",
"description": "Npm scope for importing libs."
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 @@ -24,6 +24,11 @@
"enum": ["eslint"],
"default": "eslint"
},
"routing": {
"description": "Add routing to the generated application.",
"type": "boolean",
"default": true
},
"style": {
"description": "The file extension to be used for style files.",
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function runCreateWorkspace(
useDetectedPm = false,
cwd = e2eCwd,
bundler,
routing,
}: {
preset: string;
appName?: string;
Expand All @@ -149,6 +150,7 @@ export function runCreateWorkspace(
useDetectedPm?: boolean;
cwd?: string;
bundler?: 'webpack' | 'vite';
routing?: boolean;
}
) {
projName = name;
Expand All @@ -170,6 +172,10 @@ export function runCreateWorkspace(
command += ` --bundler=${bundler}`;
}

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

if (base) {
command += ` --defaultBase="${base}"`;
}
Expand Down
22 changes: 21 additions & 1 deletion e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,31 @@ describe('create-nx-workspace', () => {

afterEach(() => cleanupProject());

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

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

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

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

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

checkFilesExist('package.json');
Expand Down Expand Up @@ -112,6 +129,7 @@ describe('create-nx-workspace', () => {
style: 'css',
appName,
packageManager,
routing: true,
});
});

Expand All @@ -127,6 +145,7 @@ describe('create-nx-workspace', () => {
style: 'css',
appName,
packageManager,
routing: true,
});
} catch (e) {
expect(e).toBeTruthy();
Expand Down Expand Up @@ -293,6 +312,7 @@ describe('create-nx-workspace', () => {
appName,
style: 'css',
packageManager: 'npm',
routing: true,
});

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 @@ -33,6 +33,7 @@ type Arguments = {
framework: string;
docker: boolean;
nxCloud: boolean;
routing: string;
allPrompts: boolean;
packageManager: PackageManager;
defaultBase: string;
Expand Down Expand Up @@ -143,6 +144,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('routing', {
describe: chalk.dim`Add a routing setup when a preset with pregenerated app is selected`,
type: 'string',
})
.option('bundler', {
describe: chalk.dim`Bundler to be used to build the application`,
type: 'string',
Expand Down Expand Up @@ -227,6 +232,7 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
preset,
appName,
style,
routing,
nxCloud,
packageManager,
defaultBase,
Expand Down Expand Up @@ -257,6 +263,7 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
preset,
appName,
style,
routing,
nxCloud,
defaultBase,
framework,
Expand Down Expand Up @@ -314,7 +321,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, routing;

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

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

if (preset === Preset.AngularMonorepo) {
routing = await determineRouting(argv);
}
}
style = await determineStyle(preset, argv);
}
Expand All @@ -386,6 +401,7 @@ async function getConfiguration(
preset,
appName,
style,
routing,
framework,
nxCloud,
packageManager,
Expand Down Expand Up @@ -855,6 +871,36 @@ async function determineStyle(
return Promise.resolve(parsedArgs.style);
}

async function determineRouting(
parsedArgs: yargs.Arguments<Arguments>
): Promise<string> {
if (!parsedArgs.routing) {
return enquirer
.prompt([
{
name: 'routing',
message: 'Would you like to add routing?',
type: 'autocomplete',
choices: [
{
name: 'Yes',
},

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

return parsedArgs.routing;
}

async function determineBundler(
parsedArgs: yargs.Arguments<Arguments>
): Promise<'vite' | 'webpack'> {
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
opts.docker ? `--docker=${opts.docker}` : null,
opts.packageManager ? `--packageManager=${opts.packageManager}` : null,
parsedArgs.interactive ? '--interactive=true' : '--interactive=false',
opts.routing !== undefined ? `--routing=${opts.routing}` : null,
].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';
routing?: 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 @@ -20,6 +20,11 @@
"type": "string",
"default": "css"
},
"routing": {
"description": "Add routing to the generated application.",
"type": "boolean",
"default": true
},
"npmScope": {
"type": "string",
"description": "Npm scope for importing libs."
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,
routing: options.routing,
});
} else if (options.preset === Preset.AngularStandalone) {
const {
Expand All @@ -42,6 +43,7 @@ async function createPreset(tree: Tree, options: Schema) {
name: options.name,
style: options.style,
linter: options.linter,
routing: options.routing,
rootProject: true,
});
} else if (options.preset === Preset.ReactMonorepo) {
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 @@ -12,4 +12,5 @@ export interface Schema {
packageManager?: PackageManager;
bundler?: 'vite' | 'webpack';
docker?: boolean;
routing?: 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 @@ -24,6 +24,11 @@
"enum": ["eslint"],
"default": "eslint"
},
"routing": {
"description": "Add routing to the generated application.",
"type": "boolean",
"default": true
},
"style": {
"description": "The file extension to be used for style files.",
"type": "string",
Expand Down

0 comments on commit a2dd9f0

Please sign in to comment.