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

Commit 0f3ca30

Browse files
committed
refactor(*): findNextJsProjectRoot function is moved to helper.ts
1 parent 9378e4f commit 0f3ca30

File tree

4 files changed

+22
-48
lines changed

4 files changed

+22
-48
lines changed

bin/typedcssx.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
#!/usr/bin/env node
22

3+
const { findNextJsProjectRoot } = require('../src/_internal');
34
const { execSync } = require('child_process');
4-
const { join, dirname } = require('path');
5-
const { existsSync } = require('fs');
6-
7-
function findNextJsProjectRoot(startPath) {
8-
let currentPath = startPath;
9-
while (currentPath !== '/') {
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-
}
5+
const { join } = require('path');
206

217
if (process.argv.includes('--compile')) {
228
try {
@@ -26,7 +12,7 @@ if (process.argv.includes('--compile')) {
2612
}
2713

2814
console.log('Running TypeScript compiler...');
29-
execSync('npx tsc --noEmit', {
15+
execSync('npx tsc --noEmit compiler/src/index.ts', {
3016
stdio: 'inherit',
3117
cwd: join(packageRoot, 'node_modules/typedcssx'),
3218
});

compiler/src/clean-up.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1+
import { findNextJsProjectRoot } from '../../src/_internal';
12
import { existsSync, writeFileSync } from 'fs';
2-
import { join, dirname } from 'path';
3-
4-
function findNextJsProjectRoot(startPath: string): string | null {
5-
let currentPath = startPath;
6-
while (currentPath !== '/') {
7-
if (
8-
existsSync(join(currentPath, 'package.json')) &&
9-
(existsSync(join(currentPath, 'next.config.js')) || existsSync(join(currentPath, 'next.config.mjs')))
10-
) {
11-
return currentPath;
12-
}
13-
currentPath = dirname(currentPath);
14-
}
15-
return null;
16-
}
3+
import { join } from 'path';
174

185
export const cleanUp = async () => {
196
const projectRoot = findNextJsProjectRoot(__dirname);

src/_internal/utils/build-in.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
'use server';
22

33
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-
while (currentPath !== '/') {
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-
}
4+
import { findNextJsProjectRoot, isServer } from './helper';
5+
import { join } from 'path';
206

217
export const buildIn = (styleSheet: string, global?: string) => {
228
const projectRoot = findNextJsProjectRoot(__dirname);

src/_internal/utils/helper.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import * as path from 'path';
2+
import * as fs from 'fs';
23
import type { ClassesObjectType, HasEPE, HasECE } from '..';
34
import htmlTags from './html-tags';
45

6+
export function findNextJsProjectRoot(startPath: string): string | null {
7+
let currentPath = startPath;
8+
while (currentPath !== '/') {
9+
if (
10+
fs.existsSync(path.join(currentPath, 'package.json')) &&
11+
(fs.existsSync(path.join(currentPath, 'next.config.js')) || fs.existsSync(path.join(currentPath, 'next.config.mjs')))
12+
) {
13+
return currentPath;
14+
}
15+
currentPath = path.dirname(currentPath);
16+
}
17+
return null;
18+
}
19+
520
const isWindowDefined = typeof window !== 'undefined';
621
const isDocumentDefined = typeof document !== 'undefined';
722
export const isServer = !isWindowDefined || !isDocumentDefined;

0 commit comments

Comments
 (0)