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 walker #120

Closed
milahu opened this issue Sep 16, 2021 · 0 comments
Closed

feat: tree walker #120

milahu opened this issue Sep 16, 2021 · 0 comments
Labels

Comments

@milahu
Copy link

milahu commented Sep 16, 2021

every tree needs a tree walker ; )

npm/logical-tree

npm/logical-tree fork

i use this to build a deep node_modules with symlinks, just like pnpm

its useful to have an array of parent nodes in the walker function

sample

  const deptree = await buildDepTree(
    read('package.json'),
    read(lockfilePath),
    false,
    lockfileTypeOfName[path.basename(lockfilePath)],
    true,
  );

  function walk_deptree(_this, enter, _seen, deptreePath = []) {

    // note: this will deduplicate deps
    if (!_seen) { _seen = new Set() }
    if (_seen.has(_this)) { return }
    _seen.add(_this)

    enter(_this, function recurse() {
      for (let key in _this.dependencies) {
        walk_deptree(_this.dependencies[key], enter, _seen, deptreePath.concat([_this]))
      }
    }, deptreePath)
  }

  //deptree.forEach((dep, recurse, deptreePath) => {
  walk_deptree(deptree, function enter(dep, recurse, deptreePath) {
    // enter
    console.log(`${deptreePath.map(_ => `+ `).join('')}${dep.name}@${dep.version}`);
    recurse();
    // leave
  });
@github-actions github-actions bot added the Stale label May 15, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant