#!/usr/bin/env node // vim: set ft=javascript: 'use strict' const program = require('commander') const path = require('path') const minimatch = require('minimatch') const dotenv = require('dotenv') const migrate = require('../') const runMigrations = require('../lib/migrate') const log = require('../lib/log') const registerCompiler = require('../lib/register-compiler') const pkg = require('../package.json') program .version(pkg.version) .usage('[options] ') .option('-c, --chdir ', 'Change the working directory', process.cwd()) .option('-f, --state-file ', 'Set path to state file', '.migrate') .option('-s, --store ', 'Set the migrations store', path.join(__dirname, '..', 'lib', 'file-store')) .option('--clean', 'Tears down the migration state before running up') .option('-F, --force', 'Force through the command, ignoring warnings') .option('--init', 'Runs init for the store') .option('--migrations-dir ', 'Change the migrations directory name', 'migrations') .option('--matches ', 'A glob pattern to filter migration files', '*') .option('--compiler ', 'Use the given module to compile files') .option('--env [name]', 'Use dotenv to load an environment file') .parse(process.argv) // Change the working dir process.chdir(program.chdir) // Setup environment if (program.env) { const e = dotenv.config({ path: typeof program.env === 'string' ? program.env : '.env' }) if (e && e.error instanceof Error) { throw e.error } } // Check clean flag, exit if NODE_ENV === 'production' and force not specified if (program.clean && process.env.NODE_ENV === 'production' && !program.force) { log.error('error', 'Cowardly refusing to clean while node environment set to production, use --force to continue.') process.exit(1) } // Check init flag, exit if NODE_ENV === 'production' and force not specified if (program.init && process.env.NODE_ENV === 'production' && !program.force) { log.error('error', 'Cowardly refusing to init while node environment set to production, use --force to continue.') process.exit(1) } // Load compiler if (program.compiler) { registerCompiler(program.compiler) } // Setup store if (program.store[0] === '.') program.store = path.join(process.cwd(), program.store) const StoreImport = require(program.store) const Store = StoreImport.default || StoreImport const store = new Store(program.stateFile) // Call store init if (program.init && typeof store.init === 'function') { store.init(function (err) { if (err) return log.error(err) loadAndGo() }) } else { loadAndGo() } // Load in migrations function loadAndGo () { migrate.load({ stateStore: store, migrationsDirectory: program.migrationsDir, filterFunction: minimatch.filter(program.matches), ignoreMissing: program.force }, function (err, set) { if (err) { log.error('error', err) process.exit(1) } set.on('warning', function (msg) { log('warning', msg) }) set.on('migration', function (migration, direction) { log(direction, migration.title) }) // Run ;(program.clean ? cleanUp : up)(set, function (err) { if (err) { log.error('error', err) process.exit(1) } log('migration', 'complete') process.exit(0) }) }) } function cleanUp (set, fn) { runMigrations(set, 'down', null, function (err) { if (err) { return fn(err) } up(set, fn) }) } function up (set, fn) { runMigrations(set, 'up', program.args[0], function (err) { if (err) { return fn(err) } fn() }) }