/
format-files.ts
143 lines (130 loc) · 3.72 KB
/
format-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { Tree } from 'nx/src/shared/tree';
import * as path from 'path';
import type * as Prettier from 'prettier';
import { getWorkspacePath } from '../utils/get-workspace-layout';
import { readJson, updateJson, writeJson } from '../utils/json';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
import {
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from './project-configuration';
/**
* Formats all the created or updated files using Prettier
* @param tree - the file system tree
*/
export async function formatFiles(tree: Tree): Promise<void> {
let prettier: typeof Prettier;
try {
prettier = await import('prettier');
} catch {}
ensurePropertiesAreInNewLocations(tree);
sortWorkspaceJson(tree);
sortTsConfig(tree);
if (!prettier) return;
const files = new Set(
tree.listChanges().filter((file) => file.type !== 'DELETE')
);
await Promise.all(
Array.from(files).map(async (file) => {
const systemPath = path.join(tree.root, file.path);
let options: Prettier.FileInfoOptions = {
filepath: systemPath,
};
const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});
if (!resolvedOptions) {
return;
}
options = {
...options,
...resolvedOptions,
};
const support = await prettier.getFileInfo(systemPath, options);
if (support.ignored || !support.inferredParser) {
return;
}
try {
tree.write(
file.path,
prettier.format(file.content.toString('utf-8'), options)
);
} catch (e) {
console.warn(`Could not format ${file.path}. Error: "${e.message}"`);
}
})
);
}
function sortWorkspaceJson(tree: Tree) {
const workspaceJsonPath = getWorkspacePath(tree);
if (!workspaceJsonPath) {
return;
}
try {
const workspaceJson = readJson(tree, workspaceJsonPath);
if (Object.entries(workspaceJson.projects).length !== 0) {
const sortedProjects = sortObjectByKeys(workspaceJson.projects);
writeJson(tree, workspaceJsonPath, {
...workspaceJson,
projects: sortedProjects,
});
}
} catch (e) {
// catch noop
}
}
/**
* `updateWorkspaceConfiguration` already handles
* placing properties in their new locations, so
* reading + updating it ensures that props are placed
* correctly.
*/
function ensurePropertiesAreInNewLocations(tree: Tree) {
const workspacePath = getWorkspacePath(tree);
if (!workspacePath) {
return;
}
const wc = readWorkspaceConfiguration(tree);
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
wc.generators ??= json.generators ?? (json as any).schematics;
if (wc.cli) {
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
wc.cli.packageManager ??= json.cli?.packageManager;
} else if (json.cli) {
wc.cli ??= json.cli;
}
wc.defaultProject ??= json.defaultProject;
delete json.cli;
delete json.defaultProject;
delete (json as any).schematics;
delete json.generators;
return json;
});
updateWorkspaceConfiguration(tree, wc);
}
function sortTsConfig(tree: Tree) {
try {
const tsConfigPath = getRootTsConfigPath(tree);
if (!tsConfigPath) {
return;
}
updateJson(tree, tsConfigPath, (tsconfig) => ({
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
paths: sortObjectByKeys(tsconfig.compilerOptions.paths),
},
}));
} catch (e) {
// catch noop
}
}
function getRootTsConfigPath(tree: Tree): string | null {
for (const path of ['tsconfig.base.json', 'tsconfig.json']) {
if (tree.exists(path)) {
return path;
}
}
return null;
}