-
Notifications
You must be signed in to change notification settings - Fork 99
/
prepare-protonmail-webclient.ts
129 lines (109 loc) · 4.16 KB
/
prepare-protonmail-webclient.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
import byline from "byline";
import chalk from "chalk";
import fsExtra from "fs-extra";
import path from "path";
import spawnAsync from "@expo/spawn-async";
import {GitProcess} from "dugite";
import {Arguments} from "src/shared/types";
import {PROVIDER_REPO} from "src/shared/constants";
// tslint:disable-next-line:no-console
const consoleLog = console.log;
// tslint:disable-next-line:no-console
const consoleError = console.error;
const distDir = process.argv[2];
const repoDir = path.resolve(process.cwd(), `./output/git/protonmail/webclient/${PROVIDER_REPO.protonmail.commit}`);
const repoDistDir = path.resolve(repoDir, "./dist");
const foldersAsDomains = [
"mail.protonmail.com",
];
(async () => {
for (const folder of foldersAsDomains) {
const resolvedDistDir = path.resolve(distDir, folder);
consoleLog(chalk.magenta(`Preparing built-in WebClient build:`), resolvedDistDir);
if (await fsExtra.pathExists(resolvedDistDir)) {
consoleLog(chalk.yellow(`Skipping as directory already exists:`), resolvedDistDir);
continue;
}
if (await fsExtra.pathExists(repoDir)) {
consoleLog(chalk.yellow(`Skipping cloning`));
} else {
await fsExtra.ensureDir(repoDir);
await clone(repoDir);
}
if (await fsExtra.pathExists(path.resolve(repoDir, "node_modules"))) {
consoleLog(chalk.yellow(`Skipping dependencies installing`));
} else {
await installDependencies(repoDir);
}
if (await fsExtra.pathExists(repoDistDir)) {
consoleLog(chalk.yellow(`Skipping building`));
} else {
await build(repoDir);
}
consoleLog(chalk.magenta(`Copying:`), `${repoDistDir}" to "${resolvedDistDir}`);
await fsExtra.copy(repoDistDir, resolvedDistDir);
}
})().catch((error) => {
consoleError("Uncaught exception", error);
process.exit(1);
});
async function build(dir: string) {
await _exec(["npm", ["run", "config"], {cwd: dir}]);
await _exec(["npm", ["run", "dist"], {cwd: dir}]);
}
async function installDependencies(dir: string) {
await _exec(["npm", ["install"], {cwd: dir}]);
}
async function clone(destDir: string) {
const {repo, commit} = PROVIDER_REPO.protonmail;
await _execGit([
["clone", "--progress", repo, "."],
destDir,
]);
await _execGit([
["checkout", commit],
destDir,
]);
// TODO call "_execGit" instead of "_exec"
await _exec(["git", ["show", "--summary"], {cwd: destDir}]);
}
async function _execGit([commands, pathArg, options]: Arguments<typeof GitProcess.exec>) {
const args: Arguments<typeof GitProcess.exec> = [
commands,
pathArg,
{
processCallback: ({stderr}) => {
byline(stderr).on("data", (chunk) => consoleLog(formatChunk(chunk)));
},
...options,
},
];
consoleLog(chalk.magenta(`Executing Git command:`), JSON.stringify(args));
const result = await GitProcess.exec(...args);
if (result.exitCode) {
throw new Error(String(result.stderr).trim());
}
}
async function _exec(args: Arguments<typeof spawnAsync>) {
consoleLog(chalk.magenta(`Executing Shell command: `), JSON.stringify(args));
const taskPromise = spawnAsync(...args);
byline(taskPromise.child.stdout).on("data", (chunk) => consoleLog(formatChunk(chunk)));
byline(taskPromise.child.stderr).on("data", (chunk) => consoleError(formatChunk(chunk)));
taskPromise.child.on("uncaughtException", (error) => {
consoleError(`Failed Shell command execution (uncaught exception): ${JSON.stringify(args)}`, error);
process.exit(1);
});
try {
const {status: exitCode} = await taskPromise;
if (exitCode) {
consoleError(`Failed Shell command execution (${exitCode} exit code): ${JSON.stringify(args)}`);
process.exit(exitCode);
}
} catch (error) {
consoleError(`Failed Shell command execution: ${JSON.stringify(args)}`, error.stack);
process.exit(1);
}
}
function formatChunk(chunk: any): string {
return Buffer.from(chunk, "UTF-8").toString();
}