11import { existsSync , lstatSync , readdirSync } from "fs" ;
2+ import { readFile , writeFile } from "fs/promises" ;
23import { isSolidStart } from "@solid-cli/utils" ;
34import { join , resolve } from "path" ;
45import { $ } from "execa" ;
@@ -7,96 +8,98 @@ import * as p from "@clack/prompts";
78import color from "picocolors" ;
89
910export const getProjectRoot = async ( ) => {
10- const { stdout } = await $ `npm root` ;
11+ const { stdout } = await $ `npm root` ;
1112
12- return stdout . slice ( 0 , stdout . lastIndexOf ( "/" ) ) ;
13+ return stdout . slice ( 0 , stdout . lastIndexOf ( "/" ) ) ;
1314} ;
1415
1516export const getRootFile = async ( ) => {
16- if ( await isSolidStart ( ) ) {
17- return "src/app.tsx" ;
18- }
19- return "src/index.tsx" ;
17+ if ( await isSolidStart ( ) ) {
18+ return "src/app.tsx" ;
19+ }
20+ return "src/index.tsx" ;
2021} ;
2122
2223export const fileExists = ( path : string ) => {
23- return existsSync ( path ) ;
24+ return existsSync ( path ) ;
2425} ;
2526
2627export function validateFilePath ( path : string , lookingFor : string ) : string | undefined ;
2728export function validateFilePath ( path : string , lookingFor : string [ ] ) : string | undefined ;
2829export function validateFilePath ( path : string , lookingFor : string | string [ ] ) : string | undefined {
29- path = resolve ( path ) ;
30- let isDir : boolean ;
31- try {
32- console . log ( path )
33- isDir = lstatSync ( path ) . isDirectory ( ) ;
34- }
35- catch ( e ) { return undefined }
36- if ( isDir ) {
37- const files = readdirSync ( path , { withFileTypes : true } ) ;
38-
39- const config = files . find ( ( file ) => {
40- if ( Array . isArray ( lookingFor ) ) {
41- return lookingFor . some ( ( s ) => file . name . startsWith ( s ) ) ;
42- }
43-
44- return file . name . startsWith ( lookingFor ) ;
45- } ) ;
46- return config ? join ( path , config . name ) : undefined ;
47- }
48-
49- const pathIsValid = Array . isArray ( lookingFor )
50- ? lookingFor . some ( ( s ) => path . startsWith ( s ) )
51- : path . startsWith ( lookingFor ) ;
52-
53- const exists = fileExists ( path ) && pathIsValid ;
54-
55- return exists ? path : undefined ;
30+ path = resolve ( path ) ;
31+ let isDir : boolean ;
32+ try {
33+ console . log ( path ) ;
34+ isDir = lstatSync ( path ) . isDirectory ( ) ;
35+ } catch ( e ) {
36+ return undefined ;
37+ }
38+ if ( isDir ) {
39+ const files = readdirSync ( path , { withFileTypes : true } ) ;
40+
41+ const config = files . find ( ( file ) => {
42+ if ( Array . isArray ( lookingFor ) ) {
43+ return lookingFor . some ( ( s ) => file . name . startsWith ( s ) ) ;
44+ }
45+
46+ return file . name . startsWith ( lookingFor ) ;
47+ } ) ;
48+ return config ? join ( path , config . name ) : undefined ;
49+ }
50+
51+ const pathIsValid = Array . isArray ( lookingFor )
52+ ? lookingFor . some ( ( s ) => path . startsWith ( s ) )
53+ : path . startsWith ( lookingFor ) ;
54+
55+ const exists = fileExists ( path ) && pathIsValid ;
56+
57+ return exists ? path : undefined ;
5658}
5759
5860export async function findFiles (
59- startPath : string ,
60- lookingFor : string | string [ ] ,
61- opts : { depth ?: number ; ignoreDirs ?: string [ ] ; startsWith ?: boolean } ,
61+ startPath : string ,
62+ lookingFor : string | string [ ] ,
63+ opts : { depth ?: number ; ignoreDirs ?: string [ ] ; startsWith ?: boolean } ,
6264) : Promise < string [ ] > {
63- let { depth = Infinity , ignoreDirs = [ "node_modules" , "." ] , startsWith = true } = opts ;
64-
65- startPath = resolve ( startPath ) ;
66- let isDir : boolean ;
67- try {
68- isDir = lstatSync ( startPath ) . isDirectory ( ) ;
69- }
70- catch ( e ) { return [ ] } ;
71- if ( ! isDir ) {
72- startPath = resolve ( startPath . slice ( 0 , startPath . lastIndexOf ( "/" ) ) ) ;
73- }
74-
75- let filePaths : string [ ] = [ ] ;
76-
77- const files = readdirSync ( startPath , { withFileTypes : true } ) ;
78-
79- for ( const file of files ) {
80- if ( file . isDirectory ( ) && ! ignoreDirs . some ( ( s ) => file . name . includes ( s ) ) ) {
81- if ( Number . isFinite ( depth ) && depth -- <= 0 ) continue ;
82- filePaths = filePaths . concat ( await findFiles ( resolve ( startPath , file . name ) , lookingFor , opts ) ) ;
83- continue ;
84- }
85-
86- if ( file . isFile ( ) ) {
87- const fileMatch = Array . isArray ( lookingFor )
88- ? lookingFor . some ( ( s ) => ( startsWith ? file . name . startsWith ( s ) : file . name . endsWith ( s ) ) )
89- : startsWith
90- ? file . name . startsWith ( lookingFor )
91- : file . name . endsWith ( lookingFor ) ;
92-
93- if ( fileMatch ) {
94- filePaths . push ( resolve ( startPath , file . name ) ) ;
95- }
96- }
97- }
98-
99- return filePaths ;
65+ let { depth = Infinity , ignoreDirs = [ "node_modules" , "." ] , startsWith = true } = opts ;
66+
67+ startPath = resolve ( startPath ) ;
68+ let isDir : boolean ;
69+ try {
70+ isDir = lstatSync ( startPath ) . isDirectory ( ) ;
71+ } catch ( e ) {
72+ return [ ] ;
73+ }
74+ if ( ! isDir ) {
75+ startPath = resolve ( startPath . slice ( 0 , startPath . lastIndexOf ( "/" ) ) ) ;
76+ }
77+
78+ let filePaths : string [ ] = [ ] ;
79+
80+ const files = readdirSync ( startPath , { withFileTypes : true } ) ;
81+
82+ for ( const file of files ) {
83+ if ( file . isDirectory ( ) && ! ignoreDirs . some ( ( s ) => file . name . includes ( s ) ) ) {
84+ if ( Number . isFinite ( depth ) && depth -- <= 0 ) continue ;
85+ filePaths = filePaths . concat ( await findFiles ( resolve ( startPath , file . name ) , lookingFor , opts ) ) ;
86+ continue ;
87+ }
88+
89+ if ( file . isFile ( ) ) {
90+ const fileMatch = Array . isArray ( lookingFor )
91+ ? lookingFor . some ( ( s ) => ( startsWith ? file . name . startsWith ( s ) : file . name . endsWith ( s ) ) )
92+ : startsWith
93+ ? file . name . startsWith ( lookingFor )
94+ : file . name . endsWith ( lookingFor ) ;
95+
96+ if ( fileMatch ) {
97+ filePaths . push ( resolve ( startPath , file . name ) ) ;
98+ }
99+ }
100+ }
101+
102+ return filePaths ;
100103}
101104
102105export const getConfigFile = async ( file : "app" | "vite" = "app" ) => {
@@ -133,3 +136,17 @@ export const getConfigFile = async (file: "app" | "vite" = "app") => {
133136
134137 return configFile ;
135138} ;
139+
140+ export async function manipulateJsonFile ( name : string , manipulate : ( obj : Record < string , any > ) => Record < string , any > ) {
141+ try {
142+ const jsonString = await readFile ( name , "utf8" ) ;
143+ const jsonObj = JSON . parse ( jsonString ) ;
144+ await writeFile (
145+ name ,
146+ JSON . stringify ( manipulate ( jsonObj ) , null , / ^ ( \t | \s + ) / m. exec ( jsonString ) ?. [ 0 ] || 2 ) + "\n" ,
147+ "utf8" ,
148+ ) ;
149+ } catch ( error ) {
150+ p . log . error ( color . red ( error ? error . toString ( ) : `unknown error when manipulating ${ name } ` ) ) ;
151+ }
152+ }
0 commit comments