forked from cloudflare/cloudflare-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-pool.ts
More file actions
91 lines (72 loc) · 2.18 KB
/
format-pool.ts
File metadata and controls
91 lines (72 loc) · 2.18 KB
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
import * as fs from 'fs/promises';
import { join, resolve } from 'path';
import { Callback, Pool } from './pool';
import { options } from './prettier.config';
import type { Result } from './worker';
const ROOT = resolve('.');
const ROOTLEN = ROOT.length + 1;
const isSILENT = process.argv.includes('--quiet');
const isCHECK = process.argv.includes('--check');
const isFILE = /\.(mdx?|[mc]?[tj]sx?|json|ya?ml|s?css)$/;
// Unknown languages / missing parsers
const Missing = new Set<string>();
let warns = 0;
let errors = 0;
const task = new Pool({
script: join(ROOT, 'bin/worker.ts'),
spawn: {
execArgv: ['--loader', 'tsm'],
env: { NODE_NO_WARNINGS: '1' },
workerData: { isCHECK, ROOTLEN, options },
},
});
const handler: Callback<Result> = (err, result) => {
if (err) return console.error(err);
errors += result.error;
warns += result.warn;
if (result.warn && isCHECK) process.exitCode = 1;
else if (result.error) process.exitCode = 1;
result.missing.forEach(x => {
Missing.add(x);
});
};
async function walk(dir: string): Promise<void> {
let files = await fs.readdir(dir);
await Promise.all(
files.map(fname => {
let absolute = join(dir, fname);
if (fname === '.github') return walk(absolute);
if (fname === 'node_modules' || fname === 'public') return;
if (/^[._]/.test(fname) || /\.hbs$/.test(fname)) return;
// TODO: temporarily disable `*.hbs` formatting
if (isFILE.test(fname)) {
return task.run(absolute, handler);
}
return fs.stat(absolute).then(stats => {
if (stats.isDirectory()) return walk(absolute);
});
})
);
}
try {
await walk(ROOT);
if (Missing.size > 0) {
let langs = [...Missing].sort();
console.warn('\n\nMissing parser for language(s):\n');
console.warn(langs.map(x => ' - ' + x).join('\n'));
}
if (errors || warns) {
console.error('\n');
if (errors) {
console.error('Finished with %d error(s)', errors);
}
if (isCHECK && warns) {
console.error('Finished with %d warning(s)', warns);
}
console.error('\n');
isSILENT || process.exit(1);
}
} catch (err) {
console.error(err.stack || err);
process.exit(1);
}