1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | require("source-map-support/register");
|
6 | const path_1 = __importDefault(require("path"));
|
7 | const lodash_assignin_1 = __importDefault(require("lodash.assignin"));
|
8 | const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
9 | const module_1 = __importDefault(require("module"));
|
10 | const find_up_1 = __importDefault(require("find-up"));
|
11 | const configstore_1 = __importDefault(require("configstore"));
|
12 | const error_1 = __importDefault(require("@truffle/error"));
|
13 | const original_require_1 = __importDefault(require("original-require"));
|
14 | const configDefaults_1 = require("./configDefaults");
|
15 | const events_1 = require("@truffle/events");
|
16 | const DEFAULT_CONFIG_FILENAME = "truffle-config.js";
|
17 | const BACKUP_CONFIG_FILENAME = "truffle.js";
|
18 | class TruffleConfig {
|
19 | constructor(truffleDirectory, workingDirectory, network) {
|
20 | this._deepCopy = ["compilers"];
|
21 | this._values = configDefaults_1.getInitialConfig({
|
22 | truffleDirectory,
|
23 | workingDirectory,
|
24 | network
|
25 | });
|
26 | const eventsOptions = this.eventManagerOptions(this);
|
27 | this.events = new events_1.EventManager(eventsOptions);
|
28 | const props = configDefaults_1.configProps({ configObject: this });
|
29 | Object.entries(props).forEach(([propName, descriptor]) => this.addProp(propName, descriptor));
|
30 | }
|
31 | eventManagerOptions(config) {
|
32 | let muteLogging;
|
33 | const { quiet, logger, subscribers } = config;
|
34 | if (quiet)
|
35 | muteLogging = true;
|
36 | return { logger, muteLogging, subscribers };
|
37 | }
|
38 | addProp(propertyName, descriptor) {
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const self = this;
|
46 | Object.defineProperty(this, propertyName, {
|
47 | get: descriptor.get ||
|
48 | function () {
|
49 |
|
50 | if (propertyName in self._values) {
|
51 | return self._values[propertyName];
|
52 | }
|
53 |
|
54 | if (descriptor.default) {
|
55 | return descriptor.default();
|
56 | }
|
57 |
|
58 | return descriptor();
|
59 | },
|
60 | set: descriptor.set ||
|
61 | function (value) {
|
62 | self._values[propertyName] = descriptor.transform
|
63 | ? descriptor.transform(value)
|
64 | : value;
|
65 | },
|
66 | configurable: true,
|
67 | enumerable: true
|
68 | });
|
69 | }
|
70 | normalize(obj) {
|
71 | const clone = {};
|
72 | Object.keys(obj).forEach(key => {
|
73 | try {
|
74 | clone[key] = obj[key];
|
75 | }
|
76 | catch (e) {
|
77 |
|
78 | }
|
79 | });
|
80 | return clone;
|
81 | }
|
82 | with(obj) {
|
83 | const current = this.normalize(this);
|
84 | const normalized = this.normalize(obj);
|
85 | let eventsOptions = this.eventManagerOptions(this);
|
86 | this.events.updateSubscriberOptions(eventsOptions);
|
87 | return lodash_assignin_1.default(Object.create(TruffleConfig.prototype), current, normalized);
|
88 | }
|
89 | merge(obj) {
|
90 | const clone = this.normalize(obj);
|
91 |
|
92 | const propertyNames = Object.keys(obj);
|
93 | propertyNames.forEach(key => {
|
94 | try {
|
95 | if (typeof clone[key] === "object" && this._deepCopy.includes(key)) {
|
96 | this[key] = lodash_merge_1.default(this[key], clone[key]);
|
97 | }
|
98 | else {
|
99 | this[key] = clone[key];
|
100 | }
|
101 | }
|
102 | catch (e) {
|
103 |
|
104 | }
|
105 | });
|
106 | const eventsOptions = this.eventManagerOptions(this);
|
107 | this.events.updateSubscriberOptions(eventsOptions);
|
108 | return this;
|
109 | }
|
110 | static default() {
|
111 | return new TruffleConfig();
|
112 | }
|
113 | static search(options = {}, filename) {
|
114 | const searchOptions = {
|
115 | cwd: options.working_directory || options.workingDirectory
|
116 | };
|
117 | if (!filename) {
|
118 | const isWin = process.platform === "win32";
|
119 | const defaultConfig = find_up_1.default.sync(DEFAULT_CONFIG_FILENAME, searchOptions);
|
120 | const backupConfig = find_up_1.default.sync(BACKUP_CONFIG_FILENAME, searchOptions);
|
121 | if (defaultConfig && backupConfig) {
|
122 | console.warn(`Warning: Both ${DEFAULT_CONFIG_FILENAME} and ${BACKUP_CONFIG_FILENAME} were found. Using ${DEFAULT_CONFIG_FILENAME}.`);
|
123 | return defaultConfig;
|
124 | }
|
125 | else if (backupConfig && !defaultConfig) {
|
126 | if (isWin)
|
127 | console.warn(`Warning: Please rename ${BACKUP_CONFIG_FILENAME} to ${DEFAULT_CONFIG_FILENAME} to ensure Windows compatibility.`);
|
128 | return backupConfig;
|
129 | }
|
130 | else {
|
131 | return defaultConfig;
|
132 | }
|
133 | }
|
134 | return find_up_1.default.sync(filename, searchOptions);
|
135 | }
|
136 | static detect(options = {}, filename) {
|
137 | let configFile;
|
138 | const configPath = options.config;
|
139 | if (configPath) {
|
140 | configFile = path_1.default.isAbsolute(configPath)
|
141 | ? configPath
|
142 | : path_1.default.resolve(configPath);
|
143 | }
|
144 | else {
|
145 | configFile = TruffleConfig.search(options, filename);
|
146 | }
|
147 | if (!configFile) {
|
148 | throw new error_1.default("Could not find suitable configuration file.");
|
149 | }
|
150 | return TruffleConfig.load(configFile, options);
|
151 | }
|
152 | static load(file, options = {}) {
|
153 | const workingDirectory = options.config
|
154 | ? process.cwd()
|
155 | : path_1.default.dirname(path_1.default.resolve(file));
|
156 | const config = new TruffleConfig(undefined, workingDirectory, undefined);
|
157 |
|
158 |
|
159 |
|
160 | delete require.cache[module_1.default._resolveFilename(file, module)];
|
161 | const staticConfig = original_require_1.default(file);
|
162 | config.merge(staticConfig);
|
163 | config.merge(options);
|
164 |
|
165 | const eventsOptions = config.eventManagerOptions(config);
|
166 | config.events.updateSubscriberOptions(eventsOptions);
|
167 | config.events.initializeUserSubscribers(eventsOptions);
|
168 | return config;
|
169 | }
|
170 | static getUserConfig() {
|
171 | return new configstore_1.default("truffle", {}, { globalConfigPath: true });
|
172 | }
|
173 | static getTruffleDataDirectory() {
|
174 | const configStore = TruffleConfig.getUserConfig();
|
175 | return path_1.default.dirname(configStore.path);
|
176 | }
|
177 | }
|
178 | module.exports = TruffleConfig;
|
179 |
|
\ | No newline at end of file |