Skip to content

Commit

Permalink
Closes #121 - Use template file extention as default extention
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-elminda authored and wesleytodd committed Apr 20, 2018
1 parent 00c201d commit 1207f82
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bin/migrate-create
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ program
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('-d, --date-format [format]', 'Set a date format to use')
.option('-t, --template-file [filePath]', 'Set path to template file to use for new migrations', path.join(__dirname, '..', 'lib', 'template.js'))
.option('-e, --extension [extension]', 'Use the given extension to create the file', '.js')
.option('-e, --extension [extension]', 'Use the given extension to create the file')
.option('--extention [extension]', '. Use the given extension to create the file. Deprecated as of v1.2. Replace with -e or --extension.')
.option('-g, --generator <name>', 'A template generator function', path.join(__dirname, '..', 'lib', 'template-generator'))
.option('--env [name]', 'Use dotenv to load an environment file')
Expand Down Expand Up @@ -59,7 +59,7 @@ function create (name) {
dateFormat: program.dateFormat,
templateFile: program.templateFile,
migrationsDirectory: program.migrationsDir,
extension: program.extension
extension: program.extension || path.extname(program.templateFile)
}, function (err, p) {
if (err) {
log.error('Template generation error', err.message)
Expand Down
2 changes: 1 addition & 1 deletion lib/template-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function templateGenerator (opts, cb) {
var dateFormat = opts.dateFormat
var templateFile = opts.templateFile || path.join(__dirname, 'template.js')
var migrationsDirectory = opts.migrationsDirectory || 'migrations'
var extension = opts.extension || '.js'
var extension = opts.extension

loadTemplate(templateFile, function (err, template) {
if (err) return cb(err)
Expand Down
16 changes: 16 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ describe('$ migrate', function () {
})
})

it('should default the extension to the template file extension', function (done) {
const name = 'test'
const fmt = 'yyyy-mm-dd'
const ext = '.mjs'
const now = formatDate(new Date(), fmt)

create([name, '-d', fmt, '-t', path.join(__dirname, 'util', 'tmpl' + ext)], function (err, out, code) {
assert(!err)
assert.equal(code, 0)
assert.doesNotThrow(() => {
fs.accessSync(path.join(TMP_DIR, 'migrations', now + '-' + name + ext))
})
done()
})
})

it('should use the --template-file flag', function (done) {
create(['test', '-t', path.join(__dirname, 'util', 'tmpl.js')], function (err, out, code) {
assert(!err)
Expand Down
10 changes: 10 additions & 0 deletions test/util/tmpl.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'
export const description = 'test'

export const up = function (next) {
next()
}

export const down = function (next) {
next()
}

0 comments on commit 1207f82

Please sign in to comment.