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

feat: tree path #149

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ module.exports = function(options = {}) {
return config.isListForm ? [] : {};
}

const results = traverse(config);
const params = {
treePath: []
};

const results = traverse(config, params);
debug('traversal complete', results);

dedupeNonExistent(config.nonExistent);
Expand Down Expand Up @@ -80,9 +84,10 @@ module.exports.toList = function(options = {}) {
* @param {Config} config
* @return {Array}
*/
module.exports._getDependencies = function(config = {}) {
module.exports._getDependencies = function(config = {}, params = {}) {
const precinctOptions = config.detectiveConfig;
precinctOptions.includeCore = false;
precinctOptions.treePath = params.treePath;
let dependencies;

try {
Expand Down Expand Up @@ -131,9 +136,10 @@ module.exports._getDependencies = function(config = {}) {

/**
* @param {Config} config
* @param {Object} params
* @return {Object|Set}
*/
function traverse(config = {}) {
function traverse(config = {}, params = {}) {
const subTree = config.isListForm ? new Set() : {};

debug(`traversing ${config.filename}`);
Expand All @@ -143,7 +149,7 @@ function traverse(config = {}) {
return config.visited[config.filename];
}

let dependencies = module.exports._getDependencies(config);
let dependencies = module.exports._getDependencies(config, params);

debug('cabinet-resolved all dependencies: ', dependencies);
// Prevents cycles by eagerly marking the current file as read
Expand All @@ -162,12 +168,20 @@ function traverse(config = {}) {
const localConfig = config.clone();
localConfig.filename = dependency;

const newParams = {
...params,
treePath: [
...(params.treePath ?? []),
config.filename
]
};

if (localConfig.isListForm) {
for (const item of traverse(localConfig)) {
for (const item of traverse(localConfig, newParams)) {
subTree.add(item);
}
} else {
subTree[dependency] = traverse(localConfig);
subTree[dependency] = traverse(localConfig, newParams);
}
}

Expand Down
Loading