This commit is contained in:
Sandeep Somavarapu
2019-01-10 19:21:48 +01:00
parent fe5f523735
commit 674807885b

View File

@@ -62,8 +62,6 @@ export function getIdAndVersion(id: string): [string, string | undefined] {
}
type Task = { (): Promise<void> };
class Main {
constructor(
@@ -82,7 +80,7 @@ class Main {
} else if (argv['install-extension']) {
const arg = argv['install-extension'];
const args: string[] = typeof arg === 'string' ? [arg] : arg;
await this.installExtension(args, argv['force']);
await this.installExtensions(args, argv['force']);
} else if (argv['uninstall-extension']) {
const arg = argv['uninstall-extension'];
@@ -100,77 +98,84 @@ class Main {
extensions.forEach(e => console.log(getId(e.manifest, showVersions)));
}
private installExtension(extensions: string[], force: boolean): Promise<any> {
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) {
return this.extensionManagementService.install(URI.file(extension)).then(() => {
console.log(localize('successVsixInstall', "Extension '{0}' was successfully installed!", getBaseLabel(extension)));
}, error => {
if (isPromiseCanceledError(error)) {
console.log(localize('cancelVsixInstall', "Cancelled installing Extension '{0}'.", getBaseLabel(extension)));
return null;
} else {
return Promise.reject(error);
}
});
}
return null;
});
});
const galleryTasks: Task[] = extensions
.filter(e => !/\.vsix$/i.test(e))
.map(e => () => {
const [id, version] = getIdAndVersion(e);
return this.extensionManagementService.getInstalled(LocalExtensionType.User)
.then(installed => this.extensionGalleryService.getExtension({ id }, version)
.then<IGalleryExtension>(null, err => {
if (err.responseText) {
try {
const response = JSON.parse(err.responseText);
return Promise.reject(response.message);
} catch (e) {
// noop
}
}
return Promise.reject(err);
})
.then(extension => {
if (!extension) {
return Promise.reject(new Error(`${notFound(version ? `${id}@${version}` : id)}\n${useId}`));
}
const [installedExtension] = installed.filter(e => areSameExtensions({ id: getGalleryExtensionIdFromLocal(e) }, { id }));
if (installedExtension) {
if (extension.version !== installedExtension.manifest.version) {
if (version || force) {
console.log(localize('updateMessage', "Updating the Extension '{0}' to the version {1}", id, extension.version));
return this.installFromGallery(id, extension);
} else {
console.log(localize('forceUpdate', "Extension '{0}' v{1} is already installed, but a newer version {2} is available in the marketplace. Use '--force' option to update to newer version.", id, installedExtension.manifest.version, extension.version));
return Promise.resolve(null);
}
} else {
console.log(localize('alreadyInstalled', "Extension '{0}' is already installed.", version ? `${id}@${version}` : id));
return Promise.resolve(null);
}
} else {
console.log(localize('foundExtension', "Found '{0}' in the marketplace.", id));
return this.installFromGallery(id, extension);
}
}));
});
return sequence([...vsixTasks, ...galleryTasks]);
private async installExtensions(extensions: string[], force: boolean): Promise<void> {
let failed: string[] = [];
for (const extension of extensions) {
try {
await this.installExtension(extension, force);
} catch (err) {
console.error(err.message || err.stack || err);
failed.push(extension);
}
}
return failed.length ? Promise.reject(localize('installation failed', "Failed Installing Extensions: {0}", failed.join(', '))) : Promise.resolve();
}
private installExtension(extension: string, force: boolean): Promise<any> {
if (/\.vsix$/i.test(extension)) {
extension = path.isAbsolute(extension) ? extension : path.join(process.cwd(), extension);
return this.validate(extension, force)
.then(valid => {
if (valid) {
return this.extensionManagementService.install(URI.file(extension)).then(() => {
console.log(localize('successVsixInstall', "Extension '{0}' was successfully installed!", getBaseLabel(extension)));
}, error => {
if (isPromiseCanceledError(error)) {
console.log(localize('cancelVsixInstall', "Cancelled installing Extension '{0}'.", getBaseLabel(extension)));
return null;
} else {
return Promise.reject(error);
}
});
}
return null;
});
}
const [id, version] = getIdAndVersion(extension);
return this.extensionManagementService.getInstalled(LocalExtensionType.User)
.then(installed => this.extensionGalleryService.getExtension({ id }, version)
.then<IGalleryExtension>(null, err => {
if (err.responseText) {
try {
const response = JSON.parse(err.responseText);
return Promise.reject(response.message);
} catch (e) {
// noop
}
}
return Promise.reject(err);
})
.then(extension => {
if (!extension) {
return Promise.reject(new Error(`${notFound(version ? `${id}@${version}` : id)}\n${useId}`));
}
const [installedExtension] = installed.filter(e => areSameExtensions({ id: getGalleryExtensionIdFromLocal(e) }, { id }));
if (installedExtension) {
if (extension.version !== installedExtension.manifest.version) {
if (version || force) {
console.log(localize('updateMessage', "Updating the Extension '{0}' to the version {1}", id, extension.version));
return this.installFromGallery(id, extension);
} else {
console.log(localize('forceUpdate', "Extension '{0}' v{1} is already installed, but a newer version {2} is available in the marketplace. Use '--force' option to update to newer version.", id, installedExtension.manifest.version, extension.version));
return Promise.resolve(null);
}
} else {
console.log(localize('alreadyInstalled', "Extension '{0}' is already installed.", version ? `${id}@${version}` : id));
return Promise.resolve(null);
}
} else {
console.log(localize('foundExtension', "Found '{0}' in the marketplace.", id));
return this.installFromGallery(id, extension);
}
}));
}
private async validate(vsix: string, force: boolean): Promise<boolean> {
const manifest = await getManifest(vsix);