1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | const EventEmitter = require('events');
|
16 | const chalk = require('chalk');
|
17 | const { HelixConfig, IndexConfig } = require('@adobe/helix-shared');
|
18 | const { getOrCreateLogger } = require('./log-common');
|
19 | const ConfigUtils = require('./config/config-utils.js');
|
20 |
|
21 | class 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 |
|
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 |
|
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 |
|
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 |
|
111 | module.exports = AbstractCommand;
|