Skip to content

Commit

Permalink
docs ~ add CJS example with dynamic require (using package 'main')
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Feb 9, 2021
1 parent 536a3db commit e4c692d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions eg/show-paths.dynamic.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-env es6, node */
'use strict';

// eslint-disable-next-line security-node/detect-non-literal-require-calls , security/detect-non-literal-require
const osPaths = require('../' + require('../package.json').main);

/* eslint-disable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf */

function objectEntries(obj) {
const map = {};
Object.keys(obj).forEach((key) => {
const value = obj[key];
const val = typeof value === 'function' ? value() : value;
map[key] = val;
});
return map;
}

console.log({ osPaths });
console.log(objectEntries(osPaths));

process.env.TMPDIR = process.env.TEMP = process.env.TMP = 'temp';
console.log(objectEntries(osPaths));

/* eslint-enable no-console, functional/immutable-data , security/detect-object-injection , security-node/detect-crlf */

0 comments on commit e4c692d

Please sign in to comment.