-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·197 lines (167 loc) · 7.44 KB
/
index.js
File metadata and controls
executable file
·197 lines (167 loc) · 7.44 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#! /usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
const fetch = require('node-fetch');
const Progress = require('node-fetch-progress');
const unzipper = require('unzipper');
const { getLocations, getPlatform, constants } = require('./common');
const JSON5 = require('json5');
const args = process.argv.slice(2);
async function downloadAndUnzip(url, dest) {
const response = await fetch(url);
const progress = new Progress(response, { throttle: 100 })
let prevEta;
progress.on('progress', (p) => {
if (prevEta !== p.etah) {
console.log(p.etah + ' remaining');
prevEta = p.etah;
}
});
await new Promise((resolve, reject) => {
const unzipperInstance = unzipper.Extract({ path: dest });
unzipperInstance.promise().then(resolve, reject);
response.body.pipe(unzipperInstance);
});
}
async function main() {
const command = args.length > 0 ? args[0] : 'help';
const version = (args.length > 1 && (['use', 'install', 'remove'].includes(command))) ? args[1] : undefined;
const { downloadDir } = await getLocations();
const isUseCommand = command === 'use' && !!version;
const isInstallCommand = command === 'install' && !!version;
const isListCommand = command === 'list';
const isRemoveCommand = command === 'remove';
if (isListCommand) {
const versions = fs.readdirSync(downloadDir);
const versionFile = path.join(downloadDir, 'funcvm-core-tools-version.txt');
const localVersionFile = path.join(process.cwd(), '.func-version');
let currentVersion, localVersion;
if (fs.existsSync(versionFile)) {
currentVersion = fs.readFileSync(versionFile, 'utf8');
}
if (fs.existsSync(localVersionFile)) {
localVersion = fs.readFileSync(localVersionFile, 'utf8');
}
for (const version of versions) {
if (/^\d+\.\d+\.\d+/.test(version) && fs.lstatSync(path.join(downloadDir, version)).isDirectory()) {
const tags = [];
currentVersion === version && tags.push('global');
localVersion === version && tags.push('local');
console.log(`${version}${tags.length > 0 ? ` (${tags.join(', ')})`: ''}`);
}
}
process.exit(0);
} else if (isRemoveCommand && !!version) {
const versionDir = path.join(downloadDir, version);
if (!fs.existsSync(versionDir)) {
console.error(`Version '${version}' is not installed. Run 'funcvm list' to see installed versions.`);
process.exit(1);
}
fs.rmdirSync(versionDir, { recursive: true });
process.exit(0);
} else if (!isUseCommand && !isInstallCommand) {
console.log(`
Azure Functions Core Tools Version Manager (unofficial)
Usage: funcvm <command> [version]
Examples:
Install and use latest stable 3.x version:
funcvm use 3
Install and use exact version:
funcvm use 4.0.3928
Install and use the version only for the current directory:
funcvm use 4.0.3928 --local
Install exact version:
funcvm install 4.0.3928
List installed versions:
funcvm list
Remove an installed version:
funcvm remove 4.0.3928\n`);
process.exit(0);
}
const feedResponse = await fetch("https://aka.ms/AAeq1v7");
const feedText = await feedResponse.text();
const feed = JSON5.parse(feedText);
const feedTags = feed.tags;
const tags = {};
const platform = getPlatform();
for (const [tagName, tagInfo] of Object.entries(feedTags)) {
if (tagInfo.hidden) {
continue;
}
const releaseVersion = tagInfo.release;
const release = feed.releases[releaseVersion];
// hack for Windows, there's no x64 in the feed
const archToFind = platform.os === 'Windows' ? 'x86' : platform.arch;
const releaseCoreTool = release.coreTools.find(
tool => tool.OS === platform.os && tool.Architecture === archToFind && tool.size == 'full');
if (releaseCoreTool) {
tagInfo.coreToolsUrl = releaseCoreTool.downloadLink;
// hack for Windows, there's no x64 in the feed
if (platform.os === 'Windows') {
tagInfo.coreToolsUrl = tagInfo.coreToolsUrl.replace('x86', 'x64');
}
const match = releaseCoreTool.downloadLink.match(/\/(\d+\.\d+\.\d+)\//);
if (match) {
tagInfo.coreToolsVersion = match[1];
}
tags[tagName] = tagInfo;
}
}
let tag = tags[`v${version}`];
if (!tag) {
tag = Object.values(tags).find(tag => tag.coreToolsVersion === version);
}
if (!tag) {
// check GitHub releases
const releaseResponse = await fetch(`https://api.github.com/repos/Azure/azure-functions-core-tools/releases/tags/${version}`);
if (releaseResponse.status !== 200) {
console.error(`Unable to find version ${version} on GitHub releases https://github.com/Azure/azure-functions-core-tools/releases`);
process.exit(1);
}
const releaseText = await releaseResponse.text();
const release = JSON5.parse(releaseText);
const name = `Azure.Functions.Cli.${platform.label}.${version}.zip`;
const asset = release.assets.find(asset => asset.name === name);
if (!asset) {
console.error(`Unable to find suitable asset for ${platform.label} on GitHub releases https://github.com/Azure/azure-functions-core-tools/releases/tag/${version}`);
process.exit(1);
}
const coreToolsUrl = asset.browser_download_url;
tag = {
coreToolsUrl,
coreToolsVersion: version,
};
}
const versionDownloadDir = path.join(downloadDir, tag.coreToolsVersion);
if (!fs.existsSync(versionDownloadDir)) {
console.log(`Downloading ${tag.coreToolsUrl} to ${versionDownloadDir}...`);
await downloadAndUnzip(tag.coreToolsUrl, versionDownloadDir);
const funcBin = path.join(versionDownloadDir, 'func');
if (os.platform() === 'linux' || os.platform() === 'darwin') {
fs.chmodSync(funcBin, 0o755);
fs.chmodSync(path.join(versionDownloadDir, 'gozip'), 0o755);
}
if (isInstallCommand) {
console.log(`${tag.coreToolsVersion} installed. Run 'funcvm use ${tag.coreToolsVersion}' or set '${constants.versionEnvironmentVariableName}' environment variable to use it.`);
}
} else {
if (isInstallCommand) {
console.log(`${tag.coreToolsVersion} already installed. Run 'funcvm use ${tag.coreToolsVersion}' or set '${constants.versionEnvironmentVariableName}' environment variable to use it.`);
}
}
if (isUseCommand) {
const localVersionFile = path.join(process.cwd(), '.func-version');
if (args.includes('--local')) {
fs.writeFileSync(localVersionFile, tag.coreToolsVersion, 'utf8');
} else {
if (fs.existsSync(localVersionFile)) {
console.log(`Local version file '${localVersionFile}' already exists. Remove it or use 'funcvm use ${tag.coreToolsVersion} --local' to update it.`);
process.exit(1);
}
fs.writeFileSync(path.join(downloadDir, 'funcvm-core-tools-version.txt'), tag.coreToolsVersion, 'utf8');
}
console.log(`Using ${tag.coreToolsVersion}`);
}
}
main();