-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.js
74 lines (57 loc) · 1.52 KB
/
gen.js
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
const { nodes } = require("lib-ruby-parser");
const fs = require("fs");
const force = process.argv.includes("--force");
let operation;
if (process.argv.includes("update")) operation = "update";
const template = (name) => {
return `// gen:mayoverwrite
import { nodes } from "lib-ruby-parser";
import { doc } from "prettier";
import { NodePrinter } from "../printer";
const { builders: b } = doc;
const print${name}: NodePrinter<nodes.${name}> = (path, options, print) => {
const node = path.getValue();
console.log(\`-${name}-\`);
return \`❗️${name}\`;
}
export default print${name};
`;
};
const templateTest = (name) => {
return `// gen:mayoverwrite
import { runNodeFixtureTests } from "../test-helper";
runNodeFixtureTests("${name}");
`;
};
const templateFixtures = (name) => {
return `# ${name} Formatting
## Fails because no tests are written
Before:
\`\`\`ruby
foo
\`\`\`
After:
\`\`\`ruby
bar
\`\`\`
`;
};
const update = (name, path, template) => {
const exists = fs.existsSync(path);
let mayoverwrite = false;
if (exists) {
mayoverwrite = fs.readFileSync(path, "utf8").includes("gen:mayoverwrite");
}
if (exists && !mayoverwrite && !force) {
console.log(`S ${path}`);
return;
}
console.log(`W ${path}`);
fs.writeFileSync(path, template(name));
};
for (const name of Object.keys(nodes)) {
// update(name, `src/nodes/${name}.ts`, template);
update(name, `src/nodes/${name}.test.ts`, templateTest);
// update(name, `src/nodes/${name}.fixtures.md`, templateFixtures);
}
console.log("done");