Skip to content

Instantly share code, notes, and snippets.

@Whoaa512
Created November 14, 2019 21: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 Whoaa512/f65902089923ba4c9eb4caf1c9560557 to your computer and use it in GitHub Desktop.
Save Whoaa512/f65902089923ba4c9eb4caf1c9560557 to your computer and use it in GitHub Desktop.
Some helpers to test hygen
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