Skip to content

Commit 7b96cab

Browse files
author
Massimiliano Pippi
committed
prettify code
1 parent b58767c commit 7b96cab

File tree

2 files changed

+138
-138
lines changed

2 files changed

+138
-138
lines changed

src/installer.ts

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import * as restm from "typed-rest-client/RestClient";
88
import * as semver from "semver";
99

1010
if (!tempDirectory) {
11-
let baseLocation;
12-
if (process.platform === "win32") {
13-
// On windows use the USERPROFILE env variable
14-
baseLocation = process.env["USERPROFILE"] || "C:\\";
11+
let baseLocation;
12+
if (process.platform === "win32") {
13+
// On windows use the USERPROFILE env variable
14+
baseLocation = process.env["USERPROFILE"] || "C:\\";
15+
} else {
16+
if (process.platform === "darwin") {
17+
baseLocation = "/Users";
1518
} else {
16-
if (process.platform === "darwin") {
17-
baseLocation = "/Users";
18-
} else {
19-
baseLocation = "/home";
20-
}
19+
baseLocation = "/home";
2120
}
22-
tempDirectory = path.join(baseLocation, "actions", "temp");
21+
}
22+
tempDirectory = path.join(baseLocation, "actions", "temp");
2323
}
2424

2525
import * as core from "@actions/core";
@@ -30,156 +30,156 @@ let osPlat: string = os.platform();
3030
let osArch: string = os.arch();
3131

3232
interface ITaskRef {
33-
ref: string;
33+
ref: string;
3434
}
3535

3636
export async function getArduinoCli(version: string) {
37-
// resolve the version number
38-
const targetVersion = await computeVersion(version);
39-
if (targetVersion) {
40-
version = targetVersion;
41-
}
42-
43-
// look if the binary is cached
44-
let toolPath: string;
45-
toolPath = tc.find("arduino-cli", version);
46-
47-
// if not: download, extract and cache
48-
if (!toolPath) {
49-
toolPath = await downloadRelease(version);
50-
core.debug("CLI cached under " + toolPath);
51-
}
52-
53-
core.addPath(toolPath);
37+
// resolve the version number
38+
const targetVersion = await computeVersion(version);
39+
if (targetVersion) {
40+
version = targetVersion;
41+
}
42+
43+
// look if the binary is cached
44+
let toolPath: string;
45+
toolPath = tc.find("arduino-cli", version);
46+
47+
// if not: download, extract and cache
48+
if (!toolPath) {
49+
toolPath = await downloadRelease(version);
50+
core.debug("CLI cached under " + toolPath);
51+
}
52+
53+
core.addPath(toolPath);
5454
}
5555

5656
async function downloadRelease(version: string): Promise<string> {
57-
// Download
58-
let fileName: string = getFileName(version);
59-
let downloadUrl: string = util.format(
60-
"https://github.com/Arduino/arduino-cli/releases/download/%s/%s",
61-
version,
62-
fileName
63-
);
64-
let downloadPath: string | null = null;
65-
try {
66-
downloadPath = await tc.downloadTool(downloadUrl);
67-
} catch (error) {
68-
core.debug(error);
69-
throw `Failed to download version ${version}: ${error}`;
70-
}
71-
72-
// Extract
73-
let extPath: string | null = null;
74-
if (osPlat == "win32") {
75-
extPath = await tc.extractZip(downloadPath);
76-
} else {
77-
extPath = await tc.extractTar(downloadPath);
78-
}
79-
80-
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
81-
return await tc.cacheDir(extPath, "arduino-cli", version);
57+
// Download
58+
let fileName: string = getFileName(version);
59+
let downloadUrl: string = util.format(
60+
"https://github.com/Arduino/arduino-cli/releases/download/%s/%s",
61+
version,
62+
fileName
63+
);
64+
let downloadPath: string | null = null;
65+
try {
66+
downloadPath = await tc.downloadTool(downloadUrl);
67+
} catch (error) {
68+
core.debug(error);
69+
throw `Failed to download version ${version}: ${error}`;
70+
}
71+
72+
// Extract
73+
let extPath: string | null = null;
74+
if (osPlat == "win32") {
75+
extPath = await tc.extractZip(downloadPath);
76+
} else {
77+
extPath = await tc.extractTar(downloadPath);
78+
}
79+
80+
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
81+
return await tc.cacheDir(extPath, "arduino-cli", version);
8282
}
8383

8484
function getFileName(version: string): string {
85-
const arch: string = osArch == "x64" ? "64bit" : "32bit";
86-
let platform = "";
87-
let ext = "";
88-
switch (osPlat) {
89-
case "win32":
90-
platform = "Windows";
91-
ext = "zip";
92-
break;
93-
case "linux":
94-
platform = "Linux";
95-
ext = "tar.gz";
96-
break;
97-
case "darwin":
98-
platform = "macOS";
99-
ext = "tar.gz";
100-
break;
101-
}
102-
103-
return util.format("arduino-cli_%s_%s_%s.%s", version, platform, arch, ext);
85+
const arch: string = osArch == "x64" ? "64bit" : "32bit";
86+
let platform = "";
87+
let ext = "";
88+
switch (osPlat) {
89+
case "win32":
90+
platform = "Windows";
91+
ext = "zip";
92+
break;
93+
case "linux":
94+
platform = "Linux";
95+
ext = "tar.gz";
96+
break;
97+
case "darwin":
98+
platform = "macOS";
99+
ext = "tar.gz";
100+
break;
101+
}
102+
103+
return util.format("arduino-cli_%s_%s_%s.%s", version, platform, arch, ext);
104104
}
105105

106106
// Retrieve a list of versions scraping tags from the Github API
107107
async function fetchVersions(): Promise<string[]> {
108-
let rest: restm.RestClient = new restm.RestClient("setup-arduino-cli");
109-
let tags: ITaskRef[] =
110-
(await rest.get<ITaskRef[]>(
111-
"https://api.github.com/repos/Arduino/arduino-cli/git/refs/tags"
112-
)).result || [];
113-
114-
return tags
115-
.filter(tag => tag.ref.match(/\d+\.[\w\.]+/g))
116-
.map(tag => tag.ref.replace("refs/tags/", ""));
108+
let rest: restm.RestClient = new restm.RestClient("setup-arduino-cli");
109+
let tags: ITaskRef[] =
110+
(await rest.get<ITaskRef[]>(
111+
"https://api.github.com/repos/Arduino/arduino-cli/git/refs/tags"
112+
)).result || [];
113+
114+
return tags
115+
.filter(tag => tag.ref.match(/\d+\.[\w\.]+/g))
116+
.map(tag => tag.ref.replace("refs/tags/", ""));
117117
}
118118

119119
// Compute an actual version starting from the `version` configuration param.
120120
async function computeVersion(version: string): Promise<string> {
121-
// strip trailing .x chars
122-
if (version.endsWith(".x")) {
123-
version = version.slice(0, version.length - 2);
124-
}
121+
// strip trailing .x chars
122+
if (version.endsWith(".x")) {
123+
version = version.slice(0, version.length - 2);
124+
}
125125

126-
const allVersions = await fetchVersions();
127-
const possibleVersions = allVersions.filter(v => v.startsWith(version));
126+
const allVersions = await fetchVersions();
127+
const possibleVersions = allVersions.filter(v => v.startsWith(version));
128128

129-
const versionMap = new Map();
130-
possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v));
129+
const versionMap = new Map();
130+
possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v));
131131

132-
const versions = Array.from(versionMap.keys())
133-
.sort(semver.rcompare)
134-
.map(v => versionMap.get(v));
132+
const versions = Array.from(versionMap.keys())
133+
.sort(semver.rcompare)
134+
.map(v => versionMap.get(v));
135135

136-
core.debug(`evaluating ${versions.length} versions`);
136+
core.debug(`evaluating ${versions.length} versions`);
137137

138-
if (versions.length === 0) {
139-
throw new Error("unable to get latest version");
140-
}
138+
if (versions.length === 0) {
139+
throw new Error("unable to get latest version");
140+
}
141141

142-
core.debug(`matched: ${versions[0]}`);
142+
core.debug(`matched: ${versions[0]}`);
143143

144-
return versions[0];
144+
return versions[0];
145145
}
146146

147147
// Make partial versions semver compliant.
148148
function normalizeVersion(version: string): string {
149-
const preStrings = ["beta", "rc", "preview"];
150-
151-
const versionPart = version.split(".");
152-
if (versionPart[1] == null) {
153-
//append minor and patch version if not available
154-
// e.g. 2 -> 2.0.0
155-
return version.concat(".0.0");
156-
} else {
157-
// handle beta and rc
158-
// e.g. 1.10beta1 -? 1.10.0-beta1, 1.10rc1 -> 1.10.0-rc1
159-
if (preStrings.some(el => versionPart[1].includes(el))) {
160-
versionPart[1] = versionPart[1]
161-
.replace("beta", ".0-beta")
162-
.replace("rc", ".0-rc")
163-
.replace("preview", ".0-preview");
164-
return versionPart.join(".");
165-
}
149+
const preStrings = ["beta", "rc", "preview"];
150+
151+
const versionPart = version.split(".");
152+
if (versionPart[1] == null) {
153+
//append minor and patch version if not available
154+
// e.g. 2 -> 2.0.0
155+
return version.concat(".0.0");
156+
} else {
157+
// handle beta and rc
158+
// e.g. 1.10beta1 -? 1.10.0-beta1, 1.10rc1 -> 1.10.0-rc1
159+
if (preStrings.some(el => versionPart[1].includes(el))) {
160+
versionPart[1] = versionPart[1]
161+
.replace("beta", ".0-beta")
162+
.replace("rc", ".0-rc")
163+
.replace("preview", ".0-preview");
164+
return versionPart.join(".");
166165
}
167-
168-
if (versionPart[2] == null) {
169-
//append patch version if not available
170-
// e.g. 2.1 -> 2.1.0
171-
return version.concat(".0");
172-
} else {
173-
// handle beta and rc
174-
// e.g. 1.8.5beta1 -> 1.8.5-beta1, 1.8.5rc1 -> 1.8.5-rc1
175-
if (preStrings.some(el => versionPart[2].includes(el))) {
176-
versionPart[2] = versionPart[2]
177-
.replace("beta", "-beta")
178-
.replace("rc", "-rc")
179-
.replace("preview", "-preview");
180-
return versionPart.join(".");
181-
}
166+
}
167+
168+
if (versionPart[2] == null) {
169+
//append patch version if not available
170+
// e.g. 2.1 -> 2.1.0
171+
return version.concat(".0");
172+
} else {
173+
// handle beta and rc
174+
// e.g. 1.8.5beta1 -> 1.8.5-beta1, 1.8.5rc1 -> 1.8.5-rc1
175+
if (preStrings.some(el => versionPart[2].includes(el))) {
176+
versionPart[2] = versionPart[2]
177+
.replace("beta", "-beta")
178+
.replace("rc", "-rc")
179+
.replace("preview", "-preview");
180+
return versionPart.join(".");
182181
}
182+
}
183183

184-
return version;
184+
return version;
185185
}

src/main.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import * as core from "@actions/core";
22
import * as installer from "./installer";
33

44
async function run() {
5-
try {
6-
let version = core.getInput("version");
5+
try {
6+
let version = core.getInput("version");
77

8-
if (version) {
9-
await installer.getArduinoCli(version);
10-
}
11-
} catch (error) {
12-
core.setFailed(error.message);
8+
if (version) {
9+
await installer.getArduinoCli(version);
1310
}
11+
} catch (error) {
12+
core.setFailed(error.message);
13+
}
1414
}
1515

1616
run();

0 commit comments

Comments
 (0)