Skip to content

Commit d39624c

Browse files
authored
feat(js): @nx/js:init generator does not generate prettier and tsconfig.base.json files by default (#27406)
In order to use `nx release` you must run `nx add @nx/js`. However, the init generator will add Prettier and `tsconfig.base.json` files by default, which is not what the user wants. This PR adds two options: - `addTsConfigBase` - generates `tsconfig.base.json` when `true`. Default to`false` - `setUpPrettier` - adds `prettier` and generates `.prettierrc` and `.prettierignore` files when `true`. Defaults to `false`. The programmatic `initGenerator` API defaults both options to `true` to usages remain unaffected. The one behavior change is if users run `nx g @nx/js:init` then the two options default to `false`. However, since this is an internal API, the actual usages should be through `nx add` or the programmatic API. <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## 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 #
1 parent 22b654a commit d39624c

File tree

5 files changed

+97
-34
lines changed

5 files changed

+97
-34
lines changed

docs/generated/packages/js/generators/init.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,22 @@
3030
"description": "Keep existing dependencies versions",
3131
"default": false
3232
},
33+
"addTsConfigBase": {
34+
"type": "boolean",
35+
"description": "Add a base tsconfig file to the workspace.",
36+
"x-priority": "internal",
37+
"default": false
38+
},
3339
"tsConfigName": {
3440
"type": "string",
35-
"description": "Customize the generated tsconfig file name.",
41+
"description": "Customize the generated base tsconfig file name.",
3642
"x-priority": "internal"
43+
},
44+
"setUpPrettier": {
45+
"type": "boolean",
46+
"description": "Add Prettier and corresponding configuration files.",
47+
"x-priority": "internal",
48+
"default": false
3749
}
3850
},
3951
"presets": []

packages/js/src/generators/init/init.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ describe('js init generator', () => {
88

99
beforeEach(() => {
1010
tree = createTreeWithEmptyWorkspace();
11+
// Remove files that should be part of the init generator
12+
tree.delete('tsconfig.base.json');
13+
tree.delete('.prettierrc');
1114
});
1215

1316
it('should install prettier package', async () => {
@@ -117,4 +120,23 @@ describe('js init generator', () => {
117120
typescriptVersion
118121
);
119122
});
123+
124+
it('should support skipping base tsconfig file', async () => {
125+
await init(tree, {
126+
addTsConfigBase: false,
127+
});
128+
129+
expect(tree.exists('tsconfig.base.json')).toBeFalsy();
130+
});
131+
132+
it('should support skipping prettier setup', async () => {
133+
await init(tree, {
134+
setUpPrettier: false,
135+
});
136+
137+
const packageJson = readJson(tree, 'package.json');
138+
expect(packageJson.devDependencies['prettier']).toBeUndefined();
139+
expect(tree.exists('.prettierignore')).toBeFalsy();
140+
expect(tree.exists('.prettierrc')).toBeFalsy();
141+
});
120142
});

packages/js/src/generators/init/init.ts

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ async function getInstalledTypescriptVersion(
6363
export async function initGenerator(
6464
tree: Tree,
6565
schema: InitSchema
66+
): Promise<GeneratorCallback> {
67+
return initGeneratorInternal(tree, {
68+
addTsConfigBase: true,
69+
setUpPrettier: true,
70+
...schema,
71+
});
72+
}
73+
74+
export async function initGeneratorInternal(
75+
tree: Tree,
76+
schema: InitSchema
6677
): Promise<GeneratorCallback> {
6778
const tasks: GeneratorCallback[] = [];
6879
// add tsconfig.base.json
69-
if (!getRootTsConfigFileName(tree)) {
80+
if (schema.addTsConfigBase && !getRootTsConfigFileName(tree)) {
7081
generateFiles(tree, join(__dirname, './files'), '.', {
7182
fileName: schema.tsConfigName ?? 'tsconfig.base.json',
7283
});
7384
}
7485
const devDependencies = {
7586
'@nx/js': nxVersion,
76-
prettier: prettierVersion,
7787
// When loading .ts config files (e.g. webpack.config.ts, jest.config.ts, etc.)
7888
// we prefer to use SWC, and fallback to ts-node for workspaces that don't use SWC.
7989
'@swc-node/register': swcNodeVersion,
@@ -94,40 +104,45 @@ export async function initGenerator(
94104
}
95105
}
96106

97-
// https://prettier.io/docs/en/configuration.html
98-
const prettierrcNameOptions = [
99-
'.prettierrc',
100-
'.prettierrc.json',
101-
'.prettierrc.yml',
102-
'.prettierrc.yaml',
103-
'.prettierrc.json5',
104-
'.prettierrc.js',
105-
'.prettierrc.cjs',
106-
'.prettierrc.mjs',
107-
'.prettierrc.toml',
108-
'prettier.config.js',
109-
'prettier.config.cjs',
110-
'prettier.config.mjs',
111-
];
112-
113-
if (prettierrcNameOptions.every((name) => !tree.exists(name))) {
114-
writeJson(tree, '.prettierrc', {
115-
singleQuote: true,
116-
});
117-
}
107+
if (schema.setUpPrettier) {
108+
devDependencies['prettier'] = prettierVersion;
109+
110+
// https://prettier.io/docs/en/configuration.html
111+
const prettierrcNameOptions = [
112+
'.prettierrc',
113+
'.prettierrc.json',
114+
'.prettierrc.yml',
115+
'.prettierrc.yaml',
116+
'.prettierrc.json5',
117+
'.prettierrc.js',
118+
'.prettierrc.cjs',
119+
'.prettierrc.mjs',
120+
'.prettierrc.toml',
121+
'prettier.config.js',
122+
'prettier.config.cjs',
123+
'prettier.config.mjs',
124+
];
125+
126+
if (prettierrcNameOptions.every((name) => !tree.exists(name))) {
127+
writeJson(tree, '.prettierrc', {
128+
singleQuote: true,
129+
});
130+
}
118131

119-
if (!tree.exists(`.prettierignore`)) {
120-
tree.write(
121-
'.prettierignore',
122-
stripIndents`
132+
if (!tree.exists(`.prettierignore`)) {
133+
tree.write(
134+
'.prettierignore',
135+
stripIndents`
123136
# Add files here to ignore them from prettier formatting
124137
/dist
125138
/coverage
126139
/.nx/cache
127140
/.nx/workspace-data
128141
`
129-
);
142+
);
143+
}
130144
}
145+
131146
if (tree.exists('.vscode/extensions.json')) {
132147
updateJson(tree, '.vscode/extensions.json', (json) => {
133148
json.recommendations ??= [];
@@ -150,9 +165,9 @@ export async function initGenerator(
150165
: () => {};
151166
tasks.push(installTask);
152167

153-
ensurePackage('prettier', prettierVersion);
154-
if (!schema.skipFormat) {
155-
await formatFiles(tree);
168+
if (schema.setUpPrettier) {
169+
ensurePackage('prettier', prettierVersion);
170+
if (!schema.skipFormat) await formatFiles(tree);
156171
}
157172

158173
return async () => {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export interface InitSchema {
2+
addTsConfigBase?: boolean;
23
js?: boolean;
4+
keepExistingVersions?: boolean;
5+
setUpPrettier?: boolean;
36
skipFormat?: boolean;
47
skipPackageJson?: boolean;
5-
keepExistingVersions?: boolean;
68
tsConfigName?: string;
79
}

packages/js/src/generators/init/schema.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@
2727
"description": "Keep existing dependencies versions",
2828
"default": false
2929
},
30+
"addTsConfigBase": {
31+
"type": "boolean",
32+
"description": "Add a base tsconfig file to the workspace.",
33+
"x-priority": "internal",
34+
"default": false
35+
},
3036
"tsConfigName": {
3137
"type": "string",
32-
"description": "Customize the generated tsconfig file name.",
38+
"description": "Customize the generated base tsconfig file name.",
3339
"x-priority": "internal"
40+
},
41+
"setUpPrettier": {
42+
"type": "boolean",
43+
"description": "Add Prettier and corresponding configuration files.",
44+
"x-priority": "internal",
45+
"default": false
3446
}
3547
}
3648
}

0 commit comments

Comments
 (0)