Skip to content

Commit

Permalink
fix: add apply/check command to js
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Feb 6, 2019
1 parent ef79104 commit 3da7d5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/cmds/git-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ exports.handler = async function(argv) {
process.exit(1)
}
}

38 changes: 20 additions & 18 deletions src/cmds/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ function cwd() {
}

// plugin
exports.command = 'js <action>'
exports.command = 'js <cmd>'

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) {
Expand 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)
Expand Down
11 changes: 6 additions & 5 deletions src/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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('#!')) {
Expand All @@ -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)
Expand Down

0 comments on commit 3da7d5c

Please sign in to comment.