Skip to content

Commit

Permalink
feat(commands): add seed command
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 1, 2017
1 parent 33dece6 commit dd56409
Show file tree
Hide file tree
Showing 5 changed files with 2,005 additions and 270 deletions.
136 changes: 136 additions & 0 deletions commands/Seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const { Command } = require('@adonisjs/ace')
const requireAll = require('require-all')
const _ = require('lodash')
const { ioc } = require('@adonisjs/fold')

class SeedDatabase extends Command {
constructor (Helpers) {
super()
this._seedsPath = Helpers.seedsPath()
}

/**
* IoC container injections
*
* @method inject
*
* @return {Array}
*/
static get inject () {
return ['Adonis/Src/Helpers']
}

/**
* Returns an object of all schema files
*
* @method _getSeedFiles
*
* @return {Object}
*
* @private
*/
_getSeedFiles (selectedFiles) {
return requireAll({
dirname: this._seedsPath,
filters: /(.*)\.js$/,
filter: (fileName) => {
if (!selectedFiles) {
return fileName
}
return _.find(selectedFiles, (file) => file.trim().endsWith(fileName))
}
})
}

/**
* Throws exception when trying to run migrations are
* executed in production and not using force flag.
*
* @method _validateState
*
* @param {Boolean} force
*
* @return {void}
*
* @private
*
* @throws {Error} If NODE_ENV is production
*/
_validateState (force) {
if (process.env.NODE_ENV === 'production' && !force) {
throw new Error('Cannot run migrations in production. Use --force flag to continue')
}
}

/**
* Command signature required by ace
*
* @method signature
*
* @return {String}
*/
static get signature () {
return `
seed
{ -f, --force: Forcefully seed database in production }
{ --files=@value: Run only selected files }
`
}

/**
* Command description
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Seed database using seed files'
}

/**
* Method called when command is executed. This method will
* require all files from the migrations directory
* and execute all pending schema files
*
* @method handle
*
* @param {Object} args
* @param {Boolean} options.force
* @param {String} options.files
*
* @return {void|Array}
*/
async handle (args, { force, files }) {
this._validateState(force)

files = typeof (files) === 'string' ? files.split(',') : null
const allFiles = this._getSeedFiles(files)

if (!_.size(allFiles)) {
return this.viaAce ? this.info('Nothing to seed') : 'Nothing to seed'
}

for (const file of _.keys(allFiles)) {
const seedInstance = ioc.make(allFiles[file])
if (typeof (seedInstance.run) === 'function') {
await seedInstance.run()
} else {
this.warn(`${seedInstance.constructor.name} does not have a run method`)
}
}
}
}

module.exports = SeedDatabase
Loading

0 comments on commit dd56409

Please sign in to comment.