Add createMigration task

This commit is contained in:
Jerko Steiner 2019-08-02 22:39:04 +07:00
parent 3690908918
commit 745834848f
3 changed files with 16 additions and 4 deletions

View File

@ -15,13 +15,14 @@ export class Subprocess {
public readonly stdio: StdioOptions = StdioOptions.PIPE,
) {}
async run() {
async run(cwd?: string) {
return new Promise((resolve, reject) => {
process.stderr.write(`> ${this.command} ${this.args.join(' ')}\n`)
const subprocess = spawn(this.command, this.args, {
shell: false,
stdio: this.stdio,
env: this.environment,
cwd,
})
if (this.stdio === StdioOptions.PIPE) {

View File

@ -1,5 +1,6 @@
import {run} from '../run'
import {join} from 'path'
import {findNodeModules} from '../modules'
import * as fs from 'fs'
import * as p from 'path'
@ -38,7 +39,17 @@ export async function exec(file: string) {
'--project',
findTsConfig(file),
] : []
return run(command, [...args, file])
await run(command, [...args, file])
}
export async function createMigration(project: string, name: string) {
const typeorm = findNodeModules(project)
.map(nm => p.join(nm, 'typeorm'))
.find(t => fs.existsSync(t))
if (!typeorm) {
throw new Error('typeorm not found')
}
await run('ts-node', [typeorm, 'migration:create', '--name', name], project)
}
function findTsConfig(file: string): string {

View File

@ -1,10 +1,10 @@
import {Subprocess} from './Subprocess'
import {getPathVariable} from './modules'
export async function run(command: string, args: string[]) {
export async function run(command: string, args: string[], cwd?: string) {
return new Subprocess(command, args, {
...process.env,
PATH: getPathVariable(),
})
.run()
.run(cwd)
}