@transact-open-ux/cli
Version:
Command line for rapid development/deployment on the Transact Platform
101 lines (87 loc) • 3.12 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 fs = require("fs");
const path = require("path");
const REQUIRED_DIRS = ["buildDir", "appDef"];
const REQUIRES_ONE_SEED_DIRS = ["formXmlFile", "domainModelFile"];
const REQUIRED_AUTH_PARAMS = ["tmHost", "username", "password"];
const isDirectory = filePath =>
fs.existsSync(filePath) && fs.lstatSync(filePath).isDirectory();
const validateConfig = (buildConfig, authConfig) => {
const missingConfigFiles = [];
if (!buildConfig) missingConfigFiles.push("transact-config.json");
if (!authConfig) missingConfigFiles.push(".transact-auth");
if (missingConfigFiles.length) {
throw Error(
"The following transact config files are missing in the deploy directory:\n" +
`${missingConfigFiles.join(", ")}`
);
}
const missingDirs = REQUIRED_DIRS.filter(
dir => !buildConfig.hasOwnProperty(dir)
);
const missingSeedDirs = REQUIRES_ONE_SEED_DIRS.filter(
dir => !buildConfig.hasOwnProperty(dir)
);
const missingRootParams = REQUIRED_AUTH_PARAMS.filter(
dir => typeof authConfig[dir] === "undefined"
);
if (missingRootParams.length) {
throw Error(
"The following properties are missing in the .transact-auth file:\n" +
`${missingRootParams.join(", ")}`
);
}
if (missingSeedDirs.length === REQUIRES_ONE_SEED_DIRS.length) {
throw Error(
"One of the following properties are required in the transact-config.json file:\n" +
`${missingSeedDirs.join(", ")}`
);
}
if (missingDirs.length) {
throw Error(
"The following properties are missing in the transact-config.json\n" +
`${missingDirs.join(", ")}`
);
}
if (!isDirectory(path.resolve(process.cwd(), buildConfig.buildDir))) {
throw Error(
`Build directory '${
buildConfig.buildDir
}' not found!\nPlease ensure you have ran your production build script`
);
}
if (buildConfig.appDef && typeof buildConfig.appDef !== "object") {
throw Error(
"transact-config.json\nappDef must be an app definition object"
);
}
if (
buildConfig.domainModelFile &&
["json", "js"].indexOf(buildConfig.domainModelFile.split(".").pop()) === -1
) {
throw Error(
"transact-config.json\ndomainModelFile must be a file of type json or js"
);
}
if (buildConfig.formXmlFile && !buildConfig.formXmlFile.endsWith("xml")) {
throw Error("transact-config.json\nformXmlFile must be a file of type xml");
}
return true;
};
module.exports = validateConfig;