Skip to content

Commit

Permalink
feat: add option to not stage formatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Feb 7, 2019
1 parent 3a61532 commit 00d5bc7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
20 changes: 13 additions & 7 deletions src/cmds/js_cmds/apply.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { cwd, collectFiles, whitelisted } = require('../../files.js')
const { collectFiles, whitelisted } = require('../../files.js')
const log = require('@dhis2/cli-helpers-engine').reporter

const { apply_fmt } = require('../../prettier.js')
const { stage, staged } = require('../../git.js')
const { stage_files, staged_files } = require('../../git.js')

const configure = require('../../config.js')

Expand All @@ -12,24 +12,28 @@ exports.describe = 'Apply JS format.'

exports.builder = {
all: {
describe:
'Default behaviour is to only format files staged with Git, use this option to format all files.',
type: 'boolean',
default: 'false',
},
staged: {
stage: {
describe:
'By default the changed files are staged automatically, use `--no-stage` to avoid staging files automatically.',
type: 'boolean',
default: 'true',
},
}

exports.handler = argv => {
const { all } = argv
const { all, stage } = argv
const root_dir = process.cwd()

let codeFiles
if (all) {
codeFiles = collectFiles(root_dir).filter(whitelisted)
} else {
codeFiles = staged(root_dir).filter(whitelisted)
codeFiles = staged_files(root_dir).filter(whitelisted)
}

// debug information about the folders
Expand All @@ -44,6 +48,8 @@ exports.handler = argv => {

configure(root_dir)

const stagedFiles = stage(prettyFiles, root_dir)
log.debug('Staged files', stagedFiles)
if (stage) {
const stagedFiles = stage_files(prettyFiles, root_dir)
log.debug('Staged files', stagedFiles)
}
}
8 changes: 3 additions & 5 deletions src/cmds/js_cmds/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { cwd, collectFiles, whitelisted } = require('../../files.js')
const { collectFiles, whitelisted } = require('../../files.js')
const log = require('@dhis2/cli-helpers-engine').reporter

const { check_fmt } = require('../../prettier.js')
Expand All @@ -12,13 +12,11 @@ exports.describe = 'Check JS format.'

exports.builder = {
all: {
describe:
'Default behaviour is to only format files staged with Git, use this option to format all files.',
type: 'boolean',
default: 'false',
},
staged: {
type: 'boolean',
default: 'true',
},
}

exports.handler = argv => {
Expand Down
6 changes: 3 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')

const log = require('@dhis2/cli-helpers-engine').reporter

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

function wipe_prop_cfg(repo) {
const pkg_path = path.join(repo, 'package.json')
Expand Down Expand Up @@ -68,11 +68,11 @@ function configure(repo) {
// then fun stuff
const cfgs = [
[
path.join(cwd(), 'config', 'prettier.config.js'),
path.join(__dirname, '..', 'config', 'prettier.config.js'),
path.join(repo, '.prettierrc.js'),
],
[
path.join(cwd(), 'config', 'browserslist.config.rc'),
path.join(__dirname, '..', 'config', 'browserslist.config.rc'),
path.join(repo, '.browserslistrc'),
],
].map(cfg => copy(cfg[0], cfg[1]))
Expand Down
5 changes: 0 additions & 5 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ function writeFile(fp, content) {
}
}

function cwd() {
return path.join(__dirname, '..')
}

module.exports = {
collectFiles,
readFile,
writeFile,
whitelisted,
cwd,
}
6 changes: 2 additions & 4 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { execSync } = require('child_process')

const log = require('@dhis2/cli-helpers-engine').reporter

function stage(files, dir) {
exports.stage_files = function stage_files(files, dir) {
log.info(`Stage ${files.length} file(s).`)
return files.map(file => {
log.info(`Staging ${file}...`)
Expand All @@ -19,7 +19,7 @@ function stage(files, dir) {
})
}

function staged(dir) {
exports.staged_files = function staged_files(dir) {
const files = execSync('git diff --cached --name-only', {
cwd: dir,
encoding: 'utf8',
Expand All @@ -32,5 +32,3 @@ function staged(dir) {
log.info('No staged files found.')
return []
}

module.exports = { stage, staged }
19 changes: 15 additions & 4 deletions src/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ const path = require('path')

const log = require('@dhis2/cli-helpers-engine').reporter

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

exports.check_fmt = function check_prettier(files) {
const prettierConfig = path.join(cwd(), 'config', 'prettier.config.js')
const prettierConfig = path.join(
__dirname,
'..',
'config',
'prettier.config.js'
)
log.debug('Prettier configuration file', prettierConfig)

const not_pretty_files = []
for (const file of files) {
Expand Down Expand Up @@ -44,8 +50,13 @@ exports.check_fmt = function check_prettier(files) {

exports.apply_fmt = function apply_prettier(files) {
// Prettier setup
const prettierConfig = path.join(cwd(), 'config', 'prettier.config.js')
log.debug('prettierConfig', prettierConfig)
const prettierConfig = path.join(
__dirname,
'..',
'config',
'prettier.config.js'
)
log.debug('Prettier configuration file', prettierConfig)

const pretty_files = []
for (const file of files) {
Expand Down

0 comments on commit 00d5bc7

Please sign in to comment.