Unverified Commit 348c5501 authored by Thomas Knickman's avatar Thomas Knickman Committed by GitHub

feat(turbo): use turbo gen instead of plop (#53684)

### What?

Use [Turborepo generators](https://turbo.build/repo/docs/core-concepts/monorepos/code-generation#custom-generators) instead of plop. 

Turborepo generators are built on top of plop, but include some additional tooling to make working in monorepos easier. It also supports zero config typescript for configs. 

### Why?
Same Team!

### How?
Ported the existing plopfile to a turbo generator config
Co-authored-by: 's avatarJiachi Liu <4800338+huozhi@users.noreply.github.com>
parent 520fbc88
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
"packages/*" "packages/*"
], ],
"scripts": { "scripts": {
"new-error": "plop error", "new-error": "turbo gen error",
"new-test": "plop test", "new-test": "turbo gen test",
"clean": "lerna clean -y && lerna bootstrap && lerna run clean && lerna exec 'node ../../scripts/rm.mjs dist'", "clean": "lerna clean -y && lerna bootstrap && lerna run clean && lerna exec 'node ../../scripts/rm.mjs dist'",
"build": "turbo run build --remote-cache-timeout 60 --summarize true", "build": "turbo run build --remote-cache-timeout 60 --summarize true",
"lerna": "lerna", "lerna": "lerna",
...@@ -173,6 +173,7 @@ ...@@ -173,6 +173,7 @@
"nanoid": "3.1.30", "nanoid": "3.1.30",
"next": "workspace:*", "next": "workspace:*",
"node-fetch": "2.6.7", "node-fetch": "2.6.7",
"node-plop": "0.31.1",
"npm-run-all": "4.1.5", "npm-run-all": "4.1.5",
"nprogress": "0.2.0", "nprogress": "0.2.0",
"octokit": "3.1.0", "octokit": "3.1.0",
...@@ -181,7 +182,6 @@ ...@@ -181,7 +182,6 @@
"pixrem": "5.0.0", "pixrem": "5.0.0",
"playwright-chromium": "1.35.1", "playwright-chromium": "1.35.1",
"playwright-core": "1.35.1", "playwright-core": "1.35.1",
"plop": "3.0.5",
"postcss": "8.4.31", "postcss": "8.4.31",
"postcss-nested": "4.2.1", "postcss-nested": "4.2.1",
"postcss-pseudoelements": "5.0.0", "postcss-pseudoelements": "5.0.0",
......
This diff is collapsed.
module.exports = function (plop) { import { type NodePlopAPI } from 'node-plop'
const toFileName = (str) => str.toLowerCase().replace(/ /g, '-') import path from 'path'
plop.setHelper('toFileName', toFileName) import * as helpers from './helpers'
interface TestResponse {
appDir: string
type: 'e2e' | 'production' | 'development' | 'unit'
}
interface ErrorResponse {
name: string
title: string
why: string
fix: string
}
function validateNonEmptyString(field: string) {
return function (value: string) {
if (/.+/.test(value)) {
return true
}
return `${field} is required`
}
}
export default function generator(plop: NodePlopAPI): void {
// make our custom helpers available for use in templates as handlebars helpers
helpers.init(plop)
plop.setGenerator('test', { plop.setGenerator('test', {
description: 'Create a new test', description: 'Create a new test',
...@@ -15,6 +40,7 @@ module.exports = function (plop) { ...@@ -15,6 +40,7 @@ module.exports = function (plop) {
type: 'input', type: 'input',
name: 'name', name: 'name',
message: 'Test name', message: 'Test name',
validate: validateNonEmptyString('test name'),
}, },
{ {
type: 'list', type: 'list',
...@@ -34,12 +60,18 @@ module.exports = function (plop) { ...@@ -34,12 +60,18 @@ module.exports = function (plop) {
], ],
}, },
], ],
actions: function (data) { actions: function (answers) {
const appDirPath = data.appDir ? 'app-dir/' : '' const { appDir, type } = answers as TestResponse
let templatePath = `test/${ const testRoot = path.join(plop.getDestBasePath(), 'test')
data.type === 'unit' ? 'unit' : 'e2e'
}/${appDirPath}test-template` const appDirPath = appDir ? 'app-dir/' : ''
let targetPath = `test/{{ type }}/${appDirPath}` let templatePath = path.join(
testRoot,
type === 'unit' ? 'unit' : 'e2e',
appDirPath,
'test-template'
)
let targetPath = path.join(testRoot, type, appDirPath)
return [ return [
{ {
...@@ -59,24 +91,30 @@ module.exports = function (plop) { ...@@ -59,24 +91,30 @@ module.exports = function (plop) {
name: 'name', name: 'name',
type: 'input', type: 'input',
message: 'Url path with dashes. E.g. circular-structure', message: 'Url path with dashes. E.g. circular-structure',
validate: validateNonEmptyString('path'),
}, },
{ {
name: 'title', name: 'title',
type: 'input', type: 'input',
message: 'Title for the error. E.g. Circular Structure', message: 'Title for the error. E.g. Circular Structure',
validate: validateNonEmptyString('title'),
}, },
{ {
name: 'why', name: 'why',
type: 'input', type: 'input',
message: 'What caused the error to happen?', message: 'What caused the error to happen?',
validate: validateNonEmptyString('why'),
}, },
{ {
name: 'fix', name: 'fix',
type: 'input', type: 'input',
message: 'What are the possible ways to fix it?', message: 'What are the possible ways to fix it?',
validate: validateNonEmptyString('fix'),
}, },
], ],
actions: function ({ name }) { actions: function (answers) {
const { name } = answers as ErrorResponse
return [ return [
{ {
type: 'add', type: 'add',
...@@ -86,16 +124,16 @@ module.exports = function (plop) { ...@@ -86,16 +124,16 @@ module.exports = function (plop) {
{ {
type: 'modify', type: 'modify',
path: 'errors/manifest.json', path: 'errors/manifest.json',
transform(fileContents) { transform(fileContents: string) {
const manifestData = JSON.parse(fileContents) const manifestData = JSON.parse(fileContents)
manifestData.routes[0].routes.push({ manifestData.routes[0].routes.push({
title: toFileName(name), title: helpers.toFileName(name),
path: `/errors/${toFileName(name)}.md`, path: `/errors/${helpers.toFileName(name)}.md`,
}) })
return JSON.stringify(manifestData, null, 2) return JSON.stringify(manifestData, null, 2)
}, },
}, },
`Url for the error: https://nextjs.org/docs/messages/${toFileName( `Url for the error: https://nextjs.org/docs/messages/${helpers.toFileName(
name name
)}`, )}`,
] ]
......
import type { NodePlopAPI } from 'node-plop'
export function toFileName(str: string) {
return str.toLowerCase().replace(/ /g, '-')
}
export function init(plop: NodePlopAPI): void {
plop.setHelper('toFileName', toFileName)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment