Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multi env #208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// vim: set ft=javascript:
'use strict'

const program = require('commander')
const { Command } = require('commander')
const pkg = require('../package.json')

const program = new Command()
program
.version(pkg.version)
.command('init', 'Initalize the migrations tool in a project')
Expand Down
52 changes: 27 additions & 25 deletions bin/migrate-create
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// vim: set ft=javascript:
'use strict'

const program = require('commander')
const { Command } = require('commander')
const path = require('path')
const dotenv = require('dotenv')
const log = require('../lib/log')
const registerCompiler = require('../lib/register-compiler')
const pkg = require('../package.json')

const program = new Command()
program
.version(pkg.version)
.option('-c, --chdir [dir]', 'Change the working directory', process.cwd())
Expand All @@ -19,52 +20,53 @@ program
.option('-e, --extension [extension]', 'Use the given extension to create the file')
.option('--extention [extension]', '. Use the given extension to create the file. Deprecated as of v1.2. Replace with -e or --extension.')
.option('-g, --generator <name>', 'A template generator function', path.join(__dirname, '..', 'lib', 'template-generator'))
.option('--env [name]', 'Use dotenv to load an environment file')
.option('--env [name...]', 'Use dotenv to load an environment file')
.arguments('<name>')
.action(create)
.parse(process.argv)

if (program.extention) {
console.log('create', '"--extention" argument is deprecated. Use "--extension" instead')
}

// 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
}
}

// eslint-disable-next-line no-var
var _name
function create (name) {
const opts = program.opts()

if (opts.extention) {
console.log('create', '"--extention" argument is deprecated. Use "--extension" instead')
}

// Setup environment
if (opts.env) {
const e = dotenv.config({
path: opts.env
})
if (e && e.error instanceof Error) {
throw e.error
}
}
// Name provided?
_name = name

// Change the working dir
process.chdir(program.chdir)
process.chdir(opts.chdir)

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
if (opts.compiler) {
registerCompiler(opts.compiler)
}

// Load the template generator
let gen
try {
gen = require(program.generator)
gen = require(opts.generator)
} catch (e) {
gen = require(path.resolve(program.generator))
gen = require(path.resolve(opts.generator))
}
gen({
name,
dateFormat: program.dateFormat,
templateFile: program.templateFile,
migrationsDirectory: program.migrationsDir,
extension: program.extension || path.extname(program.templateFile)
dateFormat: opts.dateFormat,
templateFile: opts.templateFile,
migrationsDirectory: opts.migrationsDir,
extension: opts.extension || path.extname(opts.templateFile)
}, function (err, p) {
if (err) {
log.error('Template generation error', err.message)
Expand Down
28 changes: 15 additions & 13 deletions bin/migrate-down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// vim: set ft=javascript:
'use strict'

const program = require('commander')
const { Command } = require('commander')
const path = require('path')
const minimatch = require('minimatch')
const dotenv = require('dotenv')
Expand All @@ -12,6 +12,7 @@ const log = require('../lib/log')
const registerCompiler = require('../lib/register-compiler')
const pkg = require('../package.json')

const program = new Command()
program
.version(pkg.version)
.usage('[options] <name>')
Expand All @@ -21,41 +22,42 @@ program
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('--matches <glob>', 'A glob pattern to filter migration files', '*')
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('--env [name]', 'Use dotenv to load an environment file')
.option('--env [name...]', 'Use dotenv to load an environment file')
.option('-F, --force', 'Force through the command, ignoring warnings')
.parse(process.argv)
const opts = program.opts()

// Change the working dir
process.chdir(program.chdir)
process.chdir(opts.chdir)

// Setup environment
if (program.env) {
if (opts.env) {
const e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
path: opts.env
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
if (opts.compiler) {
registerCompiler(opts.compiler)
}

// Setup store
if (program.store[0] === '.') program.store = path.join(process.cwd(), program.store)
if (opts.store[0] === '.') opts.store = path.join(process.cwd(), opts.store)

const StoreImport = require(program.store)
const StoreImport = require(opts.store)
const Store = StoreImport.default || StoreImport
const store = new Store(program.stateFile)
const store = new Store(opts.stateFile)

// Load in migrations
migrate.load({
stateStore: store,
migrationsDirectory: program.migrationsDir,
filterFunction: minimatch.filter(program.matches),
ignoreMissing: program.force
migrationsDirectory: opts.migrationsDir,
filterFunction: minimatch.filter(opts.matches),
ignoreMissing: opts.force
}, function (err, set) {
if (err) {
log.error('error', err)
Expand Down
22 changes: 12 additions & 10 deletions bin/migrate-init
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// vim: set ft=javascript:
'use strict'

const program = require('commander')
const { Command } = 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')

const program = new Command()
program
.version(pkg.version)
.option('-f, --state-file <path>', 'Set path to state file', '.migrate')
Expand All @@ -19,34 +20,35 @@ program
.option('-c, --chdir [dir]', 'Change the working directory', process.cwd())
.option('--env [name]', 'Use dotenv to load an environment file')
.parse(process.argv)
const opts = program.opts()

// Change the working dir
process.chdir(program.chdir)
process.chdir(opts.chdir)

// Setup environment
if (program.env) {
if (opts.env) {
const e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
path: opts.env
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
if (opts.compiler) {
registerCompiler(opts.compiler)
}

// Setup store
if (program.store[0] === '.') program.store = path.join(process.cwd(), program.store)
if (opts.store[0] === '.') opts.store = path.join(process.cwd(), opts.store)

const StoreImport = require(program.store)
const StoreImport = require(opts.store)
const Store = StoreImport.default || StoreImport
const store = new Store(program.stateFile)
const store = new Store(opts.stateFile)

// Create migrations dir path
const p = path.resolve(process.cwd(), program.migrationsDir)
const p = path.resolve(process.cwd(), opts.migrationsDir)

log('migrations dir', p)
mkdirp.sync(p)
Expand Down
30 changes: 16 additions & 14 deletions bin/migrate-list
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// vim: set ft=javascript:
'use strict'

const program = require('commander')
const { Command } = require('commander')
const path = require('path')
const dateFormat = require('dateformat')
const minimatch = require('minimatch')
Expand All @@ -12,6 +12,7 @@ const log = require('../lib/log')
const registerCompiler = require('../lib/register-compiler')
const pkg = require('../package.json')

const program = new Command()
program
.version(pkg.version)
.usage('[options] <name>')
Expand All @@ -22,45 +23,46 @@ program
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('--matches <glob>', 'A glob pattern to filter migration files', '*')
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('--env [name]', 'Use dotenv to load an environment file')
.option('--env [name...]', 'Use dotenv to load an environment file')
.parse(process.argv)
const opts = program.opts()

// Check clean flag, exit if NODE_ENV === 'production' and force not specified
if (program.clean && process.env.NODE_ENV === 'production' && !program.force) {
if (opts.clean && process.env.NODE_ENV === 'production' && !opts.force) {
log.error('error', 'Cowardly refusing to clean while node environment set to production, use --force to continue.')
process.exit(1)
}

// Change the working dir
process.chdir(program.chdir)
process.chdir(opts.chdir)

// Setup environment
if (program.env) {
if (opts.env) {
const e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
path: opts.env
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
if (opts.compiler) {
registerCompiler(opts.compiler)
}

// Setup store
if (program.store[0] === '.') program.store = path.join(process.cwd(), program.store)
if (opts.store[0] === '.') opts.store = path.join(process.cwd(), opts.store)

const StoreImport = require(program.store)
const StoreImport = require(opts.store)
const Store = StoreImport.default || StoreImport
const store = new Store(program.stateFile)
const store = new Store(opts.stateFile)

// Load in migrations
migrate.load({
stateStore: store,
migrationsDirectory: program.migrationsDir,
filterFunction: minimatch.filter(program.matches)
migrationsDirectory: opts.migrationsDir,
filterFunction: minimatch.filter(opts.matches)
}, function (err, set) {
if (err) {
log.error('error', err)
Expand All @@ -72,7 +74,7 @@ migrate.load({
}

set.migrations.forEach(function (migration) {
log(migration.title + (migration.timestamp ? ' [' + dateFormat(migration.timestamp, program.dateFormat) + ']' : ' [not run]'), migration.description || '<No Description>')
log(migration.title + (migration.timestamp ? ' [' + dateFormat(migration.timestamp, opts.dateFormat) + ']' : ' [not run]'), migration.description || '<No Description>')
})

process.exit(0)
Expand Down
Loading