Skip to content

Commit a424a6b

Browse files
committed
Use js which as fallback
1 parent 70f6883 commit a424a6b

File tree

3 files changed

+99
-87
lines changed

3 files changed

+99
-87
lines changed

package-lock.json

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
"@types/node": "^14.0.3",
399399
"@types/request-promise-native": "^1.0.17",
400400
"@types/vscode": "^1.52.0",
401+
"@types/which": "^2.0.1",
401402
"@types/yauzl": "^2.9.1",
402403
"@vscode/test-electron": "^1.6.2",
403404
"glob": "^7.1.4",
@@ -425,6 +426,7 @@
425426
"request": "^2.88.2",
426427
"request-promise-native": "^1.0.8",
427428
"vscode-languageclient": "^7.0.0",
429+
"which": "^2.0.1",
428430
"yauzl": "^2.10.0"
429431
}
430432
}

src/utils.ts

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as url from 'url';
1010
import { promisify } from 'util';
1111
import { OutputChannel, ProgressLocation, window, WorkspaceFolder } from 'vscode';
1212
import { Logger } from 'vscode-languageclient';
13+
import * as which from 'which';
1314
import * as yazul from 'yauzl';
1415
import { createGunzip } from 'zlib';
1516

@@ -18,7 +19,7 @@ enum LogLevel {
1819
Error,
1920
Warn,
2021
Info,
21-
Debug
22+
Debug,
2223
}
2324
export class ExtensionLogger implements Logger {
2425
public readonly name: string;
@@ -52,7 +53,7 @@ export class ExtensionLogger implements Logger {
5253
let now = new Date();
5354
// Ugly hack to make js date iso format similar to hls one
5455
const offset = now.getTimezoneOffset();
55-
now = new Date(now.getTime() - (offset * 60 * 1000));
56+
now = new Date(now.getTime() - offset * 60 * 1000);
5657
const timedMsg = `${new Date().toISOString().replace('T', ' ').replace('Z', '0000')} ${msg}`;
5758
this.channel.appendLine(timedMsg);
5859
if (this.logFile) {
@@ -161,87 +162,90 @@ export async function downloadFile(titleMsg: string, src: string, dest: string):
161162
fs.unlinkSync(downloadDest);
162163
}
163164

164-
const downloadTask = window.withProgress(
165-
{
166-
location: ProgressLocation.Notification,
167-
title: titleMsg,
168-
cancellable: false,
169-
},
170-
async (progress) => {
171-
const p = new Promise<void>((resolve, reject) => {
172-
const srcUrl = url.parse(src);
173-
const opts: https.RequestOptions = {
174-
host: srcUrl.host,
175-
path: srcUrl.path,
176-
protocol: srcUrl.protocol,
177-
port: srcUrl.port,
178-
headers: userAgentHeader,
179-
};
180-
getWithRedirects(opts, (res) => {
181-
const totalSize = parseInt(res.headers['content-length'] || '1', 10);
182-
const fileStream = fs.createWriteStream(downloadDest, { mode: 0o744 });
183-
let curSize = 0;
165+
const downloadTask = window
166+
.withProgress(
167+
{
168+
location: ProgressLocation.Notification,
169+
title: titleMsg,
170+
cancellable: false,
171+
},
172+
async (progress) => {
173+
const p = new Promise<void>((resolve, reject) => {
174+
const srcUrl = url.parse(src);
175+
const opts: https.RequestOptions = {
176+
host: srcUrl.host,
177+
path: srcUrl.path,
178+
protocol: srcUrl.protocol,
179+
port: srcUrl.port,
180+
headers: userAgentHeader,
181+
};
182+
getWithRedirects(opts, (res) => {
183+
const totalSize = parseInt(res.headers['content-length'] || '1', 10);
184+
const fileStream = fs.createWriteStream(downloadDest, { mode: 0o744 });
185+
let curSize = 0;
184186

185-
// Decompress it if it's a gzip or zip
186-
const needsGunzip =
187-
res.headers['content-type'] === 'application/gzip' || extname(srcUrl.path ?? '') === '.gz';
188-
const needsUnzip = res.headers['content-type'] === 'application/zip' || extname(srcUrl.path ?? '') === '.zip';
189-
if (needsGunzip) {
190-
const gunzip = createGunzip();
191-
gunzip.on('error', reject);
192-
res.pipe(gunzip).pipe(fileStream);
193-
} else if (needsUnzip) {
194-
const zipDest = downloadDest + '.zip';
195-
const zipFs = fs.createWriteStream(zipDest);
196-
zipFs.on('error', reject);
197-
zipFs.on('close', () => {
198-
yazul.open(zipDest, (err, zipfile) => {
199-
if (err) {
200-
throw err;
201-
}
202-
if (!zipfile) {
203-
throw Error("Couldn't decompress zip");
204-
}
187+
// Decompress it if it's a gzip or zip
188+
const needsGunzip =
189+
res.headers['content-type'] === 'application/gzip' || extname(srcUrl.path ?? '') === '.gz';
190+
const needsUnzip =
191+
res.headers['content-type'] === 'application/zip' || extname(srcUrl.path ?? '') === '.zip';
192+
if (needsGunzip) {
193+
const gunzip = createGunzip();
194+
gunzip.on('error', reject);
195+
res.pipe(gunzip).pipe(fileStream);
196+
} else if (needsUnzip) {
197+
const zipDest = downloadDest + '.zip';
198+
const zipFs = fs.createWriteStream(zipDest);
199+
zipFs.on('error', reject);
200+
zipFs.on('close', () => {
201+
yazul.open(zipDest, (err, zipfile) => {
202+
if (err) {
203+
throw err;
204+
}
205+
if (!zipfile) {
206+
throw Error("Couldn't decompress zip");
207+
}
205208

206-
// We only expect *one* file inside each zip
207-
zipfile.on('entry', (entry: yazul.Entry) => {
208-
zipfile.openReadStream(entry, (err2, readStream) => {
209-
if (err2) {
210-
throw err2;
211-
}
212-
readStream?.pipe(fileStream);
209+
// We only expect *one* file inside each zip
210+
zipfile.on('entry', (entry: yazul.Entry) => {
211+
zipfile.openReadStream(entry, (err2, readStream) => {
212+
if (err2) {
213+
throw err2;
214+
}
215+
readStream?.pipe(fileStream);
216+
});
213217
});
214218
});
215219
});
216-
});
217-
res.pipe(zipFs);
218-
} else {
219-
res.pipe(fileStream);
220-
}
220+
res.pipe(zipFs);
221+
} else {
222+
res.pipe(fileStream);
223+
}
221224

222-
function toMB(bytes: number) {
223-
return bytes / (1024 * 1024);
224-
}
225+
function toMB(bytes: number) {
226+
return bytes / (1024 * 1024);
227+
}
225228

226-
res.on('data', (chunk: Buffer) => {
227-
curSize += chunk.byteLength;
228-
const msg = `${toMB(curSize).toFixed(1)}MB / ${toMB(totalSize).toFixed(1)}MB`;
229-
progress.report({ message: msg, increment: (chunk.length / totalSize) * 100 });
230-
});
231-
res.on('error', reject);
232-
fileStream.on('close', resolve);
233-
}).on('error', reject);
234-
});
235-
try {
236-
await p;
237-
// Finally rename it to the actual dest
238-
fs.renameSync(downloadDest, dest);
239-
} finally {
240-
// And remember to remove it from the list of current downloads
241-
inFlightDownloads.get(src)?.delete(dest);
229+
res.on('data', (chunk: Buffer) => {
230+
curSize += chunk.byteLength;
231+
const msg = `${toMB(curSize).toFixed(1)}MB / ${toMB(totalSize).toFixed(1)}MB`;
232+
progress.report({ message: msg, increment: (chunk.length / totalSize) * 100 });
233+
});
234+
res.on('error', reject);
235+
fileStream.on('close', resolve);
236+
}).on('error', reject);
237+
});
238+
try {
239+
await p;
240+
// Finally rename it to the actual dest
241+
fs.renameSync(downloadDest, dest);
242+
} finally {
243+
// And remember to remove it from the list of current downloads
244+
inFlightDownloads.get(src)?.delete(dest);
245+
}
242246
}
243-
}
244-
).then(_ => true);
247+
)
248+
.then((_) => true);
245249

246250
try {
247251
if (inFlightDownloads.has(src)) {
@@ -250,7 +254,7 @@ export async function downloadFile(titleMsg: string, src: string, dest: string):
250254
inFlightDownloads.set(src, new Map([[dest, downloadTask]]));
251255
}
252256
return await downloadTask;
253-
} catch (e: any) {
257+
} catch (e) {
254258
await promisify(fs.unlink)(downloadDest).catch(ignoreFileNotExists);
255259
throw new Error(`Failed to download ${src}:\n${e.message}`);
256260
}
@@ -277,17 +281,13 @@ export function executableExists(exe: string): boolean {
277281
const isWindows = process.platform === 'win32';
278282
const cmd: string = isWindows ? 'where' : 'which';
279283
const out = child_process.spawnSync(cmd, [exe]);
280-
return out.status === 0 || (isWindows && fileExists(exe));
284+
return out.status === 0 || (!isWindows && (which.sync(exe, { nothrow: true }) ?? '') !== '');
281285
}
282286

283287
export function directoryExists(path: string): boolean {
284288
return fs.existsSync(path) && fs.lstatSync(path).isDirectory();
285289
}
286290

287-
function fileExists(path: string): boolean {
288-
return fs.existsSync(path) && fs.lstatSync(path).isFile();
289-
}
290-
291291
export function resolvePathPlaceHolders(path: string, folder?: WorkspaceFolder) {
292292
path = path.replace('${HOME}', os.homedir).replace('${home}', os.homedir).replace(/^~/, os.homedir);
293293
if (folder) {

0 commit comments

Comments
 (0)