Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Q usages from the extension folder #725

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/customRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ try {
: eval("require"); // tslint:disable-line:no-eval
} catch {
// Use a noop in case both `__non_webpack_require__` and `require` does not exist
customRequire = () => { }; // tslint:disable-line:no-empty
customRequire = () => {}; // eslint-disable-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-empty-function
}

export default customRequire;
19 changes: 9 additions & 10 deletions src/cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { SimulateOptions } from "cordova-simulate";
import * as vscode from "vscode";
import { CordovaProjectHelper } from "./utils/cordovaProjectHelper";
import { CordovaCommandHelper } from "./utils/cordovaCommandHelper";
import * as Q from "q";
import * as semver from "semver";
import { Telemetry } from "./utils/telemetry";
import { TelemetryHelper } from "./utils/telemetryHelper";
Expand Down Expand Up @@ -113,11 +112,11 @@ export function onFolderAdded(folder: vscode.WorkspaceFolder): void {
TelemetryHelper.determineProjectTypes(workspaceRoot)
.then((projType) => {
cordovaProjectTypeEvent.properties["projectType"] = projType;
throw new Error("afsdfa");
})
.finally(() => {
Telemetry.send(cordovaProjectTypeEvent);
})
.done();
});

// We need to update the type definitions added to the project
// as and when plugins are added or removed. For this reason,
Expand Down Expand Up @@ -201,9 +200,9 @@ export function onFolderAdded(folder: vscode.WorkspaceFolder): void {
let jsconfigPath: string = path.join(workspaceRoot, JSCONFIG_FILENAME);
let tsconfigPath: string = path.join(workspaceRoot, TSCONFIG_FILENAME);

Q.all([Q.nfcall(fs.exists, jsconfigPath), Q.nfcall(fs.exists, tsconfigPath)]).spread((jsExists: boolean, tsExists: boolean) => {
Promise.all([CordovaProjectHelper.exists(jsconfigPath), CordovaProjectHelper.exists(tsconfigPath)]).then(([jsExists, tsExists]) => {
if (!jsExists && !tsExists) {
Q.nfcall(fs.writeFile, jsconfigPath, "{}").then(() => {
fs.promises.writeFile(jsconfigPath, "{}").then(() => {
// Any open file must be reloaded to enable intellisense on them, so inform the user
vscode.window.showInformationMessage("A 'jsconfig.json' file was created to enable IntelliSense. You may need to reload your open JS file(s).");
});
Expand Down Expand Up @@ -349,7 +348,7 @@ function updatePluginTypeDefinitions(cordovaProjectRoot: string): void {
}

/* Launches a simulate command and records telemetry for it */
function launchSimulateCommand(cordovaProjectRoot: string, options: SimulateOptions): Q.Promise<void> {
function launchSimulateCommand(cordovaProjectRoot: string, options: SimulateOptions): Promise<void> {
return TelemetryHelper.generate("simulateCommand", (generator) => {
return TelemetryHelper.determineProjectTypes(cordovaProjectRoot)
.then((projectType) => {
Expand Down Expand Up @@ -394,10 +393,10 @@ function registerCordovaCommands(cordovaSessionManager: CordovaSessionManager):
}));
}

function selectProject(): Q.Promise<CordovaWorkspaceManager> {
function selectProject(): Promise<CordovaWorkspaceManager> {
let keys = Object.keys(ProjectsStorage.projectsCache);
if (keys.length > 1) {
return Q.Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
vscode.window.showQuickPick(keys)
.then((selected) => {
if (selected) {
Expand All @@ -406,9 +405,9 @@ function selectProject(): Q.Promise<CordovaWorkspaceManager> {
}, reject);
});
} else if (keys.length === 1) {
return Q.resolve(ProjectsStorage.projectsCache[keys[0]]);
return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]);
} else {
return Q.reject(new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")));
return Promise.reject(new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ChromeCDPMessageHandler extends CDPMessageHandlerBase {
};
}

public configureHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs) { }
public configureHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs): void { }

private fixSourcemapLocation(reqParams: any): any {
let absoluteSourcePath = this.sourcemapPathTransformer.getClientPathFromHttpBasedUrl(reqParams.url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class SafariCDPMessageHandler extends CDPMessageHandlerBase {
}
}

public configureHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs) {
public configureHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs): void {
this.isTargeted = semver.gte(args.iOSVersion, "12.2.0");
if (!this.isIonicProject) {
if (args.iOSAppPackagePath) {
Expand Down
4 changes: 2 additions & 2 deletions src/debugger/cdp-proxy/cordovaCDPProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ export class CordovaCDPProxy {
this.applicationTargetPort = applicationTargetPort;
}

public setBrowserInspectUri(browserInspectUri: string) {
public setBrowserInspectUri(browserInspectUri: string): void {
this.browserInspectUri = browserInspectUri;
}

public configureCDPMessageHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs) {
public configureCDPMessageHandlerAccordingToProcessedAttachArgs(args: ICordovaAttachRequestArgs): void {
if (
args.iOSVersion
&& !this.communicationPreparationsDone
Expand Down
6 changes: 3 additions & 3 deletions src/debugger/cordovaDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ export class CordovaDebugSession extends LoggingDebugSession {
enum IonicDevServerStatus {
ServerReady,
AppReady,
};
}

if (!launchArgs.runArguments || launchArgs.runArguments.length === 0) {
if (launchArgs.devServerAddress) {
Expand All @@ -973,7 +973,7 @@ export class CordovaDebugSession extends LoggingDebugSession {
let ionicLivereloadProcessStatus = {
serverReady: false,
appReady: false,
}
};
let serverReadyTimeout: number = launchArgs.devServerTimeout || 30000;
let appReadyTimeout: number = launchArgs.devServerTimeout || 120000; // If we're not serving, the app needs to build and deploy (and potentially start the emulator), which can be very long
let serverOut: string = "";
Expand Down Expand Up @@ -1050,7 +1050,7 @@ export class CordovaDebugSession extends LoggingDebugSession {
ionicLivereloadProcessStatus.appReady = true;
_resolve(serverUrls);
}
}
};

this.ionicLivereloadProcess.on("error", (err: { code: string }) => {
if (err.code === "ENOENT") {
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/cordovaIosDeviceLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class CordovaIosDeviceLauncher {
const bundleId = allConfigs[targetConfigUUID].buildSettings.PRODUCT_BUNDLE_IDENTIFIER;
return Promise.resolve(bundleId);
}

private static mountDeveloperImage(): Promise<void> {
return CordovaIosDeviceLauncher.getDiskImage()
.then((path: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function cordovaRunCommand(command: string, args: string[], env, cordovaR
cordovaProcess.on("error", error => {
reject(error);
});
})
});
}

export function cordovaStartCommand(command: string, args: string[], env: any, cordovaRootPath: string): child_process.ChildProcess {
Expand Down
24 changes: 11 additions & 13 deletions src/extension/cordovaWorkspaceManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import * as Q from "q";
import { PluginSimulator } from "./simulate";
import { SimulationInfo } from "../common/simulationInfo";
import { SimulateOptions } from "cordova-simulate";
Expand Down Expand Up @@ -52,9 +51,9 @@ export class CordovaWorkspaceManager implements vscode.Disposable {
/**
* Sends telemetry
*/
public sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: { [key: string]: string }, measures: { [key: string]: number }): Q.Promise<any> {
public sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: { [key: string]: string }, measures: { [key: string]: number }): Promise<void> {
Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures);
return Q.resolve({});
return Promise.resolve();
}

/**
Expand All @@ -63,7 +62,7 @@ export class CordovaWorkspaceManager implements vscode.Disposable {
*
* Returns info about the running simulate server
*/
public simulate(fsPath: string, simulateOptions: SimulateOptions, projectType: IProjectType): Q.Promise<SimulationInfo> {
public simulate(fsPath: string, simulateOptions: SimulateOptions, projectType: IProjectType): Promise<SimulationInfo> {
return this.launchSimulateServer(fsPath, simulateOptions, projectType)
.then((simulateInfo: SimulationInfo) => {
return this.launchSimHost(simulateOptions.target).then(() => simulateInfo);
Expand All @@ -75,31 +74,30 @@ export class CordovaWorkspaceManager implements vscode.Disposable {
*
* Returns info about the running simulate server
*/
public launchSimulateServer(fsPath: string, simulateOptions: SimulateOptions, projectType: IProjectType): Q.Promise<SimulationInfo> {
public launchSimulateServer(fsPath: string, simulateOptions: SimulateOptions, projectType: IProjectType): Promise<SimulationInfo> {
return this.pluginSimulator.launchServer(fsPath, simulateOptions, projectType);
}

/**
* Launches sim-host using an already running simulate server.
*/
public launchSimHost(target: string): Q.Promise<void> {
public launchSimHost(target: string): Promise<void> {
return this.pluginSimulator.launchSimHost(target);
}

/**
* Returns the number of currently visible editors.
*/
public getVisibleEditorsCount(): Q.Promise<number> {
public getVisibleEditorsCount(): Promise<number> {
// visibleTextEditors is null proof (returns empty array if no editors visible)
return Q.resolve(vscode.window.visibleTextEditors.length);
return Promise.resolve(vscode.window.visibleTextEditors.length);
}

public getRunArguments(fsPath: string): Q.Promise<string[]> {
return Q.resolve(CordovaCommandHelper.getRunArguments(fsPath));
public getRunArguments(fsPath: string): Promise<string[]> {
return Promise.resolve(CordovaCommandHelper.getRunArguments(fsPath));
}


public getCordovaExecutable(fsPath: string): Q.Promise<string> {
return Q.resolve(CordovaCommandHelper.getCordovaExecutable(fsPath));
public getCordovaExecutable(fsPath: string): Promise<string> {
return Promise.resolve(CordovaCommandHelper.getCordovaExecutable(fsPath));
}
}
97 changes: 48 additions & 49 deletions src/extension/simulate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import * as Q from "q";
import * as path from "path";
import * as CordovaSimulate from "cordova-simulate";
import { CordovaSimulateTelemetry } from "../utils/cordovaSimulateTelemetry";
Expand Down Expand Up @@ -40,30 +39,30 @@ export class PluginSimulator implements vscode.Disposable {
private simulatePackage: typeof CordovaSimulate;
private packageInstallProc: cp.ChildProcess | null = null;

public simulate(fsPath: string, simulateOptions: CordovaSimulate.SimulateOptions, projectType: IProjectType): Q.Promise<any> {
public simulate(fsPath: string, simulateOptions: CordovaSimulate.SimulateOptions, projectType: IProjectType): Promise<any> {
return this.launchServer(fsPath, simulateOptions, projectType)
.then(() => this.launchSimHost(simulateOptions.target))
.then(() => this.launchAppHost(simulateOptions.target));
}

public launchAppHost(target: string): Q.Promise<void> {
public launchAppHost(target: string): Promise<void> {
return this.getPackage()
.then(simulate => {
return simulate.launchBrowser(target, this.simulationInfo.appHostUrl);
});
}

public launchSimHost(target: string): Q.Promise<void> {
public launchSimHost(target: string): Promise<void> {
if (!this.simulator) {
return Q.reject<void>(new Error(localize("LaunchingSimHostBeforeStartSimulationServer", "Launching sim host before starting simulation server")));
return Promise.reject<void>(new Error(localize("LaunchingSimHostBeforeStartSimulationServer", "Launching sim host before starting simulation server")));
}
return this.getPackage()
.then(simulate => {
return simulate.launchBrowser(target, this.simulator.simHostUrl());
});
}

public launchServer(fsPath: string, simulateOptions: CordovaSimulate.SimulateOptions, projectType: IProjectType): Q.Promise<SimulationInfo> {
public launchServer(fsPath: string, simulateOptions: CordovaSimulate.SimulateOptions, projectType: IProjectType): Promise<SimulationInfo> {
const uri = vscode.Uri.file(fsPath);
const workspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(uri);
simulateOptions.dir = workspaceFolder.uri.fsPath;
Expand Down Expand Up @@ -124,20 +123,20 @@ export class PluginSimulator implements vscode.Disposable {
}

if (this.simulator) {
this.simulator.stopSimulation().done(() => { }, () => { });
this.simulator.stopSimulation().then(() => { }, () => { });
this.simulator = null;
}
}

public getPackage(): Q.Promise<typeof CordovaSimulate> {
public getPackage(): Promise<typeof CordovaSimulate> {
if (this.simulatePackage) {
return Q.resolve(this.simulatePackage);
return Promise.resolve(this.simulatePackage);
}
// Don't do the require if we don't actually need it
try {
const simulate = customRequire(this.CORDOVA_SIMULATE_PACKAGE) as typeof CordovaSimulate;
this.simulatePackage = simulate;
return Q.resolve(this.simulatePackage);
return Promise.resolve(this.simulatePackage);
} catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
OutputChannelLogger.getMainChannel().log(localize("CordovaSimulateDepNotPresent", "cordova-simulate dependency not present. Installing it..."));
Expand All @@ -146,47 +145,47 @@ export class PluginSimulator implements vscode.Disposable {
}
}

const packageFound = Q.defer<typeof CordovaSimulate>();
if (!this.packageInstallProc) {
this.packageInstallProc = cp.spawn(process.platform === "win32" ? "npm.cmd" : "npm",
["install", this.CORDOVA_SIMULATE_PACKAGE, "--verbose", "--no-save"],
{ cwd: path.dirname(findFileInFolderHierarchy(__dirname, "package.json")) });

this.packageInstallProc.once("exit", (code: number) => {
if (code === 0) {
this.simulatePackage = customRequire(this.CORDOVA_SIMULATE_PACKAGE);
packageFound.resolve(this.simulatePackage);
} else {
OutputChannelLogger.getMainChannel().log(localize("ErrorWhileInstallingCordovaSimulateDep", "Error while installing cordova-simulate dependency to the extension"));
packageFound.reject(localize("ErrorWhileInstallingCordovaSimulateDep", "Error while installing cordova-simulate dependency to the extension"));
}
});

let lastDotTime = 0;
const printDot = () => {
const now = Date.now();
if (now - lastDotTime > 1500) {
lastDotTime = now;
OutputChannelLogger.getMainChannel().append(".");
}
};
return new Promise((resolve, reject) => {
if (!this.packageInstallProc) {
this.packageInstallProc = cp.spawn(process.platform === "win32" ? "npm.cmd" : "npm",
["install", this.CORDOVA_SIMULATE_PACKAGE, "--verbose", "--no-save"],
{ cwd: path.dirname(findFileInFolderHierarchy(__dirname, "package.json")) });

this.packageInstallProc.once("exit", (code: number) => {
if (code === 0) {
this.simulatePackage = customRequire(this.CORDOVA_SIMULATE_PACKAGE);
resolve(this.simulatePackage);
} else {
OutputChannelLogger.getMainChannel().log(localize("ErrorWhileInstallingCordovaSimulateDep", "Error while installing cordova-simulate dependency to the extension"));
reject(localize("ErrorWhileInstallingCordovaSimulateDep", "Error while installing cordova-simulate dependency to the extension"));
}
});

let lastDotTime = 0;
const printDot = () => {
const now = Date.now();
if (now - lastDotTime > 1500) {
lastDotTime = now;
OutputChannelLogger.getMainChannel().append(".");
}
};

this.packageInstallProc.stdout.on("data", () => {
printDot();
});
this.packageInstallProc.stdout.on("data", () => {
printDot();
});

this.packageInstallProc.stderr.on("data", (data: Buffer) => {
printDot();
});
} else {
const packageCheck = setInterval(() => {
if (this.simulatePackage) {
clearInterval(packageCheck);
packageFound.resolve(this.simulatePackage);
}
}, 1000);
}
return packageFound.promise;
this.packageInstallProc.stderr.on("data", (data: Buffer) => {
printDot();
});
} else {
const packageCheck = setInterval(() => {
if (this.simulatePackage) {
clearInterval(packageCheck);
resolve(this.simulatePackage);
}
}, 1000);
}
});
}

private isServerRunning(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cordovaCommandHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class CordovaCommandHelper {
});
}

public static restartCordovaDebugging(projectRoot: string, cordovaSessionManager: CordovaSessionManager) {
public static restartCordovaDebugging(projectRoot: string, cordovaSessionManager: CordovaSessionManager): void {
const cordovaDebugSession = cordovaSessionManager.getCordovaDebugSessionByProjectRoot(projectRoot);
if (cordovaDebugSession) {
switch (cordovaDebugSession.getStatus()) {
Expand Down