Skip to content

Commit

Permalink
refactor: rename command modules
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Feb 6, 2019
1 parent 5d4169d commit 4877cfe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/cmds/fmt-commit.js → src/cmds/git-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const load = require('@commitlint/load')
const lint = require('@commitlint/lint')
const format = require('@commitlint/format')

exports.command = 'commit'
exports.command = '$0'

exports.describe = 'Format commit messages according to DHIS2 rules.'

Expand Down Expand Up @@ -43,3 +43,4 @@ exports.handler = async function(argv) {
process.exit(1)
}
}

37 changes: 25 additions & 12 deletions src/cmds/fmt-code-js.js → src/cmds/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,32 @@ function cwd() {
}

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

exports.describe = 'Format js code according to DHIS2 rules.'
exports.describe = 'Format javascript according to standards'

exports.builder = {
all: { boolean: true },
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.handler = function(argv) {
const { all } = argv
const { all, action } = argv
const root_dir = process.cwd()
const dep_dir = cwd()

const check = action === 'check'
const apply = action === 'apply'

let codeFiles
if (all) {
codeFiles = collectFiles(root_dir).filter(whitelisted)
Expand All @@ -55,14 +68,14 @@ exports.handler = function(argv) {
log.debug('depDir?', dep_dir)
log.debug('codeFiles?', codeFiles)

const prettyFiles = prettify(dep_dir, codeFiles)

log.debug('Pretty?', prettyFiles)
const prettyFiles = prettify(dep_dir, codeFiles, check)

configure(dep_dir, root_dir)
if (apply) {
log.debug('Pretty?', prettyFiles)

const stagedFiles = stage(prettyFiles, root_dir)
log.debug('Staged files', stagedFiles)
configure(dep_dir, root_dir)

log.info('Code style complete.')
const stagedFiles = stage(prettyFiles, root_dir)
log.debug('Staged files', stagedFiles)
}
}
58 changes: 36 additions & 22 deletions src/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const log = require('./log.js')

const { readFile, writeFile } = require('./files.js')

function prettify(cwd, files) {
function prettify(cwd, files, check = false) {
// Prettier setup
const prettierConfig = path.join(cwd, 'config', 'prettier.config.js')
log.debug('prettierConfig', prettierConfig)
Expand All @@ -25,37 +25,51 @@ function prettify(cwd, files) {
editorconfig: false,
config: prettierConfig,
})
if (text.startsWith('#!')) {
const firstNL = text.indexOf('\n')
const hashbang = text.slice(0, firstNL + 1)
const rest = text.slice(firstNL, -1)
const restFormatted = prettier.format(rest, {


if (check) {
const checked = prettier.check(text, {
...options,
filepath: file,
})
formatted = hashbang.concat(restFormatted)

if (!checked) {
log.info(`... ${file}`)
}
} else {
formatted = prettier.format(text, {
...options,
filepath: file,
})
if (text.startsWith('#!')) {
const firstNL = text.indexOf('\n')
const hashbang = text.slice(0, firstNL + 1)
const rest = text.slice(firstNL, -1)
const restFormatted = prettier.format(rest, {
...options,
filepath: file,
})
formatted = hashbang.concat(restFormatted)
} else {
formatted = prettier.format(text, {
...options,
filepath: file,
})
}

if (formatted !== text) {
const success = writeFile(file, formatted)
success
? log.debug('file written to disk')
: log.debug('file write FAILED')
} else {
log.debug('Input/output identical, skipping.', file)
return null
}

log.info(`Reformatted: ${file}`)
}
} catch (error) {
log.error('Formatting failed.', file, error)
return null
}

if (formatted !== text) {
const success = writeFile(file, formatted)
success
? log.debug('file written to disk')
: log.debug('file write FAILED')
} else {
log.debug('Input/output identical, skipping.', file)
return null
}

log.info(`Reformatted: ${file}`)
return file
})
.filter(i => i)
Expand Down

0 comments on commit 4877cfe

Please sign in to comment.