UNPKG

6.42 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.buildProject = void 0;
16const util_1 = __importDefault(require("util"));
17const fs_1 = __importDefault(require("fs"));
18const path_1 = __importDefault(require("path"));
19const glob_1 = __importDefault(require("glob"));
20const ncp = util_1.default.promisify(require('ncp').ncp);
21const templateFile = (fileName, replacements) => {
22 const fileContent = fs_1.default.readFileSync(fileName, 'utf8').toString();
23 const template = Object.entries(replacements).reduce((acc, [key, value]) => {
24 var _a;
25 return acc.replace(new RegExp(`(\{\{${key}\}\}|\{\{ ${key} \}\})`, 'g'), (_a = value === null || value === void 0 ? void 0 : value.toString()) !== null && _a !== void 0 ? _a : '');
26 }, fileContent);
27 fs_1.default.writeFileSync(fileName, template);
28};
29const renameGitignore = (projectName) => {
30 if (fs_1.default.existsSync(path_1.default.normalize(`${projectName}/gitignore`))) {
31 fs_1.default.renameSync(path_1.default.normalize(`${projectName}/gitignore`), path_1.default.normalize(`${projectName}/.gitignore`));
32 }
33};
34const renameStoryBook = (projectName) => {
35 if (fs_1.default.existsSync(path_1.default.normalize(`${projectName}/storybook`))) {
36 fs_1.default.renameSync(path_1.default.normalize(`${projectName}/storybook`), path_1.default.normalize(`${projectName}/.storybook`));
37 }
38};
39const buildProfiler = ({ type, toolsbuild, typeweb, framework, language, name, css, port, }) => {
40 const profiler = {
41 NAME: name,
42 TOOLSBUILD: toolsbuild,
43 TYPEWEB: typeweb === 'SPA' ? 'SPA' : 'SSR',
44 FRAMEWORK: framework,
45 CSS: css,
46 SAFE_NAME: name.replace(/-/g, '_').trim(),
47 LANGUAGE: language === 'typescript' || framework === 'angular' ? 'TypeScript' : 'JavaScript',
48 };
49 if (type === 'StoryBook' || type === 'Application' || type === 'Flutter') {
50 profiler.PORT = port;
51 }
52 return profiler;
53};
54const buildProject = (project) => __awaiter(void 0, void 0, void 0, function* () {
55 const { language, name, framework, typeweb, type, toolsbuild } = project;
56 const lang = language === 'typescript' ? 'ts' : 'js';
57 const tempDir = type.toLowerCase();
58 const profiler = buildProfiler(project);
59 switch (type) {
60 case 'Packages':
61 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}`), project.name);
62 break;
63 case 'Flutter':
64 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}`), project.name);
65 break;
66 case 'StoryBook':
67 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${framework}/base`), name);
68 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${framework}/${lang}`), name);
69 break;
70 case 'SingleSpa':
71 {
72 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${framework}`), name);
73 }
74 break;
75 case 'Application':
76 {
77 if (framework === 'angular' || (toolsbuild === 'Vite' && framework === 'react')) {
78 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}`), project.name);
79 }
80 else {
81 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}/base`), name);
82 yield ncp(path_1.default.join(__dirname, `../templates/${tempDir}/${toolsbuild}/${typeweb}/${framework}/${lang}`), name);
83 }
84 if (profiler.CSS === 'Tailwind') {
85 profiler.CONTAINER = 'mt-10 text-3xl mx-auto max-w-6xl';
86 profiler.CSS_EXTENSION = 'scss';
87 fs_1.default.unlinkSync(path_1.default.normalize(`${name}/src/styles/index.css`));
88 yield ncp(path_1.default.join(__dirname, '../templates/application-extras/tailwind'), name);
89 const packageJSON = JSON.parse(fs_1.default.readFileSync(path_1.default.join(name, 'package.json'), 'utf8'));
90 packageJSON.devDependencies.tailwindcss = '^2.0.2';
91 fs_1.default.writeFileSync(path_1.default.join(name, 'package.json'), JSON.stringify(packageJSON, null, 2));
92 }
93 if (profiler.CSS === 'Bootsrap') {
94 profiler.CONTAINER = 'container';
95 profiler.CSS_EXTENSION = 'scss';
96 fs_1.default.unlinkSync(path_1.default.normalize(`${name}/src/styles/index.css`));
97 yield ncp(path_1.default.join(__dirname, '../templates/application-extras/bootstrap'), name);
98 const packageJSON = JSON.parse(fs_1.default.readFileSync(path_1.default.join(name, 'package.json'), 'utf8'));
99 packageJSON.devDependencies.bootstrap = '^5.2.3';
100 fs_1.default.writeFileSync(path_1.default.join(name, 'package.json'), JSON.stringify(packageJSON, null, 2));
101 }
102 if (profiler.CSS === 'CSS') {
103 profiler.CONTAINER = 'container';
104 profiler.CSS_EXTENSION = 'css';
105 }
106 }
107 break;
108 }
109 renameGitignore(name);
110 glob_1.default.sync(`${name}/**/*`).forEach((file) => {
111 if (fs_1.default.lstatSync(file).isFile()) {
112 templateFile(file, profiler);
113 }
114 });
115 renameStoryBook(name);
116});
117exports.buildProject = buildProject;