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 @@
"packages/*"
],
"scripts": {
"new-error": "plop error",
"new-test": "plop test",
"new-error": "turbo gen error",
"new-test": "turbo gen test",
"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",
"lerna": "lerna",
......@@ -173,6 +173,7 @@
"nanoid": "3.1.30",
"next": "workspace:*",
"node-fetch": "2.6.7",
"node-plop": "0.31.1",
"npm-run-all": "4.1.5",
"nprogress": "0.2.0",
"octokit": "3.1.0",
......@@ -181,7 +182,6 @@
"pixrem": "5.0.0",
"playwright-chromium": "1.35.1",
"playwright-core": "1.35.1",
"plop": "3.0.5",
"postcss": "8.4.31",
"postcss-nested": "4.2.1",
"postcss-pseudoelements": "5.0.0",
......
This diff is collapsed.
module.exports = function (plop) {
const toFileName = (str) => str.toLowerCase().replace(/ /g, '-')
plop.setHelper('toFileName', toFileName)
import { type NodePlopAPI } from 'node-plop'
import path from 'path'
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', {
description: 'Create a new test',
......@@ -15,6 +40,7 @@ module.exports = function (plop) {
type: 'input',
name: 'name',
message: 'Test name',
validate: validateNonEmptyString('test name'),
},
{
type: 'list',
......@@ -34,12 +60,18 @@ module.exports = function (plop) {
],
},
],
actions: function (data) {
const appDirPath = data.appDir ? 'app-dir/' : ''
let templatePath = `test/${
data.type === 'unit' ? 'unit' : 'e2e'
}/${appDirPath}test-template`
let targetPath = `test/{{ type }}/${appDirPath}`
actions: function (answers) {
const { appDir, type } = answers as TestResponse
const testRoot = path.join(plop.getDestBasePath(), 'test')
const appDirPath = appDir ? 'app-dir/' : ''
let templatePath = path.join(
testRoot,
type === 'unit' ? 'unit' : 'e2e',
appDirPath,
'test-template'
)
let targetPath = path.join(testRoot, type, appDirPath)
return [
{
......@@ -59,24 +91,30 @@ module.exports = function (plop) {
name: 'name',
type: 'input',
message: 'Url path with dashes. E.g. circular-structure',
validate: validateNonEmptyString('path'),
},
{
name: 'title',
type: 'input',
message: 'Title for the error. E.g. Circular Structure',
validate: validateNonEmptyString('title'),
},
{
name: 'why',
type: 'input',
message: 'What caused the error to happen?',
validate: validateNonEmptyString('why'),
},
{
name: 'fix',
type: 'input',
message: 'What are the possible ways to fix it?',
validate: validateNonEmptyString('fix'),
},
],
actions: function ({ name }) {
actions: function (answers) {
const { name } = answers as ErrorResponse
return [
{
type: 'add',
......@@ -86,16 +124,16 @@ module.exports = function (plop) {
{
type: 'modify',
path: 'errors/manifest.json',
transform(fileContents) {
transform(fileContents: string) {
const manifestData = JSON.parse(fileContents)
manifestData.routes[0].routes.push({
title: toFileName(name),
path: `/errors/${toFileName(name)}.md`,
title: helpers.toFileName(name),
path: `/errors/${helpers.toFileName(name)}.md`,
})
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
)}`,
]
......
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