Skip to content

Commit

Permalink
Add migrations options
Browse files Browse the repository at this point in the history
  • Loading branch information
JPBlancoDB committed Feb 9, 2019
1 parent ab007b9 commit 5f41130
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Expand Up @@ -4,20 +4,27 @@
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 <name>', 'Create new migration file')
.parse(process.argv);
.option('-m, --migration <name>', '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);
process.exit(0);
}

if (program.migration) {
console.log(program.migration);
migrations.create(program.migration);
process.exit(0);
}

Expand Down
20 changes: 20 additions & 0 deletions 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
};
24 changes: 24 additions & 0 deletions 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;
});
});

0 comments on commit 5f41130

Please sign in to comment.