From 5f4113060238d3b63159c3fe2a3d6f062f687856 Mon Sep 17 00:00:00 2001 From: JPBlancoDB Date: Sat, 9 Feb 2019 20:33:48 +0100 Subject: [PATCH] Add migrations options --- index.js | 13 ++++++++++--- src/migrations/index.js | 20 ++++++++++++++++++++ test/migrations.spec.js | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/migrations/index.js create mode 100644 test/migrations.spec.js diff --git a/index.js b/index.js index 7532385..7477cfc 100755 --- a/index.js +++ b/index.js @@ -4,12 +4,19 @@ const program = require('commander'); const pkg = require('./package.json'); const project = require('./src/projects'); +const migrations = require('./src/migrations'); program .version(pkg.version, '-v, --version') .option('-p, --project [db]', 'Create new project') - .option('-m, --migration ', 'Create new migration file') - .parse(process.argv); + .option('-m, --migration ', 'Create new migration file'); + +program.command('*').action(function() { + console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' ')); + process.exit(1); +}); + +program.parse(process.argv); if (program.project) { project.create(program.project); @@ -17,7 +24,7 @@ if (program.project) { } if (program.migration) { - console.log(program.migration); + migrations.create(program.migration); process.exit(0); } diff --git a/src/migrations/index.js b/src/migrations/index.js new file mode 100644 index 0000000..103b197 --- /dev/null +++ b/src/migrations/index.js @@ -0,0 +1,20 @@ +'use strict'; + +const path = require('path'); +const templatePath = path.join(__dirname, '/../../templates/'); +const copy = require('../utils/copy'); + +const create = name => { + const file = path.join(templatePath, 'migration-default.yml'); + const snakeCase = name + .split(/(?=[A-Z])/) + .join('_') + .toLowerCase(); + const fileName = `${Date.now()}_${snakeCase}`; + + copy.file(file, fileName, 'migrations'); +}; + +module.exports = { + create +}; diff --git a/test/migrations.spec.js b/test/migrations.spec.js new file mode 100644 index 0000000..be7f5d4 --- /dev/null +++ b/test/migrations.spec.js @@ -0,0 +1,24 @@ +const sinon = require('sinon'); +const expect = require('chai').expect; +const path = require('path'); +const migrations = require('../src/migrations'); +const copy = require('../src/utils/copy'); + +describe('Migrations', function() { + afterEach(function() { + sinon.restore(); + }); + + it('should invoke copy.file with migration-default file', function() { + const fakePath = 'directory/migration-default.yml'; + const pathStub = sinon.stub(path, 'join').returns(fakePath); + const copyStub = sinon.stub(copy, 'file'); + const now = Date.now(); + sinon.useFakeTimers(now); + + migrations.create('CustomName'); + + expect(pathStub.calledOnceWithExactly(sinon.match.string, 'migration-default.yml')).to.be.true; + expect(copyStub.calledOnceWithExactly(fakePath, `${now}_custom_name`, 'migrations')).to.be.true; + }); +});