Skip to content

Commit

Permalink
feat(cli): prefer project-local tools
Browse files Browse the repository at this point in the history
Increases the utility of the global d2 tool for developers by preferring
the version of the CLI tool that is installed over the globally bundled
one.

Before this change these two commands run in the same project can yield
different results:

    d2 style check          # global
    yarn d2-style check     # local

After the change, they will both use the local version:

    d2 style check          # local
    yarn d2-style check     # local

Jira-ticket-key: CLI-68
Jira-ticket-URL: https://jira.dhis2.org/browse/CLI-68
  • Loading branch information
varl committed Nov 15, 2021
1 parent 6c3a69d commit 50e1eae
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions packages/main/src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
const { namespace } = require('@dhis2/cli-helpers-engine')
const path = require('path')
const {
namespace,
findProjectRoot,
reporter,
} = require('@dhis2/cli-helpers-engine')

function load(tool) {
const root = findProjectRoot()
const paths = root ? [path.resolve(root, 'node_modules')] : []

try {
const resolved = require.resolve(tool, {
paths,
})

reporter.debug(`Loading tool from: ${resolved}`)
return require(resolved)
} catch (err) {
reporter.debug(`Loading tool from: ${tool}`)
return require(tool)
}
}

const command = namespace('d2', {
desc: 'DHIS2 CLI',
builder: yargs => {
yargs.command(require('@dhis2/cli-app'))
yargs.command(require('@dhis2/cli-cluster'))
yargs.command(require('@dhis2/cli-create'))
yargs.command(require('@dhis2/cli-style').command)
yargs.command(require('@dhis2/cli-utils'))
yargs.command(load('@dhis2/cli-app'))
yargs.command(load('@dhis2/cli-cluster'))
yargs.command(load('@dhis2/cli-create'))
yargs.command(load('@dhis2/cli-style').command)
yargs.command(load('@dhis2/cli-utils'))
yargs.commandDir('commands')
},
})
Expand Down

0 comments on commit 50e1eae

Please sign in to comment.