@sap-ux/project-access
Version:
Library to access SAP Fiori tools projects
71 lines • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWebappPath = getWebappPath;
exports.readUi5Yaml = readUi5Yaml;
exports.getAllUi5YamlFileNames = getAllUi5YamlFileNames;
const path_1 = require("path");
const ui5_config_1 = require("@sap-ux/ui5-config");
const constants_1 = require("../constants");
const file_1 = require("../file");
/**
* Get path to webapp.
*
* @param appRoot - root to the application
* @param [memFs] - optional mem-fs editor instance
* @returns - path to webapp folder
*/
async function getWebappPath(appRoot, memFs) {
const ui5YamlPath = (0, path_1.join)(appRoot, constants_1.FileName.Ui5Yaml);
let webappPath = (0, path_1.join)(appRoot, constants_1.DirName.Webapp);
if (await (0, file_1.fileExists)(ui5YamlPath, memFs)) {
const yamlString = await (0, file_1.readFile)(ui5YamlPath, memFs);
const ui5Config = await ui5_config_1.UI5Config.newInstance(yamlString);
const relativeWebappPath = ui5Config.getConfiguration()?.paths?.webapp;
if (relativeWebappPath) {
// Search for folder with package.json inside
const packageJsonPath = await (0, file_1.findFileUp)(constants_1.FileName.Package, appRoot, memFs);
if (packageJsonPath) {
const packageJsonDirPath = (0, path_1.dirname)(packageJsonPath);
webappPath = (0, path_1.join)(packageJsonDirPath, relativeWebappPath);
}
}
}
return webappPath;
}
/**
* Checks if UI5 config yaml file exists and returns its content.
*
* @param projectRoot - path to project root
* @param fileName - name of yaml file to be read
* @param [memFs] - optional mem-fs editor instance
* @param options - options
* @param [options.validateSchema] - optional flag to validate the schema of the yaml file
* @returns {UI5Config} UI5 config file in yaml format
* @throws {Error} if file is not found
*/
async function readUi5Yaml(projectRoot, fileName, memFs, options) {
const ui5YamlPath = (0, path_1.join)(projectRoot, fileName);
if (await (0, file_1.fileExists)(ui5YamlPath, memFs)) {
const yamlString = await (0, file_1.readFile)(ui5YamlPath, memFs);
return await ui5_config_1.UI5Config.newInstance(yamlString, { validateSchema: options?.validateSchema });
}
throw Error(`File '${fileName}' not found in project '${projectRoot}'`);
}
/**
* Scans the project directory for ui5 configuration yaml files.
*
* @param projectRoot - path to project root, where ui5 configuration y*ml files are located
* @param [memFs] - optional mem-fs editor instance
* @returns list of valid and invalid UI5 configuration yaml file names
* @throws {Error} if an error occurs while reading files from projectRoot
*/
async function getAllUi5YamlFileNames(projectRoot, memFs) {
try {
const yamlFilePaths = await (0, file_1.findFilesByExtension)('.yaml', projectRoot, [], memFs, true);
return yamlFilePaths.map((path) => (0, path_1.basename)(path));
}
catch (error) {
throw new Error(`There was an error reading files from the directory '${projectRoot}': ${error}`);
}
}
//# sourceMappingURL=ui5-config.js.map