Skip to content

Commit

Permalink
Revert "Bundle installation (#183)" (#190)
Browse files Browse the repository at this point in the history
This reverts commit 59bb8b7.
  • Loading branch information
d-gubert authored Nov 21, 2019
1 parent 32be2d2 commit 59541c4
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 204 deletions.
2 changes: 1 addition & 1 deletion src/definition/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rocket.chat/apps-ts-definition",
"version": "1.8.0",
"version": "1.6.0",
"description": "Contains the TypeScript definitions for the Rocket.Chat Applications.",
"main": "index.js",
"typings": "index",
Expand Down
60 changes: 16 additions & 44 deletions src/server/AppManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppBridges } from './bridges';
import { AppCompiler, AppFabricationFulfillment, AppPackageParser, IParseAppZipResult, IParseBundleZipResult, ZipContentType } from './compiler';
import { AppCompiler, AppFabricationFulfillment, AppPackageParser } from './compiler';
import { IGetAppsFilter } from './IGetAppsFilter';
import {
AppAccessorManager,
Expand Down Expand Up @@ -155,20 +155,14 @@ export class AppManager {
const aff = new AppFabricationFulfillment();

try {
const zipResult = await this.getParser().parseZip(this.getCompiler(), Buffer.from(item.zip, 'base64'));

if (zipResult.contentType !== ZipContentType.APP) {
throw new Error('Invalid zip content provided');
}

const result = zipResult.parsed as IParseAppZipResult;
const result = await this.getParser().parseZip(this.getCompiler(), item.zip);

aff.setAppInfo(result.info);
aff.setImplementedInterfaces(result.implemented.getValues());
aff.setCompilerErrors(result.compilerErrors);

if (result.compilerErrors.length > 0) {
const errors = result.compilerErrors.map(({ message }: { message: string }) => message).join('\n');
const errors = result.compilerErrors.map(({ message }) => message).join('\n');

throw new Error(`Failed to compile due to ${ result.compilerErrors.length } errors:\n${ errors }`);
}
Expand Down Expand Up @@ -377,43 +371,27 @@ export class AppManager {
return true;
}

public async add(zipContentsBase64d: string, enable = true, marketplaceInfo?: IMarketplaceInfo): Promise<Array<AppFabricationFulfillment>> {
const parseResult = await this.getParser().parseZip(this.getCompiler(), Buffer.from(zipContentsBase64d, 'base64'));

if (parseResult.contentType === ZipContentType.APP) {
return [await this.addApp(parseResult.parsed as IParseAppZipResult, enable, marketplaceInfo)];
}

if (parseResult.contentType === ZipContentType.BUNDLE) {
return this.addBundle(parseResult.parsed as IParseBundleZipResult, enable);
}
}

private addBundle(bundleParseResult: IParseBundleZipResult, enable: boolean): Promise<Array<AppFabricationFulfillment>> {
return Promise.all(bundleParseResult.apps.map(({ parseResult, license }) => this.addApp(parseResult, enable)));
}

private async addApp(parseResult: IParseAppZipResult, enable: boolean, marketplaceInfo?: IMarketplaceInfo) {
public async add(zipContentsBase64d: string, enable = true, marketplaceInfo?: IMarketplaceInfo): Promise<AppFabricationFulfillment> {
const aff = new AppFabricationFulfillment();
const result = await this.getParser().parseZip(this.getCompiler(), zipContentsBase64d);

aff.setAppInfo(parseResult.info);
aff.setCompilerErrors(parseResult.compilerErrors);
aff.setAppInfo(result.info);
aff.setImplementedInterfaces(result.implemented.getValues());
aff.setCompilerErrors(result.compilerErrors);

if (parseResult.compilerErrors.length > 0) {
if (result.compilerErrors.length > 0) {
return aff;
}

aff.setImplementedInterfaces(parseResult.implemented.getValues());

const created = await this.storage.create({
id: parseResult.info.id,
info: parseResult.info,
id: result.info.id,
info: result.info,
status: AppStatus.UNKNOWN,
zip: parseResult.zipContentsBase64d,
compiled: parseResult.compiledFiles,
languageContent: parseResult.languageContent,
zip: zipContentsBase64d,
compiled: result.compiledFiles,
languageContent: result.languageContent,
settings: {},
implemented: parseResult.implemented.getValues(),
implemented: result.implemented.getValues(),
marketplaceInfo,
});

Expand Down Expand Up @@ -472,13 +450,7 @@ export class AppManager {

public async update(zipContentsBase64d: string): Promise<AppFabricationFulfillment> {
const aff = new AppFabricationFulfillment();
const zipResult = await this.getParser().parseZip(this.getCompiler(), Buffer.from(zipContentsBase64d, 'base64'));

if (zipResult.contentType !== ZipContentType.APP) {
throw new Error('Invalid zip content provided');
}

const result = zipResult.parsed as IParseAppZipResult;
const result = await this.getParser().parseZip(this.getCompiler(), zipContentsBase64d);

aff.setAppInfo(result.info);
aff.setImplementedInterfaces(result.implemented.getValues());
Expand Down
2 changes: 1 addition & 1 deletion src/server/compiler/AppCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class AppCompiler {
character,
message: `${dia.file.fileName} (${line + 1},${character + 1}): ${msg}`,
});
});
});

Object.keys(result.files).forEach((key) => {
const file: ICompilerFile = result.files[key];
Expand Down
111 changes: 10 additions & 101 deletions src/server/compiler/AppPackageParser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { RequiredApiVersionError } from '../errors';
import { AppCompiler } from './AppCompiler';
import { ICompilerFile } from './ICompilerFile';
import { IParseZipResult } from './IParseZipResult';

import * as AdmZip from 'adm-zip';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as uuidv4 from 'uuid/v4';

import { IAppInfo } from '../../definition/metadata/IAppInfo';
import { RequiredApiVersionError } from '../errors';
import { AppCompiler } from './AppCompiler';
import { IBundleManifest } from './IBundleManifest';
import { ICompilerFile } from './ICompilerFile';
import { ICompilerResult } from './ICompilerResult';
import { IParseAppZipResult } from './IParseAppZipResult';
import { IBundleZipAppEntry, IParseBundleZipResult } from './IParseBundleZipResult';
import { IParseZipResult, ZipContentType } from './IParseZipResult';

export class AppPackageParser {
// tslint:disable-next-line:max-line-length
Expand All @@ -24,87 +20,12 @@ export class AppPackageParser {
this.appsEngineVersion = this.getEngineVersion();
}

public parseZip(compiler: AppCompiler, zipContents: Buffer): IParseZipResult {
const zip = new AdmZip(zipContents);
public async parseZip(compiler: AppCompiler, zipBase64: string): Promise<IParseZipResult> {
const zip = new AdmZip(Buffer.from(zipBase64, 'base64'));
const infoZip = zip.getEntry('app.json');
const bundleManifest = zip.getEntry('manifest.json');

if (infoZip) {
return {
contentType: ZipContentType.APP,
parsed: this.parseAppZip(compiler, zip, infoZip),
};
}

if (bundleManifest) {
return {
contentType: ZipContentType.BUNDLE,
parsed: this.parseBundleZip(compiler, zip, bundleManifest),
};
}

throw new Error('Invalid zip provided');
}

private parseBundleZip(compiler: AppCompiler, zip: AdmZip, bundleManifest: AdmZip.IZipEntry): IParseBundleZipResult {
let manifest: IBundleManifest;

if (!bundleManifest.isDirectory) {
try {
manifest = JSON.parse(bundleManifest.getData().toString()) as IBundleManifest;
} catch (error) {
throw new Error('Invalid bundle manifest file');
}
} else {
throw new Error('Invalid bundle manifest file');
}

const apps = manifest.apps.map(({ appId, license, filename }) => {
const newEntry = { appId, license };
const entry = zip.getEntry(filename);

if (!entry || entry.isDirectory) {
return {
...newEntry,
error: `Couldn't find entry from the manifest: ${filename}`,
};
}

let parseResult: IParseZipResult;

try {
parseResult = this.parseZip(compiler, entry.getData());
} catch (error) {
return {
...newEntry,
error: error.message,
};
}

if (parseResult.contentType !== ZipContentType.APP) {
return {
...newEntry,
error: `Entry provided in manifest is not an App: ${filename}`,
};
}

return {
...newEntry,
parseResult: parseResult.parsed,
} as IBundleZipAppEntry;
});

return {
version: manifest.version,
workspaceId: manifest.workspaceId,
apps,
};
}

private parseAppZip(compiler: AppCompiler, zip: AdmZip, infoZip: AdmZip.IZipEntry): IParseAppZipResult {
let info: IAppInfo;

if (!infoZip.isDirectory) {
if (infoZip && !infoZip.isDirectory) {
try {
info = JSON.parse(infoZip.getData().toString()) as IAppInfo;

Expand Down Expand Up @@ -151,19 +72,8 @@ export class AppPackageParser {
const languageContent = this.getLanguageContent(zip);

// Compile all the typescript files to javascript
let result = {} as ICompilerResult;

try {
result = compiler.toJs(info, tsFiles);
tsFiles = result.files;
} catch (e) {
result.compilerErrors = [{
message: e.message,
file: '<unknown>',
line: -1,
character: -1,
}];
}
const result = compiler.toJs(info, tsFiles);
tsFiles = result.files;

const compiledFiles: { [s: string]: string } = {};
Object.keys(tsFiles).forEach((name) => {
Expand All @@ -183,7 +93,6 @@ export class AppPackageParser {
languageContent,
implemented: result.implemented,
compilerErrors: result.compilerErrors,
zipContentsBase64d: zip.toBuffer().toString('base64'),
};
}

Expand Down
9 changes: 0 additions & 9 deletions src/server/compiler/IBundleManifest.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/server/compiler/IParseAppZipResult.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/server/compiler/IParseBundleZipResult.ts

This file was deleted.

17 changes: 8 additions & 9 deletions src/server/compiler/IParseZipResult.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { IParseAppZipResult } from './IParseAppZipResult';
import { IParseBundleZipResult } from './IParseBundleZipResult';

export enum ZipContentType {
APP = 'app',
BUNDLE = 'bundle',
}
import { IAppInfo } from '../../definition/metadata';
import { AppImplements } from './AppImplements';
import { ICompilerError } from './ICompilerError';

export interface IParseZipResult {
contentType: ZipContentType;
parsed: IParseAppZipResult | IParseBundleZipResult;
info: IAppInfo;
compiledFiles: { [key: string]: string };
languageContent: { [key: string]: object };
implemented: AppImplements;
compilerErrors: Array<ICompilerError>;
}
4 changes: 0 additions & 4 deletions src/server/compiler/InstallZipType.ts

This file was deleted.

8 changes: 1 addition & 7 deletions src/server/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { AppPackageParser } from './AppPackageParser';
import { ICompilerError } from './ICompilerError';
import { ICompilerFile } from './ICompilerFile';
import { ICompilerResult } from './ICompilerResult';
import { IParseAppZipResult } from './IParseAppZipResult';
import { IBundleZipAppEntry, IParseBundleZipResult } from './IParseBundleZipResult';
import { IParseZipResult, ZipContentType } from './IParseZipResult';
import { IParseZipResult } from './IParseZipResult';

export {
AppCompiler,
Expand All @@ -18,9 +16,5 @@ export {
ICompilerFile,
ICompilerError,
ICompilerResult,
IParseAppZipResult,
IParseBundleZipResult,
IParseZipResult,
IBundleZipAppEntry,
ZipContentType,
};
3 changes: 1 addition & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"max-line-length": [true, {
"limit": 160,
"ignore-pattern": "^import | *export .*? {"
}],
"member-ordering": false
}]
},
"jsRules": {
"quotemark": [true, "single"]
Expand Down

0 comments on commit 59541c4

Please sign in to comment.