UNPKG

3.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const bearer_tst_js_1 = require("@bearer/transpiler/lib/bin/bearer-tst.js");
5const command_1 = require("@oclif/command");
6const chokidar = require("chokidar");
7const fs = require("fs-extra");
8const Listr = require("listr");
9const path = require("path");
10const base_command_1 = require("../../base-command");
11const install_dependencies_1 = require("../../tasks/install-dependencies");
12const decorators_1 = require("../../utils/decorators");
13const skipInstall = 'skip-install';
14class BuildViews extends base_command_1.default {
15 constructor() {
16 super(...arguments);
17 this.transpile = () => {
18 const prefix = ['bearer', this.bearerConfig.orgId].join('-');
19 const suffix = this.bearerConfig.integrationId;
20 try {
21 bearer_tst_js_1.default(['--no-watcher', '--prefix-tag', prefix, '--suffix-tag', suffix, '--no-process', '--build', true]);
22 }
23 catch (e) {
24 this.error(e);
25 }
26 };
27 this.watchNonTsFiles = async (watchedPath, destPath) => {
28 return new Promise((resolve, _reject) => {
29 const callback = (error) => {
30 if (error) {
31 this.error(error);
32 }
33 };
34 const watcher = chokidar.watch(`${watchedPath}/**`, {
35 ignored: /\.tsx?$/,
36 persistent: true,
37 followSymlinks: false
38 });
39 watcher.on('ready', () => {
40 resolve(watcher);
41 });
42 watcher.on('all', (event, filePath) => {
43 const relativePath = filePath.replace(watchedPath, '');
44 const targetPath = path.join(destPath, relativePath);
45 // Creating symlink
46 if (event === 'add') {
47 this.debug('creating symlink', filePath, targetPath);
48 fs.ensureSymlink(filePath, targetPath, callback);
49 }
50 // // Deleting symlink
51 if (event === 'unlink') {
52 this.debug('deleting symlink');
53 fs.unlink(targetPath, err => {
54 if (err)
55 throw err;
56 this.debug(`${targetPath} was deleted`);
57 });
58 }
59 });
60 });
61 };
62 }
63 async run() {
64 const { flags } = this.parse(BuildViews);
65 const tasks = [
66 {
67 title: 'Transpile views',
68 task: async (_ctx, _task) => this.transpile()
69 },
70 {
71 title: 'Link non TS files',
72 task: async (_ctx, _task) => {
73 const watcher = await this.watchNonTsFiles(this.locator.srcViewsDir, this.locator.buildViewsComponentsDir);
74 // TODO: update here when watch mode required
75 // if(flags.noWatch) { // or similar
76 watcher.close();
77 // }
78 }
79 }
80 ];
81 if (!flags[skipInstall]) {
82 tasks.unshift(install_dependencies_1.default({ cwd: this.locator.integrationRoot }));
83 }
84 try {
85 await new Listr(tasks).run();
86 this.success('Built views');
87 }
88 catch (e) {
89 this.error(e);
90 }
91 }
92}
93BuildViews.description = 'Build integration views';
94BuildViews.hidden = true;
95BuildViews.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
96BuildViews.args = [];
97tslib_1.__decorate([
98 decorators_1.RequireIntegrationFolder()
99], BuildViews.prototype, "run", null);
100exports.default = BuildViews;