UNPKG

4.26 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 try {
19 bearer_tst_js_1.default([
20 '--no-watcher',
21 '--prefix-tag',
22 'bearer',
23 '--suffix-tag',
24 this.bearerConfig.bearerUid,
25 '--no-process',
26 '--build',
27 true,
28 '--fail-fast',
29 true,
30 '--buid',
31 this.bearerConfig.bearerUid
32 ]);
33 }
34 catch (e) {
35 this.error(e);
36 }
37 };
38 this.watchNonTsFiles = async (watchedPath, destPath) => {
39 return new Promise((resolve, _reject) => {
40 const callback = (error) => {
41 if (error) {
42 this.error(error);
43 }
44 };
45 const watcher = chokidar.watch(`${watchedPath}/**`, {
46 ignored: /\.tsx?$/,
47 persistent: true,
48 followSymlinks: false
49 });
50 watcher.on('ready', () => {
51 resolve(watcher);
52 });
53 watcher.on('all', (event, filePath) => {
54 const relativePath = filePath.replace(watchedPath, '');
55 const targetPath = path.join(destPath, relativePath);
56 // Creating symlink
57 if (event === 'add') {
58 this.debug('creating symlink', filePath, targetPath);
59 fs.ensureSymlink(filePath, targetPath, callback);
60 }
61 // // Deleting symlink
62 if (event === 'unlink') {
63 this.debug('deleting symlink');
64 fs.unlink(targetPath, err => {
65 if (err)
66 throw err;
67 this.debug(`${targetPath} was deleted`);
68 });
69 }
70 });
71 });
72 };
73 }
74 async run() {
75 const { flags } = this.parse(BuildViews);
76 const tasks = [
77 {
78 title: 'Transpile views',
79 task: async (_ctx, _task) => this.transpile()
80 },
81 {
82 title: 'Link non TS files',
83 task: async (_ctx, _task) => {
84 const watcher = await this.watchNonTsFiles(this.locator.srcViewsDir, this.locator.buildViewsComponentsDir);
85 // TODO: update here when watch mode required
86 // if(flags.noWatch) { // or similar
87 watcher.close();
88 // }
89 }
90 }
91 ];
92 if (!flags[skipInstall]) {
93 tasks.unshift(install_dependencies_1.default({ cwd: this.locator.integrationRoot }));
94 }
95 try {
96 await new Listr(tasks).run();
97 this.success('Built views');
98 }
99 catch (e) {
100 this.error(e);
101 }
102 }
103}
104BuildViews.description = 'Build integration views';
105BuildViews.hidden = true;
106BuildViews.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
107BuildViews.args = [];
108tslib_1.__decorate([
109 decorators_1.skipIfNoViews(),
110 decorators_1.RequireIntegrationFolder(),
111 decorators_1.RequireLinkedIntegration(false)
112], BuildViews.prototype, "run", null);
113exports.default = BuildViews;