ryuu
Version:
Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo
139 lines • 4.62 kB
JavaScript
import fs from 'fs-extra';
import path from 'path';
import semver from 'semver';
import { globSync } from 'glob';
import { log } from '../util/log.js';
import isValidJSName from '../util/isValidJSName.js';
/**
* 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.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.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);
}
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.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.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 (!isValidJSName(name)) {
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 = globSync(`*/${name}`, {
ignore: ['dist/*', 'build/*', 'templates/*'],
});
if (matches.length > 0) {
return matches[0];
}
return name;
};
export const ManifestUtils = {
getManifest,
hasManifest,
persistDesignId,
updateManifest,
updateManifestProperties,
findManifest,
};
//# sourceMappingURL=manifest.js.map