-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
144 lines (119 loc) · 3.6 KB
/
build.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
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
import * as fs from "fs";
import * as path from "path";
import esbuild from "esbuild";
import { SourceInfo } from "./sources/types";
import archiver from "archiver";
/**
* @param {String} sourceDir: /some/folder/to/compress
* @param {String} outPath: /path/to/created.zip
* @returns {Promise}
*/
function zipDirectory(sourceDir: string, outPath: string): Promise<void> {
// @ts-ignore
const archive = archiver("zip", { zlib: { level: 9 } });
const stream = fs.createWriteStream(outPath);
return new Promise((resolve, reject) => {
archive
.directory(sourceDir, false)
.on("error", (err: archiver.ArchiverError) => reject(err))
.pipe(stream);
stream.on("close", () => resolve());
archive.finalize();
});
}
var rmdir = function (dir: string) {
var list = fs.readdirSync(dir);
for (var i = 0; i < list.length; i++) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if (filename == "." || filename == "..") {
// pass these files
} else if (stat.isDirectory()) {
// rmdir recursively
rmdir(filename);
} else {
// rm fiilename
fs.unlinkSync(filename);
}
}
fs.rmdirSync(dir);
};
class Build {
static buildPath = path.join(process.cwd(), "bundles");
/*
gives the build path source info e.g
{
"name": "GoodReads",
"description": "GoodReads scraper",
"websiteURL": "https://www.goodreads.com/"
}
*/
static async generateSourceInfo(filePaths: string[]) {
const sourcesInfoPath = path.join(this.buildPath, "sources.json");
let sources: SourceInfo[] = [];
filePaths.forEach((filePath) => {
const sourceInfo = JSON.parse(fs.readFileSync(filePath, "utf8"));
sources.push(sourceInfo);
});
fs.writeFileSync(sourcesInfoPath, JSON.stringify(sources));
}
static createBuildDir() {
if (fs.existsSync(this.buildPath)) {
rmdir(this.buildPath);
}
fs.mkdirSync(this.buildPath);
}
/*
sourceDirectory of files must be /src/{extension_name}/{extension_name}.ts
*/
static async build(sourceDirectory: string) {
this.createBuildDir();
const files = fs.readdirSync(sourceDirectory).filter((file) => {
return fs.existsSync(path.join(sourceDirectory, file, `${file}.ts`));
});
const buildFiles = files.flatMap((file) => [
{
in: path.join(sourceDirectory, file, `${file}.ts`),
out: path.join(file, "index"),
},
]);
var sourceFiles: string[] = [];
files.forEach((file) => {
const jsonFile = path.join(sourceDirectory, file, "source.json");
//
const dest = path.join(this.buildPath, file);
const sourceJSONFile = path.join(dest, "source.json");
if (fs.existsSync(dest) === false) {
fs.mkdirSync(dest);
}
fs.copyFileSync(jsonFile, sourceJSONFile);
//
if (fs.existsSync(jsonFile)) {
sourceFiles.push(jsonFile);
}
});
this.generateSourceInfo(sourceFiles);
await esbuild.build({
bundle: true,
entryPoints: buildFiles,
format: "iife",
globalName: "source",
metafile: true,
outdir: this.buildPath,
});
// zip files in bundles dir
const dirs = fs
.readdirSync(this.buildPath)
.filter((file) => {
return fs.lstatSync(path.join(this.buildPath, file)).isDirectory();
})
.flatMap((file) => path.join(this.buildPath, file));
dirs.forEach(async (dir) => {
await zipDirectory(
dir,
path.join(this.buildPath, path.parse(dir).base) + ".zip",
);
});
}
}
Build.build("/Users/mirnaolvera/MALLEL/read/sources");