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

Added ability to specify custom filename during model generation. #962

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pids

# Editor files
.vscode
.idea

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
10 changes: 8 additions & 2 deletions src/commands/model_generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ exports.builder = (yargs) =>
type: 'string',
demandOption: true,
})
.option('file-name', {
describe:
'Specify custom file name for the model created. Default name is model name in lowercase',
type: 'string',
demandOption: false,
})
.option('force', {
describe: 'Forcefully re-creates model with the same name',
type: 'string',
Expand All @@ -37,7 +43,7 @@ exports.handler = function (args) {
helpers.migration.generateTableCreationFile(args);
helpers.view.log(
'New model was created at',
clc.blueBright(helpers.path.getModelPath(args.name)),
clc.blueBright(helpers.path.getModelPath(args.name, args['file-name'])),
'.'
);
helpers.view.log(
Expand Down Expand Up @@ -78,7 +84,7 @@ function ensureMigrationsFolder() {
}

function checkModelFileExistence(args) {
const modelPath = helpers.path.getModelPath(args.name);
const modelPath = helpers.path.getModelPath(args.name, args['file-name']);

if (args.force === undefined && helpers.model.modelFileExists(modelPath)) {
helpers.view.notifyAboutExistingFile(modelPath);
Expand Down
7 changes: 4 additions & 3 deletions src/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ function seedAll(args) {
function seedUndoAll(args) {
return getMigrator('seeder', args)
.then((migrator) => {
return (helpers.umzug.getStorage('seeder') === 'none'
? migrator.pending()
: migrator.executed()
return (
helpers.umzug.getStorage('seeder') === 'none'
? migrator.pending()
: migrator.executed()
).then((seeders) => {
if (seeders.length === 0) {
helpers.view.log('No seeders found.');
Expand Down
3 changes: 2 additions & 1 deletion src/core/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export async function getMigrator(type, args) {
}

export function ensureCurrentMetaSchema(migrator) {
const queryInterface = migrator.options.storageOptions.sequelize.getQueryInterface();
const queryInterface =
migrator.options.storageOptions.sequelize.getQueryInterface();
const tableName = migrator.options.storageOptions.tableName;
const columnName = migrator.options.storageOptions.columnName;

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/model-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
},

generateFile(args) {
const modelPath = helpers.path.getModelPath(args.name);
const modelPath = helpers.path.getModelPath(args.name, args['file-name']);

helpers.asset.write(modelPath, this.generateFileContent(args));
},
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/path-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ module.exports = {
return args.modelsPath || path.resolve(process.cwd(), 'models');
},

getModelPath(modelName) {
getModelPath(modelName, fileName) {
return path.resolve(
this.getModelsPath(),
this.addFileExtension(modelName.toLowerCase())
this.addFileExtension(fileName ? fileName : modelName.toLowerCase())
);
},

Expand Down
70 changes: 36 additions & 34 deletions test/support/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ module.exports = {

clearDirectory: function () {
return through.obj(function (file, encoding, callback) {
exec('rm -rf * & rm -rf .sequelizerc', { cwd: file.path }, function (
err
) {
callback(err, file);
});
exec(
'rm -rf * & rm -rf .sequelizerc',
{ cwd: file.path },
function (err) {
callback(err, file);
}
);
});
},

Expand All @@ -53,41 +55,41 @@ module.exports = {

logToFile(command);

exec(command, { cwd: file.path, env: env }, function (
err,
stdout,
stderr
) {
var result = file;
exec(
command,
{ cwd: file.path, env: env },
function (err, stdout, stderr) {
var result = file;

logToFile({ err: err, stdout: stdout, stderr: stderr });
logToFile({ err: err, stdout: stdout, stderr: stderr });

if (stdout) {
expect(stdout).to.not.contain('EventEmitter');
}
if (stdout) {
expect(stdout).to.not.contain('EventEmitter');
}

if (options.pipeStdout) {
result = stdout;
} else if (options.pipeStderr) {
result = stderr;
}
if (options.pipeStdout) {
result = stdout;
} else if (options.pipeStderr) {
result = stderr;
}

if (options.exitCode) {
try {
expect(err).to.be.ok();
expect(err.code).to.equal(1);
callback(null, result);
} catch (e) {
callback(
new Error('Expected cli to exit with a non-zero code'),
null
);
if (options.exitCode) {
try {
expect(err).to.be.ok();
expect(err.code).to.equal(1);
callback(null, result);
} catch (e) {
callback(
new Error('Expected cli to exit with a non-zero code'),
null
);
}
} else {
err = options.pipeStderr ? null : err;
callback(err, result);
}
} else {
err = options.pipeStderr ? null : err;
callback(err, result);
}
});
);
});
},

Expand Down