|
1 | 1 | 'use server'; |
2 | 2 |
|
3 | | -import { readFileSync, appendFileSync, mkdirSync, existsSync } from 'fs'; |
| 3 | +import { readFileSync, appendFileSync, existsSync } from 'fs'; |
4 | 4 | import { isServer } from './helper'; |
5 | | -import { join, dirname } from 'path'; |
6 | | -import * as fs from 'fs'; |
7 | | - |
8 | | -// Those affected by builds that exist in dist |
9 | | -// Functions required on the outside and inside |
10 | | -async function findProjectRoot(startPath: string): Promise<string | null> { |
11 | | - if (!isServer) return null; |
12 | | - let currentPath = startPath; |
13 | | - while (currentPath !== '/') { |
14 | | - const packageJsonPath = join(currentPath, 'package.json'); |
15 | | - const lockFiles = ['pnpm-lock.yaml', 'package-lock.json', 'yarn.lock', 'bun.lockb'].some(file => fs.existsSync(join(currentPath, file))); |
16 | | - if (fs.existsSync(packageJsonPath) && lockFiles) { |
17 | | - return currentPath; |
18 | | - } |
19 | | - currentPath = dirname(currentPath); |
20 | | - } |
21 | | - return null; |
22 | | -} |
| 5 | +import { join } from 'path'; |
23 | 6 |
|
24 | 7 | export const buildIn = async (styleSheet: string, global?: string): Promise<void> => { |
25 | | - const projectRoot = await findProjectRoot(__dirname); |
26 | | - |
27 | | - if (!projectRoot) { |
28 | | - console.error('Next.js project root not found'); |
29 | | - return; |
30 | | - } |
31 | | - |
32 | | - const srcDir = join(projectRoot, 'src'); |
33 | | - let stylesDir: string; |
34 | | - |
35 | | - if (existsSync(srcDir)) { |
36 | | - stylesDir = join(srcDir, 'styles'); |
37 | | - } else { |
38 | | - stylesDir = join(projectRoot, 'styles'); |
39 | | - } |
40 | | - |
41 | 8 | const styleFilePath = join(__dirname, '../../core/styles/style.module.css'); |
42 | | - const globalFilePath = join(stylesDir, 'typedcssx-global.css'); |
| 9 | + const globalFilePath = join(__dirname, '../../core/styles/global.css'); |
43 | 10 |
|
44 | 11 | const filePath = global === '--global' ? globalFilePath : styleFilePath; |
45 | 12 | const message = global === '--global' ? ' ✅ Generating global static css \n' : ' ✅ Generating module static css \n'; |
46 | 13 |
|
47 | | - if (!existsSync(stylesDir)) { |
48 | | - mkdirSync(stylesDir); |
49 | | - } |
50 | | - |
51 | | - if (isServer) { |
52 | | - try { |
53 | | - if (existsSync(filePath)) { |
54 | | - const cssData = readFileSync(filePath, 'utf-8'); |
55 | | - if (!cssData.includes(styleSheet)) { |
56 | | - appendFileSync(filePath, styleSheet, 'utf-8'); |
57 | | - console.log(message + styleSheet); |
58 | | - } |
59 | | - } else { |
| 14 | + if (!isServer) return; |
| 15 | + try { |
| 16 | + if (existsSync(filePath)) { |
| 17 | + const cssData = readFileSync(filePath, 'utf-8'); |
| 18 | + if (!cssData.includes(styleSheet)) { |
60 | 19 | appendFileSync(filePath, styleSheet, 'utf-8'); |
61 | 20 | console.log(message + styleSheet); |
62 | 21 | } |
63 | | - } catch (error) { |
64 | | - console.error('Error writing to file:', error); |
65 | | - console.error('Stack trace:', error); |
| 22 | + } else { |
| 23 | + appendFileSync(filePath, styleSheet, 'utf-8'); |
| 24 | + console.log(message + styleSheet); |
66 | 25 | } |
| 26 | + } catch (error) { |
| 27 | + console.error('Error writing to file:', error); |
| 28 | + console.error('Stack trace:', error); |
67 | 29 | } |
68 | 30 | }; |
0 commit comments