diff --git a/index.js b/index.js index 75313d2f..41b73b41 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const { namespace } = require('@dhis2/cli-helpers-engine') const command = namespace('style', { desc: 'DHIS2 programmatic style for commit msgs/code', aliases: 's', - builder: require('./src/cmds'), + builder: require('./src/cmds.js'), }) module.exports = command diff --git a/src/cmds/git-commit.js b/src/cmds/git-commit.js index 7479a6a5..36198610 100644 --- a/src/cmds/git-commit.js +++ b/src/cmds/git-commit.js @@ -43,4 +43,3 @@ exports.handler = async function(argv) { process.exit(1) } } - diff --git a/src/cmds/javascript.js b/src/cmds/javascript.js index a085007f..42383144 100644 --- a/src/cmds/javascript.js +++ b/src/cmds/javascript.js @@ -30,31 +30,31 @@ function cwd() { } // plugin -exports.command = 'js ' +exports.command = 'js ' exports.describe = 'Format javascript according to standards' -exports.builder = yargs => { - yargs - .positional('action', { - describe: 'Print the changes to stdout or apply them to your files', - choices: ['check', 'apply'], - type: 'string', - }) - .option('all', { - describe: 'If set scans all files, default is to scan staged files', - default: false, - type: 'boolean', - }) +exports.builder = { + cmd: { + describe: 'check or apply style', + choices: ['check', 'apply'], + type: 'string', + }, + all: { + type: 'boolean', + default: false, + describe: + 'default is to only check staged files, use this to check all files', + }, } exports.handler = function(argv) { - const { all, action } = argv + const { all, cmd } = argv const root_dir = process.cwd() const dep_dir = cwd() - const check = action === 'check' - const apply = action === 'apply' + const check = cmd === 'check' + const apply = cmd === 'apply' let codeFiles if (all) { @@ -70,9 +70,11 @@ exports.handler = function(argv) { const prettyFiles = prettify(dep_dir, codeFiles, check) - if (apply) { - log.debug('Pretty?', prettyFiles) + prettyFiles.length > 0 + ? log.info(`Files to style:\n${prettyFiles.join('\n')}`) + : log.info('No files to style.') + if (apply) { configure(dep_dir, root_dir) const stagedFiles = stage(prettyFiles, root_dir) diff --git a/src/prettier.js b/src/prettier.js index 34acb7d1..df009d90 100644 --- a/src/prettier.js +++ b/src/prettier.js @@ -13,6 +13,7 @@ function prettify(cwd, files, check = false) { return files .map(file => { const text = readFile(file) + const name = path.basename(file) if (!text) { log.debug('No text work on.', file, text) @@ -26,15 +27,15 @@ function prettify(cwd, files, check = false) { config: prettierConfig, }) - if (check) { const checked = prettier.check(text, { ...options, filepath: file, }) - if (!checked) { - log.info(`... ${file}`) + if (checked) { + log.debug(`${name} is formatted according to style`) + return null } } else { if (text.startsWith('#!')) { @@ -59,11 +60,11 @@ function prettify(cwd, files, check = false) { ? log.debug('file written to disk') : log.debug('file write FAILED') } else { - log.debug('Input/output identical, skipping.', file) + log.debug('Input/output identical, skipping.', name) return null } - log.info(`Reformatted: ${file}`) + log.info(`Reformatted: ${name}`) } } catch (error) { log.error('Formatting failed.', file, error)