#!/usr/bin/env node // vim: set ft=javascript: 'use strict' const program = require('commander') const mkdirp = require('mkdirp') const dotenv = require('dotenv') const path = require('path') const log = require('../lib/log') const pkg = require('../package.json') const registerCompiler = require('../lib/register-compiler') program .version(pkg.version) .option('-f, --state-file ', 'Set path to state file', '.migrate') .option('-s, --store ', 'Set the migrations store', path.join(__dirname, '..', 'lib', 'file-store')) .option('--migrations-dir ', 'Change the migrations directory name', 'migrations') .option('--compiler ', 'Use the given module to compile files') .option('-c, --chdir [dir]', 'Change the working directory', process.cwd()) .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 } } // 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) // Create migrations dir path const p = path.resolve(process.cwd(), program.migrationsDir) log('migrations dir', p) mkdirp.sync(p) // Call store init if (typeof store.init === 'function') { store.init(function (err) { if (err) return log.error(err) log('init') }) }