1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | const path = require('path');
|
16 | const AbstractCommand = require('./abstract.cmd.js');
|
17 | const Builder = require('./builder/Builder.js');
|
18 | const HelixPages = require('./helix-pages.js');
|
19 |
|
20 | const HLX_PIPELINE_MOD = '@adobe/helix-pipeline';
|
21 |
|
22 | class BuildCommand extends AbstractCommand {
|
23 | constructor(logger) {
|
24 | super(logger);
|
25 | this._target = null;
|
26 | this._files = null;
|
27 | this._sourceRoot = './';
|
28 | this._helixPagesRepo = '';
|
29 | this._helixPages = null;
|
30 | this._modulePaths = [];
|
31 | this._requiredModules = [{ name: HLX_PIPELINE_MOD, descriptor: `${HLX_PIPELINE_MOD}@latest` }];
|
32 | }
|
33 |
|
34 |
|
35 | get requireConfigFile() {
|
36 | return false;
|
37 | }
|
38 |
|
39 | withTargetDir(target) {
|
40 | this._target = target;
|
41 | return this;
|
42 | }
|
43 |
|
44 | withFiles(files) {
|
45 | this._files = files;
|
46 | return this;
|
47 | }
|
48 |
|
49 | withSourceRoot(value) {
|
50 | this._sourceRoot = value;
|
51 | return this;
|
52 | }
|
53 |
|
54 | get helixPages() {
|
55 | return this._helixPages;
|
56 | }
|
57 |
|
58 | withModulePaths(value) {
|
59 | this._modulePaths = value;
|
60 | return this;
|
61 | }
|
62 |
|
63 | get modulePaths() {
|
64 | return this._modulePaths;
|
65 | }
|
66 |
|
67 | withRequiredModules(mods) {
|
68 | if (mods) {
|
69 | this._requiredModules = mods;
|
70 | }
|
71 | return this;
|
72 | }
|
73 |
|
74 | withCustomPipeline(customPipeline) {
|
75 | const mod = this._requiredModules.find((m) => m.name === HLX_PIPELINE_MOD);
|
76 | if (mod) {
|
77 | mod.descriptor = customPipeline;
|
78 | } else {
|
79 | this._requiredModules.push({ name: HLX_PIPELINE_MOD, descriptor: customPipeline });
|
80 | }
|
81 | return this;
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 | async init() {
|
88 | await super.init();
|
89 |
|
90 | this._helixPages = new HelixPages(this._logger).withDirectory(this.directory);
|
91 |
|
92 | if (this._helixPagesRepo) {
|
93 | this._helixPages.withRepo(this._helixPagesRepo);
|
94 | }
|
95 | await this._helixPages.init();
|
96 |
|
97 |
|
98 | this._target = path.resolve(this.directory, this._target);
|
99 | this._sourceRoot = path.resolve(this.directory, this._sourceRoot);
|
100 | }
|
101 |
|
102 | async build() {
|
103 | this.emit('buildStart');
|
104 |
|
105 | const builder = new Builder()
|
106 | .withDirectory(this.directory)
|
107 | .withSourceRoot(this._sourceRoot)
|
108 | .withBuildDir(this._target)
|
109 | .withLogger(this.log)
|
110 | .withFiles(this._files)
|
111 | .withRequiredModules(this._requiredModules)
|
112 | .withShowReport(true);
|
113 |
|
114 | if (await this.helixPages.isPagesProject()) {
|
115 | await this.helixPages.prepare();
|
116 |
|
117 |
|
118 | builder
|
119 | .withFiles(['src/**/*.htl', 'src/**/*.js'])
|
120 | .withSourceRoot(this.helixPages.checkoutDirectory)
|
121 | .withModulePaths([
|
122 | path.resolve(this.helixPages.checkoutDirectory, 'node_modules'),
|
123 | path.resolve(this._target, 'node_modules'),
|
124 | ]);
|
125 | }
|
126 |
|
127 |
|
128 | if (this._modulePaths.length > 0) {
|
129 | builder.withModulePaths(this._modulePaths);
|
130 | } else {
|
131 | this._modulePaths = builder.modulePaths;
|
132 | }
|
133 |
|
134 | try {
|
135 | await builder.run();
|
136 | } catch (e) {
|
137 | this.log.error(e.message);
|
138 | }
|
139 | this.emit('buildEnd');
|
140 | }
|
141 |
|
142 | async run() {
|
143 | await this.init();
|
144 | await this.build();
|
145 | }
|
146 | }
|
147 |
|
148 | module.exports = BuildCommand;
|