Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/absolute-cache-path #597

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 4.0.3

_New Features:_

- Added support for absolute paths in the `HIGHCHARTS_CACHE_PATH` option [(#562)](https://github.com/highcharts/node-export-server/issues/562).

# 4.0.2

_Hotfix_:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ These variables are set in your environment and take precedence over options fro
- `HIGHCHARTS_MODULE_SCRIPTS`: Highcharts module scripts to fetch (defaults to ``).
- `HIGHCHARTS_INDICATOR_SCRIPTS`: Highcharts indicator scripts to fetch (defaults to ``).
- `HIGHCHARTS_FORCE_FETCH`: The flag that determines whether to refetch all scripts after each server rerun (defaults to `false`).
- `HIGHCHARTS_CACHE_PATH`: In which directory should the fetched Highcharts scripts be placed (defaults to `.cache`).
- `HIGHCHARTS_CACHE_PATH`: In which directory should the fetched Highcharts scripts be placed (defaults to `.cache`). Since v4.0.3 can be either absolute or relative path.
- `HIGHCHARTS_ADMIN_TOKEN`: An authentication token that is required to switch the Highcharts version on the server at runtime (defaults to ``).

### Export Config
Expand Down
25 changes: 19 additions & 6 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See LICENSE file in root for details.
// before starting the service

import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { isAbsolute, join } from 'path';

import { HttpsProxyAgent } from 'https-proxy-agent';

Expand Down Expand Up @@ -83,7 +83,7 @@ export const saveConfigToManifest = async (config, fetchedModules) => {
log(3, '[cache] Writing a new manifest.');
try {
writeFileSync(
join(__dirname, config.cachePath, 'manifest.json'),
join(getCachePath(), 'manifest.json'),
JSON.stringify(newManifest),
'utf8'
);
Expand Down Expand Up @@ -306,15 +306,18 @@ export const updateVersion = async (newVersion) => {
*/
export const checkAndUpdateCache = async (options) => {
const { highcharts, server } = options;
const cachePath = join(__dirname, highcharts.cachePath);

let fetchedModules;

// Get the cache path
const cachePath = getCachePath();

// Prepare paths to manifest and sources from the .cache folder
const manifestPath = join(cachePath, 'manifest.json');
const sourcePath = join(cachePath, 'sources.js');

// Create the cache destination if it doesn't exist already
!existsSync(cachePath) && mkdirSync(cachePath);
!existsSync(cachePath) && mkdirSync(cachePath, { recursive: true });

// Fetch all the scripts either if manifest.json does not exist
// or if the forceFetch option is enabled
Expand Down Expand Up @@ -387,8 +390,18 @@ export const checkAndUpdateCache = async (options) => {
await saveConfigToManifest(highcharts, fetchedModules);
};

export const getCachePath = () =>
join(__dirname, getOptions().highcharts.cachePath);
/**
* Returns the path to the cache folder.
*
* @returns {string} The path to the cache folder.
*/
export const getCachePath = () => {
const cachePathOption = getOptions().highcharts.cachePath;

return isAbsolute(cachePathOption)
? cachePathOption
: join(__dirname, cachePathOption);
};

export const getCache = () => cache;

Expand Down
6 changes: 1 addition & 5 deletions lib/highcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ export async function triggerExport(chartOptions, options, displayErrors) {
let constr = options.export.constr || 'chart';
constr = typeof Highcharts[constr] !== 'undefined' ? constr : 'chart';

Highcharts[constr](
'container',
finalOptions,
finalCallback
);
Highcharts[constr]('container', finalOptions, finalCallback);

// Get the current global options
const defaultOptions = getOptions();
Expand Down
Loading