Skip to content

Commit

Permalink
fix: use the nearest bin folder to exec cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Apr 29, 2020
1 parent d756e3a commit 761611f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"fast-glob": "^3.2.2",
"find-up": "^4.1.0",
"fs-extra": "^8.1.0",
"husky": "^4.2.5",
"perfy": "^1.1.5",
Expand Down
8 changes: 4 additions & 4 deletions src/tools/commitlint.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { run } = require('../utils/run.js')
const { bin } = require('../utils/run.js')
const { COMMITLINT_CONFIG } = require('../utils/paths.js')

exports.commitlint = (config = COMMITLINT_CONFIG) => {
const cmd = 'npx'
const args = ['--no-install', 'commitlint', `--config=${config}`, '--edit']
const cmd = 'commitlint'
const args = ['commitlint', `--config=${config}`, '--edit']

run(cmd, { args })
bin(cmd, { args })
}
8 changes: 3 additions & 5 deletions src/tools/eslint.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { run } = require('../utils/run.js')
const { bin } = require('../utils/run.js')
const { resolveIgnoreFile } = require('../utils/files.js')

exports.eslint = ({ files = [], apply = false, config }) => {
const ignoreFile = resolveIgnoreFile(['.eslintignore'])
const cmd = 'npx'
const cmd = 'eslint'
const args = [
'--no-install',
'eslint',
'--no-color',
'--report-unused-disable-directives',
'--ignore',
Expand All @@ -18,5 +16,5 @@ exports.eslint = ({ files = [], apply = false, config }) => {
...files,
]

run(cmd, { args })
bin(cmd, { args })
}
8 changes: 3 additions & 5 deletions src/tools/prettier.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const log = require('@dhis2/cli-helpers-engine').reporter

const { run } = require('../utils/run.js')
const { bin } = require('../utils/run.js')
const { resolveIgnoreFile } = require('../utils/files.js')

exports.prettier = ({ files = [], apply = false, config }) => {
const ignoreFile = resolveIgnoreFile(['.prettierignore'])
const cmd = 'npx'
const cmd = 'prettier'
const args = [
'--no-install',
'prettier',
'--list-different',
...(config ? ['--config', config] : []),
...(ignoreFile ? ['--ignore-path', ignoreFile] : []),
...(apply ? ['--write'] : []),
...files,
]

run(cmd, { args }, ({ status }) => {
bin(cmd, { args }, ({ status }) => {
if (status === 1 && !apply) {
log.warn(`Code style issues found in the above file(s).`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/paths.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path')

const CONSUMING_ROOT = path.join(process.cwd())
const CONFIG_ROOT = path.join(__dirname, '..', '..')
const CONFIG_DIR = path.join(CONFIG_ROOT, 'config')
const PACKAGE_ROOT = path.join(__dirname, '..', '..')
const CONFIG_DIR = path.join(PACKAGE_ROOT, 'config')

const ESLINT_CONFIG = path.join(CONFIG_DIR, 'js', 'eslint.config.js')
const ESLINT_REACT_CONFIG = path.join(
Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports = {
CONSUMING_ROOT,
BROWSERSLIST_CONFIG,
COMMITLINT_CONFIG,
CONFIG_ROOT,
PACKAGE_ROOT,
CONFIG_DIR,
DEPENDABOT_CONFIG,
EDITORCONFIG_CONFIG,
Expand Down
21 changes: 21 additions & 0 deletions src/utils/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const path = require('path')
const spawn = require('cross-spawn')
const findup = require('find-up')
const { PACKAGE_ROOT } = require('./paths.js')

exports.spawn = (cmd, args, opts) =>
spawn.sync(cmd, args, {
Expand All @@ -16,6 +19,24 @@ exports.run = (cmd, { args, opts }, callback) => {
)
}

exports.bin = (cmd, { args, opts }, callback) => {
const nodemodules = findup.sync('node_modules', {
cwd: PACKAGE_ROOT,
type: 'directory',
allowSymlinks: true,
})

const binCmd = path.join(nodemodules, '.bin', cmd)

return handleRun(
spawn.sync(binCmd, args, {
stdio: 'inherit',
...opts,
}),
callback
)
}

function handleRun(result, callback) {
if (result.error) {
throw result.error
Expand Down

0 comments on commit 761611f

Please sign in to comment.