fix(core): fix error message on formatFiles schematic and generator (#8003)
This commit is contained in:
parent
1560a43acd
commit
f2ffd83d4d
@ -160,15 +160,15 @@ function check(patterns: string[]) {
|
||||
}
|
||||
|
||||
function updateWorkspaceJsonToMatchFormatVersion() {
|
||||
const workspaceConfig = workspaceConfigName(appRootPath);
|
||||
const workspaceConfigPath = workspaceConfigName(appRootPath);
|
||||
try {
|
||||
const workspaceJson = readJsonFile(workspaceConfig);
|
||||
const workspaceJson = readJsonFile(workspaceConfigPath);
|
||||
const reformatted = reformattedWorkspaceJsonOrNull(workspaceJson);
|
||||
if (reformatted) {
|
||||
writeJsonFile(workspaceConfig, reformatted);
|
||||
writeJsonFile(workspaceConfigPath, reformatted);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Failed to format: ${path}`);
|
||||
console.error(`Failed to format workspace config: ${workspaceConfigPath}`);
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,27 @@ describe('formatFiles', () => {
|
||||
.spyOn(prettier, 'format')
|
||||
.mockImplementation((input) => `formatted :: ${input}`);
|
||||
tree = Tree.empty();
|
||||
|
||||
tree.create(
|
||||
'workspace.json',
|
||||
`{
|
||||
"version": 1,
|
||||
"projects": {
|
||||
"app1": {
|
||||
"projectType": "application",
|
||||
"schematics": {},
|
||||
"root": "apps/app1",
|
||||
"sourceRoot": "apps/app1/src",
|
||||
"prefix": "nx",
|
||||
"architect": {}
|
||||
}
|
||||
},
|
||||
"cli": {
|
||||
"defaultCollection": "@nrwl/angular"
|
||||
},
|
||||
"defaultProject": "app1"
|
||||
}`
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => jest.clearAllMocks());
|
||||
@ -84,6 +105,18 @@ describe('formatFiles', () => {
|
||||
expect(result.read('b.ts').toString()).toEqual('formatted :: const a=a');
|
||||
});
|
||||
|
||||
it('should have a readable error when workspace file cannot be formatted', async () => {
|
||||
tree.delete('workspace.json');
|
||||
|
||||
const errorSpy = jest.spyOn(console, 'error');
|
||||
|
||||
await schematicRunner.callRule(formatFiles(), tree).toPromise();
|
||||
|
||||
expect(errorSpy).toHaveBeenCalledWith(
|
||||
'Failed to format workspace config: workspace.json'
|
||||
);
|
||||
});
|
||||
|
||||
describe('--skip-format', () => {
|
||||
it('should not format created files', async () => {
|
||||
jest.spyOn(prettier, 'resolveConfig').mockReturnValue(
|
||||
|
||||
@ -80,18 +80,20 @@ export function formatFiles(
|
||||
}
|
||||
|
||||
function updateWorkspaceJsonToMatchFormatVersion(host: Tree) {
|
||||
const workspaceConfig = workspaceConfigName(appRootPath);
|
||||
const workspaceConfigPath = workspaceConfigName(appRootPath);
|
||||
|
||||
try {
|
||||
if (workspaceConfig) {
|
||||
const workspaceJson = parseJson(host.read(workspaceConfig).toString());
|
||||
if (workspaceConfigPath) {
|
||||
const workspaceJson = parseJson(
|
||||
host.read(workspaceConfigPath).toString()
|
||||
);
|
||||
const reformatted = reformattedWorkspaceJsonOrNull(workspaceJson);
|
||||
if (reformatted) {
|
||||
host.overwrite(workspaceConfig, serializeJson(reformatted));
|
||||
host.overwrite(workspaceConfigPath, serializeJson(reformatted));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Failed to format: ${path}`);
|
||||
console.error(`Failed to format workspace config: ${workspaceConfigPath}`);
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user