Skip to content

Commit 956ae30

Browse files
committed
feat: pass config file in CLI options
BREAKING CHANGE: wrong parameters in config files now throw errors
1 parent 17daa6b commit 956ae30

File tree

11 files changed

+74
-44
lines changed

11 files changed

+74
-44
lines changed

__tests__/core.test.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ describe('testing core functionalities', () => {
1818
expect(defaults.integration).toEqual(1)
1919
})
2020

21-
test('load default values if no config files - part I', () => {
22-
const defaults = core.loadConfigFile()
23-
24-
expect(defaults.main).toMatch('master')
25-
expect(defaults.usedev).not.toBeTruthy()
26-
expect(defaults.integration).toEqual(1)
21+
test('throw if no config file is found', () => {
22+
expect(() => core.loadConfigFile()).toThrow(/^Cannot load/)
2723
})
2824

29-
test('load default values if no config files - part II', () => {
25+
test('load default values if no default config files are found', () => {
3026
const defaults = core.loadConfigValues()
3127

3228
expect(defaults.main).toMatch('master')
@@ -66,10 +62,7 @@ describe('testing core functionalities', () => {
6662
}`
6763
).to(tempConfigFile)
6864

69-
const defaults = core.loadConfigFile(tempConfigFile)
70-
71-
expect(defaults).toBeDefined()
72-
expect(defaults.development).toMatch('develop')
65+
expect(() => core.loadConfigFile(tempConfigFile)).toThrow(/^development/)
7366
})
7467

7568
test('write to (and load from) .js file', () => {
@@ -95,27 +88,35 @@ describe('testing core functionalities', () => {
9588
})
9689

9790
test('config value for a branch name is invalid', () => {
98-
core.writeConfigFile({
99-
data: { development: 'akn//&&svn...#k/' }
100-
})
91+
expect(() =>
92+
core.writeConfigFile({
93+
data: { development: 'akn//&&svn...#k/' }
94+
})
95+
).toThrow(/^development/)
10196
})
10297

10398
test('config value usedev is invalid', () => {
104-
core.writeConfigFile({
105-
data: { usedev: 'true' }
106-
})
99+
expect(() =>
100+
core.writeConfigFile({
101+
data: { usedev: 'true' }
102+
})
103+
).toThrow(/^usedev/)
107104
})
108105

109106
test('config value push is invalid', () => {
110-
core.writeConfigFile({
111-
data: { push: 'maybe' }
112-
})
107+
expect(() =>
108+
core.writeConfigFile({
109+
data: { push: 'maybe' }
110+
})
111+
).toThrow(/^push/)
113112
})
114113

115114
test('config value integration is invalid', () => {
116-
core.writeConfigFile({
117-
data: { integration: 4 }
118-
})
115+
expect(() =>
116+
core.writeConfigFile({
117+
data: { integration: 4 }
118+
})
119+
).toThrow(/^integration/)
119120
})
120121
})
121122

src/cli.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ const argv = yargs
3131
.command(new Feature())
3232
.command(new Release())
3333
.command(new Hotfix())
34+
.option('c', {
35+
alias: 'config',
36+
type: 'string',
37+
description: 'Config file to use'
38+
})
3439
.help()
3540
.alias('h', 'help').argv
3641

37-
if (argv._.length <= 0) {
42+
if (argv._.length <= 0)
3843
console.log(warning(`Try ${basename(process.argv[1])} --help`))
39-
}

src/cmds/feature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export class Feature implements CommandModule {
1818
return yargs.command(new StartFeature()).command(new FinishFeature())
1919
}
2020

21-
public handler = (): void => {}
21+
public handler = () => {}
2222
}

src/cmds/feature/finish.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { spawnSync } from 'child_process'
99
import { prompt } from 'inquirer'
1010
import { exec } from 'shelljs'
1111
import { Arguments, Argv, CommandModule } from 'yargs'
12-
import { isValidBranchName } from '../../core'
12+
import { isValidBranchName, loadConfigFile } from '../../core'
1313

1414
export class FinishFeature implements CommandModule {
1515
public command: string = 'finish <featureBranch> [options]'
@@ -25,6 +25,8 @@ export class FinishFeature implements CommandModule {
2525
}
2626

2727
public handler = (argv: Arguments) => {
28+
if (argv.c) loadConfigFile(argv.c as string)
29+
2830
const mergeInto = argv.usedev ? argv.development : argv.main
2931
if (isValidBranchName(mergeInto)) return handleFinish(argv, mergeInto)
3032
}

src/cmds/feature/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
*/
77
import { exec } from 'shelljs'
88
import { Arguments, CommandModule } from 'yargs'
9-
import { isValidBranchName } from '../../core'
9+
import { isValidBranchName, loadConfigFile } from '../../core'
1010

1111
export class StartFeature implements CommandModule {
1212
public command: string = 'start <featureBranch>'
1313

1414
public describe: string = 'Start a new feature'
1515

1616
public handler = (argv: Arguments) => {
17+
if (argv.c) loadConfigFile(argv.c as string)
18+
1719
const branchOff = argv.usedev ? argv.development : argv.main
1820

1921
if (

src/cmds/hotfix/finish.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { prompt } from 'inquirer'
99
import { exec } from 'shelljs'
1010
import { Arguments, CommandModule } from 'yargs'
1111
import { info } from '../../utils/text'
12+
import { loadConfigFile } from '../../core'
1213

1314
export class FinishHotfix implements CommandModule {
1415
public command: string = 'finish <hotfixName>'
1516

1617
public describe: string = 'Finishes a hotfix.'
1718

1819
public handler = (argv: Arguments): Promise<void> => {
20+
if (argv.c) loadConfigFile(argv.c as string)
21+
1922
return handleFinish(argv)
2023
}
2124
}

src/cmds/hotfix/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { exec } from 'shelljs'
99
import { Arguments, CommandModule } from 'yargs'
10-
import { isValidBranchName } from '../../core'
10+
import { isValidBranchName, loadConfigFile } from '../../core'
1111

1212
export class StartHotfix implements CommandModule {
1313
public command: string = 'start <hotfixName> <from>'
@@ -17,6 +17,8 @@ export class StartHotfix implements CommandModule {
1717
<from> should be a branch (e.g. develop), a tag (e.g. 2.3.0) or a commit (e.g. 9af345)`
1818

1919
public handler = (argv: Arguments): void => {
20+
if (argv.c) loadConfigFile(argv.c as string)
21+
2022
if (
2123
isValidBranchName(argv.hotfixName) &&
2224
(argv.from ? isValidBranchName(argv.from) : true)

src/cmds/release/finish.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { prompt } from 'inquirer'
99
import { exec } from 'shelljs'
1010
import { Arguments, CommandModule } from 'yargs'
1111
import { info } from '../../utils/text'
12+
import { loadConfigFile } from '../../core'
1213

1314
export class FinishRelease implements CommandModule {
1415
public command: string = 'finish <releaseName>'
@@ -21,6 +22,8 @@ export class FinishRelease implements CommandModule {
2122
}
2223

2324
const handleFinish = async (argv: Arguments) => {
25+
if (argv.c) loadConfigFile(argv.c as string)
26+
2427
const mergeInto = argv.usedev ? argv.development : argv.main
2528

2629
exec(`git checkout ${argv.release}/${argv.releaseName}`)

src/cmds/release/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { exec } from 'shelljs'
99
import { Arguments, CommandModule } from 'yargs'
10-
import { isValidBranchName } from '../../core'
10+
import { isValidBranchName, loadConfigFile } from '../../core'
1111

1212
export class StartRelease implements CommandModule {
1313
public command: string = 'start <releaseName> <from>'
@@ -17,6 +17,8 @@ export class StartRelease implements CommandModule {
1717
<from> should be a branch (e.g. develop) or a commit (e.g. 9af345)`
1818

1919
public handler = (argv: Arguments): void => {
20+
if (argv.c) loadConfigFile(argv.c as string)
21+
2022
if (
2123
isValidBranchName(argv.releaseName) &&
2224
(argv.from ? isValidBranchName(argv.from) : true)

src/core.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ export const getDefaultConfigValues = (): ConfigValues => {
5050
*/
5151
export const loadConfigFile = (configFile?: string): ConfigValues => {
5252
if (!configFile || !test('-f', configFile)) {
53-
return defaultConfigValues
53+
throw new Error(`Cannot load configuration values from: ${configFile}`)
5454
}
5555

5656
const configValues =
5757
getFileExt(configFile) === '.js'
5858
? require(configFile)
5959
: JSON.parse(sed(/(\/\*[\w\W]+\*\/|(\/\/.*))/g, '', configFile))
6060

61-
if (sanityCheck(configValues)) {
62-
return { ...defaultConfigValues, ...configValues }
63-
} else {
64-
return { ...defaultConfigValues }
65-
}
61+
sanityCheck(configValues)
62+
return { ...defaultConfigValues, ...configValues }
6663
}
6764

6865
/**
@@ -73,6 +70,8 @@ export const loadConfigFile = (configFile?: string): ConfigValues => {
7370
export const loadConfigValues = (): ConfigValues => {
7471
const configFile = findUp.sync(defaultConfigFileNames) || undefined
7572

73+
if (!configFile) return defaultConfigValues
74+
7675
return loadConfigFile(configFile)
7776
}
7877

@@ -92,7 +91,7 @@ export const writeConfigFile = ({
9291
}): boolean => {
9392
let toWrite: string
9493

95-
if (!sanityCheck(data)) return false
94+
sanityCheck(data)
9695

9796
switch (getFileExt(file)) {
9897
case '.js':
@@ -139,17 +138,23 @@ const sanityCheck = (configValues: ConfigValues): boolean => {
139138
case 'release':
140139
case 'feature':
141140
if (!isValidBranchName(element)) {
142-
return false
141+
throw new Error(
142+
`${key} branch name is invalid. Value found: ${element}`
143+
)
143144
}
144145
break
145146
case 'usedev':
146147
if (typeof element !== 'boolean') {
147-
return false
148+
throw new Error(
149+
`${key} has to be either 'true' or 'false'. Value found: ${element}`
150+
)
148151
}
149152
break
150153
case 'integration':
151154
if (typeof element !== 'number' || (element < 1 || element > 3)) {
152-
return false
155+
throw new Error(
156+
`${key} has to be a number >=1 and <=3. Value found: ${element}`
157+
)
153158
}
154159
break
155160
case 'interactive':
@@ -159,12 +164,16 @@ const sanityCheck = (configValues: ConfigValues): boolean => {
159164
typeof element !== 'string' ||
160165
!element.match(/(ask|always|never)/)
161166
) {
162-
return false
167+
throw new Error(
168+
`${key} has to be either 'ask', 'always' or 'never'. Value found: ${element}`
169+
)
163170
}
164171
break
165172
case 'tags':
166173
if (typeof element !== 'boolean') {
167-
return false
174+
throw new Error(
175+
`${key} has to be either 'true' or 'false'. Value found: ${element}`
176+
)
168177
}
169178
break
170179
}
@@ -200,7 +209,7 @@ const defaultConfigFileName: string = 'gof.config.js'
200209
const defaultConfigFileNames: string[] = [
201210
defaultConfigFileName,
202211
'.gofrc.js',
203-
'.gofrc.json'
212+
'.gofrc'
204213
]
205214

206215
// const supportedExtensions = ['.js', '.json']

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
// Search under node_modules for non-relative imports.
66
"moduleResolution": "node",
77
// Process & infer types from .js files.
8-
"allowJs": true,
8+
// "allowJs": true,
99
// Don't emit; allow Babel to transform files.
1010
"noEmit": true,
1111
// Enable strictest settings like strictNullChecks & noImplicitAny.
1212
"strict": true,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
1315
// Disallow features that require cross-file information for emit.
1416
// "isolatedModules": true,
1517
// Import non-ES modules as default imports.

0 commit comments

Comments
 (0)