-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·173 lines (112 loc) · 4.42 KB
/
build.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
import { readdir, stat, readFile } from "fs/promises";
import path, { basename, dirname } from "path";
import fs, { cpSync, existsSync, glob, globSync, mkdirSync, rmSync, statSync, writeSync } from "fs";
import archiver from "archiver";
import ignore from "ignore";
import fg from "fast-glob";
import dotenv from "dotenv";
dotenv.config();
const STORAGE_URL = process.env.PHASER_EDITOR_V5_TEMPLATES_URL;
const REPO_URL = "https://github.com/phaserjs/phaser-editor-v5-starter-templates";
// computing the ignore rules
const baseDir = process.cwd();
const gitignoreFiles = await fg("**/.gitignore", { cwd: baseDir, absolute: true });
const ignoreRules = [];
for (const file of gitignoreFiles) {
const relDir = path.dirname(path.relative(baseDir, file));
const content = fs.readFileSync(file, "utf8");
const ig = ignore().add(content);
ignoreRules.push({
base: path.join(baseDir, relDir),
ig
});
}
function shouldIgnore(filePath) {
for (const { base, ig } of ignoreRules) {
const relativeToBase = path.relative(base, filePath);
if (relativeToBase !== "" && !relativeToBase.startsWith("..") && !path.isAbsolute(relativeToBase)) {
if (ig.ignores(relativeToBase)) {
return true;
}
}
}
return false;
}
async function zipFolder(sourceFolder, outputZipPath) {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(outputZipPath);
const archive = archiver("zip", { zlib: { level: 9 }, });
output.on("close", () => {
console.log(`Created ${outputZipPath} (${archive.pointer()} bytes)`);
resolve();
});
archive.on("error", err => reject(err));
archive.pipe(output);
archive.directory(sourceFolder, basename(sourceFolder));
archive.finalize();
});
}
console.log("\nCleaning build folder...\n");
rmSync("build/", { recursive: true, force: true });
[
"build",
"build/examples/files",
"build/starters/files",
"build/examples/screenshots",
"build/starters/screenshots",
].forEach(d => mkdirSync(d, { recursive: true }));
const projectNames = (await readdir(".")).filter((name) => name.startsWith("editor-") && existsSync(`${name}/template.json`));
const examplesData = [];
const startersData = [];
for (const projectName of projectNames) {
console.log(`\nProcessing '${projectName}'...\n`);
// build metadata
const isExample = projectName.includes("-example-");
const siteName = isExample ? "examples" : "starters";
const templateJSON = JSON.parse(await readFile(`${projectName}/template.json`, "utf8"));
const projectVersion = templateJSON.version;
const templateData = {
...templateJSON,
website: `${REPO_URL}/tree/main/${projectName}`,
image: `${STORAGE_URL}/${siteName}/screenshots/${projectName}.png`,
zip_url: `${STORAGE_URL}/${siteName}/files/${projectName}.zip?v=${projectVersion}`
};
(isExample ? examplesData : startersData).push(templateData);
// copy template.png
cpSync(`${projectName}/template.png`, `build/${siteName}/screenshots/${projectName}.png`);
console.log(`Zipping files...`);
let countIgnored = 0;
cpSync(projectName, `build/${projectName}`, {
recursive: true,
filter: (src) => {
const absPath = path.resolve(src);
const ignoreIt = shouldIgnore(absPath);
countIgnored += ignoreIt ? 1 : 0;
return !ignoreIt;
}
});
await zipFolder(`build/${projectName}`, `build/${siteName}/files/${projectName}.zip`);
rmSync(`build/${projectName}`, { recursive: true, force: true });
console.log(`Ignored ${countIgnored} files`);
}
// write metadata
writeSync(
fs.openSync("build/examples/templates.json", "w"),
JSON.stringify(examplesData, null, 4)
);
writeSync(
fs.openSync("build/starters/templates.json", "w"),
JSON.stringify(startersData, null, 4)
);
console.log("Copying Phaser template files...");
fs.cpSync("phaser-site", `build/phaser`, { recursive: true });
// fix Phaser templates.json
const phaserTemplatesJSON = JSON.parse(await readFile(`build/phaser/templates.json`, "utf8"));
for (const item of phaserTemplatesJSON) {
item.image = `${STORAGE_URL}/phaser/${item.image}`;
}
writeSync(
fs.openSync("build/phaser/templates.json", "w"),
JSON.stringify(phaserTemplatesJSON, null, 4)
);