Skip to content

Commit

Permalink
Merge pull request #21 from marcosrjjunior/14-custom_template
Browse files Browse the repository at this point in the history
#14 custom template
  • Loading branch information
acro5piano authored Feb 27, 2024
2 parents 70fffc4 + c535c93 commit a0e0fac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export async function down(db: Kysely<any>): Promise<void> {
}
```

If you want to modify the default template, you can pass an option `--template=yourtemplate.txt` pointing to a file containing the custom template

If you want to change the path where migration files are stored, please modify the CLI code as follows:

```typescript
Expand Down
14 changes: 11 additions & 3 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,36 @@ export function run(db: Kysely<any>, migrator: Migrator, path: string = './migra
program
.command('create')
.argument('<input-file>')
.option('--template <template>')
.description(
'Create a new migration with the given description, and the current time as the version',
)
.action(async (name) => {
.action(async (name, options) => {
const dateStr = new Date().toISOString().replace(/[-:]/g, '').split('.')[0]
const fileName = `${path}/${dateStr}-${name}.ts`
const mkdir = () => fs.mkdirSync(path)

let migrationTemplate = DEFAULT_TEMPLATE

try {
if (options.template) {
migrationTemplate = fs.readFileSync(options.template, 'utf8')
}

if (!fs.lstatSync(path).isDirectory()) {
mkdir()
}
} catch {
fs.mkdirSync(path)
}
fs.writeFileSync(fileName, TEMPLATE, 'utf8')
fs.writeFileSync(fileName, migrationTemplate, 'utf8')
console.log('Created Migration:', fileName)
})

program.parseAsync().then(() => db.destroy())
}

const TEMPLATE = `import { type Kysely } from 'kysely'
const DEFAULT_TEMPLATE = `import { type Kysely } from 'kysely'
export async function up(db: Kysely<any>): Promise<void> {
}
Expand Down

0 comments on commit a0e0fac

Please sign in to comment.