-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
format.ts
138 lines (123 loc) · 3.45 KB
/
format.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
import { execSync } from 'child_process';
import * as path from 'path';
import * as resolve from 'resolve';
import { getProjectRoots, parseFiles } from './shared';
import { fileExists } from '../utils/fileutils';
import { output } from '../utils/output';
import { createProjectGraph } from '../core/project-graph';
import { filterAffected } from '../core/affected-project-graph';
import { calculateFileChanges } from '../core/file-utils';
import * as yargs from 'yargs';
import { NxArgs, splitArgsIntoNxArgsAndOverrides } from './utils';
const PRETTIER_EXTENSIONS = [
'ts',
'js',
'tsx',
'jsx',
'scss',
'less',
'css',
'html',
'json',
'md',
'mdx',
];
export function format(command: 'check' | 'write', args: yargs.Arguments) {
let patterns: string[];
const { nxArgs } = splitArgsIntoNxArgsAndOverrides(args, 'affected');
try {
patterns = getPatterns({
...args,
...nxArgs,
} as any);
} catch (e) {
output.error({
title: e.message,
bodyLines: [
`Pass the SHA range: ${output.bold(
`npm run format:${command} -- --base=SHA1 --head=SHA2`
)}`,
'',
`Or pass the list of files: ${output.bold(
`npm run format:${command} -- --files="libs/mylib/index.ts,libs/mylib2/index.ts"`
)}`,
],
});
process.exit(1);
}
// Chunkify the patterns array to prevent crashing the windows terminal
const chunkList: string[][] = chunkify(patterns, 50);
switch (command) {
case 'write':
chunkList.forEach((chunk) => write(chunk));
break;
case 'check':
chunkList.forEach((chunk) => check(chunk));
break;
}
}
function getPatterns(args: NxArgs & { libsAndApps: boolean; _: string[] }) {
const allFilesPattern = [`"**/*.{${PRETTIER_EXTENSIONS.join(',')}}"`];
try {
if (args.all) {
return allFilesPattern;
}
const p = parseFiles(args);
let patterns = p.files
.filter((f) => fileExists(f))
.filter((f) =>
PRETTIER_EXTENSIONS.map((ext) => '.' + ext).includes(path.extname(f))
);
const libsAndApp = args.libsAndApps;
return libsAndApp
? getPatternsFromApps(patterns)
: patterns.map((f) => `"${f}"`);
} catch (e) {
return allFilesPattern;
}
}
function getPatternsFromApps(affectedFiles: string[]): string[] {
const graph = createProjectGraph();
const affectedGraph = filterAffected(
graph,
calculateFileChanges(affectedFiles)
);
const roots = getProjectRoots(Object.keys(affectedGraph.nodes));
return roots.map(
(root) => `"${root}/**/*.{${PRETTIER_EXTENSIONS.join(',')}}"`
);
}
function chunkify(target: string[], size: number): string[][] {
return target.reduce((current: string[][], value: string, index: number) => {
if (index % size === 0) current.push([]);
current[current.length - 1].push(value);
return current;
}, []);
}
function write(patterns: string[]) {
if (patterns.length > 0) {
execSync(`node "${prettierPath()}" --write ${patterns.join(' ')}`, {
stdio: [0, 1, 2],
});
}
}
function check(patterns: string[]) {
if (patterns.length > 0) {
try {
execSync(
`node "${prettierPath()}" --list-different ${patterns.join(' ')}`,
{
stdio: [0, 1, 2],
}
);
} catch (e) {
process.exit(1);
}
}
}
function prettierPath() {
const basePath = path.dirname(
resolve.sync('prettier', { basedir: __dirname })
);
return path.join(basePath, 'bin-prettier.js');
}