diff --git a/src/vs/code/node/cliProcessMain.ts b/src/vs/code/node/cliProcessMain.ts index d13358368ccc338bf0508c8e0c44c6eac0665c94..8df36e47959c46b0f59330d05a6791a2135a07c8 100644 --- a/src/vs/code/node/cliProcessMain.ts +++ b/src/vs/code/node/cliProcessMain.ts @@ -9,7 +9,6 @@ import pkg from 'vs/platform/node/package'; import * as path from 'path'; import * as semver from 'semver'; -import { TPromise } from 'vs/base/common/winjs.base'; import { sequence } from 'vs/base/common/async'; import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; @@ -63,7 +62,7 @@ export function getIdAndVersion(id: string): [string, string] { } -type Task = { (): TPromise }; +type Task = { (): Thenable }; class Main { @@ -73,41 +72,40 @@ class Main { @IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService ) { } - run(argv: ParsedArgs): TPromise { - // TODO@joao - make this contributable - - let returnPromise: TPromise; + async run(argv: ParsedArgs): Promise { if (argv['install-source']) { - returnPromise = this.setInstallSource(argv['install-source']); + await this.setInstallSource(argv['install-source']); + } else if (argv['list-extensions']) { - returnPromise = this.listExtensions(argv['show-versions']); + await this.listExtensions(argv['show-versions']); + } else if (argv['install-extension']) { const arg = argv['install-extension']; const args: string[] = typeof arg === 'string' ? [arg] : arg; - returnPromise = this.installExtension(args, argv['force']); + await this.installExtension(args, argv['force']); + } else if (argv['uninstall-extension']) { const arg = argv['uninstall-extension']; const ids: string[] = typeof arg === 'string' ? [arg] : arg; - returnPromise = this.uninstallExtension(ids); + await this.uninstallExtension(ids); } - return returnPromise || TPromise.as(null); } - private setInstallSource(installSource: string): TPromise { + private setInstallSource(installSource: string): Promise { return writeFile(this.environmentService.installSourcePath, installSource.slice(0, 30)); } - private listExtensions(showVersions: boolean): TPromise { - return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(extensions => { - extensions.forEach(e => console.log(getId(e.manifest, showVersions))); - }); + private async listExtensions(showVersions: boolean): Promise { + const extensions = await this.extensionManagementService.getInstalled(LocalExtensionType.User); + extensions.forEach(e => console.log(getId(e.manifest, showVersions))); } - private installExtension(extensions: string[], force: boolean): TPromise { + private installExtension(extensions: string[], force: boolean): Promise { const vsixTasks: Task[] = extensions .filter(e => /\.vsix$/i.test(e)) .map(id => () => { const extension = path.isAbsolute(id) ? id : path.join(process.cwd(), id); + return this.validate(extension, force) .then(valid => { if (valid) { @@ -118,7 +116,7 @@ class Main { console.log(localize('cancelVsixInstall', "Cancelled installing Extension '{0}'.", getBaseLabel(extension))); return null; } else { - return TPromise.wrapError(error); + return Promise.reject(error); } }); } @@ -136,16 +134,16 @@ class Main { if (err.responseText) { try { const response = JSON.parse(err.responseText); - return TPromise.wrapError(response.message); + return Promise.reject(response.message); } catch (e) { // noop } } - return TPromise.wrapError(err); + return Promise.reject(err); }) .then(extension => { if (!extension) { - return TPromise.wrapError(new Error(`${notFound(version ? `${id}@${version}` : id)}\n${useId}`)); + return Promise.reject(new Error(`${notFound(version ? `${id}@${version}` : id)}\n${useId}`)); } const [installedExtension] = installed.filter(e => areSameExtensions({ id: getGalleryExtensionIdFromLocal(e) }, { id })); @@ -160,7 +158,7 @@ class Main { } } else { console.log(localize('alreadyInstalled', "Extension '{0}' is already installed.", version ? `${id}@${version}` : id)); - return TPromise.as(null); + return Promise.resolve(null); } } else { console.log(localize('foundExtension', "Found '{0}' in the marketplace.", id)); @@ -173,42 +171,41 @@ class Main { return sequence([...vsixTasks, ...galleryTasks]); } - private validate(vsix: string, force: boolean): Thenable { - return getManifest(vsix) - .then(manifest => { - if (manifest) { - const extensionIdentifier = { id: getGalleryExtensionId(manifest.publisher, manifest.name) }; - return this.extensionManagementService.getInstalled(LocalExtensionType.User) - .then(installedExtensions => { - const newer = installedExtensions.filter(local => areSameExtensions(extensionIdentifier, { id: getGalleryExtensionIdFromLocal(local) }) && semver.gt(local.manifest.version, manifest.version))[0]; - if (newer && !force) { - console.log(localize('forceDowngrade', "A newer version of this extension '{0}' v{1} is already installed. Use '--force' option to downgrade to older version.", newer.galleryIdentifier.id, newer.manifest.version, manifest.version)); - return false; - } - return true; - }); - } else { - return Promise.reject(new Error('Invalid vsix')); - } - }); + private async validate(vsix: string, force: boolean): Promise { + const manifest = await getManifest(vsix); + + if (!manifest) { + throw new Error('Invalid vsix'); + } + + const extensionIdentifier = { id: getGalleryExtensionId(manifest.publisher, manifest.name) }; + const installedExtensions = await this.extensionManagementService.getInstalled(LocalExtensionType.User); + const newer = installedExtensions.filter(local => areSameExtensions(extensionIdentifier, { id: getGalleryExtensionIdFromLocal(local) }) && semver.gt(local.manifest.version, manifest.version))[0]; + + if (newer && !force) { + console.log(localize('forceDowngrade', "A newer version of this extension '{0}' v{1} is already installed. Use '--force' option to downgrade to older version.", newer.galleryIdentifier.id, newer.manifest.version, manifest.version)); + return false; + } + + return true; } - private installFromGallery(id: string, extension: IGalleryExtension): TPromise { + private async installFromGallery(id: string, extension: IGalleryExtension): Promise { console.log(localize('installing', "Installing...")); - return this.extensionManagementService.installFromGallery(extension) - .then( - () => console.log(localize('successInstall', "Extension '{0}' v{1} was successfully installed!", id, extension.version)), - error => { - if (isPromiseCanceledError(error)) { - console.log(localize('cancelVsixInstall', "Cancelled installing Extension '{0}'.", id)); - return null; - } else { - return TPromise.wrapError(error); - } - }); + + try { + await this.extensionManagementService.installFromGallery(extension); + console.log(localize('successInstall', "Extension '{0}' v{1} was successfully installed!", id, extension.version)); + } catch (error) { + if (isPromiseCanceledError(error)) { + console.log(localize('cancelVsixInstall', "Cancelled installing Extension '{0}'.", id)); + } else { + throw error; + } + } } - private uninstallExtension(extensions: string[]): TPromise { + private uninstallExtension(extensions: string[]): Thenable { async function getExtensionId(extensionDescription: string): Promise { if (!/\.vsix$/i.test(extensionDescription)) { return extensionDescription; @@ -225,7 +222,7 @@ class Main { const [extension] = installed.filter(e => areSameExtensions({ id: getGalleryExtensionIdFromLocal(e) }, { id })); if (!extension) { - return TPromise.wrapError(new Error(`${notInstalled(id)}\n${useId}`)); + return Promise.reject(new Error(`${notInstalled(id)}\n${useId}`)); } console.log(localize('uninstalling', "Uninstalling {0}...", id)); @@ -240,7 +237,7 @@ class Main { const eventPrefix = 'monacoworkbench'; -export function main(argv: ParsedArgs): TPromise { +export function main(argv: ParsedArgs): Promise { const services = new ServiceCollection(); const environmentService = new EnvironmentService(argv, process.execPath); @@ -259,7 +256,7 @@ export function main(argv: ParsedArgs): TPromise { const envService = accessor.get(IEnvironmentService); const stateService = accessor.get(IStateService); - return TPromise.join([envService.appSettingsHome, envService.extensionsPath].map(p => mkdirp(p))).then(() => { + return Promise.all([envService.appSettingsHome, envService.extensionsPath].map(p => mkdirp(p))).then(() => { const { appRoot, extensionsPath, extensionDevelopmentLocationURI, isBuilt, installSourcePath } = envService; const services = new ServiceCollection();