@communities-webruntime/services
Version:
If you would like to run Lightning Web Runtime without the CLI, we expose some of our programmatic APIs available in Node.js. If you're looking for the CLI documentation [you can find that here](https://www.npmjs.com/package/@communities-webruntime/cli).
94 lines • 2.97 kB
JavaScript
;
/** @hidden */
/**
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
const fs = require('./filesystem');
const { assert } = require('./assert');
/**
* Returns true if path is actually a dir.
* @param {string} name display name of the file to check
* @param {string} file filepath of file to check
*/
function isDirectory(name, dir) {
assert(fs.lstatSync(dir).isDirectory(), `${name} is not a directory: ${dir}`);
}
/**
* Returns true if path is actually a file
* @param {string} name display name of the file to check
* @param {string} file filepath of file to check
*/
function isFile(name, file) {
assert(fs.lstatSync(file).isFile(), `${name} is not a directory: ${file}`);
}
/**
* Returns true if file exists, otherwise throws
* @param {string} name display name of the file to check
* @param {string} path filepath of file to check
*/
function exists(name, path) {
assert(fs.existsSync(path), `${name} does not exist: ${path}`);
}
/**
* Returns true if json is valid, otherwise throws
* @param {string} name display name of property to check
* @param {?} object the parameter to check if is valid json
*/
function jsonExists(name, object) {
assert(typeof object === 'object', `${name} is not valid json: ${object}`);
}
/**
* Ensures that the given paths are valid
* @param {Object} paths Paths, keyed by any kind of name
* @throws If one of the paths does not exist
*/
function checkValid(paths, validator) {
Object.entries(paths).forEach(([name, path]) => {
assert(path, `${name} must be specified`);
if (validator) {
validator(name, path);
}
});
}
/**
* Ensures that the given paths exist
* @param {Object} paths Paths, keyed by any kind of name
* @throws If one of the paths does not exist
*/
function checkExist(paths) {
checkValid(paths, exists);
}
/**
* Ensures that the given directories exist
* @param {Object} dirs Directory paths, keyed by any kind of name
* @throws If one of the paths does not exist or is not a directory
*/
function checkDirsExist(dirs) {
checkExist(dirs);
checkValid(dirs, isDirectory);
}
/**
* Ensures that the given files exist, or are valid json objects
* @param {Object} files File paths or json properties, keyed by any kind of name
* @throws If one of the paths does not exist or is not a file or a json object.
*/
function checkFilesOrJsonObjectExist(filesOrJson) {
checkValid(filesOrJson);
Object.entries(filesOrJson).forEach(([name, file]) => {
if (typeof file === 'object') {
jsonExists(name, file);
}
else {
exists(name, file);
isFile(name, file);
}
});
}
module.exports = {
checkDirsExist,
checkFilesOrJsonObjectExist,
};
//# sourceMappingURL=preconditions.js.map