diff --git a/package.json b/package.json index 6b8b76a1..5e2744bd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/react-front-kit/package.json b/packages/react-front-kit/package.json index 7360cb0f..8edb4ca8 100644 --- a/packages/react-front-kit/package.json +++ b/packages/react-front-kit/package.json @@ -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": { diff --git a/packages/react-front-kit/turbo/generators/config.ts b/turbo/generators/config.cjs similarity index 59% rename from packages/react-front-kit/turbo/generators/config.ts rename to turbo/generators/config.cjs index 9b6c6bd8..e6a2d50d 100644 --- a/packages/react-front-kit/turbo/generators/config.ts +++ b/turbo/generators/config.cjs @@ -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' @@ -26,21 +48,21 @@ 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}}';", @@ -48,7 +70,7 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void { }); 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}}';", @@ -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 => { - 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', @@ -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 }; }, }); -} +}; diff --git a/packages/react-front-kit/turbo/generators/templates/component.hbs b/turbo/generators/templates/component.hbs similarity index 100% rename from packages/react-front-kit/turbo/generators/templates/component.hbs rename to turbo/generators/templates/component.hbs diff --git a/packages/react-front-kit/turbo/generators/templates/pageCode.hbs b/turbo/generators/templates/pageCode.hbs similarity index 100% rename from packages/react-front-kit/turbo/generators/templates/pageCode.hbs rename to turbo/generators/templates/pageCode.hbs diff --git a/packages/react-front-kit/turbo/generators/templates/pageStory.hbs b/turbo/generators/templates/pageStory.hbs similarity index 100% rename from packages/react-front-kit/turbo/generators/templates/pageStory.hbs rename to turbo/generators/templates/pageStory.hbs diff --git a/packages/react-front-kit/turbo/generators/templates/story.hbs b/turbo/generators/templates/story.hbs similarity index 100% rename from packages/react-front-kit/turbo/generators/templates/story.hbs rename to turbo/generators/templates/story.hbs diff --git a/packages/react-front-kit/turbo/generators/templates/test.hbs b/turbo/generators/templates/test.hbs similarity index 100% rename from packages/react-front-kit/turbo/generators/templates/test.hbs rename to turbo/generators/templates/test.hbs