-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
139 lines (119 loc) · 6.24 KB
/
start.js
File metadata and controls
139 lines (119 loc) · 6.24 KB
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
import fs from 'node:fs';
import path from 'node:path';
import getProject from "./creation/mongodb.js";
import dotenv from 'dotenv';
import {mainLayout, pageLayout, localeLayout, noLocaleLayout} from './creation/layouts.js';
import createThemeColors from './creation/createThemeColors.js';
import generateSitemap from './creation/sitemap.js';
import generateRobots from './creation/robots.js';
dotenv.config({ path:'.env.local' });
// console.log('dotenv MONGO_STRING', process.env);
// console.log('VERCEL_GIT_REPO_ID check: ', process?.env?.VERCEL_GIT_REPO_ID);
const createProjectFileStructure = async (paramsPath) =>{
const params = fs.readFileSync(paramsPath);
const projectData = JSON.parse(params);
const projectDirPath = path.resolve(process.cwd(), 'src/app/');
const mainLayoutPath = path.resolve(process.cwd(), 'src/app/layout.tsx');
/*
navPages array for top menu:
* {pageName} - string, text name of the page
* {pageLink} - string, link on page
* array[{pageName:'', pageLink:''}]
*/
const navPages = [];
for(const page of projectData.pages){
navPages.push({pageName:page.name, pageLink:page.fileName});
// console.log('Page Name', page.name + '\n');
// console.log('Page fileName', page.fileName + '\n');
// console.log('Page robots', page.robots + '\n');
if(page.fileName == 'index'){
const indexPagePath = path.resolve(projectDirPath, 'page.tsx');
// console.log('indexPagePath', indexPagePath);
// remove page.tsx file before create new one
fs.rmSync(indexPagePath,{force:true});
fs.writeFileSync(indexPagePath, pageLayout(
page.fileName,
projectData.pages.find(p=>p.fileName == page.fileName),
process.env.NEXT_PUBLIC_PRIMARY_LOCALE
), {flag: "w", mode: 0o644});
if(projectData.languageSettings.additionalLanguages.length > 1){
const localeDirPath = path.resolve(projectDirPath, '[locale]');
const localeIndexPagePath = path.resolve(projectDirPath, '[locale]/page.tsx');
const localeLayoutIndexPagePath = path.resolve(projectDirPath, '[locale]/layout.tsx');
//locale dir creatioin
fs.mkdirSync(localeDirPath, {recursive:true});
fs.writeFileSync(localeIndexPagePath, pageLayout(
page.fileName,
projectData.pages.find(p=>p.fileName == page.fileName),
null
), {flag: "w", mode: 0o644});
fs.writeFileSync(localeLayoutIndexPagePath, localeLayout(
projectData.brandAttributes,
projectData.languageSettings,
page.fileName
), {flag: "w", mode: 0o644});
}
} else {
const pagePath = path.resolve(projectDirPath, `${page.fileName}/page.tsx`);
const layoutPagePath = path.resolve(projectDirPath, `${page.fileName}/layout.tsx`);
const pageDirPath = path.resolve(projectDirPath, `${page.fileName}`);
fs.rmSync(pagePath,{force:true, recursive:true});
fs.mkdirSync(pageDirPath, {recursive:true});
fs.writeFileSync(pagePath, pageLayout(
page.fileName,
projectData.pages.find(p=>p.fileName == page.fileName),
process.env.NEXT_PUBLIC_PRIMARY_LOCALE
), {flag: "w", mode: 0o644});
fs.writeFileSync(layoutPagePath, noLocaleLayout(
projectData.brandAttributes,
projectData.languageSettings,
page.fileName
), {flag: "w", mode: 0o644});
if(projectData.languageSettings.additionalLanguages.length > 1){
const localeDirPath = path.resolve(projectDirPath, `[locale]/${page.fileName}`);
const localeIndexPagePath = path.resolve(projectDirPath, `[locale]/${page.fileName}/page.tsx`);
const localeLayoutIndexPagePath = path.resolve(projectDirPath, `[locale]/${page.fileName}/layout.tsx`);
fs.mkdirSync(localeDirPath, {recursive:true});
fs.writeFileSync(localeIndexPagePath, pageLayout(
page.fileName,
projectData.pages.find(p=>p.fileName == page.fileName),
null
), {flag: "w", mode: 0o644});
fs.writeFileSync(localeLayoutIndexPagePath, localeLayout(
projectData.brandAttributes,
projectData.languageSettings,
page.fileName
), {flag: "w", mode: 0o644});
}
}
}
console.log('navPages', navPages);
fs.rmSync(mainLayoutPath,{force:true, recursive:true});
fs.writeFileSync(mainLayoutPath, mainLayout(
projectData.brandAttributes,
projectData.designTemplate.header,
projectData.designTemplate.footer,
projectData.designTemplate.contentWrapper,
projectData.languageSettings,
navPages
), {flag: "w", mode: 0o644});
const tailwindConfigPath = path.resolve(process.cwd(), 'tailwind.config.js');
fs.rmSync(tailwindConfigPath,{force:true, recursive:true});
fs.writeFileSync(tailwindConfigPath, createThemeColors(projectData.brandAttributes.colorPalette), {flag: "w", mode: 0o644});
const sitemapGenPath = path.resolve(process.cwd(), 'src/app/sitemap.ts');
fs.writeFileSync(sitemapGenPath, generateSitemap(projectData.languageSettings, projectData.pages), {flag: "w", mode: 0o644});
const robotsGenPath = path.resolve(process.cwd(), 'src/app/robots.ts');
fs.writeFileSync(robotsGenPath, generateRobots(), {flag: "w", mode: 0o644});
}
try{
const project = await getProject(process.env.NEXT_PUBLIC_PROJECT_NAME);
const projectPath = path.resolve(process.cwd(), 'creation/website_settings.json');
fs.rmSync(projectPath,{force:true});
fs.writeFileSync(projectPath, JSON.stringify(project), {flag: "w", mode: 0o644}, (err)=>{
throw new Error(`Failed when trying to write ${projectPath} data`);
});
await createProjectFileStructure(projectPath);
} catch(error){
console.error("Error while assembling website: ", error);
process.exit(1);
}