-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwriteGenerated.ts
35 lines (29 loc) · 1.35 KB
/
writeGenerated.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
import { ESLint } from "eslint";
import { promises as fs } from "fs";
import * as path from "path";
export async function writeGenerated(filePath: string, contents: string): Promise<void> {
const header = `// This file has been generated by comet admin-generator.
// You may choose to use this file as scaffold by moving this file out of generated folder and removing this comment.
`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
const eslint = new ESLint({
cwd: process.cwd(),
fix: true,
});
// Write not linted generated code into file. This is necessary to avoid linting errors like: Parsing error: file/path/file.tsx was not found by the project service.
await fs.writeFile(filePath, contents);
const lintResult = await eslint.lintText(header + contents, {
filePath,
});
if (lintResult[0].errorCount > 0 || lintResult[0].fatalErrorCount > 0) {
const errorMessage = lintResult[0].messages
.map((message) => {
return message.message;
})
.join(".");
console.log(`❌ Linting error in ${filePath}: \n${errorMessage}`);
}
const output = lintResult[0] && lintResult[0].output ? lintResult[0].output : lintResult[0].source;
await fs.writeFile(filePath, output ?? contents);
console.log(`generated ${filePath}`);
}