-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from nealwp/updates/v1.1
Updates/v1.1
- Loading branch information
Showing
14 changed files
with
553 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.vscode | ||
dist | ||
dist | ||
coverage |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,152 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'node:fs' | ||
import { argv } from 'node:process'; | ||
import minimist from 'minimist'; | ||
import { generateModelInputs } from './parser'; | ||
import { modelTemplate } from './templates/model.smodg'; | ||
import { modelTemplate, migrationTemplate } from './templates'; | ||
import { kebabCase } from './formatters'; | ||
import { version } from '../package.json' | ||
|
||
try { | ||
const sourceCode = fs.readFileSync(argv[2], 'utf-8') | ||
const modelInputs = generateModelInputs(sourceCode) | ||
export const main = (args: minimist.ParsedArgs) => { | ||
|
||
const model = modelTemplate(modelInputs) | ||
const filePath = args._[0] | ||
|
||
let generateMigrationFile = false | ||
let schema = "" | ||
let outputDir = 'src/models' | ||
|
||
if(args.h || args.help){ | ||
printHelp() | ||
return | ||
} | ||
|
||
if(args.v || args.version){ | ||
printVersion() | ||
return | ||
} | ||
|
||
if(args.migration){ | ||
generateMigrationFile = true | ||
} | ||
|
||
if(args.s){ | ||
schema = args.s | ||
} else if (args.schema) { | ||
schema = args.schema | ||
} | ||
|
||
if(args.o){ | ||
outputDir = args.o | ||
} else if (args.outputDir) { | ||
outputDir = args.outputDir | ||
} | ||
|
||
if (!fs.existsSync('./src/models')) { | ||
fs.mkdirSync('./src/models') | ||
if(!filePath){ | ||
console.error('error: path to type file is required!') | ||
return | ||
} | ||
|
||
try { | ||
fs.writeFileSync(`./src/models/${kebabCase(modelInputs.modelName)}.model.ts`, model) | ||
|
||
const sourceCode = fs.readFileSync(filePath, 'utf-8') | ||
const modelInputs = generateModelInputs(sourceCode, schema) | ||
const model = modelTemplate(modelInputs) | ||
|
||
writeModelToFile(model, { outputDir, modelName: modelInputs.modelName }) | ||
|
||
if (generateMigrationFile) { | ||
|
||
const migrationInputs = { | ||
tableDefinition: modelInputs.tableDefinition, | ||
columnDefinitions: modelInputs.columnDefinitions | ||
} | ||
|
||
const migration = migrationTemplate(migrationInputs) | ||
writeMigrationToFile(migration, {modelName: modelInputs.modelName}) | ||
} | ||
} catch(error) { | ||
console.error(error) | ||
} | ||
|
||
} | ||
|
||
export const writeModelToFile = (model: string, options: {outputDir: string, modelName: string}) => { | ||
|
||
if (!fs.existsSync(`./${options.outputDir}`)) { | ||
fs.mkdirSync(`./${options.outputDir}`) | ||
} | ||
|
||
try { | ||
fs.writeFileSync(`./${options.outputDir}/${kebabCase(options.modelName)}.model.ts`, model) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
const writeMigrationToFile = (migration: string, options: {modelName: string}) => { | ||
|
||
if (!fs.existsSync(`./src/migrations`)) { | ||
fs.mkdirSync(`./src/migrations`) | ||
} | ||
|
||
try { | ||
fs.writeFileSync(`./src/migrations/${dateFormatString()}-Create-Table-${options.modelName}.ts`, migration) | ||
} catch(error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
|
||
const dateFormatString = () => { | ||
const currentDate = new Date(); | ||
const year = currentDate.getFullYear(); | ||
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); | ||
const day = String(currentDate.getDate()).padStart(2, '0'); | ||
const hours = String(currentDate.getHours()).padStart(2, '0'); | ||
const minutes = String(currentDate.getMinutes()).padStart(2, '0'); | ||
const seconds = String(currentDate.getSeconds()).padStart(2, '0'); | ||
|
||
return `${year}.${month}.${day}T${hours}.${minutes}.${seconds}`; | ||
} | ||
|
||
export const printHelp = () => { | ||
const helpText = ` | ||
================================= | ||
Sequelize Model Generator | ||
================================= | ||
Generate a Sequelize model based on a TypeScript type declaration. | ||
Usage: | ||
smodg [options] <filepath> | ||
Options: | ||
--help, -h show help | ||
--migration create an Umzug migration. default: false | ||
--outputDir=PATH, model output directory, relative to current path. default: "src/models" | ||
-o PATH | ||
--schema=NAME specify a schema. default is no schema | ||
-s NAME | ||
--version, -v print installed version | ||
` | ||
|
||
console.log(helpText) | ||
} | ||
|
||
export const printVersion = () => { | ||
console.log(`smodg v${version}`) | ||
} | ||
|
||
|
||
const args = minimist(process.argv.slice(2), { | ||
stopEarly: true, | ||
boolean: true | ||
}) | ||
|
||
} catch (error) { | ||
console.error(error) | ||
} | ||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
import { | ||
Column, | ||
Table, | ||
Model, | ||
DataType, | ||
CreatedAt, | ||
UpdatedAt, | ||
ForeignKey, | ||
HasMany, | ||
} from 'sequelize-typescript'; | ||
|
||
import { as Type } from '@_YOUR_TYPES' | ||
import { ModelAttributeColumnOptions } from 'sequelize'; | ||
|
||
interface CreationAttributes extends Type {} | ||
|
||
interface Attributes extends CreationAttributes { | ||
id: number; | ||
createdBy: string; | ||
createdDate: Date; | ||
updatedBy: string; | ||
updatedDate: Date; | ||
} | ||
|
||
type Keys = keyof Attributes | ||
|
||
interface ColumnOptions extends ModelAttributeColumnOptions { | ||
field: string | ||
} | ||
|
||
export const tableDefinition = { | ||
tableName: '', | ||
} | ||
|
||
export const columnDefinition: Record<Keys, ColumnOptions> = { | ||
id: { | ||
field: "id", | ||
type: DataType.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
|
||
createdBy: { | ||
field: "created_by", | ||
type: DataType.STRING, | ||
}, | ||
createdDate: { | ||
field: "created_date", | ||
type: DataType.DATE, | ||
}, | ||
updatedBy: { | ||
field: "updated_by", | ||
type: DataType.STRING, | ||
}, | ||
updatedDate: { | ||
field: "updated_date", | ||
type: DataType.DATE, | ||
}, | ||
} | ||
|
||
@Table(tableDefinition) | ||
export class | ||
extends Model<Attributes, CreationAttributes> | ||
implements Attributes { | ||
@Column(columnDefinition.id) | ||
id!: number; | ||
|
||
@Column(columnDefinition.createdBy) | ||
createdBy!: string; | ||
|
||
@CreatedAt | ||
@Column(columnDefinition.createdDate) | ||
createdDate!: Date; | ||
|
||
@Column(columnDefinition.updatedBy) | ||
updatedBy!: string; | ||
|
||
@UpdatedAt | ||
@Column(columnDefinition.updatedDate) | ||
updatedDate!: Date; | ||
} | ||
|
Oops, something went wrong.