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: depchecks-fix script #451

Open
jeswr opened this issue Mar 28, 2022 · 0 comments
Open

feat: depchecks-fix script #451

jeswr opened this issue Mar 28, 2022 · 0 comments

Comments

@jeswr
Copy link

jeswr commented Mar 28, 2022

Similar to how there is a depchecks script, I have made the following depchecks-fix script for a project I am working on which I thought might be useful to have upstream.

The idea is that it will go through each module, run yarn add on the missing deps and run yarn remove on any unneeded deps. In my case since all dev dependencies should be listed in the main package.json for the repo; I remove the devDependencies of any packages in the repo.

My current implementation is

const {loadPackages, exec, iter} = require('lerna-script')
const checkDeps = require('depcheck')
const path = require('path');

async function depcheckTask(log) {
  return iter.forEach((await (log.packages || loadPackages())).filter(package => package.location.startsWith(path.join(__dirname, '/packages'))), { log })(async package => {
    const {dependencies, devDependencies, missing, using} = await checkDeps(package.location, { ignorePatterns: [
      // files matching these patterns will be ignored
      'test/*',
    ], }, val => val);

    log.info(package.name)
    
    const missing_deps = Object.keys(missing)
    if (missing_deps.length > 0) {
      try {
        log.info('    add:', missing_deps.join(', '))
        await exec.command(package)(`yarn add ${missing_deps.join(' ')}`);
      } catch (e) {
        for (const dep of missing_deps) {
          try {
            await exec.command(package)(`yarn add ${dep}`);
          } catch (e) {
            log.error('    CANNOT ADD:', dep);
          }
        }
      }
    }

    const unused_deps = [...dependencies, ...devDependencies].filter(elem => !Object.keys(using).includes(elem));
    if (unused_deps.length > 0) {
      try {
        log.info('    remove:', unused_deps.join(', '))
        await exec.command(package)(`yarn remove ${unused_deps.join(' ')}`);
      } catch (e) {
        for (const dep of unused_deps) {
          try {
            await exec.command(package)(`yarn remove ${dep}`);
          } catch (e) {
            log.error('    CANNOT REMOVE:', dep);
          }
        }
      }
    }
  })
}

module.exports.depcheckTask = depcheckTask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant