Skip to content

Commit

Permalink
Merged clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 29, 2015
2 parents 1df14a7 + 9e53e00 commit 3499ba3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
37 changes: 24 additions & 13 deletions src/Commands/Make.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ let Make = exports = module.exports = {}
Make.description = 'Create a new migration file'
Make.signature = '{name}'

/**
* @description writes file with content to a given path
* @method writeFile
* @param {String} migrationPath
* @param {String} migrationContent
* @return {Object}
* @public
*/
Make.writeFile = function (migrationPath, migrationContent) {
return new Promise((resolve, reject) => {
fs.writeFile(migrationPath, migrationContent, function (error) {
if (error) {
return reject(error)
}
resolve()
})
})
}

/**
* @description creates a new migration file
* @method handle
Expand All @@ -40,21 +59,13 @@ Make.signature = '{name}'
* @return {Object}
* @public
*/
Make.handle = function (options) {
Make.handle = function * (options) {
const helpers = Ioc.make('Adonis/Src/Helpers')
const Console = Ioc.use('Adonis/Src/Console')

return new Promise((resolve, reject) => {
const name = `${new Date().getTime()}_${options.name}.js`
const migrationPath = helpers.migrationsPath(name)
const name = `${new Date().getTime()}_${options.name}.js`
const migrationPath = helpers.migrationsPath(name)

fs.writeFile(migrationPath, migrationContent, function (error) {
if (error) {
reject(error)
} else {
Console.success(Console.icon('success') + ' created %s migration successfully', name)
resolve()
}
})
})
yield Make.writeFile(migrationPath, migrationContent)
Console.success(Console.icon('success') + ' created %s migration successfully', name)
}
2 changes: 2 additions & 0 deletions src/Runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class Runner {
return ''
})
.then((response) => {
this.knex.destroy()
const status = _.size(migrated) > 0 ? 'completed' : 'skipped'
resolve({migrated, status})
})
Expand All @@ -348,6 +349,7 @@ class Runner {
}
})
.then((response) => {
this.knex.destroy()
const status = _.size(migrated) > 0 ? 'completed' : 'skipped'
resolve({migrated, status})
})
Expand Down
40 changes: 23 additions & 17 deletions test/unit/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ describe('Commands', function () {
}
})

Make
.handle({name: 'create_users_table'})
.catch(function (error) {
expect(error.code).to.equal('ENOENT')
done()
})
co(function *() {
yield Make.handle({name: 'create_users_table'})
})
.then(function (){

})
.catch(function(error) {
expect(error.code).to.equal('ENOENT')
done()
})
})

it('should create a file inside migrations directory', function (done) {
Expand All @@ -101,18 +105,20 @@ describe('Commands', function () {
return Helpers
})

Make
.handle({name: 'create_users_table'})
.then(function () {
fs.ensureFile(migName, function (err) {
if (err) {
done(err)
} else {
done()
}
})
co(function * () {
yield Make.handle({name: 'create_users_table'})
})
.then(function () {
fs.ensureFile(migName, function (err) {
if (err) {
done(err)
} else {
done()
}
})
.catch(done)
})
.catch(done)

})
})

Expand Down

0 comments on commit 3499ba3

Please sign in to comment.