Implement a no sync rule (#7216)

* implement a no sync rule

* fix linting disable

* fix unused imports

* exclude more testing

* clean up fs usage

* clean up more fs usage

* remove duplicate of code

* fix compile errors
This commit is contained in:
Anthony Dresser
2019-09-17 13:32:42 -07:00
committed by GitHub
parent 4d62983680
commit 28d453fced
31 changed files with 305 additions and 201 deletions

View File

@@ -26,10 +26,12 @@ export class PlatformService implements IPlatformService {
}
copyFile(source: string, target: string): void {
// tslint:disable-next-line:no-sync
fs.copyFileSync(source, target);
}
fileExists(file: string): boolean {
// tslint:disable-next-line:no-sync
return fs.existsSync(file);
}
@@ -44,4 +46,4 @@ export class PlatformService implements IPlatformService {
isNotebookNameUsed(title: string): boolean {
return (azdata.nb.notebookDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title) > -1);
}
}
}

View File

@@ -6,7 +6,7 @@
import * as azdata from 'azdata';
import * as cp from 'child_process';
import * as fs from 'fs';
import { createWriteStream, promises as fs } from 'fs';
import * as https from 'https';
import * as os from 'os';
import * as path from 'path';
@@ -226,7 +226,7 @@ export class ResourceTypeService implements IResourceTypeService {
private download(url: string): Promise<string> {
const self = this;
const promise = new Promise<string>((resolve, reject) => {
https.get(url, function (response) {
https.get(url, async function (response) {
console.log('Download installer from: ' + url);
if (response.statusCode === 301 || response.statusCode === 302) {
// Redirect and download from new location
@@ -247,19 +247,19 @@ export class ResourceTypeService implements IResourceTypeService {
let fileName = originalFileName;
const downloadFolder = os.homedir();
let cnt = 1;
while (fs.existsSync(path.join(downloadFolder, fileName + extension))) {
while (await exists(path.join(downloadFolder, fileName + extension))) {
fileName = `${originalFileName}-${cnt}`;
cnt++;
}
fileName = path.join(downloadFolder, fileName + extension);
const file = fs.createWriteStream(fileName);
const file = createWriteStream(fileName);
response.pipe(file);
file.on('finish', () => {
file.close();
resolve(fileName);
});
file.on('error', (err) => {
fs.unlink(fileName, () => { });
file.on('error', async (err) => {
await fs.unlink(fileName);
reject(err.message);
});
});
@@ -268,3 +268,12 @@ export class ResourceTypeService implements IResourceTypeService {
}
}
async function exists(path: string): Promise<boolean> {
try {
await fs.access(path);
return true;
} catch (e) {
return false;
}
}