11#!/usr/bin/env node
22
33const { execSync } = require ( 'child_process' ) ;
4- const path = require ( 'path' ) ;
5- const fs = require ( 'fs' ) ;
4+ const { join , dirname } = require ( 'path' ) ;
5+ const { existsSync } = require ( 'fs' ) ;
66
7- const args = process . argv . slice ( 2 ) ;
8-
9- function findPackageRoot ( startDir ) {
10- let currentDir = startDir ;
11- while ( currentDir !== path . parse ( currentDir ) . root ) {
12- const packageJsonPath = path . join ( currentDir , 'node_modules' , 'typedcssx' , 'package.json' ) ;
13- if ( fs . existsSync ( packageJsonPath ) ) {
14- return path . dirname ( packageJsonPath ) ;
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 ;
1515 }
16- currentDir = path . dirname ( currentDir ) ;
16+ currentPath = dirname ( currentPath ) ;
1717 }
18- throw new Error ( 'Could not find typedcssx package' ) ;
18+ return null ;
1919}
2020
21- if ( args . includes ( '--compile' ) ) {
21+ if ( process . argv . includes ( '--compile' ) ) {
2222 try {
23- const packageRoot = findPackageRoot ( process . cwd ( ) ) ;
24- const compilerPath = path . join ( packageRoot , 'compiler/src/index.ts' ) ;
23+ const packageRoot = findNextJsProjectRoot ( __dirname ) ;
24+ if ( ! packageRoot ) {
25+ throw new Error ( 'Could not find Next.js project root' ) ;
26+ }
2527
2628 console . log ( 'Running TypeScript compiler...' ) ;
27- execSync ( `npx tsc --noEmit "${ compilerPath } "` , { stdio : 'inherit' } ) ;
28-
29+ execSync ( 'npx tsc --noEmit' , {
30+ stdio : 'inherit' ,
31+ cwd : join ( packageRoot , 'node_modules/typedcssx' ) ,
32+ } ) ;
33+
2934 console . log ( 'Executing compiler...' ) ;
30- execSync ( `npx tsx "${ compilerPath } "` , { stdio : 'inherit' } ) ;
31-
35+ execSync ( 'npx tsx compiler/src/index.ts' , {
36+ stdio : 'inherit' ,
37+ cwd : join ( packageRoot , 'node_modules/typedcssx' ) ,
38+ } ) ;
39+
3240 console . log ( 'Compilation completed successfully.' ) ;
3341 } catch ( error ) {
3442 console . error ( 'Compilation failed:' , error . message ) ;
3543 process . exit ( 1 ) ;
3644 }
3745} else {
3846 console . log ( 'Usage: typedcssx --compile' ) ;
39- }
47+ }
0 commit comments