Skip to content

Commit

Permalink
feat: tc init [--config config.json]
Browse files Browse the repository at this point in the history
  • Loading branch information
mokele committed Nov 5, 2020
1 parent 1466d5a commit f3cb36f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
node-version: 12
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm test

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ $ rain deploy ...
Some arguments can come before the proxied command e.g. `tc <here> aws ...`
* `tc init` create a `template-configuration/default.json` file, with optional
additional `--config` argument
* `--debug` echos out the command that is also ran
* `--dryrun` only echo out the command that would otherwise be ran – implies `--debug`
* `--config <configuration-file>` override `template-configuration/default.json` with another local file path
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"template-configuration": "src/bin.js"
},
"scripts": {
"lint": "standard",
"test": "jest"
},
"publishConfig": {
Expand Down
10 changes: 7 additions & 3 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ const main = (args) => {
// TODO is linux vs windows any different here?
const command = new TCCommand()
command.setLogger(console.log)
command.readConfiguration(file => JSON.parse(fs.readFileSync(file)))
command.readConfiguration(fs.readFileSync)
command.existsConfiguration(fs.existsSync)
command.writeConfiguration(fs.writeFileSync)
command.pushArguments(args)
const cmdArguments = command.getArguments()
if (!command.isDryRun()) {
if (command.isOwnCommand()) {
command.run()
} else if (!command.isDryRun()) {
const cmdArguments = command.getArguments()
childProcess.spawnSync('command', cmdArguments, { stdio: 'inherit' })
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ class TCCommand {
this.logger = console.error
this.loggingEnabled = false
this.dryRun = false
this.ownCommand = null
this.command = null
this.commandName = null
this.arguments = []
this.configurationFile = path.resolve('template-configuration', 'default.json')
this.readConfigurationFunction = f => {
throw new Error('No configuration read function defined')
}
this.existsConfigurationFunction = f => {
throw new Error('No configuration exists function defined')
}
this.writeConfigurationFunction = f => {
throw new Error('No configuration write function defined')
}
}

isDryRun () {
Expand All @@ -104,6 +111,14 @@ class TCCommand {
this.readConfigurationFunction = f
}

existsConfiguration (f) {
this.existsConfigurationFunction = f
}

writeConfiguration (f) {
this.writeConfigurationFunction = f
}

pushArguments (args) {
args.forEach(arg => this.pushArgument(arg))
}
Expand All @@ -124,6 +139,9 @@ class TCCommand {
this.configurationFile = path.resolve(arg)
delete this.nextArgumentConfig
return
} else if (arg === 'init') {
this.ownCommand = 'init'
return
}
}
this.arguments.push(arg)
Expand Down Expand Up @@ -174,7 +192,7 @@ class TCCommand {
}

getArguments () {
const { Parameters, Tags } = this.readConfigurationFunction(this.configurationFile)
const { Parameters, Tags } = JSON.parse(this.readConfigurationFunction(this.configurationFile))
const parameterType = this.getParameterType()
// TODO empty Parameters and Empty Tags
const tagArguments = parameterType
Expand All @@ -197,6 +215,32 @@ class TCCommand {
this.log(args.join(' '))
return args
}

isOwnCommand () {
return Boolean(this.ownCommand)
}

init () {
this.loggingEnabled = true // implicit

if (this.existsConfigurationFunction(this.configurationFile)) {
throw new Error(`${this.configurationFile} already exists - not overwriting`)
}

this.writeConfigurationFunction(this.configurationFile, JSON.stringify({
Parameters: {},
Tags: {},
StackPolicy: {
Statement: []
}
}, null, 2))

this.log(`written ${this.configurationFile}`)
}

run () {
this.init()
}
}

module.exports = {
Expand Down
67 changes: 63 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,74 @@ describe('no readConfiguration function', () => {
})

test('getArguments throws', () => {
expect(() => cmd.getArguments())
.toThrow()
expect(() => cmd.getArguments()).toThrow()
})
})

describe('init', () => {
beforeEach(() => {
cmd = new TCCommand()
cmd.pushArguments(['init'])
})

test('isOwnCommand', () => {
expect(cmd.isOwnCommand()).toBe(true)
})

test('without existsConfigurationFunction', () => {
expect(() => cmd.run()).toThrow()
})

describe('with existsConfigurationFunction => true', () => {
let writeConfig, existsConfig
beforeEach(() => {
writeConfig = jest.fn()
existsConfig = jest.fn().mockReturnValue(true)
cmd.existsConfiguration(existsConfig)
cmd.writeConfiguration(writeConfig)
})

test('throws file exist', () => {
expect(() => cmd.run()).toThrow()
expect(existsConfig).toHaveBeenCalled()
})

test('does not call writeConfiguration', () => {
expect(writeConfig).not.toHaveBeenCalled()
})
})

describe('with existsConfigurationFunction => false', () => {
let existsConfig
beforeEach(() => {
existsConfig = jest.fn().mockReturnValue(false)
cmd.existsConfiguration(existsConfig)
})

test('without writeConfigurationFunction', () => {
expect(() => cmd.run()).toThrow()
expect(existsConfig).toHaveBeenCalled()
})

describe('with writeConfigurationFile', () => {
let writeConfig
beforeEach(() => {
writeConfig = jest.fn()
cmd.writeConfiguration(writeConfig)
cmd.run()
})

test('calls writeConfig', () => {
expect(writeConfig).toHaveBeenCalled()
})
})
})
})

describe('with readConfigution function', () => {
let readConfig
beforeEach(() => {
readConfig = jest.fn().mockReturnValue({
readConfig = jest.fn().mockReturnValue(JSON.stringify({
Parameters: {
key1: 'value1',
key2: 'value2'
Expand All @@ -30,7 +89,7 @@ describe('with readConfigution function', () => {
tagKey1: 'tagValue1',
tagKey2: 'tagValue2'
}
})
}))
cmd.readConfiguration(readConfig)
})

Expand Down

0 comments on commit f3cb36f

Please sign in to comment.