Skip to content

Commit

Permalink
fix: adds a package stub to the _hygencook dir to better handle ts files
Browse files Browse the repository at this point in the history
Hygen can parse a .ts prompt file in a template using ts-node/register, which is cool, but if you're
running templates in a directory where your package json has type: module that causes
ts-node/register to transpile to imports/exports instead of the commonjs format hygen needs. This
change drops a package.json stub into the _hygencook dir containing only type: "commonjs", which is
enough to convince ts-node/register to transpile any .ts files to the correct syntax for hygen to be
able to load.
  • Loading branch information
Bill Beesley authored and bbeesley committed Mar 28, 2022
1 parent d2aa854 commit 1a3d1d7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/add-ingredients.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdir } from 'async-fs-wrapper';
import { green, yellow } from 'chalk';
import execa from 'execa';
import { writeFile } from 'fs/promises';
import fs from 'fs-extra';
import ora from 'ora';
import { join, dirname } from 'path';
Expand Down Expand Up @@ -58,6 +59,12 @@ async function createHygenCookDir(): Promise<string> {
await mkdir(hygenCookDirPath, {
recursive: true,
});
await writeFile(
join(hygenCookDirPath, 'package.json'),
JSON.stringify({
type: 'commonjs',
}),
);
} catch (err) {
// no problem if directory exists
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/ingredients.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from 'async-fs-wrapper';
import test from 'ava';
import execa from 'execa';
import { readdir, writeFile } from 'fs/promises';
Expand Down Expand Up @@ -72,6 +73,46 @@ test.serial('imports ingredients from npm modules', async (t) => {
]);
});

test.serial(
"adds a pseudo package json so that hygen doesn't throw an error reading ts prompt files in es modules",
async (t) => {
const recipe = {
name: 'testing',
ingredients: ['hygen-emiketic-react'],
instructions: [
{
ingredient: 'hygen-emiketic-react',
generator: 'react-native-component',
action: 'new',
args: [
{
option: 'name',
value: 'testing',
},
{
option: 'feature',
value: 'test-feature',
},
],
},
],
};
const yamlRecipe = dump(recipe);
await writeFile(resolve(execOpts.cwd, 'recipe.yml'), yamlRecipe);
await cook({
recipePath: resolve(execOpts.cwd, 'recipe.yml'),
shouldOverwriteTemplates: true,
});
const pkg = await readFile(
resolve(execOpts.cwd, '_hygencook/package.json'),
{ encoding: 'utf8' },
);
t.deepEqual(JSON.parse(pkg), {
type: 'commonjs',
});
},
);

test.serial('imports ingredients from git repos', async (t) => {
const recipe = {
name: 'testing',
Expand Down

0 comments on commit 1a3d1d7

Please sign in to comment.