UNPKG

3.35 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 EventEmitter = require('events');
16const chalk = require('chalk');
17const { HelixConfig, IndexConfig } = require('@adobe/helix-shared');
18const { getOrCreateLogger } = require('./log-common');
19const ConfigUtils = require('./config/config-utils.js');
20
21class AbstractCommand extends EventEmitter {
22 constructor(logger) {
23 super();
24 this._initialized = false;
25 this._logger = logger || getOrCreateLogger();
26 this._helixConfig = new HelixConfig().withLogger(this._logger);
27 this._indexConfig = new IndexConfig().withLogger(this._logger);
28 }
29
30 withDirectory(dir) {
31 this._helixConfig.withDirectory(dir);
32 this._indexConfig.withDirectory(dir);
33 return this;
34 }
35
36 get log() {
37 return this._logger;
38 }
39
40 get directory() {
41 return this._helixConfig.directory;
42 }
43
44 // eslint-disable-next-line class-methods-use-this
45 get requireConfigFile() {
46 return true;
47 }
48
49 withConfigFile(file) {
50 this._helixConfig.withConfigPath(file);
51 return this;
52 }
53
54 withIndexConfigFile(file) {
55 this._indexConfig.withConfigPath(file);
56 return this;
57 }
58
59 get config() {
60 if (!this._initialized) {
61 throw Error('illegal access to #config before initialized');
62 }
63 return this._helixConfig;
64 }
65
66 get indexConfig() {
67 return this._indexConfig;
68 }
69
70 async init() {
71 if (!this._initialized) {
72 if (!await this._helixConfig.hasFile()) {
73 if (this.requireConfigFile) {
74 this.log.error(chalk`No {cyan helix-config.yaml}. Please add one before deployment.`);
75 this.log.info(chalk`You can auto generate a default config with\n{grey $ hlx deploy --add=default}\n`);
76 throw Error();
77 } else {
78 // set default config
79 this._helixConfig.withSource(await ConfigUtils.createDefaultConfig(this.directory));
80 }
81 }
82 await this._indexConfig.init();
83 await this._helixConfig.init();
84 this._initialized = true;
85 }
86 return this;
87 }
88
89 async reloadConfig() {
90 if (!this._initialized) {
91 return this.init();
92 }
93 this._helixConfig = new HelixConfig()
94 .withLogger(this._helixConfig.log)
95 .withConfigPath(this._helixConfig.configPath)
96 .withDirectory(this._helixConfig.directory);
97 if (!await this._helixConfig.hasFile()) {
98 // set default config
99 this._helixConfig.withSource(await ConfigUtils.createDefaultConfig(this.directory));
100 }
101 await this._helixConfig.init();
102 this._indexConfig = await (new IndexConfig()
103 .withLogger(this._indexConfig.log)
104 .withConfigPath(this._indexConfig.configPath)
105 .withDirectory(this._indexConfig.directory)
106 .init());
107 return this;
108 }
109}
110
111module.exports = AbstractCommand;