ryuu
Version:
Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo
149 lines • 5.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManifestUtils = void 0;
const fs = require("fs-extra");
const path = require("path");
const semver = require("semver");
const glob = require("glob");
const log_1 = require("../util/log");
const isValidJSName_1 = __importDefault(require("../util/isValidJSName"));
/**
* Get the local manifest.json file, ensuring it's valid.
*/
const getManifest = (manifestName = 'manifest.json', verifyValid = false) => {
const manifestPath = findManifest(manifestName);
let rawmanifest;
try {
rawmanifest = fs.readFileSync(path.resolve(manifestPath));
}
catch {
if (verifyValid) {
log_1.log.fail(`No ${manifestName} file in this directory`, `Please publish from a directory containing a Custom App design ${manifestName} file`);
}
else {
return undefined;
}
}
let manifest;
try {
manifest = JSON.parse(rawmanifest.toString());
}
catch (e) {
log_1.log.fail(`Your ${manifestName} file is not valid JSON`, e.message);
}
// add the manifest name, so it's attached to the object
manifest.fileName = manifestName;
if (verifyValid) {
manifest = updateManifestProperties(manifest);
validateManifest(manifest);
}
/*
waiting to deploy below line until I have time to refactor oauth
log.info(`Getting manifest located in ${ path.resolve(manifestPath) }`)
*/
return manifest;
};
const hasManifest = (manifestName) => {
return fs.existsSync(findManifest(manifestName));
};
/**
* update the manifest (in memory and on disk)
*/
const persistDesignId = (id, manifest) => {
const newManifest = { ...manifest, id };
updateManifest(newManifest);
return id;
};
const updateManifest = (manifest) => {
const manifestPath = findManifest(manifest.fileName);
fs.writeJsonSync(manifestPath, manifest, { spaces: 2 });
};
/**
* Validate the manifest file.
*/
const validateManifest = (manifest) => {
// validate manifest file for required fields
requireProperty(manifest, 'name');
requireProperty(manifest, 'version');
// validate semver
if (!semver.valid(manifest.version)) {
log_1.log.fail('Invalid version', 'Please make sure the `version` in your manifest.json file is a valid semantic version. See http://semver.org/');
}
return manifest;
};
const requireProperty = (obj, prop) => {
if (!obj[prop]) {
log_1.log.fail('Missing required property: ' + prop, 'Please add the missing `' +
prop +
'` property to your manifest.json file.');
}
};
const updateManifestProperties = (manifest) => {
const updatedManifest = { ...manifest };
if (Object.prototype.hasOwnProperty.call(updatedManifest, 'mapping')) {
updatedManifest.datasetsMapping = updatedManifest.mapping;
delete updatedManifest.mapping;
}
else if (!Object.prototype.hasOwnProperty.call(updatedManifest, 'datasetsMapping')) {
updatedManifest.datasetsMapping = [];
}
if (Object.prototype.hasOwnProperty.call(updatedManifest, 'collections')) {
updatedManifest.collectionsMapping = updatedManifest.collections;
delete updatedManifest.collections;
}
if (Object.prototype.hasOwnProperty.call(updatedManifest, 'appContextId')) {
updatedManifest.proxyId = updatedManifest['appContextId'];
delete updatedManifest['appContextId'];
}
else if (Object.prototype.hasOwnProperty.call(updatedManifest, 'cardId')) {
updatedManifest.proxyId = updatedManifest['cardId'];
delete updatedManifest['cardId'];
}
updateManifest(updatedManifest);
return updatedManifest;
};
/**
* Alias must be a valid JS property
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function ensureValidAlias(name) {
if (!(0, isValidJSName_1.default)(name)) {
log_1.log.fail('Alias names must be valid JavaScript properties', 'Please rename the ' +
JSON.stringify(name) +
' alias and update any /data/v1/<alias> calls with the new alias name');
}
}
/**
* Finds the manifest file in the current directory, or sub files
*/
const findManifest = (name) => {
if (fs.existsSync(name)) {
return name;
}
if (fs.existsSync(`public/${name}`)) {
return `public/${name}`;
}
if (fs.existsSync(`src/${name}`)) {
return `src/${name}`;
}
// find any matching files (one layer deep while ignoring dist, templates and build folders)
const matches = glob.sync(`*/${name}`, {
ignore: ['dist/*', 'build/*', 'templates/*'],
});
if (matches.length > 0) {
return matches[0];
}
return name;
};
exports.ManifestUtils = {
getManifest,
hasManifest,
persistDesignId,
updateManifest,
updateManifestProperties,
findManifest,
};
//# sourceMappingURL=manifest.js.map