UNPKG

3.85 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13'use strict';
14
15const path = require('path');
16const AbstractCommand = require('./abstract.cmd.js');
17const Builder = require('./builder/Builder.js');
18const HelixPages = require('./helix-pages.js');
19
20const HLX_PIPELINE_MOD = '@adobe/helix-pipeline';
21
22class 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 // eslint-disable-next-line class-methods-use-this
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 * @override
86 */
87 async init() {
88 await super.init();
89
90 this._helixPages = new HelixPages(this._logger).withDirectory(this.directory);
91 // currently only used for testing
92 if (this._helixPagesRepo) {
93 this._helixPages.withRepo(this._helixPagesRepo);
94 }
95 await this._helixPages.init();
96
97 // ensure target is absolute
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 // use bundled helix-pages sources and modules
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 // allow setting modules paths from tests
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
148module.exports = BuildCommand;