Skip to content

Commit 5ad605c

Browse files
committed
Merged branch.
2 parents 14a1376 + b663fe7 commit 5ad605c

35 files changed

+1094
-860
lines changed

README.md

Lines changed: 44 additions & 72 deletions
Large diffs are not rendered by default.

bin/cli.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import {
2626
printLicense,
2727
printUsage,
2828
printVersion,
29-
setOptions
29+
setGlobalOptions
3030
} from '../lib/config.js';
3131
import { initExport } from '../lib/index.js';
3232
import { log, logWithStack } from '../lib/logger.js';
3333
import { manualConfig } from '../lib/prompt.js';
3434
import { shutdownCleanUp } from '../lib/resourceRelease.js';
35+
3536
import { startServer } from '../lib/server/server.js';
3637

3738
import ExportError from '../lib/errors/ExportError.js';
@@ -81,7 +82,7 @@ async function start() {
8182
}
8283

8384
// Set the options, keeping the priority order of setting values
84-
const options = setOptions({}, args, true);
85+
const options = setGlobalOptions({}, args);
8586

8687
// If all options are correctly parsed
8788
if (options) {
@@ -90,35 +91,44 @@ async function start() {
9091

9192
// In this case we want to prepare config manually
9293
if (options.customLogic.createConfig) {
93-
manualConfig(options.customLogic.createConfig);
94+
manualConfig(
95+
options.customLogic.createConfig,
96+
options.customLogic.allowCodeExecution
97+
);
9498
return;
9599
}
96100

97101
// Start server
98102
if (options.server.enable) {
99103
// Init the export mechanism for the server configuration
100-
await initExport(options);
104+
await initExport();
101105

102106
// Run the server
103-
await startServer(options.server);
107+
await startServer();
104108
} else {
105109
// Perform batch exports
106110
if (options.export.batch) {
107111
// Init the export mechanism for batch exports
108-
await initExport(options);
112+
await initExport();
109113

110114
// Start batch exports
111-
await batchExport(options);
115+
await batchExport({
116+
export: options.export,
117+
customLogic: options.customLogic
118+
});
112119
} else {
113120
// No need for multiple workers in case of a single CLI export
114121
options.pool.minWorkers = 1;
115122
options.pool.maxWorkers = 1;
116123

117124
// Init the export mechanism for a single export
118-
await initExport(options);
125+
await initExport();
119126

120127
// Start a single export
121-
await singleExport(options);
128+
await singleExport({
129+
export: options.export,
130+
customLogic: options.customLogic
131+
});
122132
}
123133
}
124134
} else {

dist/index.cjs

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

dist/index.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/browser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export async function newPage(poolResource) {
221221
* @param {boolean} [hardReset=false] - A flag indicating the type of clearing
222222
* to be performed. If true, navigates to `about:blank` and resets content
223223
* and scripts. If false, clears the body content by setting a predefined HTML
224-
* structure. The default value is false.
224+
* structure. The default value is `false`.
225225
*
226226
* @returns {Promise<boolean>} A Promise that resolves to true when page
227227
* is correctly cleared and false when it is not.
@@ -268,8 +268,8 @@ export async function clearPage(poolResource, hardReset = false) {
268268
*
269269
* @param {Object} page - The Puppeteer page object to which resources will
270270
* be added.
271-
* @param {Object} customLogicOptions - The object containing `customLogic`
272-
* options.
271+
* @param {Object} customLogicOptions - The configuration object containing
272+
* `customLogic` options.
273273
*
274274
* @returns {Promise<Array.<Object>>} A Promise that resolves to an array
275275
* of injected resources.

lib/cache.js

Lines changed: 101 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -48,106 +48,117 @@ const cache = {
4848
* @async
4949
* @function checkAndUpdateCache
5050
*
51-
* @param {Object} highchartsOptions - Object containing `highcharts` options.
52-
* @param {Object} serverProxyOptions - Object containing `server.proxy`
53-
* options.
51+
* @param {Object} highchartsOptions - The configuration object containing
52+
* `highcharts` options.
53+
* @param {Object} serverProxyOptions- The configuration object containing
54+
* `server.proxy` options.
5455
*/
5556
export async function checkAndUpdateCache(
5657
highchartsOptions,
5758
serverProxyOptions
5859
) {
59-
let fetchedModules;
60-
61-
// Get the cache path
62-
const cachePath = getCachePath();
63-
64-
// Prepare paths to manifest and sources from the cache folder
65-
const manifestPath = join(cachePath, 'manifest.json');
66-
const sourcePath = join(cachePath, 'sources.js');
67-
68-
// Create the cache destination if it doesn't exist already
69-
!existsSync(cachePath) && mkdirSync(cachePath, { recursive: true });
70-
71-
// Fetch all the scripts either if the `manifest.json` does not exist
72-
// or if the `forceFetch` option is enabled
73-
if (!existsSync(manifestPath) || highchartsOptions.forceFetch) {
74-
log(3, '[cache] Fetching and caching Highcharts dependencies.');
75-
fetchedModules = await _updateCache(
76-
highchartsOptions,
77-
serverProxyOptions,
78-
sourcePath
79-
);
80-
} else {
81-
let requestUpdate = false;
60+
try {
61+
let fetchedModules;
8262

83-
// Read the manifest JSON
84-
const manifest = JSON.parse(readFileSync(manifestPath));
63+
// Get the cache path
64+
const cachePath = getCachePath();
8565

86-
// Check if the modules is an array, if so, we rewrite it to a map to make
87-
// it easier to resolve modules.
88-
if (manifest.modules && Array.isArray(manifest.modules)) {
89-
const moduleMap = {};
90-
manifest.modules.forEach((m) => (moduleMap[m] = 1));
91-
manifest.modules = moduleMap;
92-
}
66+
// Prepare paths to manifest and sources from the cache folder
67+
const manifestPath = join(cachePath, 'manifest.json');
68+
const sourcePath = join(cachePath, 'sources.js');
9369

94-
// Get the actual number of scripts to be fetched
95-
const { coreScripts, moduleScripts, indicatorScripts } = highchartsOptions;
96-
const numberOfModules =
97-
coreScripts.length + moduleScripts.length + indicatorScripts.length;
98-
99-
// Compare the loaded highcharts config with the contents in cache.
100-
// If there are changes, fetch requested modules and products,
101-
// and bake them into a giant blob. Save the blob.
102-
if (manifest.version !== highchartsOptions.version) {
103-
log(
104-
2,
105-
'[cache] A Highcharts version mismatch in the cache, need to re-fetch.'
106-
);
107-
requestUpdate = true;
108-
} else if (Object.keys(manifest.modules || {}).length !== numberOfModules) {
109-
log(
110-
2,
111-
'[cache] The cache and the requested modules do not match, need to re-fetch.'
112-
);
113-
requestUpdate = true;
114-
} else {
115-
// Check each module, if anything is missing refetch everything
116-
requestUpdate = (moduleScripts || []).some((moduleName) => {
117-
if (!manifest.modules[moduleName]) {
118-
log(
119-
2,
120-
`[cache] The ${moduleName} is missing in the cache, need to re-fetch.`
121-
);
122-
return true;
123-
}
124-
});
125-
}
70+
// Create the cache destination if it doesn't exist already
71+
!existsSync(cachePath) && mkdirSync(cachePath, { recursive: true });
12672

127-
// Update cache if needed
128-
if (requestUpdate) {
73+
// Fetch all the scripts either if the `manifest.json` does not exist
74+
// or if the `forceFetch` option is enabled
75+
if (!existsSync(manifestPath) || highchartsOptions.forceFetch) {
76+
log(3, '[cache] Fetching and caching Highcharts dependencies.');
12977
fetchedModules = await _updateCache(
13078
highchartsOptions,
13179
serverProxyOptions,
13280
sourcePath
13381
);
13482
} else {
135-
log(3, '[cache] Dependency cache is up to date, proceeding.');
83+
let requestUpdate = false;
13684

137-
// Load the sources
138-
cache.sources = readFileSync(sourcePath, 'utf8');
85+
// Read the manifest JSON
86+
const manifest = JSON.parse(readFileSync(manifestPath), 'utf8');
13987

140-
// Get current modules map
141-
fetchedModules = manifest.modules;
88+
// Check if the modules is an array, if so, we rewrite it to a map to make
89+
// it easier to resolve modules.
90+
if (manifest.modules && Array.isArray(manifest.modules)) {
91+
const moduleMap = {};
92+
manifest.modules.forEach((m) => (moduleMap[m] = 1));
93+
manifest.modules = moduleMap;
94+
}
95+
96+
// Get the actual number of scripts to be fetched
97+
const { coreScripts, moduleScripts, indicatorScripts } =
98+
highchartsOptions;
99+
const numberOfModules =
100+
coreScripts.length + moduleScripts.length + indicatorScripts.length;
101+
102+
// Compare the loaded highcharts config with the contents in cache.
103+
// If there are changes, fetch requested modules and products,
104+
// and bake them into a giant blob. Save the blob.
105+
if (manifest.version !== highchartsOptions.version) {
106+
log(
107+
2,
108+
'[cache] A Highcharts version mismatch in the cache, need to re-fetch.'
109+
);
110+
requestUpdate = true;
111+
} else if (
112+
Object.keys(manifest.modules || {}).length !== numberOfModules
113+
) {
114+
log(
115+
2,
116+
'[cache] The cache and the requested modules do not match, need to re-fetch.'
117+
);
118+
requestUpdate = true;
119+
} else {
120+
// Check each module, if anything is missing refetch everything
121+
requestUpdate = (moduleScripts || []).some((moduleName) => {
122+
if (!manifest.modules[moduleName]) {
123+
log(
124+
2,
125+
`[cache] The ${moduleName} is missing in the cache, need to re-fetch.`
126+
);
127+
return true;
128+
}
129+
});
130+
}
131+
132+
// Update cache if needed
133+
if (requestUpdate) {
134+
fetchedModules = await _updateCache(
135+
highchartsOptions,
136+
serverProxyOptions,
137+
sourcePath
138+
);
139+
} else {
140+
log(3, '[cache] Dependency cache is up to date, proceeding.');
142141

143-
// Extract and save version of currently used Highcharts
144-
cache.hcVersion = extractVersion(cache.sources);
142+
// Load the sources
143+
cache.sources = readFileSync(sourcePath, 'utf8');
144+
145+
// Get current modules map
146+
fetchedModules = manifest.modules;
147+
148+
// Extract and save version of currently used Highcharts
149+
cache.hcVersion = extractVersion(cache.sources);
150+
}
145151
}
146-
}
147152

148-
// Finally, save the new manifest, which is basically our current config
149-
// in a slightly different format
150-
await _saveConfigToManifest(highchartsOptions, fetchedModules);
153+
// Finally, save the new manifest, which is basically our current config
154+
// in a slightly different format
155+
await _saveConfigToManifest(highchartsOptions, fetchedModules);
156+
} catch (error) {
157+
throw new ExportError(
158+
'[cache] Could not configure cache and create or update the config manifest.',
159+
500
160+
).setError(error);
161+
}
151162
}
152163

153164
/**
@@ -235,7 +246,7 @@ export function getCache() {
235246
* @returns {string} The absolute path to the cache directory for Highcharts.
236247
*/
237248
export function getCachePath() {
238-
return getAbsolutePath(getOptions().highcharts.cachePath); // #562
249+
return getAbsolutePath(getOptions().highcharts.cachePath, 'utf8'); // #562
239250
}
240251

241252
/**
@@ -251,7 +262,7 @@ export function getCachePath() {
251262
* modules have been fetched.
252263
* @param {boolean} [shouldThrowError=false] - A flag to indicate if the error
253264
* should be thrown. This should be used only for the core scripts. The default
254-
* value is false.
265+
* value is `false`.
255266
*
256267
* @returns {Promise<string>} A Promise that resolves to the text representation
257268
* of the fetched script.
@@ -304,7 +315,8 @@ async function _fetchAndProcessScript(
304315
* @async
305316
* @function _saveConfigToManifest
306317
*
307-
* @param {Object} highchartsOptions - Object containing `highcharts` options.
318+
* @param {Object} highchartsOptions - The configuration object containing
319+
* `highcharts` options.
308320
* @param {Object} [fetchedModules={}] - An object which tracks which Highcharts
309321
* modules have been fetched. The default value is an empty object.
310322
*
@@ -345,8 +357,8 @@ async function _saveConfigToManifest(highchartsOptions, fetchedModules = {}) {
345357
* @param {Array.<string>} moduleScripts - Highcharts modules to fetch.
346358
* @param {Array.<string>} customScripts - Custom script paths to fetch (full
347359
* URLs).
348-
* @param {Object} serverProxyOptions - Object containing `server.proxy`
349-
* options.
360+
* @param {Object} serverProxyOptions - The configuration object containing
361+
* `server.proxy` options.
350362
* @param {Object} fetchedModules - An object which tracks which Highcharts
351363
* modules have been fetched.
352364
*
@@ -413,9 +425,10 @@ async function _fetchScripts(
413425
* @async
414426
* @function _updateCache
415427
*
416-
* @param {Object} highchartsOptions - Object containing `highcharts` options.
417-
* @param {Object} serverProxyOptions - Object containing `server.proxy`
418-
* options.
428+
* @param {Object} highchartsOptions - The configuration object containing
429+
* `highcharts` options.
430+
* @param {Object} serverProxyOptions - The configuration object containing
431+
* `server.proxy` options.
419432
* @param {string} sourcePath - The path to the source file in the cache.
420433
*
421434
* @returns {Promise<Object>} A Promise that resolves to an object representing

0 commit comments

Comments
 (0)