-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
43 lines (33 loc) · 1.08 KB
/
index.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
import { TsGeneratorPlugin, TFileDesc } from "../../../../src/publicApi";
import { join } from "path";
const generateType = (name: string, type: string): string => `export const ${name}: ${type}`;
export class JsonPlugin extends TsGeneratorPlugin {
name = "JsonPlugin";
transformFile({ path, contents }: TFileDesc): TFileDesc {
const outputFileName = `${path}.d.ts`;
const json = JSON.parse(contents);
// assume that json is 1 level nested structure
const nameTypePairs = Object.keys(json).map((k) => {
const value = json[k];
const valueType = typeof value;
return [k, valueType] as [string, string];
});
const types = nameTypePairs.map(([name, type]) => generateType(name, type)).join("\n");
return {
contents: types,
path: outputFileName,
};
}
beforeRun(): TFileDesc {
return {
path: join(this.ctx.cwd, "before.ts"),
contents: `export default "before"`,
};
}
afterRun(): TFileDesc {
return {
path: join(this.ctx.cwd, "after.ts"),
contents: `export default "after"`,
};
}
}