File size: 4,210 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import { type NodePlopAPI } from 'node-plop'
import path from 'path'
import * as helpers from './helpers'
interface TestResponse {
appDir: string
type: 'e2e' | 'production' | 'development' | 'unit'
name: string
}
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',
prompts: [
{
type: 'confirm',
name: 'appDir',
message: 'Is this test for the app directory?',
default: true,
},
{
type: 'input',
name: 'name',
message: 'Test name',
validate: validateNonEmptyString('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 (answers) {
const { appDir, type, name } = answers as TestResponse
const basePath = plop.getDestBasePath()
const testRoot = path.join(basePath, 'test')
const appDirPath = appDir ? 'app-dir/' : ''
const templatePath = path.join(
testRoot,
type === 'unit' ? 'unit' : 'e2e',
appDirPath,
'test-template'
)
const targetPath = path.join(testRoot, type, appDirPath)
const cnaTemplatePath = path.join(
basePath,
'packages/create-next-app/templates',
appDir ? 'app-empty' : 'default-empty',
'ts'
)
return [
{
type: 'addMany',
templateFiles: path.join(templatePath, '**/*'),
base: templatePath,
destination: targetPath,
},
{
type: 'add',
templateFile: path.join(cnaTemplatePath, 'tsconfig.json'),
path: path.join(targetPath, name, 'tsconfig.json'),
transform: (template: string) =>
template.replace(
'"exclude": ["node_modules"]',
'"exclude": ["node_modules", "**/*.test.ts", "**/*.test.tsx"]'
),
},
{
type: 'add',
templateFile: path.join(cnaTemplatePath, 'next-env.d.ts'),
path: path.join(targetPath, name, 'next-env.d.ts'),
},
]
},
})
plop.setGenerator('error', {
description: 'Create a new error document',
prompts: [
{
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 (answers) {
const { name } = answers as ErrorResponse
const errorsRoot = path.join(plop.getDestBasePath(), 'errors')
return [
{
type: 'add',
path: path.join(errorsRoot, `{{ toFileName name }}.mdx`),
templateFile: path.join(errorsRoot, `template.txt`),
},
`Url for the error: https://nextjs.org/docs/messages/${helpers.toFileName(
name
)}`,
]
},
})
}
|