Skip to content

Commit

Permalink
Throw when config already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleavely committed Jun 1, 2020
1 parent 0123647 commit 95b6d42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const { getConfig, getSampleConfig } = require('./config')
const { getSampleMigration, runPendingMigrations } = require('./migrations')
const { mkdir, writeFile } = require('./utils/fs')
const {
findConfig,
getConfig,
getSampleConfig,
} = require('./config')
const {
getSampleMigration,
runPendingMigrations,
} = require('./migrations')
const {
mkdir,
writeFile,
} = require('./utils/fs')
const path = require('path')
const slugify = require('slugify')

Expand All @@ -10,8 +20,18 @@ exports.getConfig = getConfig
* Create a sample configuration in the supplied path
*/
exports.init = async (targetPath) => {
const existingConfigPath = await findConfig()
.catch(err => {
if (err.code === 'NOCONFIG') return false
throw err
})
const sampleConfig = await getSampleConfig()
await writeFile(targetPath, sampleConfig)
if (existingConfigPath) {
const err = new Error(`A configuration already exists in "${existingConfigPath}"`)
err.code = 'INITEXISTS'
throw err
}
return writeFile(targetPath, sampleConfig)
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ it('can be loaded without throwing exceptions', () => {
describe('init()', () => {
it('writes a configtemplate to the supplied path', async () => {
const targetPath = path.join(process.cwd(), 'exodus-init-test.js')
config.findConfig.mockRejectedValueOnce({ code: 'NOCONFIG' })
config.getSampleConfig.mockResolvedValueOnce('Hello world')

await main.init(targetPath)

expect(fs.writeFile).toHaveBeenCalledWith(targetPath, 'Hello world')
})
it('throws if a configuration already exists', async () => {
const targetPath = path.join(process.cwd(), 'exodus-init-test.js')
config.findConfig.mockResolvedValueOnce('/interwebz/exodus.config.js')

await expect(main.init(targetPath)).rejects.toThrow('already exists')
})
})

describe('create()', () => {
Expand Down

0 comments on commit 95b6d42

Please sign in to comment.