Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c25628a

Browse files
feat(misc): add linter flag to create-nx-workspace (#3826)
* feat(misc): add linter flag to create-nx-workspace * fix(core): prompt linter when preset is angular or angular-nest Co-authored-by: Jason Jean <[email protected]>
1 parent 3f436fc commit c25628a

File tree

11 files changed

+171
-104
lines changed

11 files changed

+171
-104
lines changed

e2e/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export function runCreateWorkspace(
9191
base?: string;
9292
}
9393
) {
94-
let command = `npx create-nx-workspace@${process.env.PUBLISHED_VERSION} ${name} --cli=${cli} --preset=${preset} --no-nxCloud --no-interactive`;
94+
const linterArg =
95+
preset === 'angular' || preset === 'angular-nest' ? ' --linter=tslint' : '';
96+
let command = `npx create-nx-workspace@${process.env.PUBLISHED_VERSION} ${name} --cli=${cli} --preset=${preset} ${linterArg} --no-nxCloud --no-interactive`;
9597
if (appName) {
9698
command += ` --appName=${appName}`;
9799
}

e2e/workspace/src/workspace-aux-commands.test.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -810,17 +810,9 @@ forEachCli((cli) => {
810810
expect(project).toBeTruthy();
811811
expect(project.root).toBe(newPath);
812812
expect(project.sourceRoot).toBe(`${newPath}/src`);
813-
if (workspace === 'angular') {
814-
expect(project.architect.lint.options.tsConfig).toEqual([
815-
`libs/shared/${lib1}/data-access/tsconfig.lib.json`,
816-
`libs/shared/${lib1}/data-access/tsconfig.spec.json`,
817-
]);
818-
}
819-
if (workspace === 'workspace') {
820-
expect(project.architect.lint.options.lintFilePatterns).toEqual([
821-
`libs/shared/${lib1}/data-access/**/*.ts`,
822-
]);
823-
}
813+
expect(project.architect.lint.options.lintFilePatterns).toEqual([
814+
`libs/shared/${lib1}/data-access/**/*.ts`,
815+
]);
824816

825817
/**
826818
* Check that the import in lib2 has been updated
@@ -955,17 +947,9 @@ forEachCli((cli) => {
955947
expect(project).toBeTruthy();
956948
expect(project.root).toBe(newPath);
957949
expect(project.sourceRoot).toBe(`${newPath}/src`);
958-
if (workspace === 'angular') {
959-
expect(project.architect.lint.options.tsConfig).toEqual([
960-
`libs/shared/${lib1}/data-access/tsconfig.lib.json`,
961-
`libs/shared/${lib1}/data-access/tsconfig.spec.json`,
962-
]);
963-
}
964-
if (workspace === 'workspace') {
965-
expect(project.architect.lint.options.lintFilePatterns).toEqual([
966-
`libs/shared/${lib1}/data-access/**/*.ts`,
967-
]);
968-
}
950+
expect(project.architect.lint.options.lintFilePatterns).toEqual([
951+
`libs/shared/${lib1}/data-access/**/*.ts`,
952+
]);
969953

970954
/**
971955
* Check that the import in lib2 has been updated
@@ -1102,17 +1086,9 @@ forEachCli((cli) => {
11021086
expect(project).toBeTruthy();
11031087
expect(project.root).toBe(newPath);
11041088
expect(project.sourceRoot).toBe(`${newPath}/src`);
1105-
if (workspace === 'angular') {
1106-
expect(project.architect.lint.options.tsConfig).toEqual([
1107-
`packages/shared/${lib1}/data-access/tsconfig.lib.json`,
1108-
`packages/shared/${lib1}/data-access/tsconfig.spec.json`,
1109-
]);
1110-
}
1111-
if (workspace === 'workspace') {
1112-
expect(project.architect.lint.options.lintFilePatterns).toEqual([
1113-
`packages/shared/${lib1}/data-access/**/*.ts`,
1114-
]);
1115-
}
1089+
expect(project.architect.lint.options.lintFilePatterns).toEqual([
1090+
`packages/shared/${lib1}/data-access/**/*.ts`,
1091+
]);
11161092

11171093
/**
11181094
* Check that the import in lib2 has been updated

packages/angular/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"@nrwl/cypress": "*",
4040
"@nrwl/jest": "*",
41+
"@nrwl/linter": "*",
4142
"@angular-devkit/schematics": "~10.1.3",
4243
"@schematics/angular": "~10.1.3",
4344
"jasmine-marbles": "~0.6.0"

packages/angular/src/schematics/library/lib/update-project.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MergeStrategy,
66
mergeWith,
77
move,
8+
noop,
89
Rule,
910
SchematicContext,
1011
template,
@@ -191,15 +192,17 @@ export function updateProject(options: NormalizedSchema): Rule {
191192
},
192193
};
193194
}),
194-
updateJsonInTree(`${options.projectRoot}/tslint.json`, (json) => {
195-
return {
196-
...json,
197-
extends: `${offsetFromRoot(options.projectRoot)}tslint.json`,
198-
linterOptions: {
199-
exclude: ['!**/*'],
200-
},
201-
};
202-
}),
195+
options.linter === Linter.TsLint
196+
? updateJsonInTree(`${options.projectRoot}/tslint.json`, (json) => {
197+
return {
198+
...json,
199+
extends: `${offsetFromRoot(options.projectRoot)}tslint.json`,
200+
linterOptions: {
201+
exclude: ['!**/*'],
202+
},
203+
};
204+
})
205+
: noop(),
203206
updateJsonInTree(`/nx.json`, (json) => {
204207
return {
205208
...json,

packages/angular/src/schematics/library/library.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export default function (schema: Schema): Rule {
5959
(host) => {
6060
host.delete('tsconfig.json');
6161
},
62-
6362
move(options.name, options.projectRoot),
6463
updateProject(options),
6564
updateTsConfig(options),

packages/angular/src/schematics/stories/stories-app.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ describe('angular:stories for applications', () => {
2121
appTree
2222
);
2323

24-
console.log(appTree);
25-
console.log(tree);
26-
2724
expect(tree.exists('apps/test-app/src/app/app.component.ts')).toBeTruthy();
2825
expect(
2926
tree.exists('apps/test-app/src/app/app.component.stories.ts')

packages/create-nx-workspace/bin/create-nx-workspace.ts

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,40 @@ const presetOptions = [
3030
'empty [an empty workspace with a layout that works best for building apps]',
3131
},
3232
{
33-
value: 'oss',
34-
name:
35-
'oss [an empty workspace with a layout that works best for open-source projects]',
36-
},
37-
{
38-
value: 'web-components',
39-
name:
40-
'web components [a workspace with a single app built using web components]',
33+
value: Preset.React,
34+
name: 'react [a workspace with a single React application]',
4135
},
4236
{
4337
value: Preset.Angular,
4438
name: 'angular [a workspace with a single Angular application]',
4539
},
4640
{
47-
value: Preset.AngularWithNest,
48-
name:
49-
'angular-nest [a workspace with a full stack application (Angular + Nest)]',
41+
value: Preset.NextJs,
42+
name: 'next.js [a workspace with a single Next.js application]',
5043
},
5144
{
5245
value: Preset.Nest,
5346
name: 'nest [a workspace with a single Nest application]',
5447
},
5548
{
56-
value: Preset.React,
57-
name: 'react [a workspace with a single React application]',
49+
value: 'web-components',
50+
name:
51+
'web components [a workspace with a single app built using web components]',
5852
},
5953
{
6054
value: Preset.ReactWithExpress,
6155
name:
6256
'react-express [a workspace with a full stack application (React + Express)]',
6357
},
6458
{
65-
value: Preset.NextJs,
66-
name: 'next.js [a workspace with a single Next.js application]',
59+
value: Preset.AngularWithNest,
60+
name:
61+
'angular-nest [a workspace with a full stack application (Angular + Nest)]',
62+
},
63+
{
64+
value: 'oss',
65+
name:
66+
'oss [an empty workspace with a layout that works best for open-source projects]',
6767
},
6868
];
6969

@@ -74,7 +74,7 @@ const angularCliVersion = 'ANGULAR_CLI_VERSION';
7474
const prettierVersion = 'PRETTIER_VERSION';
7575

7676
const parsedArgs = yargsParser(process.argv, {
77-
string: ['cli', 'preset', 'appName', 'style', 'defaultBase'],
77+
string: ['cli', 'preset', 'appName', 'style', 'linter', 'defaultBase'],
7878
alias: {
7979
appName: 'app-name',
8080
nxCloud: 'nx-cloud',
@@ -93,22 +93,25 @@ determineWorkspaceName(parsedArgs).then((name) => {
9393
return determineAppName(preset, parsedArgs).then((appName) => {
9494
return determineStyle(preset, parsedArgs).then((style) => {
9595
return determineCli(preset, parsedArgs).then((cli) => {
96-
return askAboutNxCloud(parsedArgs).then((cloud) => {
97-
const tmpDir = createSandbox(packageManager);
98-
createApp(
99-
tmpDir,
100-
cli,
101-
parsedArgs,
102-
name,
103-
preset,
104-
appName,
105-
style,
106-
cloud,
107-
parsedArgs.interactive,
108-
parsedArgs.defaultBase
109-
);
110-
showNxWarning(name);
111-
pointToTutorialAndCourse(preset);
96+
return determineLinter(cli, parsedArgs).then((linter) => {
97+
return askAboutNxCloud(parsedArgs).then((cloud) => {
98+
const tmpDir = createSandbox(packageManager);
99+
createApp(
100+
tmpDir,
101+
cli,
102+
parsedArgs,
103+
name,
104+
preset,
105+
appName,
106+
style,
107+
linter,
108+
cloud,
109+
parsedArgs.interactive,
110+
parsedArgs.defaultBase
111+
);
112+
showNxWarning(name);
113+
pointToTutorialAndCourse(preset);
114+
});
112115
});
113116
});
114117
});
@@ -137,7 +140,9 @@ function showHelp() {
137140
cli CLI to power the Nx workspace (options: "nx", "angular")
138141
139142
style Default style option to be used when a non-empty preset is selected
140-
options: ("css", "scss", "styl", "less") for React/Next.js also ("styled-components", "@emotion/styled")
143+
options: ("css", "scss", "styl", "less") for React/Next.js also ("styled-components", "@emotion/styled")
144+
145+
linter Default linter. Options: "eslint", "tslint".
141146
142147
interactive Enable interactive mode when using presets (boolean)
143148
@@ -376,6 +381,45 @@ function determineStyle(preset: Preset, parsedArgs: any) {
376381
return Promise.resolve(parsedArgs.style);
377382
}
378383

384+
function determineLinter(preset: Preset, parsedArgs: any) {
385+
if (!parsedArgs.linter) {
386+
if (preset === Preset.Angular || preset === Preset.AngularWithNest) {
387+
return inquirer
388+
.prompt([
389+
{
390+
name: 'linter',
391+
message: `Default linter `,
392+
default: 'tslint',
393+
type: 'list',
394+
choices: [
395+
{
396+
value: 'tslint',
397+
name: 'TSLint [ Used by Angular CLI ]',
398+
},
399+
{
400+
value: 'eslint',
401+
name: 'ESLint [ Modern linting tool ]',
402+
},
403+
],
404+
},
405+
])
406+
.then((a) => a.linter);
407+
} else {
408+
return Promise.resolve('eslint');
409+
}
410+
} else {
411+
if (parsedArgs.linter !== 'eslint' && parsedArgs.linter !== 'tslint') {
412+
output.error({
413+
title: 'Invalid linter',
414+
bodyLines: [`It must be one of the following:`, '', 'eslint', 'tslint'],
415+
});
416+
process.exit(1);
417+
} else {
418+
return Promise.resolve(parsedArgs.linter);
419+
}
420+
}
421+
}
422+
379423
function createSandbox(packageManager: string) {
380424
console.log(`Creating a sandbox with Nx...`);
381425
const tmpDir = dirSync().name;
@@ -408,6 +452,7 @@ function createApp(
408452
preset: Preset,
409453
appName: string,
410454
style: string | null,
455+
linter: string,
411456
nxCloud: boolean,
412457
interactive: boolean,
413458
defaultBase: string
@@ -424,6 +469,7 @@ function createApp(
424469
'nxCloud',
425470
'preset',
426471
'style',
472+
'linter',
427473
];
428474

429475
// These are the arguments that are passed to the schematic
@@ -434,14 +480,15 @@ function createApp(
434480

435481
const appNameArg = appName ? ` --appName="${appName}"` : ``;
436482
const styleArg = style ? ` --style="${style}"` : ``;
483+
const linterArg = ` --linter="${linter}"`;
437484
const nxCloudArg = nxCloud ? ` --nxCloud` : ``;
438485
const interactiveArg = interactive
439486
? ` --interactive=true`
440487
: ` --interactive=false`;
441488
const defaultBaseArg = defaultBase ? ` --defaultBase="${defaultBase}"` : ``;
442489

443490
const packageExec = getPackageManagerExecuteCommand(packageManager);
444-
const command = `new ${name} ${args} --preset="${preset}"${appNameArg}${styleArg}${nxCloudArg}${interactiveArg}${defaultBaseArg} --collection=@nrwl/workspace`;
491+
const command = `new ${name} ${args} --preset="${preset}"${appNameArg}${styleArg}${linterArg}${nxCloudArg}${interactiveArg}${defaultBaseArg} --collection=@nrwl/workspace`;
445492
console.log(command);
446493

447494
const collectionJsonPath = require.resolve(

packages/workspace/src/schematics/ng-new/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
"type": "string",
8787
"description": "Root directory.",
8888
"hidden": true
89+
},
90+
"linter": {
91+
"description": "The tool to use for running lint checks.",
92+
"type": "string",
93+
"enum": ["tslint", "eslint"],
94+
"default": "eslint"
8995
}
9096
}
9197
}

0 commit comments

Comments
 (0)