diff --git a/src/extension/commands/commandUtil.ts b/src/extension/commands/commandUtil.ts new file mode 100644 index 0000000..04086e7 --- /dev/null +++ b/src/extension/commands/commandUtil.ts @@ -0,0 +1,80 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import * as vscode from "vscode"; +import * as nls from "vscode-nls"; +import { SimulateOptions } from "cordova-simulate"; +import { ProjectsStorage } from "../projectsStorage"; +import { CordovaWorkspaceManager } from "../cordovaWorkspaceManager"; +import { TelemetryHelper } from "../../utils/telemetryHelper"; + +nls.config({ + messageFormat: nls.MessageFormat.bundle, + bundleFormat: nls.BundleFormat.standalone, +})(); +const localize = nls.loadMessageBundle(); + +export function selectProject(): Promise { + const keys = Object.keys(ProjectsStorage.projectsCache); + if (keys.length > 1) { + return new Promise((resolve, reject) => { + vscode.window.showQuickPick(keys).then(selected => { + if (selected) { + resolve(ProjectsStorage.projectsCache[selected]); + } + }, reject); + }); + } else if (keys.length === 1) { + return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]); + } + return Promise.reject( + new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")), + ); +} + +export function commandWrapper(fn, args) { + return selectProject().then(project => { + return fn(project.workspaceRoot.uri.fsPath, ...args); + }); +} +/* Launches a simulate command and records telemetry for it */ +export function launchSimulateCommand( + cordovaProjectRoot: string, + options: SimulateOptions, +): Promise { + return TelemetryHelper.generate("simulateCommand", generator => { + return TelemetryHelper.determineProjectTypes(cordovaProjectRoot).then(projectType => { + generator.add( + "simulateOptions", + { + platform: options.platform, + target: options.target, + livereload: options.livereload, + forceprepare: options.forceprepare, + corsproxy: options.corsproxy, + }, + false, + ); + generator.add( + "projectType", + TelemetryHelper.prepareProjectTypesTelemetry(projectType), + false, + ); + // visibleTextEditors is null proof (returns empty array if no editors visible) + generator.add( + "visibleTextEditorsCount", + vscode.window.visibleTextEditors.length, + false, + ); + return projectType; + }); + }).then(projectType => { + const uri = vscode.Uri.file(cordovaProjectRoot); + const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri); + return ProjectsStorage.getFolder(workspaceFolder).pluginSimulator.simulate( + cordovaProjectRoot, + options, + projectType, + ); + }); +} diff --git a/src/extension/commands/commands.ts b/src/extension/commands/commands.ts new file mode 100644 index 0000000..22435c8 --- /dev/null +++ b/src/extension/commands/commands.ts @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +export * from "./restart"; +export * from "./cordovaRun"; +export * from "./cordovaBuild"; +export * from "./cordovaPrepare"; +export * from "./ionicBuild"; +export * from "./ionicPrepare"; +export * from "./ionicRun"; +export * from "./simulateAndroid"; +export * from "./simulateIos"; diff --git a/src/extension/commands/cordovaBuild.ts b/src/extension/commands/cordovaBuild.ts new file mode 100644 index 0000000..e9845f8 --- /dev/null +++ b/src/extension/commands/cordovaBuild.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class CordovaBuild { + static codeName = "cordova.build"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build"]); +} diff --git a/src/extension/commands/cordovaPrepare.ts b/src/extension/commands/cordovaPrepare.ts new file mode 100644 index 0000000..12f4993 --- /dev/null +++ b/src/extension/commands/cordovaPrepare.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class CordovaPrepare { + static codeName = "cordova.prepare"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare"]); +} diff --git a/src/extension/commands/cordovaRun.ts b/src/extension/commands/cordovaRun.ts new file mode 100644 index 0000000..d3d52cc --- /dev/null +++ b/src/extension/commands/cordovaRun.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class CordovaRun { + static codeName = "cordova.run"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run"]); +} diff --git a/src/extension/commands/ionicBuild.ts b/src/extension/commands/ionicBuild.ts new file mode 100644 index 0000000..f264f62 --- /dev/null +++ b/src/extension/commands/ionicBuild.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class IonicBuild { + static codeName = "ionic.build"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build", true]); +} diff --git a/src/extension/commands/ionicPrepare.ts b/src/extension/commands/ionicPrepare.ts new file mode 100644 index 0000000..675a233 --- /dev/null +++ b/src/extension/commands/ionicPrepare.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class IonicPrepare { + static codeName = "ionic.prepare"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare", true]); +} diff --git a/src/extension/commands/ionicRun.ts b/src/extension/commands/ionicRun.ts new file mode 100644 index 0000000..05f00da --- /dev/null +++ b/src/extension/commands/ionicRun.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; + +export class IonicRun { + static codeName = "ionic.run"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run", true]); +} diff --git a/src/extension/commands/restart.ts b/src/extension/commands/restart.ts new file mode 100644 index 0000000..06b5e4b --- /dev/null +++ b/src/extension/commands/restart.ts @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper"; +import { commandWrapper } from "./commandUtil"; +import { CordovaSessionManager } from "../cordovaSessionManager"; + +export class Restart { + static codeName = "cordova.restart"; + static createHandler = () => + commandWrapper(CordovaCommandHelper.restartCordovaDebugging, [new CordovaSessionManager()]); +} diff --git a/src/extension/commands/simulateAndroid.ts b/src/extension/commands/simulateAndroid.ts new file mode 100644 index 0000000..a3186ab --- /dev/null +++ b/src/extension/commands/simulateAndroid.ts @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. +import * as vscode from "vscode"; +import { selectProject, launchSimulateCommand } from "./commandUtil"; + +export class SimulateAndroid { + static codeName = "cordova.simulate.android"; + static createHandler = () => { + return selectProject().then(project => { + return launchSimulateCommand(project.workspaceRoot.uri.fsPath, { + dir: project.workspaceRoot.uri.fsPath, + target: "chrome", + platform: "android", + lang: vscode.env.language, + }); + }); + }; +} diff --git a/src/extension/commands/simulateIos.ts b/src/extension/commands/simulateIos.ts new file mode 100644 index 0000000..4b96a4a --- /dev/null +++ b/src/extension/commands/simulateIos.ts @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import * as vscode from "vscode"; +import { selectProject, launchSimulateCommand } from "./commandUtil"; + +export class SimulateIos { + static codeName = "cordova.simulate.ios"; + static createHandler = () => { + return selectProject().then(project => { + return launchSimulateCommand(project.workspaceRoot.uri.fsPath, { + dir: project.workspaceRoot.uri.fsPath, + target: "chrome", + platform: "ios", + lang: vscode.env.language, + }); + }); + }; +} diff --git a/src/extension/cordova-extension.ts b/src/extension/cordova-extension.ts index 61b06c2..3048067 100644 --- a/src/extension/cordova-extension.ts +++ b/src/extension/cordova-extension.ts @@ -3,12 +3,10 @@ import * as fs from "fs"; import * as path from "path"; -import { SimulateOptions } from "cordova-simulate"; import * as vscode from "vscode"; import * as semver from "semver"; import * as nls from "vscode-nls"; import { CordovaProjectHelper } from "../utils/cordovaProjectHelper"; -import { CordovaCommandHelper } from "../utils/cordovaCommandHelper"; import { Telemetry } from "../utils/telemetry"; import { TelemetryHelper } from "../utils/telemetryHelper"; import { TsdHelper } from "../utils/tsdHelper"; @@ -71,7 +69,7 @@ export function activate(context: vscode.ExtensionContext): void { vscode.workspace.workspaceFolders; if (workspaceFolders) { - registerCordovaCommands(cordovaFactory); + registerCordovaCommands(); workspaceFolders.forEach((folder: vscode.WorkspaceFolder) => { onFolderAdded(folder); }); @@ -443,130 +441,13 @@ function updatePluginTypeDefinitions(cordovaProjectRoot: string): void { }); } -/* Launches a simulate command and records telemetry for it */ -function launchSimulateCommand( - cordovaProjectRoot: string, - options: SimulateOptions, -): Promise { - return TelemetryHelper.generate("simulateCommand", generator => { - return TelemetryHelper.determineProjectTypes(cordovaProjectRoot).then(projectType => { - generator.add( - "simulateOptions", - { - platform: options.platform, - target: options.target, - livereload: options.livereload, - forceprepare: options.forceprepare, - corsproxy: options.corsproxy, - }, - false, - ); - generator.add( - "projectType", - TelemetryHelper.prepareProjectTypesTelemetry(projectType), - false, - ); - // visibleTextEditors is null proof (returns empty array if no editors visible) - generator.add( - "visibleTextEditorsCount", - vscode.window.visibleTextEditors.length, - false, - ); - return projectType; - }); - }).then(projectType => { - const uri = vscode.Uri.file(cordovaProjectRoot); - const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri); - return ProjectsStorage.getFolder(workspaceFolder).pluginSimulator.simulate( - cordovaProjectRoot, - options, - projectType, - ); +async function registerCordovaCommands() { + const commands = await import("./commands/commands"); + Object.values(commands).forEach(it => { + EXTENSION_CONTEXT.subscriptions.push(register(it)); }); } -function registerCordovaCommands(cordovaSessionManager: CordovaSessionManager): void { - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.restart", () => - commandWrapper(CordovaCommandHelper.restartCordovaDebugging, [cordovaSessionManager]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.prepare", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare"]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.build", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build"]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.run", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run"]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("ionic.prepare", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare", true]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("ionic.build", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build", true]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("ionic.run", () => - commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run", true]), - ), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.simulate.android", () => { - return selectProject().then(project => { - return launchSimulateCommand(project.workspaceRoot.uri.fsPath, { - dir: project.workspaceRoot.uri.fsPath, - target: "chrome", - platform: "android", - lang: vscode.env.language, - }); - }); - }), - ); - EXTENSION_CONTEXT.subscriptions.push( - vscode.commands.registerCommand("cordova.simulate.ios", () => { - return selectProject().then(project => { - return launchSimulateCommand(project.workspaceRoot.uri.fsPath, { - dir: project.workspaceRoot.uri.fsPath, - target: "chrome", - platform: "ios", - lang: vscode.env.language, - }); - }); - }), - ); -} - -function selectProject(): Promise { - const keys = Object.keys(ProjectsStorage.projectsCache); - if (keys.length > 1) { - return new Promise((resolve, reject) => { - vscode.window.showQuickPick(keys).then(selected => { - if (selected) { - resolve(ProjectsStorage.projectsCache[selected]); - } - }, reject); - }); - } else if (keys.length === 1) { - return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]); - } - return Promise.reject( - new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")), - ); -} - -function commandWrapper(fn, args) { - return selectProject().then(project => { - return fn(project.workspaceRoot.uri.fsPath, ...args); - }); +function register(it) { + return vscode.commands.registerCommand(it.codeName, it.createHandler); } diff --git a/test/cordova.test.ts b/test/cordova.test.ts index 3dae30a..fbee649 100644 --- a/test/cordova.test.ts +++ b/test/cordova.test.ts @@ -28,9 +28,9 @@ suite("extensionContext", () => { }); assert.deepStrictEqual(cordovaCommandsAvailable, [ "cordova.restart", - "cordova.prepare", - "cordova.build", "cordova.run", + "cordova.build", + "cordova.prepare", "cordova.simulate.android", "cordova.simulate.ios", ]);