@transact-open-ux/cli
Version:
Command line for rapid development/deployment on the Transact Platform
62 lines (53 loc) • 1.81 kB
JavaScript
/**
* @license
* Copyright (c) 2019 Avoka Technologies Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { red } = require("colorette");
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
const REQUIRED_DIRS = ["appBundle", "formDefFile", "domainModelFile"];
const isFile = filePath =>
fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
const findConfig = (root, configName) => {
let options;
let directory = root;
do {
const configFilePath = path.resolve(path.join(directory, configName));
if (isFile(configFilePath)) {
try {
if (configFilePath.endsWith(".transact-auth")) {
return dotenv.parse(fs.readFileSync(configFilePath));
}
options = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
} catch (error) {
console.log(red(`Failed to parse config file ${configFilePath}`));
return;
}
break;
}
} while (directory !== (directory = path.dirname(directory)));
if (options) {
REQUIRED_DIRS.forEach(dir => {
if (options.hasOwnProperty(dir)) {
// validation done elsewhere
options[dir] = path.resolve(directory, options[dir]);
}
});
}
return options;
};
module.exports = findConfig;