1 | import util from 'util'
|
2 | import fs from 'fs'
|
3 | import path from 'path'
|
4 | import glob from 'glob'
|
5 | import { Profiler, Project } from './types'
|
6 |
|
7 | const ncp = util.promisify(require('ncp').ncp)
|
8 |
|
9 | const templateFile = (fileName: string, replacements: Profiler) => {
|
10 | const fileContent = fs.readFileSync(fileName, 'utf8').toString()
|
11 |
|
12 | const template = Object.entries(replacements).reduce((acc, [key, value]) => {
|
13 | return acc.replace(
|
14 | new RegExp(`(\{\{${key}\}\}|\{\{ ${key} \}\})`, 'g'),
|
15 | value?.toString() ?? ''
|
16 | )
|
17 | }, fileContent)
|
18 | fs.writeFileSync(fileName, template)
|
19 | }
|
20 |
|
21 | const renameGitignore = (projectName: string) => {
|
22 | if (fs.existsSync(path.normalize(`${projectName}/gitignore`))) {
|
23 | fs.renameSync(
|
24 | path.normalize(`${projectName}/gitignore`),
|
25 | path.normalize(`${projectName}/.gitignore`)
|
26 | )
|
27 | }
|
28 | }
|
29 | const renameStoryBook = (projectName: string) => {
|
30 | if (fs.existsSync(path.normalize(`${projectName}/storybook`))) {
|
31 | fs.renameSync(
|
32 | path.normalize(`${projectName}/storybook`),
|
33 | path.normalize(`${projectName}/.storybook`)
|
34 | )
|
35 | }
|
36 | }
|
37 |
|
38 | const buildProfiler = ({
|
39 | type,
|
40 | toolsbuild,
|
41 | typeweb,
|
42 | framework,
|
43 | language,
|
44 | name,
|
45 | css,
|
46 | port,
|
47 | }: Project) => {
|
48 | const profiler: Profiler = {
|
49 | NAME: name,
|
50 | TOOLSBUILD: toolsbuild,
|
51 | TYPEWEB: typeweb === 'SPA' ? 'SPA' : 'SSR',
|
52 | FRAMEWORK: framework,
|
53 | CSS: css,
|
54 | SAFE_NAME: name.replace(/-/g, '_').trim(),
|
55 | LANGUAGE: language === 'typescript' || framework === 'angular' ? 'TypeScript' : 'JavaScript',
|
56 | }
|
57 |
|
58 | if (type === 'StoryBook' || type === 'Application' || type === 'Flutter' ) {
|
59 | profiler.PORT = port
|
60 | }
|
61 | return profiler
|
62 | }
|
63 |
|
64 | export const buildProject = async (project: Project) => {
|
65 | const { language, name, framework, typeweb, type, toolsbuild } = project
|
66 | const lang = language === 'typescript' ? 'ts' : 'js'
|
67 | const tempDir = type.toLowerCase()
|
68 | const profiler = buildProfiler(project)
|
69 |
|
70 | switch (type) {
|
71 | case 'Packages':
|
72 | await ncp(
|
73 | path.join(__dirname, `../templates/${tempDir}`),
|
74 | project.name
|
75 | )
|
76 | break
|
77 | case 'Flutter':
|
78 | await ncp(
|
79 | path.join(__dirname, `../templates/${tempDir}`),
|
80 | project.name
|
81 | )
|
82 | break
|
83 | case 'StoryBook':
|
84 | await ncp(
|
85 | path.join(__dirname, `../templates/${tempDir}/${framework}/base`),
|
86 | name
|
87 | )
|
88 | await ncp(
|
89 | path.join(__dirname, `../templates/${tempDir}/${framework}/${lang}`),
|
90 | name
|
91 | )
|
92 | break
|
93 | case 'SingleSpa':
|
94 | {
|
95 | await ncp(
|
96 | path.join(__dirname, `../templates/${tempDir}/${framework}`),
|
97 | name
|
98 | )
|
99 |
|
100 | }
|
101 | break
|
102 | case 'Application':
|
103 | {
|
104 | if(framework === 'angular' || (toolsbuild === 'Vite' && framework === 'react')) {
|
105 | await ncp(
|
106 | path.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}`),
|
107 | project.name
|
108 | )
|
109 | } else {
|
110 | await ncp(
|
111 | path.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}/base`),
|
112 | name
|
113 | )
|
114 | await ncp(
|
115 | path.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}/${lang}`),
|
116 | name
|
117 | )
|
118 | }
|
119 |
|
120 | if (profiler.CSS === 'Tailwind') {
|
121 | profiler.CONTAINER = 'mt-10 text-3xl mx-auto max-w-6xl'
|
122 | profiler.CSS_EXTENSION = 'scss'
|
123 | fs.unlinkSync(path.normalize(`${name}/src/styles/index.css`))
|
124 | await ncp(
|
125 | path.join(__dirname, '../templates/application-extras/tailwind'),
|
126 | name
|
127 | )
|
128 |
|
129 | const packageJSON = JSON.parse(
|
130 | fs.readFileSync(path.join(name, 'package.json'), 'utf8')
|
131 | )
|
132 | packageJSON.devDependencies.tailwindcss = '^2.0.2'
|
133 | fs.writeFileSync(
|
134 | path.join(name, 'package.json'),
|
135 | JSON.stringify(packageJSON, null, 2)
|
136 | )
|
137 | }
|
138 |
|
139 | if (profiler.CSS === 'Bootsrap') {
|
140 | profiler.CONTAINER = 'container'
|
141 | profiler.CSS_EXTENSION = 'scss'
|
142 | fs.unlinkSync(path.normalize(`${name}/src/styles/index.css`))
|
143 | await ncp(
|
144 | path.join(__dirname, '../templates/application-extras/bootstrap'),
|
145 | name
|
146 | )
|
147 |
|
148 | const packageJSON = JSON.parse(
|
149 | fs.readFileSync(path.join(name, 'package.json'), 'utf8')
|
150 | )
|
151 | packageJSON.devDependencies.bootstrap = '^5.2.3'
|
152 | fs.writeFileSync(
|
153 | path.join(name, 'package.json'),
|
154 | JSON.stringify(packageJSON, null, 2)
|
155 | )
|
156 | }
|
157 |
|
158 | if (profiler.CSS === 'CSS') {
|
159 | profiler.CONTAINER = 'container'
|
160 | profiler.CSS_EXTENSION = 'css'
|
161 | }
|
162 |
|
163 | }
|
164 | break
|
165 | }
|
166 | renameGitignore(name)
|
167 |
|
168 | glob.sync(`${name}/**/*`).forEach((file) => {
|
169 | if (fs.lstatSync(file).isFile()) {
|
170 | templateFile(file, profiler)
|
171 | }
|
172 | })
|
173 | renameStoryBook(name)
|
174 | }
|