Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Service } from '../../container';
import { ExternalImporterConfig } from './external-importer-config';
import { from, Observable, of } from 'rxjs';
import { RequestService } from '../request';
import { FileService } from '../file';
import { map, switchMap, take } from 'rxjs/operators';
import { BootstrapLogger } from '../bootstrap-logger/bootstrap-logger';
import { Injector } from '../../decorators/injector/injector.decorator';
import SystemJS = require('systemjs');
import { CompressionService } from '../compression/compression.service';
import { ConfigService } from '../config';
@Service()
export class ExternalImporter {
@Injector(RequestService) requestService: RequestService;
@Injector(FileService) fileService: FileService;
@Injector(BootstrapLogger) logger: BootstrapLogger;
@Injector(CompressionService) compressionService: CompressionService;
@Injector(ConfigService) private configService: ConfigService;
importExternalModule(module: string) {
return from(SystemJS.import(module));
}
validateConfig(config: ExternalImporterConfig) {
Iif (!config) {
throw new Error('Bootstrap: missing config');
}
}
encryptFile(fileFullPath: string) {
if (this.configService.config.experimental.crypto) {
return this.compressionService.readGzipFile(fileFullPath, 'dada');
} else {
return of(null);
}
}
decryptFile(fileFullPath: string) {
if (this.configService.config.experimental.crypto) {
return this.compressionService.gZipFile(fileFullPath, 'dada');
} else {
return of(null);
}
}
isWeb() {
let value = false;
try {
if (window) {
value = true;
}
} catch (e) { }
return value;
}
importModule(config: ExternalImporterConfig, token: string): Promise<any> {
this.validateConfig(config);
Iif (this.isWeb()) {
SystemJS.config(Object.assign({
map: {
[token]: config.link
}
}, config.SystemJsConfig));
return SystemJS.import(config.link);
}
return Observable.create(async observer => {
const moduleName = config.fileName;
const moduleNamespace = config.namespace;
const moduleLink = config.link;
const moduleExtension = config.extension;
const moduleSystemJsConfig = config.SystemJsConfig || {};
const modulesFolder = config.outputFolder || `/external_modules/`;
const fileFullPath = `${process.cwd()}${modulesFolder}/${moduleNamespace}/${moduleName}.${moduleExtension}`;
const folder = `${process.cwd()}${modulesFolder}${moduleNamespace}`;
const fileName = `${moduleName}.${moduleExtension}`;
Object.assign(moduleSystemJsConfig, { paths: { [moduleName]: fileFullPath, ...moduleSystemJsConfig.paths } });
SystemJS.config(moduleSystemJsConfig);
Eif (this.fileService.isPresent(fileFullPath)) {
this.logger.logImporter(`Bootstrap -> @Service('${moduleName}'): present inside .${modulesFolder}${moduleNamespace}/${moduleName}.${moduleExtension} folder and loaded from there`);
this.importExternalModule(moduleName)
.pipe(take(1))
.subscribe(
m => observer.next(m),
err => observer.error(err)
);
} else {
this.logger.logImporter(`Bootstrap -> @Service('${moduleName}'): will be downloaded inside .${modulesFolder}${moduleNamespace}/${moduleName}.${moduleExtension} folder and loaded from there`);
this.logger.logImporter(`Bootstrap -> @Service('${moduleName}'): ${moduleLink} downloading...`);
this.requestService.get(moduleLink)
.pipe(
take(1),
map((res) => {
this.logger.logImporter(`Done!`);
return res;
}),
switchMap((res) => this.fileService.writeFileSync(folder, fileName, moduleNamespace, res)),
switchMap(() => this.importExternalModule(moduleName))
)
.subscribe(
(m) => observer.next(m),
err => observer.error(err)
);
}
});
}
} |