2023-11-03 02:25:18 +01:00
|
|
|
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)
|
2022-01-06 16:45:04 +01:00
|
|
|
|
|
|
|
plop.setGenerator('test', {
|
|
|
|
description: 'Create a new test',
|
|
|
|
prompts: [
|
2022-12-19 20:08:50 +01:00
|
|
|
{
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'appDir',
|
|
|
|
message: 'Is this test for the app directory?',
|
2023-03-16 21:46:35 +01:00
|
|
|
default: true,
|
2022-12-19 20:08:50 +01:00
|
|
|
},
|
2022-01-06 16:45:04 +01:00
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
name: 'name',
|
|
|
|
message: 'Test name',
|
2023-11-03 02:25:18 +01:00
|
|
|
validate: validateNonEmptyString('test name'),
|
2022-01-06 16:45:04 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'list',
|
|
|
|
name: 'type',
|
|
|
|
message: 'Test type',
|
2022-02-21 19:09:32 +01:00
|
|
|
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' },
|
|
|
|
],
|
2022-01-06 16:45:04 +01:00
|
|
|
},
|
|
|
|
],
|
2023-11-03 02:25:18 +01:00
|
|
|
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)
|
2023-01-06 21:36:47 +01:00
|
|
|
|
2022-01-06 16:45:04 +01:00
|
|
|
return [
|
|
|
|
{
|
2023-01-06 21:36:47 +01:00
|
|
|
type: 'addMany',
|
|
|
|
templateFiles: `${templatePath}/**/*`,
|
|
|
|
base: templatePath,
|
|
|
|
destination: targetPath,
|
2022-01-06 16:45:04 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
plop.setGenerator('error', {
|
|
|
|
description: 'Create a new error document',
|
|
|
|
prompts: [
|
2022-09-16 23:54:58 +02:00
|
|
|
{
|
2023-01-25 01:47:48 +01:00
|
|
|
name: 'name',
|
2022-09-16 23:54:58 +02:00
|
|
|
type: 'input',
|
|
|
|
message: 'Url path with dashes. E.g. circular-structure',
|
2023-11-03 02:25:18 +01:00
|
|
|
validate: validateNonEmptyString('path'),
|
2022-09-16 23:54:58 +02:00
|
|
|
},
|
2022-01-06 16:45:04 +01:00
|
|
|
{
|
|
|
|
name: 'title',
|
|
|
|
type: 'input',
|
2022-09-16 23:54:58 +02:00
|
|
|
message: 'Title for the error. E.g. Circular Structure',
|
2023-11-03 02:25:18 +01:00
|
|
|
validate: validateNonEmptyString('title'),
|
2022-09-16 23:54:58 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'why',
|
|
|
|
type: 'input',
|
|
|
|
message: 'What caused the error to happen?',
|
2023-11-03 02:25:18 +01:00
|
|
|
validate: validateNonEmptyString('why'),
|
2022-09-16 23:54:58 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'fix',
|
|
|
|
type: 'input',
|
|
|
|
message: 'What are the possible ways to fix it?',
|
2023-11-03 02:25:18 +01:00
|
|
|
validate: validateNonEmptyString('fix'),
|
2022-01-06 16:45:04 +01:00
|
|
|
},
|
|
|
|
],
|
2023-11-03 02:25:18 +01:00
|
|
|
actions: function (answers) {
|
|
|
|
const { name } = answers as ErrorResponse
|
2024-01-04 20:22:21 +01:00
|
|
|
const errorsRoot = path.join(plop.getDestBasePath(), 'errors')
|
2023-11-03 02:25:18 +01:00
|
|
|
|
2022-01-06 16:45:04 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
type: 'add',
|
2024-01-04 20:22:21 +01:00
|
|
|
path: path.join(errorsRoot, `{{ toFileName name }}.mdx`),
|
|
|
|
templateFile: path.join(errorsRoot, `template.txt`),
|
2022-01-06 16:45:04 +01:00
|
|
|
},
|
2023-11-03 02:25:18 +01:00
|
|
|
`Url for the error: https://nextjs.org/docs/messages/${helpers.toFileName(
|
2023-01-25 01:47:48 +01:00
|
|
|
name
|
|
|
|
)}`,
|
2022-01-06 16:45:04 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|