Repository navigation
Expand file tree
/
Copy pathcreate-types.js
More file actions
62 lines (48 loc) · 1.6 KB
/
Copy pathcreate-types.js
File metadata and controls
62 lines (48 loc) · 1.6 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
const { join: pathJoin, relative: pathRelative } = require("path");
const { existsSync } = require("fs");
const { readFile, writeFile, mkdir } = require("fs/promises");
const postcss = require("postcss");
const importPlugin = require("postcss-import-url");
const declarePlugin = require("postcss-ts-classnames");
const dirName = process.cwd();
const cssPath = pathJoin(dirName, "cdn", "cirrus.css");
const typeOutputDir = pathJoin(dirName, "dist");
const typeOutput = pathJoin(typeOutputDir, "cirrus.d.ts");
const green = "\x1b[32m%s\x1b[0m";
const normal = "%s";
const greenNormal = `${green}${normal}`;
const encoding = "utf-8";
(async () => {
try {
const css = await readFile(cssPath, encoding);
if (!existsSync(typeOutputDir)) {
await mkdir(typeOutputDir);
}
await postcss([
importPlugin,
declarePlugin({ dest: typeOutput, isModule: true }),
]).process(css, { from: cssPath, to: "discarded" });
console.log(
greenNormal,
"Processed",
` : ${pathRelative(dirName, cssPath)}`
);
await new Promise((resolve) => setTimeout(resolve, 250));
if (!existsSync(typeOutput)) {
throw new Error("Failed to create types");
}
const rawResult = await readFile(typeOutput, encoding);
if (!rawResult.length) {
throw new Error("Results is empty");
}
const fixedResult = rawResult.replace("ClassNames", "Cirrus");
await writeFile(typeOutput, fixedResult, encoding);
console.log(
greenNormal,
"Declared",
` : ${pathRelative(dirName, typeOutput)}`
);
} catch (error) {
console.error(error);
}
})();