Skip to content

Instantly share code, notes, and snippets.

@tluyben
Last active May 24, 2023 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/2a1178b771798158d11c933198ebd27d to your computer and use it in GitHub Desktop.
Save tluyben/2a1178b771798158d11c933198ebd27d to your computer and use it in GitHub Desktop.
Expect for TypeScript
import { spawn as expectSpawn } from 'node-pty';
export async function expect(cmd: string, expectations: { expected: string, retort: string }[], dir?: string) {
let cmdSplit = cmd.split(' ')
const p = expectSpawn(cmdSplit.shift()!, cmdSplit, { cwd: dir })
p.onData((data) => {
console.log(data)
const expected = expectations.shift()
if (!expected) {
throw new Error('Unexpected output: ' + data)
}
if (data.includes(expected.expected)) {
p.write(expected.retort + '\n')
}
})
}
@tluyben
Copy link
Author

tluyben commented May 24, 2023

Added this to be able to use the Prisma CLI tools non-interactive.

Like:

expect('npx prisma migrate dev --name "my migration"', [{ expected: 'apply this migration?', retort: 'y' }], publishDir)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment