Skip to content

Commit

Permalink
Merge pull request #32 from ScopeSV/31-strip-ts-if-js
Browse files Browse the repository at this point in the history
31-strip-ts-if-js
  • Loading branch information
ScopeSV authored Nov 4, 2023
2 parents 1cf5251 + 50807df commit f0de83f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ $ npx kyseline migration:make remove_foo_from_footable foo
Add additional notes about how to deploy this on a live system -->

<!-- ## Built With
- [Dropwizard](http://www.dropwizard.io/1.0.2/docs/) - The web framework used
- [Maven](https://maven.apache.org/) - Dependency Management
- [ROME](https://rometools.github.io/rome/) - Used to generate RSS Feeds -->

<!-- ## Contributing
Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us. -->
Expand All @@ -62,8 +56,6 @@ The project uses [SemVer](http://semver.org/) for versioning. For the versions a

- **Stephan Valois** -

<!-- See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project. -->

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
17 changes: 17 additions & 0 deletions src/file-writer/FileWriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'
import { ConfigParser } from '../config-file/ConfigParser'
import ts from 'typescript'

class FileWriter {
constructor(
Expand All @@ -27,6 +28,18 @@ class FileWriter {
return fs.existsSync(tsConfigPath)
}

#transpileToJs = () => {
const defaultOptions = {
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ESNext,
jsx: ts.JsxEmit.Preserve,
}

this.template = ts.transpileModule(this.template, {
compilerOptions: defaultOptions,
}).outputText
}

determineFileExtension = () => {
const cfg = this.configParser.getConfig()
if (this.#hasTsConfigInCurrentDir() && cfg.useJsExtension !== true) {
Expand All @@ -44,6 +57,10 @@ class FileWriter {
const filename = `${this.getCurrTimeStamp()}_${this.command}.${fileExt}`
const filePath = path.join(dir, filename)

if (fileExt === 'js') {
this.#transpileToJs()
}

fs.writeFile(filePath, this.template, (err: any) => {
if (err) {
console.error(err)
Expand Down
18 changes: 18 additions & 0 deletions src/handlers/migrationHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.createTable('todo')\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()"
)
})
it('creates a table with columns', () => {
const template = generateTemplate('create_todo', [
Expand All @@ -36,6 +39,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.createTable('todo')\n\t\t.addColumn('id', 'uuid')\n\t\t.addColumn('name', 'string')\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()"
)
})
it('creates a table with multiple columns', () => {
const template = generateTemplate('create_todo', [
Expand All @@ -46,6 +52,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.createTable('todo')\n\t\t.addColumn('id', 'uuid')\n\t\t.addColumn('name', 'string')\n\t\t.addColumn('complete', 'boolean')\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()"
)
})
})
describe('AddFields', () => {
Expand All @@ -56,6 +65,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean')\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('complete')\n\t\t.execute()"
)
})
it('adds multiple columns', () => {
const template = generateTemplate('add_complete_to_todo', [
Expand All @@ -65,6 +77,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean')\n\t\t.addColumn('name', 'string')\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('name')\n\t\t.dropColumn('complete')\n\t\t.execute()"
)
})
it('adds a column with not null', () => {
const template = generateTemplate('add_complete_to_todo', [
Expand All @@ -74,6 +89,9 @@ describe('migrationHandler', () => {
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean', (col) => col.notNull())\n\t\t.execute()"
)
expect(template).toContain(
"await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('complete')\n\t\t.execute()"
)
})
it('adds an end if cant find the action', () => {
const template = generateTemplate('do_stuff', []).getTemplate()
Expand Down
57 changes: 55 additions & 2 deletions src/handlers/migrationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,34 @@ const parseInput = (input: string) => {
return { tableName, type }
}

const getOpositeMigrationType = (type: string): string => {
switch (type) {
case 'createTable':
return 'dropTable'
case 'alterTable':
return 'alterTable'
case 'dropTable':
return 'createTable'
default:
return ''
}
}

const buildTableAction = (
b: TemplateBuilder,
action: string,
tableName: string,
options: string[],
operation: (columnName: string, columnType: string, opts?: string[]) => void
operation: (
columnName: string,
columnType: string,
opts?: string[]
) => void,
downOperation?: (
columnName: string,
columnType: string,
opts?: string[]
) => void
): void => {
b.indent(1)
.addLine('await db.schema')
Expand All @@ -40,6 +62,27 @@ const buildTableAction = (

b.indent(2).do()
b.addEnd()

if (downOperation) {
const downAction = getOpositeMigrationType(action)
b.addDown()
.indent(1)
.addLine('await db.schema')
.indent(2)
.addLine(`.${downAction}('${tableName}')`)

if (action !== 'createTable') {
for (const option of options.slice().reverse()) {
const [columnName, columnType, ...stuff] = option.split(':')
downOperation(columnName, columnType, stuff)
}

b.indent(2).do()
b.addEnd()
} else {
b.indent(2).do().addEnd()
}
}
}

export const generateTemplate = (
Expand All @@ -60,6 +103,9 @@ export const generateTemplate = (
options,
(columnName, columnType, opts?: string[]) => {
b.indent(2).addColumn(columnName, columnType, opts)
},
(columnName, columnType, opts?: string[]) => {
b.indent(2).dropColumn(columnName)
}
)
break
Expand All @@ -71,7 +117,11 @@ export const generateTemplate = (
tableName,
options,
(columnName, columnType) => {
console.log('ready to create a table')
b.indent(2).addColumn(columnName, columnType)
},
(columnName, columnType, opts?: string[]) => {
b.indent(2).dropColumn(columnName).indent(2).do().addEnd()
}
)
break
Expand All @@ -84,16 +134,19 @@ export const generateTemplate = (
options,
(columnName) => {
b.indent(2).dropColumn(columnName)
},
(columnName, columnType, opts?: string[]) => {
b.indent(2).addColumn(columnName, '')
}
)
break

default:
b.addEnd()
b.addDown().addEnd()
break
}

b.addDown().addEnd()
return b
}

Expand Down
7 changes: 7 additions & 0 deletions src/template/TemplateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TemplateBuilder {

addEnd() {
this.template += '}\n\n'
return this
}

addHeader() {
Expand Down Expand Up @@ -69,8 +70,14 @@ class TemplateBuilder {
return this
}

dropTable(tableName: string) {
this.template += `.dropTable('${tableName}')\n`
return this
}

do() {
this.template += '.execute()\n'
return this
}

getTemplate() {
Expand Down

0 comments on commit f0de83f

Please sign in to comment.