Skip to content

Commit

Permalink
chore: fix generator and make them global
Browse files Browse the repository at this point in the history
  • Loading branch information
tonai committed Dec 18, 2023
1 parent 0885840 commit 08cc1b0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"lint:fix": "DEBUG=eslint:cli-engine eslint --fix --max-warnings=0 --ignore-path <(cat .gitignore .eslintignore) . && tsc --noEmit",
"test": "turbo run test",
"format": "prettier --write --ignore-unknown --ignore-path .prettierignore --ignore-path .gitignore .",
"generate": "turbo gen react-component",
"changeset": "changeset",
"version": "turbo run build lint test && changeset version",
"publish": "turbo run build lint test && changeset publish",
Expand Down
1 change: 0 additions & 1 deletion packages/react-front-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"scripts": {
"build": "tsc --project tsconfig.build.json",
"test": "jest",
"generate": "turbo gen react-component",
"prepublishOnly": "npm run build && node ../../scripts/prepublish.mjs"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
/* eslint @typescript-eslint/no-unsafe-assignment: 0 */
import type { PlopTypes } from '@turbo/gen';
import type { Answers, Inquirer } from 'inquirer';
import type { ActionType } from 'node-plop';
/* eslint-disable no-await-in-loop */
const { readdir, readFile } = require('node:fs/promises');
const { join } = require('node:path');

async function getPackages() {
const packages = [];
const src = './packages';
const dirs = await readdir(src);
for (const dir of dirs) {
const filepath = join(src, dir, 'package.json');
const file = await readFile(filepath, { encoding: 'utf-8' });
if (file) {
try {
const json = JSON.parse(file);
if (!json.private) {
packages.push(dir);
}
} catch (error) {
console.error(error);
}
}
}
return packages;
}

// Learn more about Turborepo Generators at https://turbo.build/repo/docs/core-concepts/monorepos/code-generation

export default function generator(plop: PlopTypes.NodePlopAPI): void {
/** @param plop {import('@turbo/gen').PlopTypes.NodePlopAPI} */
module.exports = (plop) => {
// A simple generator to add a new React component to the internal UI library
plop.setGenerator('react-component', {
actions: (data: Answers | undefined): ActionType[] => {
const actions: ActionType[] = [
/** @param data {import('inquirer').Answers} */
actions: (data) => {
const actions = [
{
path: 'src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.tsx',
path: 'packages/{{dir}}/src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.tsx',
templateFile: 'templates/component.hbs',
type: 'add',
},
{
path: 'src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.stories.tsx',
path: 'packages/{{dir}}/src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.stories.tsx',
templateFile:
data?.type === 'pages'
? 'templates/pageStory.hbs'
Expand All @@ -26,29 +48,29 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
];
if (data?.type !== 'pages') {
actions.push({
path: 'src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.test.tsx',
path: 'packages/{{dir}}/src/{{path}}/{{pascalCase name}}/{{pascalCase name}}.test.tsx',
templateFile: 'templates/test.hbs',
type: 'add',
});
}
if (data?.type === 'pages') {
actions.push({
path: 'src/{{path}}/{{pascalCase name}}/Code.mdx',
path: 'packages/{{dir}}/src/{{path}}/{{pascalCase name}}/Code.mdx{{dir}}',
templateFile: 'templates/pageCode.hbs',
type: 'add',
});
}
if (data?.index) {
actions.push({
path: 'src/index.tsx',
path: 'packages/{{dir}}/src/index.tsx',
pattern: /(\/\/ component exports)/g,
template:
"export { {{pascalCase name}} } from './{{path}}/{{pascalCase name}}/{{pascalCase name}}';",
type: 'append',
});

actions.push({
path: 'src/index.tsx',
path: 'packages/{{dir}}/src/index.tsx',
pattern: /(\/\/ component exports)/g,
template:
"export type { I{{pascalCase name}}Props } from './{{path}}/{{pascalCase name}}/{{pascalCase name}}';",
Expand All @@ -58,8 +80,15 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
return actions;
},
description: 'Adds a new react component',
prompts: async (inquirer: Inquirer): Promise<Answers> => {
const { type }: { type: string } = await inquirer.prompt({
/** @param inquirer {import('inquirer').Inquirer} */
prompts: async (inquirer) => {
const { dir } = await inquirer.prompt({
choices: await getPackages(),
message: 'For what package ?',
name: 'dir',
type: 'list',
});
const { type } = await inquirer.prompt({
choices: [
{
name: 'Component',
Expand Down Expand Up @@ -96,7 +125,7 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
name: 'index',
type: 'confirm',
});
return { index, name, path, type };
return { dir, index, name, path, type };
},
});
}
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 08cc1b0

Please sign in to comment.