Skip to content

Commit 031b281

Browse files
committed
Add --indentation command line option to control indent size (#561)
1 parent 7ad3d30 commit 031b281

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Options:
5151
-a, --additional Path to JSON file containing model options (for all
5252
tables). See the options: https://sequelize.org/master/class/lib/model.js~Model.html#static-method-init
5353
[string]
54+
--indentation Number of spaces to indent [number]
5455
-t, --tables Space-separated names of tables to import [array]
5556
-T, --skipTables Space-separated names of tables to skip [array]
5657
--caseModel, --cm Set case of model names: c|l|o|p|u

bin/sequelize-auto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const argv = require('yargs')
5555
type: 'string',
5656
alias: 'a'
5757
})
58+
.option('indentation', {
59+
description: 'Number of spaces to indent',
60+
type: 'number'
61+
})
5862
.option('tables', {
5963
description: 'Space-separated names of tables to import',
6064
array: true,
@@ -210,6 +214,7 @@ async function readPassword() {
210214
configFile.password = password || configFile.password || null;
211215
configFile.username = argv.user || configFile.username;
212216
configFile.useDefine = argv.useDefine || configFile.useDefine || false;
217+
configFile.indentation = argv.indentation || configFile.indentation || 2;
213218

214219
console.log(_.omit(configFile, 'password'));
215220

src/auto-generator.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from "lodash";
22
import { ColumnDescription } from "sequelize/types";
33
import { DialectOptions, FKSpec } from "./dialects/dialect-options";
4-
import { AutoOptions, CaseFileOption, CaseOption, Field, IndexSpec, LangOption, makeTableName, pluralize, qNameJoin, qNameSplit, recase, Relation, singularize, TableData, TSField } from "./types";
4+
import { AutoOptions, CaseFileOption, CaseOption, Field, IndexSpec, LangOption, makeIndent, makeTableName, pluralize, qNameJoin, qNameSplit, recase, Relation, singularize, TableData, TSField } from "./types";
55

66
/** Generates text from each table in TableData */
77
export class AutoGenerator {
@@ -35,16 +35,7 @@ export class AutoGenerator {
3535
this.dialect = dialect;
3636
this.options = options;
3737
this.options.lang = this.options.lang || 'es5';
38-
39-
// build the space array of indentation strings
40-
let sp = '';
41-
for (let x = 0; x < (this.options.indentation || 2); ++x) {
42-
sp += (this.options.spaces === true ? ' ' : "\t");
43-
}
44-
this.space = [];
45-
for (let i = 0; i < 6; i++) {
46-
this.space[i] = sp.repeat(i);
47-
}
38+
this.space = makeIndent(this.options.spaces, this.options.indentation);
4839
}
4940

5041
makeHeaderTemplate() {
@@ -132,7 +123,7 @@ export class AutoGenerator {
132123
str += "\n" + this.space[1] + "static initModel(sequelize: Sequelize.Sequelize): typeof #TABLE# {\n";
133124

134125
if (this.options.useDefine) {
135-
str += this.space[2] + "return sequelize.define('#TABLE#',{\n";
126+
str += this.space[2] + "return sequelize.define('#TABLE#', {\n";
136127

137128
} else {
138129
str += this.space[2] + "return #TABLE#.init({\n";

src/auto-writer.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import _ from "lodash";
33
import path from "path";
44
import util from "util";
55
import { FKSpec, TableData } from ".";
6-
import { AutoOptions, CaseFileOption, CaseOption, LangOption, makeTableName, pluralize, qNameSplit, recase, Relation } from "./types";
6+
import { AutoOptions, CaseFileOption, CaseOption, LangOption, makeIndent, makeTableName, pluralize, qNameSplit, recase, Relation } from "./types";
77
const mkdirp = require('mkdirp');
88

99
/** Writes text into files from TableData.text, and writes init-models */
1010
export class AutoWriter {
1111
tableText: { [name: string]: string };
1212
foreignKeys: { [tableName: string]: { [fieldName: string]: FKSpec } };
1313
relations: Relation[];
14+
space: string[];
1415
options: {
1516
caseFile?: CaseFileOption;
1617
caseModel?: CaseOption;
@@ -22,12 +23,15 @@ export class AutoWriter {
2223
noWrite?: boolean;
2324
singularize?: boolean;
2425
useDefine?: boolean;
26+
spaces?: boolean;
27+
indentation?: number;
2528
};
2629
constructor(tableData: TableData, options: AutoOptions) {
2730
this.tableText = tableData.text as { [name: string]: string };
2831
this.foreignKeys = tableData.foreignKeys;
2932
this.relations = tableData.relations;
3033
this.options = options;
34+
this.space = makeIndent(this.options.spaces, this.options.indentation);
3135
}
3236

3337
write() {
@@ -94,23 +98,24 @@ export class AutoWriter {
9498
private createAssociations(typeScript: boolean) {
9599
let strBelongs = "";
96100
let strBelongsToMany = "";
101+
const sp = this.space[1];
97102

98103
const rels = this.relations;
99104
rels.forEach(rel => {
100105
if (rel.isM2M) {
101106
const asprop = recase(this.options.caseProp, pluralize(rel.childProp));
102-
strBelongsToMany += ` ${rel.parentModel}.belongsToMany(${rel.childModel}, { as: '${asprop}', through: ${rel.joinModel}, foreignKey: "${rel.parentId}", otherKey: "${rel.childId}" });\n`;
107+
strBelongsToMany += `${sp}${rel.parentModel}.belongsToMany(${rel.childModel}, { as: '${asprop}', through: ${rel.joinModel}, foreignKey: "${rel.parentId}", otherKey: "${rel.childId}" });\n`;
103108
} else {
104109
// const bAlias = (this.options.noAlias && rel.parentModel.toLowerCase() === rel.parentProp.toLowerCase()) ? '' : `as: "${rel.parentProp}", `;
105110
const asParentProp = recase(this.options.caseProp, rel.parentProp);
106111
const bAlias = this.options.noAlias ? '' : `as: "${asParentProp}", `;
107-
strBelongs += ` ${rel.childModel}.belongsTo(${rel.parentModel}, { ${bAlias}foreignKey: "${rel.parentId}"});\n`;
112+
strBelongs += `${sp}${rel.childModel}.belongsTo(${rel.parentModel}, { ${bAlias}foreignKey: "${rel.parentId}"});\n`;
108113

109114
const hasRel = rel.isOne ? "hasOne" : "hasMany";
110115
// const hAlias = (this.options.noAlias && Utils.pluralize(rel.childModel.toLowerCase()) === rel.childProp.toLowerCase()) ? '' : `as: "${rel.childProp}", `;
111116
const asChildProp = recase(this.options.caseProp, rel.childProp);
112117
const hAlias = this.options.noAlias ? '' : `as: "${asChildProp}", `;
113-
strBelongs += ` ${rel.parentModel}.${hasRel}(${rel.childModel}, { ${hAlias}foreignKey: "${rel.parentId}"});\n`;
118+
strBelongs += `${sp}${rel.parentModel}.${hasRel}(${rel.childModel}, { ${hAlias}foreignKey: "${rel.parentId}"});\n`;
114119
}
115120
});
116121

@@ -121,6 +126,7 @@ export class AutoWriter {
121126
// create the TypeScript init-models file to load all the models into Sequelize
122127
private createTsInitString(tables: string[], assoc: string) {
123128
let str = 'import type { Sequelize } from "sequelize";\n';
129+
const sp = this.space[1];
124130
const modelNames: string[] = [];
125131
// import statements
126132
tables.forEach(t => {
@@ -133,33 +139,33 @@ export class AutoWriter {
133139
// re-export the model classes
134140
str += '\nexport {\n';
135141
modelNames.forEach(m => {
136-
str += ` _${m} as ${m},\n`;
142+
str += `${sp}_${m} as ${m},\n`;
137143
});
138144
str += '};\n';
139145

140146
// re-export the model attirbutes
141147
str += '\nexport type {\n';
142148
modelNames.forEach(m => {
143-
str += ` ${m}Attributes,\n`;
144-
str += ` ${m}CreationAttributes,\n`;
149+
str += `${sp}${m}Attributes,\n`;
150+
str += `${sp}${m}CreationAttributes,\n`;
145151
});
146152
str += '};\n\n';
147153

148154
// create the initialization function
149155
str += 'export function initModels(sequelize: Sequelize) {\n';
150156
modelNames.forEach(m => {
151-
str += ` const ${m} = _${m}.initModel(sequelize);\n`;
157+
str += `${sp}const ${m} = _${m}.initModel(sequelize);\n`;
152158
});
153159

154160
// add the asociations
155161
str += "\n" + assoc;
156162

157163
// return the models
158-
str += "\n return {\n";
164+
str += `\n${sp}return {\n`;
159165
modelNames.forEach(m => {
160-
str += ` ${m}: ${m},\n`;
166+
str += `${this.space[2]}${m}: ${m},\n`;
161167
});
162-
str += ' };\n';
168+
str += `${sp}};\n`;
163169
str += '}\n';
164170

165171
return str;
@@ -168,6 +174,7 @@ export class AutoWriter {
168174
// create the ES5 init-models file to load all the models into Sequelize
169175
private createES5InitString(tables: string[], assoc: string, vardef: string) {
170176
let str = `${vardef} DataTypes = require("sequelize").DataTypes;\n`;
177+
const sp = this.space[1];
171178
const modelNames: string[] = [];
172179
// import statements
173180
tables.forEach(t => {
@@ -180,18 +187,18 @@ export class AutoWriter {
180187
// create the initialization function
181188
str += '\nfunction initModels(sequelize) {\n';
182189
modelNames.forEach(m => {
183-
str += ` ${vardef} ${m} = _${m}(sequelize, DataTypes);\n`;
190+
str += `${sp}${vardef} ${m} = _${m}(sequelize, DataTypes);\n`;
184191
});
185192

186193
// add the asociations
187194
str += "\n" + assoc;
188195

189196
// return the models
190-
str += "\n return {\n";
197+
str += `\n${sp}return {\n`;
191198
modelNames.forEach(m => {
192-
str += ` ${m},\n`;
199+
str += `${this.space[2]}${m},\n`;
193200
});
194-
str += ' };\n';
201+
str += `${sp}};\n`;
195202
str += '}\n';
196203
str += 'module.exports = initModels;\n';
197204
str += 'module.exports.initModels = initModels;\n';
@@ -203,6 +210,7 @@ export class AutoWriter {
203210
private createESMInitString(tables: string[], assoc: string) {
204211
let str = 'import _sequelize from "sequelize";\n';
205212
str += 'const DataTypes = _sequelize.DataTypes;\n';
213+
const sp = this.space[1];
206214
const modelNames: string[] = [];
207215
// import statements
208216
tables.forEach(t => {
@@ -214,18 +222,18 @@ export class AutoWriter {
214222
// create the initialization function
215223
str += '\nexport default function initModels(sequelize) {\n';
216224
modelNames.forEach(m => {
217-
str += ` const ${m} = _${m}.init(sequelize, DataTypes);\n`;
225+
str += `${sp}const ${m} = _${m}.init(sequelize, DataTypes);\n`;
218226
});
219227

220228
// add the associations
221229
str += "\n" + assoc;
222230

223231
// return the models
224-
str += "\n return {\n";
232+
str += `\n${sp}return {\n`;
225233
modelNames.forEach(m => {
226-
str += ` ${m},\n`;
234+
str += `${this.space[2]}${m},\n`;
227235
});
228-
str += ' };\n';
236+
str += `${sp}};\n`;
229237
str += '}\n';
230238
return str;
231239
}

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,15 @@ export function makeTableName(opt: CaseOption | undefined, tableNameOrig: string
237237
return name;
238238
}
239239

240+
/** build the array of indentation strings */
241+
export function makeIndent(spaces: boolean | undefined, indent: number | undefined): string[] {
242+
let sp = '';
243+
for (let x = 0; x < (indent || 2); ++x) {
244+
sp += (spaces === true ? ' ' : "\t");
245+
}
246+
let space = [];
247+
for (let i = 0; i < 6; i++) {
248+
space[i] = sp.repeat(i);
249+
}
250+
return space;
251+
}

0 commit comments

Comments
 (0)