-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add the setup-prettier generator (#27996)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- 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 #
- Loading branch information
1 parent
3e1a879
commit 72cd1c1
Showing
14 changed files
with
298 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "setup-prettier", | ||
"factory": "./src/generators/setup-prettier/generator", | ||
"schema": { | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "NxJsSetupPrettier", | ||
"title": "Setup Prettier", | ||
"description": "Setup Prettier as the formatting tool.", | ||
"type": "object", | ||
"properties": { | ||
"skipFormat": { | ||
"description": "Skip formatting files.", | ||
"type": "boolean", | ||
"default": false, | ||
"x-priority": "internal" | ||
}, | ||
"skipPackageJson": { | ||
"description": "Do not add dependencies to `package.json`.", | ||
"type": "boolean", | ||
"default": false, | ||
"x-priority": "internal" | ||
} | ||
}, | ||
"required": [], | ||
"presets": [] | ||
}, | ||
"description": "Setup Prettier as the formatting tool.", | ||
"implementation": "/packages/js/src/generators/setup-prettier/generator.ts", | ||
"aliases": [], | ||
"hidden": false, | ||
"path": "/packages/js/src/generators/setup-prettier/schema.json", | ||
"type": "generator" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/js/src/generators/setup-prettier/generator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { readJson, writeJson, type Tree } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { prettierVersion } from '../../utils/versions'; | ||
import { setupPrettierGenerator } from './generator'; | ||
|
||
describe('setup-prettier generator', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
// remove the default generated .prettierrc file | ||
tree.delete('.prettierrc'); | ||
}); | ||
|
||
it('should install prettier package', async () => { | ||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies['prettier']).toBe(prettierVersion); | ||
}); | ||
|
||
it('should create .prettierrc and .prettierignore files', async () => { | ||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
const prettierrc = readJson(tree, '.prettierrc'); | ||
expect(prettierrc).toEqual({ singleQuote: true }); | ||
const prettierignore = tree.read('.prettierignore', 'utf-8'); | ||
expect(prettierignore).toMatch(/\n\/coverage/); | ||
expect(prettierignore).toMatch(/\n\/dist/); | ||
expect(prettierignore).toMatch(/\n\/\.nx\/cache/); | ||
}); | ||
|
||
it('should not overwrite existing .prettierrc and .prettierignore files', async () => { | ||
writeJson(tree, '.prettierrc', { singleQuote: false }); | ||
tree.write('.prettierignore', `# custom ignore file`); | ||
|
||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
const prettierrc = readJson(tree, '.prettierrc'); | ||
expect(prettierrc).toEqual({ singleQuote: false }); | ||
const prettierignore = tree.read('.prettierignore', 'utf-8'); | ||
expect(prettierignore).toContain('# custom ignore file'); | ||
}); | ||
|
||
it('should not overwrite prettier configuration specified in other formats', async () => { | ||
tree.delete('.prettierrc'); | ||
tree.delete('.prettierignore'); | ||
tree.write('.prettierrc.js', `module.exports = { singleQuote: true };`); | ||
|
||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
expect(tree.exists('.prettierrc')).toBeFalsy(); | ||
expect(tree.exists('.prettierignore')).toBeTruthy(); | ||
expect(tree.read('.prettierrc.js', 'utf-8')).toContain( | ||
`module.exports = { singleQuote: true };` | ||
); | ||
}); | ||
|
||
it('should add prettier vscode extension if .vscode/extensions.json file exists', async () => { | ||
// No existing recommendations | ||
writeJson(tree, '.vscode/extensions.json', {}); | ||
|
||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
let json = readJson(tree, '.vscode/extensions.json'); | ||
expect(json).toEqual({ | ||
recommendations: ['esbenp.prettier-vscode'], | ||
}); | ||
|
||
// Existing recommendations | ||
writeJson(tree, '.vscode/extensions.json', { recommendations: ['foo'] }); | ||
|
||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
json = readJson(tree, '.vscode/extensions.json'); | ||
expect(json).toEqual({ | ||
recommendations: ['foo', 'esbenp.prettier-vscode'], | ||
}); | ||
}); | ||
|
||
it('should skip adding prettier extension if .vscode/extensions.json file does not exist', async () => { | ||
await setupPrettierGenerator(tree, { skipFormat: true }); | ||
|
||
expect(tree.exists('.vscode/extensions.json')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
ensurePackage, | ||
formatFiles, | ||
type GeneratorCallback, | ||
type Tree, | ||
} from '@nx/devkit'; | ||
import { generatePrettierSetup } from '../../utils/prettier'; | ||
import { prettierVersion } from '../../utils/versions'; | ||
import type { GeneratorOptions } from './schema'; | ||
|
||
export async function setupPrettierGenerator( | ||
tree: Tree, | ||
options: GeneratorOptions | ||
): Promise<GeneratorCallback> { | ||
const prettierTask = generatePrettierSetup(tree, { | ||
skipPackageJson: options.skipPackageJson, | ||
}); | ||
|
||
if (!options.skipFormat) { | ||
if (!options.skipPackageJson) { | ||
ensurePackage('prettier', prettierVersion); | ||
} | ||
// even if skipPackageJson === true, we can safely run formatFiles, prettier might | ||
// have been installed earlier and if not, the formatFiles function still handles it | ||
await formatFiles(tree); | ||
} | ||
|
||
return prettierTask; | ||
} | ||
|
||
export default setupPrettierGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface GeneratorOptions { | ||
skipFormat?: boolean; | ||
skipPackageJson?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "NxJsSetupPrettier", | ||
"title": "Setup Prettier", | ||
"description": "Setup Prettier as the formatting tool.", | ||
"type": "object", | ||
"properties": { | ||
"skipFormat": { | ||
"description": "Skip formatting files.", | ||
"type": "boolean", | ||
"default": false, | ||
"x-priority": "internal" | ||
}, | ||
"skipPackageJson": { | ||
"description": "Do not add dependencies to `package.json`.", | ||
"type": "boolean", | ||
"default": false, | ||
"x-priority": "internal" | ||
} | ||
}, | ||
"required": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.