Skip to content

Commit

Permalink
Utils module updated with handleResources function moved out, touch #456
Browse files Browse the repository at this point in the history
.
  • Loading branch information
PaulDalek committed Jul 11, 2024
1 parent 8425836 commit 6c48548
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 127 deletions.
66 changes: 65 additions & 1 deletion lib/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { log, logWithStack } from './logger.js';
import { killPool, postWork, stats } from './pool.js';
import {
fixType,
handleResources,
isCorrectJSON,
optionsStringify,
roundNumber,
Expand Down Expand Up @@ -283,6 +282,66 @@ export const findChartSize = (options) => {
return size;
};

/**
* Handles and validates resources for export.
*
* @param {Object|string} resources - The resources to be handled. Can be either
* a JSON object, stringified JSON or a path to a JSON file.
* @param {boolean} allowFileResources - Whether to allow loading resources from
* files.
*
* @returns {Object|undefined} - The handled resources or undefined if no valid
* resources are found.
*/
const handleResources = (resources = false, allowFileResources) => {
const allowedProps = ['js', 'css', 'files'];

let handledResources = resources;
let correctResources = false;

// Try to load resources from a file
if (allowFileResources && resources.endsWith('.json')) {
try {
handledResources = isCorrectJSON(readFileSync(resources, 'utf8'));
} catch {
return false;
}
} else {
// Try to get JSON
handledResources = isCorrectJSON(resources);

// Get rid of the files section
if (handledResources && !allowFileResources) {
delete handledResources.files;
}
}

// Filter from unnecessary properties
for (const propName in handledResources) {
if (!allowedProps.includes(propName)) {
delete handledResources[propName];
} else if (!correctResources) {
correctResources = true;
}
}

// Check if at least one of allowed properties is present
if (!correctResources) {
return false;
}

// Handle files section
if (handledResources.files) {
handledResources.files = handledResources.files.map((item) => item.trim());
if (!handledResources.files || handledResources.files.length <= 0) {
delete handledResources.files;
}
}

// Return resources
return handledResources;
};

/**
* Function for finalizing options before export.
*
Expand Down Expand Up @@ -328,6 +387,11 @@ const doExport = async (options, chartJson, endCallback, svg) => {
);
}
}

// Check if there are any resources
if (options.customLogic.resources === false) {
log(3, `[cli] No resources found.`);
}
}

// If the allowCodeExecution flag isn't set, we should refuse the usage
Expand Down
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { initPool, killPool } from './pool.js';
import { shutdownCleanUp } from './resource_release.js';
import server, { startServer } from './server/server.js';
import { printLogo, printVersion, printUsage } from './utils.js';
import { defaultConfig } from '../lib/schemas/config.js';

/**
* Attaches exit listeners to the process, ensuring proper cleanup of resources
Expand Down Expand Up @@ -140,6 +141,8 @@ export default {
mapToNewConfig,
manualConfig,
printLogo,
printUsage,
printUsage: (noLogo) => {
printUsage(defaultConfig, noLogo);
},
printVersion
};
Loading

0 comments on commit 6c48548

Please sign in to comment.