forked from x6pnda/action-electron-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
137 lines (115 loc) · 3.85 KB
/
index.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
import { join } from "path";
import { execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
const log = (msg?: string) => console.log(`\n${msg}`);
log(" STARTING NORMAL.....................................................................................");
const exit = (msg: string) => {
console.error(msg);
process.exit(1);
};
const run = (cmd: string, cwd?: string) => {
execSync(cmd, { encoding: "utf8", stdio: "inherit", cwd });
};
const getPlatform = () => {
switch (process.platform) {
case "darwin":
return "mac";
case "win32":
return "windows";
default:
return "linux";
}
};
const getEnv = (name: string) => process.env[name.toUpperCase()] || null;
const setEnv = (name: string, value?: any) => {
if (value) {
process.env[name.toUpperCase()] = value.toString();
}
};
const getInput = <B extends boolean>(
name: string,
required?: B
): B extends true ? string : string | null => {
const value = getEnv(`INPUT_${name}`);
if (required && !value) {
exit(`"${name}" input variable is not defined`);
return "" as string;
} else {
return value as string;
}
};
(function () {
const platform = getPlatform();
const release = getInput("release", true) === "true";
const packageRoot = getInput("package_root", true);
const buildScriptName = getInput("build_script_name", true);
const skipBuild = getInput("skip_build") === "true";
const packageManager = getInput("package_manager", true)
?.toLocaleLowerCase()
?.trim();
if (!["npm", "pnpm", "yarn"].includes(packageManager)) {
exit(`"${packageManager}" not supported! Please use NPM, PNPM or Yarn`);
return;
}
const args = getInput("args") || "";
const maxAttempts = parseInt(getInput("max_build_attempts") || "1");
const packageJsonPath = join(packageRoot, "package.json");
const yarnLockPath = join(packageRoot, "yarn.lock");
// NOTE: Determine which package mananger to run
const canUseYarn = packageManager === "yarn" && existsSync(yarnLockPath);
const package_manager_used =
packageManager === "yarn" && !canUseYarn ? "npm" : packageManager;
// NOTE: Display used package manager
if (packageManager === "yarn" && !canUseYarn) {
log(`No Yarn lock file found! Falling back...`);
}
log(`Using ${package_manager_used} for directory "${packageRoot}"`);
// NOTE: package.json required
if (!existsSync(packageJsonPath)) {
exit(`"package.json" not found in "${packageJsonPath}"`);
return;
}
// NOTE: EB reads GH_TOKEN
setEnv("GH_TOKEN", getInput("github_token", true));
// NOTE: Set code signing certificate and password importing through the config
if (platform === "mac") {
setEnv("CSC_LINK", getInput("mac_certs"));
setEnv("CSC_KEY_PASSWORD", getInput("mac_certs_password"));
setEnv("APPLE_ID", getInput("apple_id"));
setEnv("APPLE_ID_PASSWORD", getInput("apple_id_password"));
setEnv("APPLE_ID_TEAM", getInput("apple_id_team"));
} else if (platform === "windows") {
setEnv("CSC_LINK", getInput("windows_certs"));
setEnv("CSC_KEY_PASSWORD", getInput("windows_certs_password"));
}
// NOTE: Disable console ads
setEnv("ADBLOCK", false);
setEnv("CI", false);
log("setting CI false...................................................................");
setEnv("CSC_LINK", getInput("mac_certs"));
log(`Installing dependencies using coskunpm`);
run( "npm install -force",
packageRoot
);
// Run NPM build script if it exists
if (skipBuild) {
log("Skipping build script...");
} else {
log("Running react-script build script…");
run('react-scripts build', packageRoot);
}
log(`Building${release ? " and releasing" : ""} the Electron application…`);
for (let i = 0; i < maxAttempts; i += 1) {
try {
run("electron-builder -c.extraMetadata.main=build/main.js");
break;
} catch (err) {
if (i < maxAttempts - 1) {
log(`Attempt ${i + 1} failed:`);
log(err?.toString());
} else {
throw err;
}
}
}
})();