Skip to content

Commit 97b967c

Browse files
committed
feat: add regex in all nextjs plugin to add providers in layout file
1 parent d98120f commit 97b967c

File tree

19 files changed

+162
-132
lines changed

19 files changed

+162
-132
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ async function main() {
8989
//antD or material ui
9090
await uiLibraryAdder(selectedLibrary);
9191

92-
//adds Provider in layout or index files
93-
await pluginEntryAdder();
94-
9592
//i18
9693
await i18nAdder(addI18n);
9794

95+
//adds Provider in layout or index files
96+
await pluginEntryAdder();
97+
9898
//install dependencies of the plugins
9999
await pluginDependencyAdder();
100100

src/operation/i18n/i18n.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import path from "path";
2+
import getCurrentProject from "@/operation/getProjectType";
3+
import {
4+
isFileExists,
5+
moveAllFilesToSubDir,
6+
writeFileFromConfig,
7+
} from "@/utils/file";
8+
import fs from "fs";
9+
import i18NReactPlugin from "@/plugins/react/i18n";
10+
import i18nNextPlugin from "@/plugins/nextjs/i18n";
11+
12+
export default async function i18nAdder(addI18n: boolean) {
13+
addI18n && (await addI18nInProject());
14+
}
15+
16+
async function addI18nInProject() {
17+
const projectType = getCurrentProject();
18+
19+
//based on the project type run the configuration
20+
switch (projectType) {
21+
case "next":
22+
await addI18nInNext();
23+
break;
24+
case "react":
25+
await addI18nInReact();
26+
break;
27+
default:
28+
break;
29+
}
30+
}
31+
32+
async function addI18nInNext() {
33+
await writeFileFromConfig(i18nNextPlugin);
34+
35+
const isTsProject = isFileExists(process.cwd(), "tsconfig.json");
36+
37+
const rootLayoutName = `${isTsProject ? "layout.tsx" : "layout.js"}`;
38+
39+
const file = fs
40+
.readFileSync(path.join(process.cwd(), "src", "app", rootLayoutName))
41+
.toString();
42+
43+
const rootLayoutParams = `RootLayout({
44+
children,
45+
params,
46+
}${
47+
isTsProject
48+
? `: {
49+
children: React.ReactNode;
50+
params: { lang: Locale };
51+
}`
52+
: ""
53+
})`;
54+
55+
const newFileContent = file.replace(/RootLayout\([^)]*\)/, rootLayoutParams);
56+
57+
fs.writeFileSync(
58+
path.join(process.cwd(), "src", "app", rootLayoutName),
59+
newFileContent
60+
);
61+
62+
moveAllFilesToSubDir(path.join(process.cwd(), "src", "app"), "[lang]");
63+
}
64+
65+
async function addI18nInReact() {
66+
await writeFileFromConfig(i18NReactPlugin);
67+
}

src/operation/i18n/index.ts

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1 @@
1-
import path from "path";
2-
import getCurrentProject from "@/operation/getProjectType";
3-
import {
4-
isFileExists,
5-
moveAllFilesToSubDir,
6-
writeFileFromConfig,
7-
} from "@/utils/file";
8-
import fs from "fs";
9-
import i18NReactPlugin from "@/plugins/react/i18n";
10-
import i18nNextPlugin from "@/plugins/nextjs/i18n";
11-
12-
export default async function i18nAdder(addI18n: boolean) {
13-
addI18n && (await addI18nInProject());
14-
}
15-
16-
async function addI18nInProject() {
17-
const projectType = getCurrentProject();
18-
19-
//based on the project type run the configuration
20-
switch (projectType) {
21-
case "next":
22-
await addI18nInNext();
23-
break;
24-
case "react":
25-
await addI18nInReact();
26-
break;
27-
default:
28-
break;
29-
}
30-
}
31-
32-
async function addI18nInNext() {
33-
await writeFileFromConfig(i18nNextPlugin);
34-
35-
const isTsProject = isFileExists(process.cwd(), "tsconfig.json");
36-
37-
const rootLayoutName = `${isTsProject ? "layout.tsx" : "layout.js"}`;
38-
39-
const file = fs
40-
.readFileSync(path.join(process.cwd(), "src", "app", rootLayoutName))
41-
.toString();
42-
43-
const rootLayoutParams = `RootLayout({
44-
children,
45-
params,
46-
}${
47-
isTsProject
48-
? `: {
49-
children: React.ReactNode;
50-
params: { lang: Locale };
51-
}`
52-
: ""
53-
})`;
54-
55-
const newFileContent = file.replace(/RootLayout\([^)]*\)/, rootLayoutParams);
56-
57-
fs.writeFileSync(
58-
path.join(process.cwd(), "src", "app", rootLayoutName),
59-
newFileContent
60-
);
61-
62-
moveAllFilesToSubDir(path.join(process.cwd(), "src", "app"), "[lang]");
63-
}
64-
65-
async function addI18nInReact() {
66-
await writeFileFromConfig(i18NReactPlugin);
67-
}
1+
export { default } from "./i18n";

src/plugins/common/prettier/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const PrettierPlugin: PluginConfigType = {
2525
};
2626
},
2727
devDependencies: "prettier",
28-
successMessage: "Prettier added successfully !",
28+
successMessage: "Successfully added Prettier !",
2929
};
3030

3131
export default PrettierPlugin;

src/plugins/nextjs/antd/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const AntDNextPlugin: PluginConfigType = {
8282
importStatements: `import StyledComponentsRegistry from "@/lib/AntdRegistry";`,
8383
addAfterMatch: `</StyledComponentsRegistry>`,
8484
addBeforeMatch: `<StyledComponentsRegistry>`,
85+
regex: /{children}/,
8586
},
8687
Page: {},
8788
},

src/plugins/nextjs/graphql/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PluginConfigType } from "@/types";
2+
import { getRegexForRootComponent } from "@/utils/fileManipulation";
23

34
const envExFileContent = "NEXT_PUBLIC_BASE_URL";
45

@@ -135,9 +136,11 @@ const GraphQlNextPlugin: PluginConfigType = {
135136
importStatements: `import { ApolloWrapper } from "@/lib/ApolloWrapper";`,
136137
addAfterMatch: "</ApolloWrapper>",
137138
addBeforeMatch: "<ApolloWrapper>",
139+
regex: getRegexForRootComponent("body"),
138140
},
139141
Page: {},
140142
},
143+
successMessage: "Successfully added graphql with Apollo Wrapper !",
141144
};
142145

143146
export default GraphQlNextPlugin;

src/plugins/nextjs/husky/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function getLintStatedRcContent(isTsProject: boolean) {
2525
);
2626
}
2727
const HuskyNextPlugin: PluginConfigType = {
28+
initializingMessage: "Adding Husky ! Please wait.. ",
2829
files: [
2930
{
3031
content: getLintStatedRcContent,
@@ -33,6 +34,7 @@ const HuskyNextPlugin: PluginConfigType = {
3334
path: [],
3435
},
3536
],
37+
successMessage: "Successfully added Husky !",
3638
};
3739

3840
export default HuskyNextPlugin;

src/plugins/nextjs/i18n/config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ export default async function About({
248248

249249
const i18nNextPlugin: PluginConfigType = {
250250
initializingMessage: "Adding i18n, Please wait !",
251-
dependencies: "negotiator @formatjs/intl-localematcher",
251+
dependencies: function (isTsProject: boolean) {
252+
return `negotiator @formatjs/intl-localematcher ${
253+
isTsProject ? "@types/negotiator" : ""
254+
}`;
255+
},
252256
files: [
253257
{
254258
content: i18nDeDictionary,
@@ -319,10 +323,11 @@ const i18nNextPlugin: PluginConfigType = {
319323
import Header from "@/components/header";`,
320324
addAfterMatch: "",
321325
addBeforeMatch: "<Header lang={params.lang} />",
326+
regex: /{children}/,
322327
},
323328
Page: {},
324329
},
325-
successMessage: "Successfully added i18n in your NextJs application",
330+
successMessage: "Successfully added i18n with language routes !",
326331
};
327332

328333
export default i18nNextPlugin;

src/plugins/nextjs/mui/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PluginConfigType } from "@/types";
2+
import { getRegexForRootComponent } from "@/utils/fileManipulation";
23

34
const emotionCacheConfig = (isTsProject: boolean) =>
45
`"use client";
@@ -138,9 +139,11 @@ const MuiNextPlugin: PluginConfigType = {
138139
importStatements: `import ThemeRegistry from "@/theme/ThemeRegistry";`,
139140
addAfterMatch: `</ThemeRegistry>`,
140141
addBeforeMatch: `<ThemeRegistry>`,
142+
regex: getRegexForRootComponent("body"),
141143
},
142144
Page: {},
143145
},
146+
successMessage: "Successfully added Material UI with theme config !",
144147
};
145148

146149
export default MuiNextPlugin;

src/plugins/nextjs/reactQuery/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PluginConfigType } from "@/types";
2+
import { getRegexForRootComponent } from "@/utils/fileManipulation";
23

34
const envExFileContent = "NEXT_PUBLIC_BASE_URL";
45

@@ -329,9 +330,11 @@ const ReactQueryNextPlugin: PluginConfigType = {
329330
importStatements: `import { Providers } from "@/components/Providers/Providers.client";`,
330331
addAfterMatch: "</Providers>",
331332
addBeforeMatch: "<Providers>",
333+
regex: getRegexForRootComponent("body"),
332334
},
333335
Page: {},
334336
},
337+
successMessage: "Successfully added React Query with Provider config !",
335338
};
336339

337340
export default ReactQueryNextPlugin;

0 commit comments

Comments
 (0)