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

fix(.sequelizerc): options files is not used by all commands #537

Merged
merged 1 commit into from
Sep 10, 2017
Merged
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
19 changes: 19 additions & 0 deletions src/core/yargs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import fs from 'fs';
import yargs from 'yargs';
import path from 'path';

function loadRCFile(optionsPath) {
const rcFile = optionsPath || path.resolve(process.cwd(), '.sequelizerc');
const rcFileResolved = path.resolve(rcFile);
return fs.existsSync(rcFileResolved)
? JSON.parse(JSON.stringify(require(rcFileResolved)))
: {};
}

const args = yargs
.config(loadRCFile(yargs.argv.optionsPath));

export default function getYArgs () {
return args;
}

export function _baseOptions (yargs) {
return yargs
.option('env', {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/config-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import fs from 'fs';
import url from 'url';
import _ from 'lodash';
import helpers from './index';
import getYArgs from '../core/yargs';

const args = require('yargs').argv;
const args = getYArgs().argv;

const api = {
config: undefined,
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/generic-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path';

const resolve = require('resolve').sync;
const args = require('yargs').argv;
import getYArgs from '../core/yargs';

const args = getYArgs().argv;

const generic = {
getEnvironment: () => {
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/path-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import path from 'path';
import fs from 'fs';

const resolve = require('resolve').sync;
const args = require('yargs').argv;
import getYArgs from '../core/yargs';

const args = getYArgs().argv;

function format (i) {
return parseInt(i, 10) < 10 ? '0' + i : i;
Expand Down
16 changes: 4 additions & 12 deletions src/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

import yargs from 'yargs';
import fs from 'fs';
import path from 'path';
import getYArgs from './core/yargs';
import cliPackage from '../package';
import Promise from 'bluebird';
import { isEmpty } from 'lodash';

const yargs = getYArgs();

Promise.coroutine.addYieldHandler(yieldedValue => {
if (Array.isArray(yieldedValue)) {
return Promise.all(yieldedValue);
Expand All @@ -31,6 +31,7 @@ import seedGenerate from './commands/seed_generate';

import helpers from './helpers/index';


helpers.view.teaser();

const cli = yargs
Expand All @@ -51,7 +52,6 @@ const cli = yargs
.command(['migration:generate', 'migration:create'], 'Generates a new migration file', migrationGenerate)
.command(['model:generate', 'model:create'], 'Generates a model and its migration', modelGenerate)
.command(['seed:generate', 'seed:create'], 'Generates a new seed file', seedGenerate)
.config(loadRCFile(yargs.argv.optionsPath))
.version(() => cliPackage.version)
.wrap(yargs.terminalWidth())
.strict()
Expand All @@ -63,11 +63,3 @@ const args = cli.argv;
if (!args._[0]) {
cli.showHelp();
}

function loadRCFile(optionsPath) {
const rcFile = optionsPath || path.resolve(process.cwd(), '.sequelizerc');
const rcFileResolved = path.resolve(rcFile);
return fs.existsSync(rcFileResolved)
? JSON.parse(JSON.stringify(require(rcFileResolved)))
: {};
};
8 changes: 2 additions & 6 deletions test/db/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
};

it('creates a SequelizeMeta table', function (done) {
const self = this;

prepare(() => {
helpers.readTables(self.sequelize, tables => {
helpers.readTables(this.sequelize, tables => {
expect(tables).to.have.length(2);
expect(tables).to.contain('SequelizeMeta');
done();
Expand All @@ -186,10 +184,8 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
});

it('creates the respective table', function (done) {
const self = this;

prepare(() => {
helpers.readTables(self.sequelize, tables => {
helpers.readTables(this.sequelize, tables => {
expect(tables).to.have.length(2);
expect(tables).to.contain('Person');
done();
Expand Down
23 changes: 17 additions & 6 deletions test/options.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Support = require(__dirname + '/support');
const helpers = require(__dirname + '/support/helpers');
const gulp = require('gulp');
const Support = require(__dirname + '/support');
const helpers = require(__dirname + '/support/helpers');
const gulp = require('gulp');
const optionsPath = Support.resolveSupportPath('config', 'options.js');

describe(Support.getTestDialectTeaser('options'), () => {
Expand All @@ -22,18 +22,29 @@ describe(Support.getTestDialectTeaser('options'), () => {
});

describe('.sequelizerc', () => {
it('uses the .sequelizerc file', done => {
it('uses .sequelizerc file', done => {
const configContent = `
var path = require('path');

module.exports = {
'config': path.resolve('config-new', 'database.json'),
'migrations-path': path.resolve('migrations-new')
};
`;

gulp
.src(Support.resolveSupportPath('tmp'))
.pipe(helpers.clearDirectory())
.pipe(helpers.copyFile(optionsPath, '.sequelizerc'))
.pipe(helpers.overwriteFile(configContent, '.sequelizerc'))
.pipe(helpers.runCli('init'))
.pipe(helpers.listFiles())
.pipe(helpers.ensureContent('models'))
.pipe(helpers.ensureContent('migrations-new'))
.pipe(helpers.ensureContent('config-new'))
.pipe(helpers.teardown(done));
});

it('prefers the CLI arguments over the sequelizerc file', done => {
it('prefers CLI arguments over .sequelizerc file', done => {
const configPath = Support.resolveSupportPath('tmp', 'config', 'config.js');

gulp
Expand Down