forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.js
108 lines (102 loc) · 2.48 KB
/
pack.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//@ts-check
var fs = require("fs");
var path = require("path");
var util = require("util");
var child_process = require("child_process");
// Note the escaping is escape to ocaml style string literals.
// `util.format` is useful to escape to js style string literrals.
// { name : "basic", children : ... }
// { name : "xx", content : ... }
/**
*
* @param {string} dir
*/
function scanDir(dir) {
var files = fs.readdirSync(dir).sort();
var children = files.map((file) => {
var curFile = path.join(dir, file);
var stat = fs.statSync(curFile);
if (stat.isFile()) {
var content = fs.readFileSync(curFile, "utf8");
return { name: file, content };
} else if (stat.isDirectory()) {
return scanDir(curFile);
}
});
return { name: path.basename(dir), children };
}
/**
*
* @param {string} s
* @param {number} indent
*/
function escape(s, indent) {
return (
" ".repeat(indent) +
'"' +
s.replace(/(\n|"|\\|\t|\r)/g, (match, _, offset) => {
switch (match) {
case "\n":
var o = `\\n\\
${" ".repeat(indent)}`;
if (s[offset + 1] === " ") {
o += "\\";
}
return o;
default:
return "\\" + match;
}
}) +
'"'
);
}
function toString(data, indent) {
var output = "";
if (data.children) {
output += `${" ".repeat(indent)}Dir("${data.name}",[
${data.children.map((x) => toString(x, indent + 1)).join(";\n")}
${" ".repeat(indent)}])`;
} else {
output += `${" ".repeat(indent)}File("${data.name}",
${escape(data.content, indent)}
${" ".repeat(indent)})`;
}
return output;
}
/**
*
*/
function updateThemes() {
var templatesDir = path.join(__dirname, "..", "jscomp", "bsb", "templates");
var output = child_process.spawnSync(`git clean -dfx ${templatesDir}`, {
shell: true,
encoding: "utf-8",
});
if (output.error) {
throw output.error;
}
console.log(output.stdout);
if (output.status !== 0) {
console.log(output.stderr);
}
// run git clean -dfx . first
fs.writeFileSync(
path.join(templatesDir, "..", "bsb_templates.ml"),
`
type node =
| Dir of string * node list
| File of string * string
let root = ([
${fs
.readdirSync(templatesDir)
.filter((x) => fs.statSync(path.join(templatesDir, x)).isDirectory())
.map((x) => toString(scanDir(path.join(templatesDir, x)), 3))
.join(";\n")}
])`,
"utf8"
);
}
exports.updateThemes = updateThemes;
if (require.main === module) {
updateThemes();
}