Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.

Commit c39fef6

Browse files
committed
Simplify input variable handling
1 parent dbc0ee1 commit c39fef6

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ inputs:
2525
app_root:
2626
description: Directory where `electron-builder` commands should be run
2727
required: false
28+
default: "."
2829
package_root:
2930
description: Directory where NPM/Yarn commands should be run
3031
required: false
32+
default: "."
3133

3234
runs:
3335
using: node12

index.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,60 +47,65 @@ const getPlatform = () => {
4747
};
4848

4949
/**
50-
* Parses the environment variable with the provided name. If `required` is set to `true`, the
51-
* program exits if the variable isn't defined
50+
* Returns the value for an environment variable (or `null` if it's not defined)
5251
*/
53-
const getEnvVariable = (name, required = false) => {
54-
const value = process.env[`INPUT_${name.toUpperCase()}`];
55-
if (required && (value === undefined || value === null || value === "")) {
56-
exit(`"${name}" input variable is not defined`);
52+
const getEnv = name => process.env[name.toUpperCase()] || null;
53+
54+
/**
55+
* Sets the specified env variable if the value isn't empty
56+
*/
57+
const setEnv = (name, value) => {
58+
if (value) {
59+
process.env[name.toUpperCase()] = value.toString();
5760
}
58-
return value;
5961
};
6062

6163
/**
62-
* Sets the specified env variable if the value isn't empty
64+
* Returns the value for an input variable (or `null` if it's not defined). If the variable is
65+
* reqiured and doesn't have a value, abort the action
6366
*/
64-
const setEnvVariable = (name, value) => {
65-
if (value !== null && value !== undefined && value !== "") {
66-
process.env[name] = value.toString();
67+
const getInput = (name, required) => {
68+
const value = getEnv(`INPUT_${name}`);
69+
if (required && !value) {
70+
exit(`"${name}" input variable is not defined`);
6771
}
72+
return value;
6873
};
6974

7075
/**
7176
* Installs NPM dependencies and builds/releases the Electron app
7277
*/
7378
const runAction = () => {
7479
const platform = getPlatform();
75-
const release = getEnvVariable("release") === "true";
76-
const appRoot = getEnvVariable("app_root") || null;
77-
const pkgRoot = getEnvVariable("package_root") || null;
80+
const release = getInput("release", true) === "true";
81+
const appRoot = getInput("app_root", true);
82+
const pkgRoot = getInput("package_root", true);
7883

7984
// Determine whether NPM should be used to run commands (instead of Yarn, which is the default)
8085
const useNpm = existsSync(join(pkgRoot, NPM_LOCKFILE_PATH));
8186

8287
// Log information about working directories
83-
log(`Will run ${useNpm ? "NPM" : "Yarn"} commands in directory "${pkgRoot || "."}"`);
84-
log(`Will run \`electron-builder\` commands in directory "${appRoot || "."}"`);
88+
log(`Will run ${useNpm ? "NPM" : "Yarn"} commands in directory "${pkgRoot}"`);
89+
log(`Will run \`electron-builder\` commands in directory "${appRoot}"`);
8590

8691
// Make sure `package.json` file exists
8792
verifyPackageJson();
8893

8994
// Copy "github_token" input variable to "GH_TOKEN" env variable (required by `electron-builder`)
90-
setEnvVariable("GH_TOKEN", getEnvVariable("github_token", true));
95+
setEnv("GH_TOKEN", getInput("github_token", true));
9196

9297
// Require code signing certificate and password if building for macOS. Export them to environment
9398
// variables (required by `electron-builder`)
9499
if (platform === "mac") {
95-
setEnvVariable("CSC_LINK", getEnvVariable("mac_certs"));
96-
setEnvVariable("CSC_KEY_PASSWORD", getEnvVariable("mac_certs_password"));
100+
setEnv("CSC_LINK", getInput("mac_certs"));
101+
setEnv("CSC_KEY_PASSWORD", getInput("mac_certs_password"));
97102
} else if (platform === "windows") {
98-
setEnvVariable("CSC_LINK", getEnvVariable("windows_certs"));
99-
setEnvVariable("CSC_KEY_PASSWORD", getEnvVariable("windows_certs_password"));
103+
setEnv("CSC_LINK", getInput("windows_certs"));
104+
setEnv("CSC_KEY_PASSWORD", getInput("windows_certs_password"));
100105
}
101106

102107
// Disable console advertisements during install phase
103-
setEnvVariable("ADBLOCK", true);
108+
setEnv("ADBLOCK", true);
104109

105110
log(`Installing dependencies using ${useNpm ? "NPM" : "Yarn"}…`);
106111
run(useNpm ? "npm install" : "yarn", pkgRoot);

0 commit comments

Comments
 (0)