Created
November 14, 2019 21:35
-
-
Save Whoaa512/f65902089923ba4c9eb4caf1c9560557 to your computer and use it in GitHub Desktop.
Some helpers to test hygen
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
import fs from 'fs'; | |
import path from 'path'; | |
import execa from 'execa'; | |
import { runner } from 'hygen'; | |
import Logger from 'hygen/dist/logger'; | |
export type ArgsObject = { [key: string]: string | boolean }; | |
export const readFileString = (path: string) => fs.readFileSync(path, 'utf8'); | |
export function toArgsArray(args: ArgsObject): string[] { | |
return Object.keys(args).reduce((memo: string[], key) => { | |
const value = args[key]; | |
if (typeof value === 'boolean') { | |
memo.push(value ? `--${key}` : `--no-${key}`); | |
} else if (typeof value === 'string') { | |
memo.push(`--${key}=${value}`); | |
} | |
return memo; | |
}, []); | |
} | |
export async function runMockHygen({ | |
namespace, | |
generator, | |
args, | |
}: { | |
namespace: string; | |
generator: string; | |
args: ArgsObject; | |
}) { | |
const cliArgs = [namespace, generator, ...toArgsArray(args)]; | |
return runner(cliArgs, { | |
templates: path.join(__dirname, '../_templates'), | |
cwd: process.cwd(), | |
logger: new Logger(jest.fn()), | |
debug: false, | |
exec: (action, body) => { | |
const opts = body && body.length > 0 ? { input: body } : {}; | |
return execa.command(action, { ...opts, shell: true }); | |
}, | |
createPrompter: () => ({ | |
prompt: jest.fn(() => { | |
throw new Error('Failed to pass all prompt args in testing'); | |
}), | |
}), | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment