1
1
import { mkdirSync } from "fs" ;
2
- import { updateStatus } from "helpers/cli" ;
2
+ import { updateStatus , warn } from "helpers/cli" ;
3
3
import { brandColor , dim } from "helpers/colors" ;
4
4
import { installPackages , runFrameworkGenerator } from "helpers/command" ;
5
- import { probePaths , usesTypescript , writeFile } from "helpers/files" ;
5
+ import {
6
+ probePaths ,
7
+ readJSON ,
8
+ usesEslint ,
9
+ usesTypescript ,
10
+ writeFile ,
11
+ writeJSON ,
12
+ } from "helpers/files" ;
13
+ import { processArgument } from "helpers/interactive" ;
6
14
import { detectPackageManager } from "helpers/packages" ;
7
15
import { getFrameworkVersion } from "../index" ;
8
16
import {
@@ -11,7 +19,7 @@ import {
11
19
apiPagesDirHelloJs ,
12
20
apiPagesDirHelloTs ,
13
21
} from "./templates" ;
14
- import type { PagesGeneratorContext , FrameworkConfig } from "types" ;
22
+ import type { PagesGeneratorContext , FrameworkConfig , C3Args } from "types" ;
15
23
16
24
const { npm, npx, dlx } = detectPackageManager ( ) ;
17
25
@@ -72,16 +80,65 @@ const configure = async (ctx: PagesGeneratorContext) => {
72
80
writeFile ( handlerPath , handlerFile ) ;
73
81
updateStatus ( "Created an example API route handler" ) ;
74
82
83
+ const installEslintPlugin = await shouldInstallNextOnPagesEslintPlugin ( ctx ) ;
84
+
85
+ if ( installEslintPlugin ) {
86
+ await writeEslintrc ( ctx ) ;
87
+ }
88
+
75
89
// Add some dev dependencies
76
90
process . chdir ( projectName ) ;
77
- const packages = [ "@cloudflare/next-on-pages@1" , "vercel" ] ;
91
+ const packages = [
92
+ "@cloudflare/next-on-pages@1" ,
93
+ "vercel" ,
94
+ ...( installEslintPlugin ? [ "eslint-plugin-next-on-pages" ] : [ ] ) ,
95
+ ] ;
78
96
await installPackages ( packages , {
79
97
dev : true ,
80
98
startText : "Adding the Cloudflare Pages adapter" ,
81
99
doneText : `${ brandColor ( `installed` ) } ${ dim ( packages . join ( ", " ) ) } ` ,
82
100
} ) ;
83
101
} ;
84
102
103
+ export const shouldInstallNextOnPagesEslintPlugin = async (
104
+ ctx : PagesGeneratorContext
105
+ ) : Promise < boolean > => {
106
+ const eslintUsage = usesEslint ( ctx ) ;
107
+
108
+ if ( ! eslintUsage . used ) return false ;
109
+
110
+ if ( eslintUsage . configType !== ".eslintrc.json" ) {
111
+ warn (
112
+ `Expected .eslintrc.json from Next.js scaffolding but found ${ eslintUsage . configType } instead`
113
+ ) ;
114
+ return false ;
115
+ }
116
+
117
+ return await processArgument ( ctx . args , "eslint-plugin" as keyof C3Args , {
118
+ type : "confirm" ,
119
+ question : "Do you want to use the next-on-pages eslint-plugin?" ,
120
+ label : "eslint-plugin" ,
121
+ defaultValue : true ,
122
+ } ) ;
123
+ } ;
124
+
125
+ export const writeEslintrc = async (
126
+ ctx : PagesGeneratorContext
127
+ ) : Promise < void > => {
128
+ const eslintConfig = readJSON ( `${ ctx . project . name } /.eslintrc.json` ) ;
129
+
130
+ eslintConfig . plugins ??= [ ] ;
131
+ eslintConfig . plugins . push ( "eslint-plugin-next-on-pages" ) ;
132
+
133
+ if ( typeof eslintConfig . extends === "string" ) {
134
+ eslintConfig . extends = [ eslintConfig . extends ] ;
135
+ }
136
+ eslintConfig . extends ??= [ ] ;
137
+ eslintConfig . extends . push ( "plugin:eslint-plugin-next-on-pages/recommended" ) ;
138
+
139
+ writeJSON ( `${ ctx . project . name } /.eslintrc.json` , eslintConfig , 2 ) ;
140
+ } ;
141
+
85
142
const config : FrameworkConfig = {
86
143
generate,
87
144
configure,
0 commit comments