diff --git a/bin/flyway.js b/bin/flyway.js index 20e8211..d8b46c0 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -3,12 +3,11 @@ 'use strict'; const program = require('commander'); -const path = require('path'); -const spawn = require('child_process').spawn; -const fs = require('fs'); -const os = require('os'); const pkg = require('../package.json'); -const download = require('../lib/download'); +const exeCommand = require('../lib/exec').exeCommand +const path = require('path'); + + process.title = 'flyway'; program @@ -18,6 +17,7 @@ program console.log(' See Flyway\'s configuration options at https://flywaydb.org/documentation/commandline/'); }); + makeCommand('migrate', 'Migrates the schema to the latest version. Flyway will create the metadata table automatically if it doesn\'t exist.'); makeCommand('clean', 'Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas. The schemas are cleaned in the order specified by the schemas property.'); makeCommand('info', 'Prints the details and status information about all the migrations.'); @@ -40,66 +40,18 @@ function makeCommand(name, desc) { program .command(name) .description(desc) - .action(exeCommand); -} - -function configFlywayArgs(config) { - const flywayArgs = config.flywayArgs || {}; - const flywayArgsKeys = Object.keys(flywayArgs); - - return flywayArgsKeys.map(function(key) { - return `-${key}=${flywayArgs[key]}`; - }); -} - -function binIsFile(path) { - const stats = fs.statSync(path); - - return !!stats && stats.isFile(); + .action(cliExec); } -function exeCommand(cmd) { - if(!program.configfile) { - throw new Error('Config file option is required'); - } - - var config = require(path.resolve(program.configfile)); - - if (typeof config === 'function') { - config = config(); - } - - download.ensureArtifacts(config, function(err, flywayBin) { - const workingDir = process.cwd(); +function cliExec(cmd) { + if(!program.configfile) { + throw new Error('Config file option is required'); + } - if(err) { - throw new Error(err); - } + var config = require(path.resolve(program.configfile)); - // Ensure that the flywayBin is a file, helps with security risk of having - // shell true in the spawn call below - if (!binIsFile(flywayBin)) { - throw new Error('Flyway bin was not found at "' + flywayBin + '"'); - } - - const args = configFlywayArgs(config) - .concat([cmd._name]); - - // Fix problem with spaces on windows OS - // https://github.com/nodejs/node/issues/7367 - const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); - const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; - - const child = spawn(safeSpawnBin, args, { - env: Object.assign({}, process.env, config.env), - cwd: workingDir, - stdio: 'inherit', - windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 - shell: isWindowsAndHasSpace, - }); - - child.on('close', code => { - process.exit(code); - }); - }); + if (typeof config === 'function') { + config = config(); + } + return exeCommand(config, cmd).then(code => process.exit(code)); } diff --git a/lib/exec.js b/lib/exec.js new file mode 100644 index 0000000..af671d6 --- /dev/null +++ b/lib/exec.js @@ -0,0 +1,63 @@ +const download = require('./download'); +const spawn = require('child_process').spawn; +const fs = require('fs'); +const os = require('os'); +const { resolve } = require('path'); + +function configFlywayArgs(config) { + const flywayArgs = config.flywayArgs || {}; + const flywayArgsKeys = Object.keys(flywayArgs); + + return flywayArgsKeys.map(function(key) { + return `-${key}=${flywayArgs[key]}`; + }); +} + +function binIsFile(path) { + const stats = fs.statSync(path); + + return !!stats && stats.isFile(); +} + +function exeCommand(config, cmd) { + return new Promise((resolve, _) => { + download.ensureArtifacts(config, function(err, flywayBin) { + const workingDir = process.cwd(); + + if(err) { + throw new Error(err); + } + + // Ensure that the flywayBin is a file, helps with security risk of having + // shell true in the spawn call below + if (!binIsFile(flywayBin)) { + throw new Error('Flyway bin was not found at "' + flywayBin + '"'); + } + + const args = configFlywayArgs(config) + .concat([cmd._name]); + + // Fix problem with spaces on windows OS + // https://github.com/nodejs/node/issues/7367 + const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); + const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; + + const child = spawn(safeSpawnBin, args, { + env: Object.assign({}, process.env, config.env), + cwd: workingDir, + stdio: 'inherit', + windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 + shell: isWindowsAndHasSpace, + }); + + child.on('close', code => { + resolve(code) + }); + } + )}); +}; + +module.exports = { + exeCommand: function(config, cmd) { return exeCommand(config, cmd) }, + migrate: function(config) { return exeCommand(config, { _name: 'migrate' } )} +}; \ No newline at end of file