Skip to content

Commit 00d5bc7

Browse files
committed
feat: add option to not stage formatted files
1 parent 3a61532 commit 00d5bc7

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

src/cmds/js_cmds/apply.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { cwd, collectFiles, whitelisted } = require('../../files.js')
1+
const { collectFiles, whitelisted } = require('../../files.js')
22
const log = require('@dhis2/cli-helpers-engine').reporter
33

44
const { apply_fmt } = require('../../prettier.js')
5-
const { stage, staged } = require('../../git.js')
5+
const { stage_files, staged_files } = require('../../git.js')
66

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

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

1313
exports.builder = {
1414
all: {
15+
describe:
16+
'Default behaviour is to only format files staged with Git, use this option to format all files.',
1517
type: 'boolean',
1618
default: 'false',
1719
},
18-
staged: {
20+
stage: {
21+
describe:
22+
'By default the changed files are staged automatically, use `--no-stage` to avoid staging files automatically.',
1923
type: 'boolean',
2024
default: 'true',
2125
},
2226
}
2327

2428
exports.handler = argv => {
25-
const { all } = argv
29+
const { all, stage } = argv
2630
const root_dir = process.cwd()
2731

2832
let codeFiles
2933
if (all) {
3034
codeFiles = collectFiles(root_dir).filter(whitelisted)
3135
} else {
32-
codeFiles = staged(root_dir).filter(whitelisted)
36+
codeFiles = staged_files(root_dir).filter(whitelisted)
3337
}
3438

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

4549
configure(root_dir)
4650

47-
const stagedFiles = stage(prettyFiles, root_dir)
48-
log.debug('Staged files', stagedFiles)
51+
if (stage) {
52+
const stagedFiles = stage_files(prettyFiles, root_dir)
53+
log.debug('Staged files', stagedFiles)
54+
}
4955
}

src/cmds/js_cmds/check.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { cwd, collectFiles, whitelisted } = require('../../files.js')
1+
const { collectFiles, whitelisted } = require('../../files.js')
22
const log = require('@dhis2/cli-helpers-engine').reporter
33

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

1313
exports.builder = {
1414
all: {
15+
describe:
16+
'Default behaviour is to only format files staged with Git, use this option to format all files.',
1517
type: 'boolean',
1618
default: 'false',
1719
},
18-
staged: {
19-
type: 'boolean',
20-
default: 'true',
21-
},
2220
}
2321

2422
exports.handler = argv => {

src/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs')
33

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

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

88
function wipe_prop_cfg(repo) {
99
const pkg_path = path.join(repo, 'package.json')
@@ -68,11 +68,11 @@ function configure(repo) {
6868
// then fun stuff
6969
const cfgs = [
7070
[
71-
path.join(cwd(), 'config', 'prettier.config.js'),
71+
path.join(__dirname, '..', 'config', 'prettier.config.js'),
7272
path.join(repo, '.prettierrc.js'),
7373
],
7474
[
75-
path.join(cwd(), 'config', 'browserslist.config.rc'),
75+
path.join(__dirname, '..', 'config', 'browserslist.config.rc'),
7676
path.join(repo, '.browserslistrc'),
7777
],
7878
].map(cfg => copy(cfg[0], cfg[1]))

src/files.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,9 @@ function writeFile(fp, content) {
4747
}
4848
}
4949

50-
function cwd() {
51-
return path.join(__dirname, '..')
52-
}
53-
5450
module.exports = {
5551
collectFiles,
5652
readFile,
5753
writeFile,
5854
whitelisted,
59-
cwd,
6055
}

src/git.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { execSync } = require('child_process')
66

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

9-
function stage(files, dir) {
9+
exports.stage_files = function stage_files(files, dir) {
1010
log.info(`Stage ${files.length} file(s).`)
1111
return files.map(file => {
1212
log.info(`Staging ${file}...`)
@@ -19,7 +19,7 @@ function stage(files, dir) {
1919
})
2020
}
2121

22-
function staged(dir) {
22+
exports.staged_files = function staged_files(dir) {
2323
const files = execSync('git diff --cached --name-only', {
2424
cwd: dir,
2525
encoding: 'utf8',
@@ -32,5 +32,3 @@ function staged(dir) {
3232
log.info('No staged files found.')
3333
return []
3434
}
35-
36-
module.exports = { stage, staged }

src/prettier.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ const path = require('path')
33

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

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

88
exports.check_fmt = function check_prettier(files) {
9-
const prettierConfig = path.join(cwd(), 'config', 'prettier.config.js')
9+
const prettierConfig = path.join(
10+
__dirname,
11+
'..',
12+
'config',
13+
'prettier.config.js'
14+
)
15+
log.debug('Prettier configuration file', prettierConfig)
1016

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

4551
exports.apply_fmt = function apply_prettier(files) {
4652
// Prettier setup
47-
const prettierConfig = path.join(cwd(), 'config', 'prettier.config.js')
48-
log.debug('prettierConfig', prettierConfig)
53+
const prettierConfig = path.join(
54+
__dirname,
55+
'..',
56+
'config',
57+
'prettier.config.js'
58+
)
59+
log.debug('Prettier configuration file', prettierConfig)
4960

5061
const pretty_files = []
5162
for (const file of files) {

0 commit comments

Comments
 (0)