diff --git a/.npmignore b/.npmignore index f1250e5..8757baa 100644 --- a/.npmignore +++ b/.npmignore @@ -2,3 +2,5 @@ support test examples *.sock +.db +.migrate diff --git a/bin/migrate-create b/bin/migrate-create index 61dda0e..31ed414 100755 --- a/bin/migrate-create +++ b/bin/migrate-create @@ -4,6 +4,7 @@ var program = require('commander') var path = require('path') +var dotenv = require('dotenv') var log = require('../lib/log') var registerCompiler = require('../lib/register-compiler') var pkg = require('../package.json') @@ -17,10 +18,21 @@ program .option('-t, --template-file [filePath]', 'Set path to template file to use for new migrations', path.join(__dirname, '..', 'lib', 'template.js')) .option('-e, --extention [extention]', 'Use the given extention to create the file', '.js') .option('-g, --generator ', 'A template generator function', path.join(__dirname, '..', 'lib', 'template-generator')) + .option('--env [name]', 'Use dotenv to load an environment file') .arguments('') .action(create) .parse(process.argv) +// Setup environment +if (program.env) { + var e = dotenv.config({ + path: typeof program.env === 'string' ? program.env : '.env' + }) + if (e && e.error instanceof Error) { + throw e.error + } +} + var _name function create (name) { // Name provided? diff --git a/bin/migrate-down b/bin/migrate-down index 82ca467..7ced975 100755 --- a/bin/migrate-down +++ b/bin/migrate-down @@ -5,6 +5,7 @@ var program = require('commander') var path = require('path') var minimatch = require('minimatch') +var dotenv = require('dotenv') var migrate = require('../') var runMigrations = require('../lib/migrate') var log = require('../lib/log') @@ -20,11 +21,22 @@ program .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) { + var 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) diff --git a/bin/migrate-init b/bin/migrate-init index e809a1a..b36d5cd 100755 --- a/bin/migrate-init +++ b/bin/migrate-init @@ -4,6 +4,7 @@ var program = require('commander') var mkdirp = require('mkdirp') +var dotenv = require('dotenv') var path = require('path') var log = require('../lib/log') var pkg = require('../package.json') @@ -14,11 +15,22 @@ program .option('-s, --store ', 'Set the migrations store', path.join(__dirname, '..', 'lib', 'file-store')) .option('--migrations-dir ', 'Change the migrations directory name', 'migrations') .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) { + var e = dotenv.config({ + path: typeof program.env === 'string' ? program.env : '.env' + }) + if (e && e.error instanceof Error) { + throw e.error + } +} + // Setup store var Store = require(program.store) var store = new Store(program.stateFile) diff --git a/bin/migrate-list b/bin/migrate-list index 8088efd..a52eed6 100755 --- a/bin/migrate-list +++ b/bin/migrate-list @@ -6,6 +6,7 @@ var program = require('commander') var path = require('path') var dateFormat = require('dateformat') var minimatch = require('minimatch') +var dotenv = require('dotenv') var migrate = require('../') var log = require('../lib/log') var registerCompiler = require('../lib/register-compiler') @@ -21,6 +22,7 @@ program .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) // Check clean flag, exit if NODE_ENV === 'production' and force not specified @@ -32,6 +34,16 @@ if (program.clean && process.env.NODE_ENV === 'production' && !program.force) { // Change the working dir process.chdir(program.chdir) +// Setup environment +if (program.env) { + var 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) diff --git a/bin/migrate-up b/bin/migrate-up index fc333ef..71d67aa 100755 --- a/bin/migrate-up +++ b/bin/migrate-up @@ -5,6 +5,7 @@ var program = require('commander') var path = require('path') var minimatch = require('minimatch') +var dotenv = require('dotenv') var migrate = require('../') var runMigrations = require('../lib/migrate') var log = require('../lib/log') @@ -23,8 +24,22 @@ program .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) { + var 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.') @@ -37,9 +52,6 @@ if (program.init && process.env.NODE_ENV === 'production' && !program.force) { process.exit(1) } -// Change the working dir -process.chdir(program.chdir) - // Load compiler if (program.compiler) { registerCompiler(program.compiler) diff --git a/examples/env/.env b/examples/env/.env new file mode 100644 index 0000000..a71ce89 --- /dev/null +++ b/examples/env/.env @@ -0,0 +1 @@ +DB=contributors diff --git a/examples/env/.foo b/examples/env/.foo new file mode 100644 index 0000000..e9902e4 --- /dev/null +++ b/examples/env/.foo @@ -0,0 +1 @@ +DB=foo diff --git a/examples/env/README.md b/examples/env/README.md new file mode 100644 index 0000000..e91a95b --- /dev/null +++ b/examples/env/README.md @@ -0,0 +1,11 @@ +# Environment Example + +``` +$ migrate up --env +$ migrate down --env +$ cat .db # should see table of `contributors` + +$ migrate up --env .foo +$ migrate down --env .foo +$ cat .db # should see table of `foo` +``` diff --git a/examples/env/db.js b/examples/env/db.js new file mode 100644 index 0000000..b151444 --- /dev/null +++ b/examples/env/db.js @@ -0,0 +1,44 @@ +'use strict' +var fs = require('fs') + +module.exports = { + loaded: false, + tables: {}, + table: function (name) { + this.tables[name] = [] + this.save() + }, + removeTable: function (name) { + delete this.tables[name] + this.save() + }, + insert: function (table, value) { + this.tables[table].push(value) + this.save() + }, + remove: function (table, value) { + this.tables[table].splice(this.tables[table].indexOf(value), 1) + this.save() + }, + save: function () { + fs.writeFileSync('.db', JSON.stringify(this)) + }, + load: function () { + if (this.loaded) return this + var json + try { + json = JSON.parse(fs.readFileSync('.db', 'utf8')) + } catch (e) { + // ignore + return this + } + this.loaded = true + this.tables = json.tables + return this + }, + toJSON: function () { + return { + tables: this.tables + } + } +} diff --git a/package.json b/package.json index e50f14a..cb70d2e 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "chalk": "^1.1.3", "commander": "^2.9.0", "dateformat": "^2.0.0", + "dotenv": "^4.0.0", "inherits": "^2.0.3", "minimatch": "^3.0.3", "mkdirp": "^0.5.1", diff --git a/test/fixtures/env/env b/test/fixtures/env/env new file mode 100644 index 0000000..8a6696f --- /dev/null +++ b/test/fixtures/env/env @@ -0,0 +1 @@ +DB=pets diff --git a/test/integration.js b/test/integration.js index f4dff6d..2965e42 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1,20 +1,25 @@ -/* global describe, it, beforeEach */ +/* global describe, it, beforeEach, afterEach */ const path = require('path') const assert = require('assert') const rimraf = require('rimraf') const mkdirp = require('mkdirp') const run = require('./util/run') +const db = require('./util/db') // Paths const TMP_DIR = path.join(__dirname, 'fixtures', 'tmp') +const ENV_DIR = path.join(__dirname, 'fixtures', 'env') function reset () { + rimraf.sync(path.join(ENV_DIR, '.migrate')) rimraf.sync(TMP_DIR) mkdirp.sync(TMP_DIR) + db.nuke() } describe('integration tests', function () { beforeEach(reset) + afterEach(reset) it('should warn when the migrations are run out of order', function (done) { run.init(TMP_DIR, [], function (err, out, code) { @@ -94,4 +99,18 @@ describe('integration tests', function () { }) }) }) + + it('should load the enviroment file when passed --env', function (done) { + run.up(ENV_DIR, ['--env', 'env'], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + assert(out.indexOf('error') === -1) + run.down(ENV_DIR, ['--env', 'env'], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + assert(out.indexOf('error') === -1) + done() + }) + }) + }) })