rsnext/plopfile.js
Jan Kaifer 015a7dd9ee
Change test template to use TS and improve template for app-dir (#44227)
Make templates actual executable tests to that we ensure there are no regressions.
It also makes the setup easier.

Also changes the layout to typescript because that's what we want to use by default anyway.

Also refactors helper function to use plop specific `{{ toFileName name }}` syntax for easier template modification.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-01-06 20:36:47 +00:00

101 lines
2.7 KiB
JavaScript

module.exports = function (plop) {
plop.setHelper('toFileName', (str) => str.toLowerCase().replace(/ /g, '-'))
plop.setGenerator('test', {
description: 'Create a new test',
prompts: [
{
type: 'confirm',
name: 'appDir',
message: 'Is this test for the app directory?',
default: false,
},
{
type: 'input',
name: 'name',
message: 'Test name',
},
{
type: 'list',
name: 'type',
message: 'Test type',
choices: [
{
name: 'e2e - Test "next dev" and "next build && next start"',
value: 'e2e',
},
{
name: 'production - Test "next build && next start"',
value: 'production',
},
{ name: 'development - Test "next dev"', value: 'development' },
{ name: 'unit - Test individual files', value: 'unit' },
],
},
],
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}`
return [
{
type: 'addMany',
templateFiles: `${templatePath}/**/*`,
base: templatePath,
destination: targetPath,
},
]
},
})
plop.setGenerator('error', {
description: 'Create a new error document',
prompts: [
{
name: 'urlPath',
type: 'input',
message: 'Url path with dashes. E.g. circular-structure',
},
{
name: 'title',
type: 'input',
message: 'Title for the error. E.g. Circular Structure',
},
{
name: 'why',
type: 'input',
message: 'What caused the error to happen?',
},
{
name: 'fix',
type: 'input',
message: 'What are the possible ways to fix it?',
},
],
actions: function (data) {
return [
{
type: 'add',
path: `errors/{{ toFileName name }}.md`,
templateFile: `errors/template.txt`,
},
{
type: 'modify',
path: 'errors/manifest.json',
transform(fileContents, data) {
const manifestData = JSON.parse(fileContents)
manifestData.routes[0].routes.push({
title: '{{ toFileName name }}',
path: `/errors/{{ toFileName name }}.md`,
})
return JSON.stringify(manifestData, null, 2)
},
},
`Url for the error: https://nextjs.org/docs/messages/{{ toFileName name }}`,
]
},
})
}