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

Envinfo config file #176

Open
wants to merge 4 commits into
base: main
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
52 changes: 52 additions & 0 deletions __tests__/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const envinfo = require('../src/envinfo');
const path = require('path');

describe('envinfo.js config file', () => {
test('will read the file and use it', async () => {
const cwd = process.cwd();
process.chdir(path.join(__dirname, 'packages', path.sep, 'test-config'));
try {
const data = JSON.parse(await envinfo.run());
expect(Object.keys(data)).toEqual(['System', 'npmPackages']);
expect(Object.keys(data.System)).toEqual(['OS', 'CPU']);
} finally {
process.chdir(cwd);
}
});

test('run will read the file and use it as a promise', async () => {
const cwd = process.cwd();
process.chdir(path.join(__dirname, 'packages', path.sep, 'test-config-as-promise'));
try {
const data = JSON.parse(await envinfo.run());
expect(Object.keys(data)).toEqual(['System', 'Binaries']);
expect(Object.keys(data.System)).toEqual(['OS', 'CPU']);
expect(Object.keys(data.Binaries)).toEqual(['Node', 'npm']);
} finally {
process.chdir(cwd);
}
});

test('command line flags will override config file', async () => {
const cwd = process.cwd();
process.chdir(path.join(__dirname, 'packages', path.sep, 'test-config'));

const consoleLogSpy = jest.spyOn(global.console, 'log');

consoleLogSpy.mockImplementation(() => () => {});

try {
await envinfo.cli({
markdown: true,
json: false,
npmPackages: false,
system: true,
console: true,
});
expect(consoleLogSpy.mock.calls[0][0].includes('##')).toBe(true);
} finally {
consoleLogSpy.mockClear();
process.chdir(cwd);
}
});
});
5 changes: 5 additions & 0 deletions __tests__/packages/test-config-as-promise/envinfo.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = () => {
return new Promise(resolve =>
resolve({ System: ['OS', 'CPU'], Binaries: ['Node', 'npm'], options: { json: true } })
);
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions __tests__/packages/test-config-as-promise/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-config-as-promise",
"version": "1.0.0",
"dependencies": {
"a": "1.0.0",
"b": "1.0.0"
}
}
3 changes: 3 additions & 0 deletions __tests__/packages/test-config/envinfo.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = () => {
return { System: ['OS', 'CPU'], npmPackages: true, options: { json: true } };
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions __tests__/packages/test-config/node_modules/a/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions __tests__/packages/test-config/node_modules/b/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions __tests__/packages/test-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-config",
"version": "1.0.0",
"dependencies": {
"a": "1.0.0",
"b": "1.0.0"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
]
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"testPathIgnorePatterns": ["/node_modules/", "/__tests__/packages/"]
},
"dependencies": {},
"devDependencies": {
Expand Down
19 changes: 18 additions & 1 deletion src/envinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ function main(props, options) {
});
}

function runMainFromConfigFile(configFile, options) {
return Promise.resolve(configFile()).then(({ options: configOptions, ...config }) => {
return main(config, { ...configOptions, ...options });
});
}

// Example usage:
// $ envinfo --system --npmPackages
function cli(options) {
const configFile = utils.findConfigFile();
if (configFile) {
return runMainFromConfigFile(configFile, options);
}

// if all option is passed, do not pass go, do not collect 200 dollars, go straight to main
if (options.all) {
return main(
Expand Down Expand Up @@ -135,8 +146,14 @@ function cli(options) {

// require('envinfo);
// envinfo.run({ system: [os, cpu]}, {fullTree: true })
function run(args, options) {
function run(args = {}, options = {}) {
if (typeof args.preset === 'string') return main(presets[args.preset], options);

const configFile = utils.findConfigFile();
if (configFile) {
return runMainFromConfigFile(configFile, options);
}

return main(args, options);
}

Expand Down
3 changes: 3 additions & 0 deletions src/nativeRequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// needed for the dynamic requiring of a config file
// https://stackoverflow.com/questions/42797313/webpack-dynamic-module-loader-by-require/54559637#54559637
module.exports = require;
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const os = require('os');
const childProcess = require('child_process');
const libWhich = require('which');
const glob = require('glob');
const nativeRequire = require('./nativeRequire');
const matchers = require('./matchers');

const run = (cmd, { unify = false } = {}) => {
Expand Down Expand Up @@ -92,9 +93,20 @@ const parseSDKManagerOutput = output => {
};
};

function findConfigFile() {
const configPath = path.join(process.cwd(), 'envinfo.config.js');
try {
const fn = nativeRequire(configPath); // eslint-disable-line global-require
return fn;
} catch (error) {
return null;
}
}

module.exports = {
run: run,
log: log,
findConfigFile: findConfigFile,
fileExists: fileExists,
readFile: readFile,
requireJson: requireJson,
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
path: path.join(__dirname, '/dist'),
},
module: {
noParse: /\/src\/nativeRequire.js$/,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, lol

rules: [
{
use: 'babel-loader',
Expand Down