forked from samuelmeuli/action-electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
95 lines (81 loc) · 2.57 KB
/
index.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
const { execSync } = require("child_process");
const { existsSync } = require("fs");
const { join } = require("path");
/**
* Logs to the console
*/
const log = (msg) => console.log(`\n${msg}`); // eslint-disable-line no-console
/**
* Exits the current process with an error code and message
*/
const exit = (msg) => {
console.error(msg);
process.exit(1);
};
/**
* Executes the provided shell command and redirects stdout/stderr to the console
*/
const run = (cmd, cwd) => execSync(cmd, { encoding: "utf8", stdio: "inherit", cwd });
/**
* Determines the current operating system (one of ["mac", "windows", "linux"])
*/
const getPlatform = () => {
switch (process.platform) {
case "darwin":
return "mac";
case "win32":
return "windows";
default:
return "linux";
}
};
/**
* Returns the value for an environment variable (or `null` if it's not defined)
*/
const getEnv = (name) => process.env[name.toUpperCase()] || null;
/**
* Sets the specified env variable if the value isn't empty
*/
const setEnv = (name, value) => {
if (value) {
process.env[name.toUpperCase()] = value.toString();
}
};
/**
* Returns the value for an input variable (or `null` if it's not defined). If the variable is
* required and doesn't have a value, abort the action
*/
const getInput = (name, required) => {
const value = getEnv(`INPUT_${name}`);
if (required && !value) {
exit(`"${name}" input variable is not defined`);
}
return value;
};
/**
* Installs NPM dependencies and builds/releases the Electron app
*/
const runAction = () => {
const platform = getPlatform();
const release = getInput("release", true) === "true";
const pkgRoot = ".";
const pkgJsonPath = join(pkgRoot, "package.json");
// Make sure `package.json` file exists
if (!existsSync(pkgJsonPath)) {
exit(`\`package.json\` file not found at path "${pkgJsonPath}"`);
}
// Copy "github_token" input variable to "GH_TOKEN" env variable (required by `electron-builder`)
setEnv("GH_TOKEN", getInput("github_token", true));
// Require code signing certificate and password if building for macOS. Export them to environment
// variables (required by `electron-builder`)
if (platform === "mac") {
setEnv("CSC_LINK", getInput("mac_certs"));
setEnv("CSC_KEY_PASSWORD", getInput("mac_certs_password"));
} else if (platform === "windows") {
setEnv("CSC_LINK", getInput("windows_certs"));
setEnv("CSC_KEY_PASSWORD", getInput("windows_certs_password"));
}
log(`Building${release ? " and releasing" : ""} the Electron app… \n`);
run( `yarn run build ${(release ? "--publish always" : "")}`, pkgRoot);
};
runAction();