@liferay/generator-js
Version:
Yeoman generators for Liferay DXP and Portal CE JavaScript projects.
97 lines (96 loc) • 3.2 kB
JavaScript
;
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSDKVersion = exports.getDefaultAnswer = exports.batchMode = exports.set = void 0;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const read_json_sync_1 = __importDefault(require("read-json-sync"));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let cfg = {};
Object.assign(cfg, safeReadJsonSync(path_1.default.join(os_1.default.homedir(), '.generator-liferay-js.json')));
process.argv.forEach((arg, index) => {
if (arg === '--config') {
cfg = read_json_sync_1.default(process.argv[index + 1]);
}
});
/**
* Set configuration to a given value (to be used from tests)
* @param {object} config the forced config
*/
function set(config) {
cfg = config;
}
exports.set = set;
/**
* Whether to run in batch mode (with no interaction with the user)
* @return {boolean} true if all prompts must be answered with default values
*/
function batchMode() {
if (cfg.batchMode === undefined) {
return false;
}
return cfg.batchMode;
}
exports.batchMode = batchMode;
/**
* Get the default answer value for a given prompt.
* @param {string} namespace unique string identifying the calling context
* @param {string} question name of property identifying the question
* @param {*} defaultDefault default value is nothing is configured
* @return {*} the default value for the answer
*/
function getDefaultAnswer(namespace, question, defaultDefault = undefined) {
// Return defaultDefault if no answers section
if (cfg.answers === undefined) {
return defaultDefault;
}
let value;
// Try to get value from specific namespace section
if (cfg.answers[namespace] !== undefined) {
value = cfg.answers[namespace][question];
}
// If not found in specific namespace section, try to get value from *
if (value === undefined) {
if (cfg.answers['*'] !== undefined) {
value = cfg.answers['*'][question];
}
}
// If not found in any section return defaultDefault
if (value === undefined) {
return defaultDefault;
}
// If found, return the configured value
return value;
}
exports.getDefaultAnswer = getDefaultAnswer;
/**
* Returns the SDK version to use when generating projects. If a path is used it
* it must point to the root folder of the SDK's lerna repo.
* @return {string} the forced SDK version to use
*/
function getSDKVersion() {
return cfg.sdkVersion;
}
exports.getSDKVersion = getSDKVersion;
/**
* Reads a JSON file returning an empty object if the file does not exist.
* @param {string} path
* @return {object}
*/
function safeReadJsonSync(path) {
try {
return read_json_sync_1.default(path);
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
return {};
}
}