Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
Last active October 7, 2020 14:16
Show Gist options
  • Save dkarmalita/7bdc3f20aa269bd918b90709e48285e4 to your computer and use it in GitHub Desktop.
Save dkarmalita/7bdc3f20aa269bd918b90709e48285e4 to your computer and use it in GitHub Desktop.
/**
@example
const { getNodeOptionValue } = require('nodeOptions');
const maxOldSpaceSize = getNodeOptionValue('max_old_space_size');
*/
const parseNodeOptions = () => (!process.env.NODE_OPTIONS ? [] : process.env.NODE_OPTIONS.split(/[=:]/));
const _optionsArr = parseNodeOptions();
const _getOptionsCount = () => (_optionsArr.length ? _optionsArr.length / 2 : 0);
const _getOptionName = i => _optionsArr[i * 2];
const _getOptionValue = i => _optionsArr[(i * 2) + 1];
const nodeOptionsCount = _getOptionsCount();
const getOptionsObject = () => {
const acc = {};
for (let i = nodeOptionsCount - 1; i >= 0; i--) {
acc[_getOptionName(i)] = _getOptionValue(i);
}
return acc;
};
const nodeOptions = getOptionsObject();
const getNodeOptionValue = id => nodeOptions[id] || nodeOptions[`--${id}`];
module.exports = {
nodeOptions,
nodeOptionsCount,
getNodeOptionValue,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment