Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
feat(migrations): add new generator to make migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 20, 2016
1 parent 5ba8e12 commit 1c3a823
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
},
"dependencies": {
"change-case": "^2.3.0",
"co-fs-extra": "^1.1.0"
"co-fs-extra": "^1.1.0",
"inflect": "^0.3.0",
"pope": "^1.0.0"
},
"directories": {
"test": "test"
Expand Down
7 changes: 7 additions & 0 deletions providers/GeneratorProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class GeneratorProvider extends ServiceProvider {
this.app.bind('Adonis/Commands/Generate:Middleware', function () {
return require('../src/Generators').Middleware
})

/**
* binding a new command to ioc container, which will scaffold a migration
*/
this.app.bind('Adonis/Commands/Generate:Migration', function () {
return require('../src/Generators').Migration
})
}

}
Expand Down
56 changes: 56 additions & 0 deletions src/Generators/Migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* adonis-commands
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const utils = require('./helpers')
const Ioc = require('adonis-fold').Ioc
const i = require('inflect')
const schemaString = require('./strings').schema

let Middleware = exports = module.exports = {}

Middleware.description = 'Generate new migration schema file'
Middleware.signature = '{name:Name of the migration file} {--create?} {--table?}'

/**
* @description handle method that will be invoked by ace when
* this command is executed
* @method
* @param {Object} options
* @param {Object} flags
* @return {void}
* @public
*/
Middleware.handle = function * (options, flags) {
const Helpers = Ioc.use('Adonis/Src/Helpers')
const Ansi = Ioc.use('Adonis/Src/Ansi')
const name = `${utils.makeName(options.name, 'Schema')}`
const migrationsPath = Helpers.migrationsPath(`${name}.js`)
const tableName = i.pluralize(name.replace('Schema', '').toLowerCase())
let templateOptions = {up: '', down: ''}
if (flags.create) {
templateOptions.up = `this.create('${tableName}', function (table) {
table.increments()
table.timestamps()
table.softDeletes()
})`
templateOptions.down = `this.drop('${tableName}')`
}

if (flags.table) {
templateOptions.up = `this.table('${tableName}', function (table) {
})`
templateOptions.down = templateOptions.up
}

try {
const response = yield utils.generateBlueprint(schemaString, migrationsPath, name, 'migration', templateOptions)
Ansi.success(response)
} catch (e) {
Ansi.error(e.message)
}
}
8 changes: 5 additions & 3 deletions src/Generators/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const changeCase = require('change-case')
const fs = require('co-fs-extra')
const pope = require('pope')

/**
* @module helpers
Expand Down Expand Up @@ -64,14 +65,15 @@ helpers.makeControllerMethod = function (name) {
* @return {String}
* @throws error when file already exists
*/
helpers.generateBlueprint = function * (contents, filePath, name, entity) {
helpers.generateBlueprint = function * (contents, filePath, name, entity, options) {
options = options || {}
const exists = yield fs.exists(filePath)
if (exists) {
throw new Error(`I am afraid ${name}.js already exists and i cannot overwrite it`)
}

const nameRegex = new RegExp('{{name}}', 'g')
contents = contents.replace(nameRegex, name)
options.name = name
contents = pope(contents, options)
yield fs.outputFile(filePath, contents)
return `Created ${name}.js ${entity} successfully`
}
19 changes: 19 additions & 0 deletions src/Generators/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,22 @@ class {{name}} extends Lucid {
module.exports = {{name}}
`

strings.schema = `'use strict'
const Schema = use('Schema')
class {{name}} extends Schema {
up () {
{{up}}
}
down () {
{{down}}
}
}
module.exports = {{name}}
`
39 changes: 39 additions & 0 deletions test/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require('co-mocha')
const GeneratorHelpers = require('../src/Generators/helpers')
const ControllerGenerator = require('../src/Generators/Controller')
const CommandGenerator = require('../src/Generators/Command')
const MigrationGenerator = require('../src/Generators/Migration')
const MiddlewareGenerator = require('../src/Generators/Middleware')
const ModelGenerator = require('../src/Generators/Model')

Expand Down Expand Up @@ -252,4 +253,42 @@ describe('Generators', function () {
expect(globalError).to.match(/I am afraid User.js already exists/)
})
})

context('Migrations', function () {
const Helpers = {
migrationsPath: function (file) {
return path.join(__dirname, './migrations/', file)
}
}

class Schema {
}

before(function * () {
Ioc.bind('Adonis/Src/Helpers', function () {
return Helpers
})
Ioc.bind('Schema', function () {
return Schema
})
yield fs.emptydir(path.join(__dirname, './migrations'))
})

after(function * () {
yield fs.emptydir(path.join(__dirname, './migrations'))
})

it('should generate a new migration file using migration handle method', function * () {
yield MigrationGenerator.handle({name: 'User'}, {table: true})
expect(globalSuccess).to.match(/Created UserSchema\.js migration/)
const User = require(path.join(__dirname, './migrations/UserSchema.js'))
const user = new User()
expect(user instanceof Schema).to.equal(true)
})

it('should handle error silently and make use of console error method to print the error', function * () {
yield MigrationGenerator.handle({name: 'User'}, {})
expect(globalError).to.match(/I am afraid UserSchema.js already exists/)
})
})
})

0 comments on commit 1c3a823

Please sign in to comment.