Skip to content

Add includeCore and includeNonExisting options #165

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -33,7 +33,9 @@ const tree = dependencyTree({
}, // optional
filter: path => path.indexOf('node_modules') === -1, // optional
nonExistent: [], // optional
noTypeDefinitions: false // optional
noTypeDefinitions: false, // optional
includeCore: false, // optional, if true include node.js core modules (for example: "fs"), they will be prefixed with ':!EXISTS: '
includeNonExisting: false // optional, if true include unresolved dependencies, they will be prefixed with ':!EXISTS: '
});

// Returns a post-order traversal (list form) of the tree with duplicate sub-trees pruned.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ module.exports.toList = function(options = {}) {
*/
module.exports._getDependencies = function(config = {}) {
const precinctOptions = config.detectiveConfig;
precinctOptions.includeCore = false;

let dependencies;

try {
@@ -113,7 +113,12 @@ module.exports._getDependencies = function(config = {}) {

if (!result) {
debug(`skipping an empty filepath resolution for partial: ${dependency}`);

config.nonExistent.push(dependency);
if (config.includeNonExisting) {
resolvedDependencies.push(`:!EXISTS: ${dependency}`);
}

continue;
}

@@ -122,6 +127,11 @@ module.exports._getDependencies = function(config = {}) {
if (!exists) {
config.nonExistent.push(dependency);
debug(`skipping non-empty but non-existent resolution: ${result} for partial: ${dependency}`);

if (config.includeNonExisting) {
resolvedDependencies.push(`:!EXISTS: ${dependency}`);
}

continue;
}

6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
@@ -16,11 +16,15 @@ module.exports = class Config {
this.requireConfig = options.config || options.requireConfig;
this.webpackConfig = options.webpackConfig;
this.nodeModulesConfig = options.nodeModulesConfig;
this.detectiveConfig = options.detective || options.detectiveConfig || {};
this.includeCore = options.includeCore || false;
this.includeNonExisting = options.includeNonExisting || options.includeCore || false;
this.tsConfig = options.tsConfig;
this.tsConfigPath = options.tsConfigPath;
this.noTypeDefinitions = options.noTypeDefinitions;
this.filter = options.filter;
this.detectiveConfig = options.detective || options.detectiveConfig || {};
this.detectiveConfig.includeCore = this.includeCore;
this.detectiveConfig.includeNonExisting = this.includeNonExisting;

if (!this.filename) throw new Error('filename not given');
if (!this.directory) throw new Error('directory not given');
20 changes: 20 additions & 0 deletions test/test.mjs
Original file line number Diff line number Diff line change
@@ -221,6 +221,26 @@ describe('dependencyTree', () => {
assert.ok(!list.includes(path.join(directory, 'required.js')));
});

it('test includeNonExisting=true', () => {
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
const filename = path.normalize(`${directory}/a.js`);

const tree = dependencyTree({ filename, directory, includeNonExisting: true });
const subTree = tree[filename];

assert.ok(Object.keys(subTree).includes(':!EXISTS: not-real'));
});

it('test includeCore=true', () => {
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
const filename = path.normalize(`${directory}/a.js`);

const tree = dependencyTree({ filename, directory, includeCore: true });
const subTree = tree[filename];

assert.ok(Object.keys(subTree).includes(':!EXISTS: path'));
});

describe('when given a detective configuration', () => {
it('passes it through to precinct', () => {
const spy = sinon.spy(precinct, 'paperwork');