|
1 | 1 | // @ts-check
|
2 | 2 |
|
3 | 3 | (async () => {
|
| 4 | + const path = require('path'); |
| 5 | + const shell = require('shelljs'); |
| 6 | + const semver = require('semver'); |
| 7 | + const moment = require('moment'); |
| 8 | + const downloader = require('./downloader'); |
| 9 | + const { goBuildFromGit } = require('./utils'); |
| 10 | + |
| 11 | + const version = (() => { |
| 12 | + const pkg = require(path.join(__dirname, '..', 'package.json')); |
| 13 | + if (!pkg) { |
| 14 | + return undefined; |
| 15 | + } |
4 | 16 |
|
5 |
| - const fs = require('fs'); |
6 |
| - const path = require('path'); |
7 |
| - const temp = require('temp'); |
8 |
| - const shell = require('shelljs'); |
9 |
| - const semver = require('semver'); |
10 |
| - const moment = require('moment'); |
11 |
| - const downloader = require('./downloader'); |
12 |
| - |
13 |
| - const version = (() => { |
14 |
| - const pkg = require(path.join(__dirname, '..', 'package.json')); |
15 |
| - if (!pkg) { |
16 |
| - return undefined; |
17 |
| - } |
| 17 | + const { arduino } = pkg; |
| 18 | + if (!arduino) { |
| 19 | + return undefined; |
| 20 | + } |
18 | 21 |
|
19 |
| - const { arduino } = pkg; |
20 |
| - if (!arduino) { |
21 |
| - return undefined; |
22 |
| - } |
| 22 | + const { cli } = arduino; |
| 23 | + if (!cli) { |
| 24 | + return undefined; |
| 25 | + } |
23 | 26 |
|
24 |
| - const { cli } = arduino; |
25 |
| - if (!cli) { |
26 |
| - return undefined; |
| 27 | + const { version } = cli; |
| 28 | + return version; |
| 29 | + })(); |
| 30 | + |
| 31 | + if (!version) { |
| 32 | + shell.echo(`Could not retrieve CLI version info from the 'package.json'.`); |
| 33 | + shell.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + const { platform, arch } = process; |
| 37 | + const buildFolder = path.join(__dirname, '..', 'build'); |
| 38 | + const cliName = `arduino-cli${platform === 'win32' ? '.exe' : ''}`; |
| 39 | + const destinationPath = path.join(buildFolder, cliName); |
| 40 | + |
| 41 | + if (typeof version === 'string') { |
| 42 | + const suffix = (() => { |
| 43 | + switch (platform) { |
| 44 | + case 'darwin': |
| 45 | + return 'macOS_64bit.tar.gz'; |
| 46 | + case 'win32': |
| 47 | + return 'Windows_64bit.zip'; |
| 48 | + case 'linux': { |
| 49 | + switch (arch) { |
| 50 | + case 'arm': |
| 51 | + return 'Linux_ARMv7.tar.gz'; |
| 52 | + case 'arm64': |
| 53 | + return 'Linux_ARM64.tar.gz'; |
| 54 | + case 'x64': |
| 55 | + return 'Linux_64bit.tar.gz'; |
| 56 | + default: |
| 57 | + return undefined; |
| 58 | + } |
27 | 59 | }
|
28 |
| - |
29 |
| - const { version } = cli; |
30 |
| - return version; |
| 60 | + default: |
| 61 | + return undefined; |
| 62 | + } |
31 | 63 | })();
|
32 |
| - |
33 |
| - if (!version) { |
34 |
| - shell.echo(`Could not retrieve CLI version info from the 'package.json'.`); |
35 |
| - shell.exit(1); |
| 64 | + if (!suffix) { |
| 65 | + shell.echo(`The CLI is not available for ${platform} ${arch}.`); |
| 66 | + shell.exit(1); |
36 | 67 | }
|
37 |
| - |
38 |
| - const { platform, arch } = process; |
39 |
| - const buildFolder = path.join(__dirname, '..', 'build'); |
40 |
| - const cliName = `arduino-cli${platform === 'win32' ? '.exe' : ''}`; |
41 |
| - const destinationPath = path.join(buildFolder, cliName); |
42 |
| - |
43 |
| - if (typeof version === 'string') { |
44 |
| - const suffix = (() => { |
45 |
| - switch (platform) { |
46 |
| - case 'darwin': return 'macOS_64bit.tar.gz'; |
47 |
| - case 'win32': return 'Windows_64bit.zip'; |
48 |
| - case 'linux': { |
49 |
| - switch (arch) { |
50 |
| - case 'arm': return 'Linux_ARMv7.tar.gz'; |
51 |
| - case 'arm64': return 'Linux_ARM64.tar.gz'; |
52 |
| - case 'x64': return 'Linux_64bit.tar.gz'; |
53 |
| - default: return undefined; |
54 |
| - } |
55 |
| - } |
56 |
| - default: return undefined; |
57 |
| - } |
58 |
| - })(); |
59 |
| - if (!suffix) { |
60 |
| - shell.echo(`The CLI is not available for ${platform} ${arch}.`); |
61 |
| - shell.exit(1); |
62 |
| - } |
63 |
| - if (semver.valid(version)) { |
64 |
| - const url = `https://downloads.arduino.cc/arduino-cli/arduino-cli_${version}_${suffix}`; |
65 |
| - shell.echo(`📦 Identified released version of the CLI. Downloading version ${version} from '${url}'`); |
66 |
| - await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
67 |
| - } else if (moment(version, 'YYYYMMDD', true).isValid()) { |
68 |
| - const url = `https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-${version}_${suffix}`; |
69 |
| - shell.echo(`🌙 Identified nightly version of the CLI. Downloading version ${version} from '${url}'`); |
70 |
| - await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
71 |
| - } else { |
72 |
| - shell.echo(`🔥 Could not interpret 'version': ${version}`); |
73 |
| - shell.exit(1); |
74 |
| - } |
| 68 | + if (semver.valid(version)) { |
| 69 | + const url = `https://downloads.arduino.cc/arduino-cli/arduino-cli_${version}_${suffix}`; |
| 70 | + shell.echo( |
| 71 | + `📦 Identified released version of the CLI. Downloading version ${version} from '${url}'` |
| 72 | + ); |
| 73 | + await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
| 74 | + } else if (moment(version, 'YYYYMMDD', true).isValid()) { |
| 75 | + const url = `https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-${version}_${suffix}`; |
| 76 | + shell.echo( |
| 77 | + `🌙 Identified nightly version of the CLI. Downloading version ${version} from '${url}'` |
| 78 | + ); |
| 79 | + await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
75 | 80 | } else {
|
76 |
| - |
77 |
| - // We assume an object with `owner`, `repo`, commitish?` properties. |
78 |
| - const { owner, repo, commitish } = version; |
79 |
| - if (!owner) { |
80 |
| - shell.echo(`Could not retrieve 'owner' from ${JSON.stringify(version)}`); |
81 |
| - shell.exit(1); |
82 |
| - } |
83 |
| - if (!repo) { |
84 |
| - shell.echo(`Could not retrieve 'repo' from ${JSON.stringify(version)}`); |
85 |
| - shell.exit(1); |
86 |
| - } |
87 |
| - const url = `https://github.com/${owner}/${repo}.git`; |
88 |
| - shell.echo(`Building CLI from ${url}. Commitish: ${commitish ? commitish : 'HEAD'}`); |
89 |
| - |
90 |
| - if (fs.existsSync(destinationPath)) { |
91 |
| - shell.echo(`Skipping the CLI build because it already exists: ${destinationPath}`); |
92 |
| - return; |
93 |
| - } |
94 |
| - |
95 |
| - if (shell.mkdir('-p', buildFolder).code !== 0) { |
96 |
| - shell.echo('Could not create build folder.'); |
97 |
| - shell.exit(1); |
98 |
| - } |
99 |
| - |
100 |
| - const tempRepoPath = temp.mkdirSync(); |
101 |
| - shell.echo(`>>> Cloning CLI source to ${tempRepoPath}...`); |
102 |
| - if (shell.exec(`git clone ${url} ${tempRepoPath}`).code !== 0) { |
103 |
| - shell.exit(1); |
104 |
| - } |
105 |
| - shell.echo('<<< Cloned CLI repo.') |
106 |
| - |
107 |
| - if (commitish) { |
108 |
| - shell.echo(`>>> Checking out ${commitish}...`); |
109 |
| - if (shell.exec(`git -C ${tempRepoPath} checkout ${commitish}`).code !== 0) { |
110 |
| - shell.exit(1); |
111 |
| - } |
112 |
| - shell.echo(`<<< Checked out ${commitish}.`); |
113 |
| - } |
114 |
| - |
115 |
| - shell.echo(`>>> Building the CLI...`); |
116 |
| - if (shell.exec('go build', { cwd: tempRepoPath }).code !== 0) { |
117 |
| - shell.exit(1); |
118 |
| - } |
119 |
| - shell.echo('<<< CLI build done.') |
120 |
| - |
121 |
| - if (!fs.existsSync(path.join(tempRepoPath, cliName))) { |
122 |
| - shell.echo(`Could not find the CLI at ${path.join(tempRepoPath, cliName)}.`); |
123 |
| - shell.exit(1); |
124 |
| - } |
125 |
| - |
126 |
| - const builtCliPath = path.join(tempRepoPath, cliName); |
127 |
| - shell.echo(`>>> Copying CLI from ${builtCliPath} to ${destinationPath}...`); |
128 |
| - if (shell.cp(builtCliPath, destinationPath).code !== 0) { |
129 |
| - shell.exit(1); |
130 |
| - } |
131 |
| - shell.echo(`<<< Copied the CLI.`); |
132 |
| - |
133 |
| - shell.echo('<<< Verifying CLI...'); |
134 |
| - if (!fs.existsSync(destinationPath)) { |
135 |
| - shell.exit(1); |
136 |
| - } |
137 |
| - shell.echo('>>> Verified CLI.'); |
138 |
| - |
| 81 | + shell.echo(`🔥 Could not interpret 'version': ${version}`); |
| 82 | + shell.exit(1); |
139 | 83 | }
|
140 |
| - |
| 84 | + } else { |
| 85 | + goBuildFromGit(version, destinationPath, 'CLI'); |
| 86 | + } |
141 | 87 | })();
|
0 commit comments