Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit b9e56f5

Browse files
committed
feat(build-in.ts): Updated to generate styles folder and typedcssx-global.css in the root of the app.
1 parent 58d663f commit b9e56f5

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

src/_internal/utils/build-in.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
11
'use server';
22

3-
import { readFileSync, appendFileSync } from 'fs';
4-
import { isServer, get } from './helper';
3+
import { readFileSync, appendFileSync, mkdirSync, existsSync } from 'fs';
4+
import { isServer } from './helper';
5+
import { join, dirname } from 'path';
6+
7+
function findNextJsProjectRoot(startPath: string): string | null {
8+
let currentPath = startPath;
9+
for (let i = 1; i <= 6; i++) {
10+
if (
11+
existsSync(join(currentPath, 'package.json')) &&
12+
(existsSync(join(currentPath, 'next.config.js')) || existsSync(join(currentPath, 'next.config.mjs')))
13+
) {
14+
return currentPath;
15+
}
16+
currentPath = dirname(currentPath);
17+
}
18+
return null;
19+
}
520

621
export const buildIn = (styleSheet: string, global?: string) => {
7-
const styleFilePath = get.dir(__dirname, '../../core/styles/style.module.css');
8-
const globalFilePath = get.dir(__dirname, '../../core/styles/global.css');
22+
const currentDir = __dirname;
23+
const projectRoot = findNextJsProjectRoot(currentDir);
24+
25+
if (!projectRoot) {
26+
console.error('Next.js project root not found');
27+
return;
28+
}
29+
30+
const srcDir = join(projectRoot, 'src');
31+
let stylesDir: string;
32+
33+
if (existsSync(srcDir)) {
34+
stylesDir = join(srcDir, 'styles');
35+
} else {
36+
stylesDir = join(projectRoot, 'styles');
37+
}
38+
39+
const styleFilePath = join(__dirname, '../../core/styles/style.module.css');
40+
const globalFilePath = join(stylesDir, 'typedcssx-global.css');
41+
942
const filePath = global === '--global' ? globalFilePath : styleFilePath;
1043
const message = global === '--global' ? ' ✅ Generating global static css \n' : ' ✅ Generating module static css \n';
1144

45+
if (!existsSync(stylesDir)) {
46+
mkdirSync(stylesDir, { recursive: true });
47+
}
48+
1249
if (isServer) {
1350
try {
14-
const cssData = readFileSync(filePath, 'utf-8');
15-
if (!cssData.includes(styleSheet)) {
51+
if (existsSync(filePath)) {
52+
const cssData = readFileSync(filePath, 'utf-8');
53+
if (!cssData.includes(styleSheet)) {
54+
appendFileSync(filePath, styleSheet, 'utf-8');
55+
console.log(message + styleSheet);
56+
}
57+
} else {
1658
appendFileSync(filePath, styleSheet, 'utf-8');
1759
console.log(message + styleSheet);
1860
}
1961
} catch (error) {
20-
console.log('write error');
62+
console.error('Error writing to file:', error);
63+
console.error('Stack trace:', error);
2164
}
2265
}
2366
};

0 commit comments

Comments
 (0)