Skip to content

Commit c301778

Browse files
Remove fs-extra-promise and fs-extra (#17226)
* Remove fs-extra-promise * const * Remove one more * Update comment
1 parent b4f9c74 commit c301778

7 files changed

+90
-135
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
"error-ex": "^1.3.0",
125125
"figures": "^1.4.0",
126126
"find-remove": "1.2.1",
127-
"fs-extra-promise": "^0.3.1",
128127
"getmac": "1.2.1",
129128
"http-proxy-agent": "^2.1.0",
130129
"https-proxy-agent": "^2.2.1",

src/languageservice/server.ts

+32-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77
import { Runtime } from '../models/platform';
88
import ServiceDownloadProvider from './serviceDownloadProvider';
99
import { IConfig, IStatusView } from './interfaces';
10-
import * as fs from 'fs-extra-promise';
10+
import * as fs from 'fs/promises';
1111

1212

1313
/*
@@ -23,29 +23,29 @@ export default class ServerProvider {
2323
/**
2424
* Given a file path, returns the path to the SQL Tools service file.
2525
*/
26-
public findServerPath(filePath: string): Promise<string> {
27-
return fs.lstatAsync(filePath).then(stats => {
28-
// If a file path was passed, assume its the launch file.
29-
if (stats.isFile()) {
30-
return filePath;
31-
}
32-
33-
// Otherwise, search the specified folder.
34-
let candidate: string;
26+
public async findServerPath(filePath: string): Promise<string | undefined> {
27+
const stats = await fs.lstat(filePath);
28+
// If a file path was passed, assume its the launch file.
29+
if (stats.isFile()) {
30+
return filePath;
31+
}
3532

36-
if (this._config !== undefined) {
37-
let executableFiles: string[] = this._config.getSqlToolsExecutableFiles();
38-
executableFiles.forEach(element => {
39-
let executableFile = path.join(filePath, element);
40-
if (candidate === undefined && fs.existsSync(executableFile)) {
41-
candidate = executableFile;
42-
return candidate;
33+
// Otherwise, search the specified folder.
34+
if (this._config !== undefined) {
35+
let executableFiles: string[] = this._config.getSqlToolsExecutableFiles();
36+
for (const executableFile of executableFiles) {
37+
const executablePath = path.join(filePath, executableFile);
38+
try {
39+
if (await fs.stat(executablePath)) {
40+
return executablePath;
4341
}
44-
});
42+
} catch (err) {
43+
// no-op, the exe files list has all possible options and so depending on the platform we expect some
44+
// to always fail
45+
}
4546
}
46-
47-
return candidate;
48-
});
47+
}
48+
return undefined;
4949
}
5050

5151
/**
@@ -75,25 +75,22 @@ export default class ServerProvider {
7575
/**
7676
* Returns the path of the installed service
7777
*/
78-
public getServerPath(runtime: Runtime): Promise<string> {
79-
const installDirectory = this._downloadProvider.getInstallDirectory(runtime);
78+
public async getServerPath(runtime: Runtime): Promise<string> {
79+
const installDirectory = await this._downloadProvider.getOrMakeInstallDirectory(runtime);
8080
return this.findServerPath(installDirectory);
8181
}
8282

8383
/**
8484
* Downloads the service and returns the path of the installed service
8585
*/
86-
public downloadServerFiles(runtime: Runtime): Promise<string> {
87-
return new Promise<string>((resolve, reject) => {
88-
const installDirectory = this._downloadProvider.getInstallDirectory(runtime);
89-
return this._downloadProvider.installSQLToolsService(runtime).then(_ => {
90-
return this.findServerPath(installDirectory).then(result => {
91-
return resolve(result);
92-
});
93-
}).catch(err => {
94-
this._statusView.serviceInstallationFailed();
95-
reject(err);
96-
});
97-
});
86+
public async downloadServerFiles(runtime: Runtime): Promise<string> {
87+
const installDirectory = await this._downloadProvider.getOrMakeInstallDirectory(runtime);
88+
try {
89+
await this._downloadProvider.installSQLToolsService(runtime);
90+
return this.findServerPath(installDirectory);
91+
} catch (err) {
92+
this._statusView.serviceInstallationFailed();
93+
throw err;
94+
}
9895
}
9996
}

src/languageservice/serviceDownloadProvider.ts

+37-40
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Runtime, getRuntimeDisplayName } from '../models/platform';
99
import { IConfig, IStatusView, IPackage, PackageError, IHttpClient, IDecompressProvider } from './interfaces';
1010
import { ILogger } from '../models/interfaces';
1111
import * as Constants from '../constants/constants';
12-
import * as fse from 'fs-extra';
12+
import * as fs from 'fs/promises';
1313

1414
/*
1515
* Service Download Provider class which handles downloading the SQL Tools service.
@@ -45,16 +45,19 @@ export default class ServiceDownloadProvider {
4545

4646

4747
/**
48-
* Returns SQL tools service installed folder.
48+
* Returns SQL tools service installed folder, creating it if it doesn't exist.
4949
*/
50-
public getInstallDirectory(platform: Runtime): string {
50+
public async getOrMakeInstallDirectory(platform: Runtime): Promise<string> {
5151

5252
let basePath = this.getInstallDirectoryRoot();
5353
let versionFromConfig = this._config.getSqlToolsPackageVersion();
5454
basePath = basePath.replace('{#version#}', versionFromConfig);
5555
basePath = basePath.replace('{#platform#}', getRuntimeDisplayName(platform));
56-
if (!fse.existsSync(basePath)) {
57-
fse.mkdirsSync(basePath);
56+
try {
57+
await fs.mkdir(basePath, { recursive: true });
58+
} catch {
59+
// Best effort to make the folder, if it already exists (expected scenario) or something else happens
60+
// then just carry on
5861
}
5962
return basePath;
6063
}
@@ -85,45 +88,39 @@ export default class ServiceDownloadProvider {
8588
/**
8689
* Downloads the SQL tools service and decompress it in the install folder.
8790
*/
88-
public installSQLToolsService(platform: Runtime): Promise<boolean> {
91+
public async installSQLToolsService(platform: Runtime): Promise<boolean> {
8992
const proxy = <string>this._config.getWorkspaceConfig('http.proxy');
9093
const strictSSL = this._config.getWorkspaceConfig('http.proxyStrictSSL', true);
9194
const authorization = this._config.getWorkspaceConfig('http.proxyAuthorization');
9295

93-
return new Promise<boolean>((resolve, reject) => {
94-
const fileName = this.getDownloadFileName(platform);
95-
const installDirectory = this.getInstallDirectory(platform);
96-
97-
this._logger.appendLine(`${Constants.serviceInstallingTo} ${installDirectory}.`);
98-
const urlString = this.getGetDownloadUrl(fileName);
99-
100-
const isZipFile: boolean = path.extname(fileName) === '.zip';
101-
102-
this._logger.appendLine(`${Constants.serviceDownloading} ${urlString}`);
103-
let pkg: IPackage = {
104-
installPath: installDirectory,
105-
url: urlString,
106-
tmpFile: undefined,
107-
isZipFile: isZipFile
108-
};
109-
this.createTempFile(pkg).then(tmpResult => {
110-
pkg.tmpFile = tmpResult;
111-
112-
this._httpClient.downloadFile(pkg.url, pkg, this._logger, this._statusView, proxy, strictSSL, authorization).then(_ => {
113-
114-
this._logger.logDebug(`Downloaded to ${pkg.tmpFile.name}...`);
115-
this._logger.appendLine(' Done!');
116-
this.install(pkg).then(result => {
117-
resolve(true);
118-
}).catch(installError => {
119-
reject(installError);
120-
});
121-
}).catch(downloadError => {
122-
this._logger.appendLine(`[ERROR] ${downloadError}`);
123-
reject(downloadError);
124-
});
125-
});
126-
});
96+
const fileName = this.getDownloadFileName(platform);
97+
const installDirectory = await this.getOrMakeInstallDirectory(platform);
98+
99+
this._logger.appendLine(`${Constants.serviceInstallingTo} ${installDirectory}.`);
100+
const urlString = this.getGetDownloadUrl(fileName);
101+
102+
const isZipFile: boolean = path.extname(fileName) === '.zip';
103+
104+
this._logger.appendLine(`${Constants.serviceDownloading} ${urlString}`);
105+
let pkg: IPackage = {
106+
installPath: installDirectory,
107+
url: urlString,
108+
tmpFile: undefined,
109+
isZipFile: isZipFile
110+
};
111+
const tmpResult = await this.createTempFile(pkg);
112+
pkg.tmpFile = tmpResult;
113+
114+
try {
115+
await this._httpClient.downloadFile(pkg.url, pkg, this._logger, this._statusView, proxy, strictSSL, authorization);
116+
this._logger.logDebug(`Downloaded to ${pkg.tmpFile.name}...`);
117+
this._logger.appendLine(' Done!');
118+
await this.install(pkg);
119+
} catch (err) {
120+
this._logger.appendLine(`[ERROR] ${err}`);
121+
throw err;
122+
}
123+
return true;
127124
}
128125

129126
private createTempFile(pkg: IPackage): Promise<tmp.SynchrounousResult> {

src/languageservice/serviceInstallerUtil.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ export function getServiceInstallDirectory(runtime: Runtime): Promise<string> {
8383
if (runtime === undefined) {
8484
PlatformInformation.getCurrent().then(platformInfo => {
8585
if (platformInfo.isValidRuntime) {
86-
resolve(downloadProvider.getInstallDirectory(platformInfo.runtimeId));
86+
resolve(downloadProvider.getOrMakeInstallDirectory(platformInfo.runtimeId));
8787
} else {
8888
reject('unsupported runtime');
8989
}
9090
}).catch(error => {
9191
reject(error);
9292
});
9393
} else {
94-
resolve(downloadProvider.getInstallDirectory(runtime));
94+
resolve(downloadProvider.getOrMakeInstallDirectory(runtime));
9595
}
9696
});
9797

test/download.test.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Runtime } from '../src/models/platform';
1414
import * as path from 'path';
1515
import { ILogger } from '../src/models/interfaces';
1616
import { Logger } from '../src/models/logger';
17-
import * as fse from 'fs-extra';
17+
import * as fs from 'fs/promises';
1818

1919
interface IFixture {
2020
downloadUrl: string;
@@ -47,7 +47,7 @@ suite('ServiceDownloadProvider Tests', () => {
4747
config.setup(x => x.getSqlToolsPackageVersion()).returns(() => expectedVersionFromConfig);
4848
let downloadProvider = new ServiceDownloadProvider(config.object, undefined, testStatusView.object,
4949
testHttpClient.object, testDecompressProvider.object);
50-
let actual = downloadProvider.getInstallDirectory(Runtime.OSX_10_11_64);
50+
let actual = downloadProvider.getOrMakeInstallDirectory(Runtime.OSX_10_11_64);
5151
assert.equal(expected, actual);
5252
done();
5353
});
@@ -62,7 +62,7 @@ suite('ServiceDownloadProvider Tests', () => {
6262
config.setup(x => x.getSqlToolsPackageVersion()).returns(() => expectedVersionFromConfig);
6363
let downloadProvider = new ServiceDownloadProvider(config.object, undefined, testStatusView.object,
6464
testHttpClient.object, testDecompressProvider.object);
65-
let actual = downloadProvider.getInstallDirectory(Runtime.OSX_10_11_64);
65+
let actual = downloadProvider.getOrMakeInstallDirectory(Runtime.OSX_10_11_64);
6666
assert.equal(expected, actual);
6767
done();
6868
});
@@ -77,7 +77,7 @@ suite('ServiceDownloadProvider Tests', () => {
7777
config.setup(x => x.getSqlToolsPackageVersion()).returns(() => expectedVersionFromConfig);
7878
let downloadProvider = new ServiceDownloadProvider(config.object, undefined, testStatusView.object,
7979
testHttpClient.object, testDecompressProvider.object);
80-
let actual = downloadProvider.getInstallDirectory(Runtime.OSX_10_11_64);
80+
let actual = downloadProvider.getOrMakeInstallDirectory(Runtime.OSX_10_11_64);
8181
assert.equal(expected, actual);
8282
done();
8383
});
@@ -92,7 +92,7 @@ suite('ServiceDownloadProvider Tests', () => {
9292
config.setup(x => x.getSqlToolsPackageVersion()).returns(() => expectedVersionFromConfig);
9393
let downloadProvider = new ServiceDownloadProvider(config.object, undefined, testStatusView.object,
9494
testHttpClient.object, testDecompressProvider.object);
95-
let actual = downloadProvider.getInstallDirectory(Runtime.OSX_10_11_64);
95+
let actual = downloadProvider.getOrMakeInstallDirectory(Runtime.OSX_10_11_64);
9696
assert.equal(expected, actual);
9797
done();
9898
});
@@ -114,18 +114,18 @@ suite('ServiceDownloadProvider Tests', () => {
114114
});
115115
});
116116

117-
function createDownloadProvider(fixture: IFixture): IFixture {
117+
async function createDownloadProvider(fixture: IFixture): Promise<IFixture> {
118118
let fileName = 'fileName';
119119
let baseDownloadUrl = 'baseDownloadUrl/{#version#}/{#fileName#}';
120120
let version = '1.0.0';
121121
let installFolder = path.join(__dirname, 'testService');
122122
let fileNamesJson = { Windows_7_64: `${fileName}` };
123123
let downloadUrl = 'baseDownloadUrl/1.0.0/fileName';
124-
fse.remove(installFolder, function (err): void {
125-
if (err) {
126-
return console.error(err);
127-
}
128-
});
124+
try {
125+
await fs.rmdir(installFolder);
126+
} catch (err) {
127+
console.error(err);
128+
}
129129

130130
config.setup(x => x.getSqlToolsInstallDirectory()).returns(() => installFolder);
131131
config.setup(x => x.getSqlToolsConfigValue('downloadFileNames')).returns(() => fileNamesJson);
@@ -151,15 +151,15 @@ suite('ServiceDownloadProvider Tests', () => {
151151
return fixture;
152152
}
153153

154-
test('installSQLToolsService should download and decompress the service and update the status', () => {
154+
test('installSQLToolsService should download and decompress the service and update the status', async () => {
155155
let fixture: IFixture = {
156156
downloadUrl: undefined,
157157
downloadProvider: undefined,
158158
downloadResult: Promise.resolve(),
159159
decompressResult: Promise.resolve()
160160
};
161161

162-
fixture = createDownloadProvider(fixture);
162+
fixture = await createDownloadProvider(fixture);
163163
return fixture.downloadProvider.installSQLToolsService(Runtime.Windows_7_64).then(_ => {
164164
testHttpClient.verify(x => x.downloadFile(fixture.downloadUrl, TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(),
165165
TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
@@ -172,15 +172,15 @@ suite('ServiceDownloadProvider Tests', () => {
172172
});
173173

174174
// @cssuh 10/22 - commented this test because it was throwing some random undefined errors
175-
test.skip('installSQLToolsService should not call decompress if download fails', () => {
175+
test.skip('installSQLToolsService should not call decompress if download fails', async () => {
176176
let fixture: IFixture = {
177177
downloadUrl: undefined,
178178
downloadProvider: undefined,
179179
downloadResult: Promise.reject('download failed'),
180180
decompressResult: Promise.resolve()
181181
};
182182

183-
fixture = createDownloadProvider(fixture);
183+
fixture = await createDownloadProvider(fixture);
184184
return fixture.downloadProvider.installSQLToolsService(Runtime.Windows_7_64).catch(_ => {
185185
testHttpClient.verify(x => x.downloadFile(fixture.downloadUrl, TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(),
186186
TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
@@ -192,15 +192,15 @@ suite('ServiceDownloadProvider Tests', () => {
192192
});
193193
});
194194

195-
test.skip('installSQLToolsService should not update status to installed decompress fails', () => {
195+
test.skip('installSQLToolsService should not update status to installed decompress fails', async () => {
196196
let fixture: IFixture = {
197197
downloadUrl: undefined,
198198
downloadProvider: undefined,
199199
downloadResult: Promise.resolve(),
200200
decompressResult: Promise.reject('download failed')
201201
};
202202

203-
fixture = createDownloadProvider(fixture);
203+
fixture = await createDownloadProvider(fixture);
204204
return fixture.downloadProvider.installSQLToolsService(Runtime.Windows_7_64).catch(_ => {
205205
testHttpClient.verify(x => x.downloadFile(fixture.downloadUrl, TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(),
206206
TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),

test/server.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ suite('Server tests', () => {
3333

3434
function setupMocks(fixture: IFixture): void {
3535
testConfig.setup(x => x.getSqlToolsExecutableFiles()).returns(() => fixture.executablesFromConfig);
36-
testDownloadProvider.setup(x => x.getInstallDirectory(fixture.runtime)).returns(() => fixture.installDir);
36+
testDownloadProvider.setup(x => x.getOrMakeInstallDirectory(fixture.runtime)).returns(() => Promise.resolve(fixture.installDir));
3737
testDownloadProvider.setup(x => x.installSQLToolsService(fixture.runtime)).callback(() => {
3838
fixture.executablesFromConfig = [fixture.executableFileName.replace(fixture.installDir, '')];
3939
}).returns(() => { return Promise.resolve(true); });

0 commit comments

Comments
 (0)