66
77import { mkdir , writeFile , copyFile } from 'fs/promises' ;
88import { readFileSync , existsSync } from 'fs' ;
9- import { join } from 'path' ;
9+ import { join , dirname } from 'path' ;
10+ import { fileURLToPath } from 'url' ;
1011import chalk from 'chalk' ;
1112
1213/**
@@ -81,8 +82,9 @@ export async function createCommand(name, options) {
8182 // Create basic files
8283 await createBasicFiles ( projectPath ) ;
8384
84- console . log ( chalk . green ( '✅ Project created successfully!' ) ) ;
85- console . log ( chalk . gray ( '\n📦 Next steps:' ) ) ;
85+ console . log ( chalk . green ( 'Project created successfully!' ) ) ;
86+ console . log ( '\n' ) ;
87+ console . log ( chalk . gray ( '\t Next steps:' ) ) ;
8688 console . log ( chalk . gray ( ` cd ${ name } ` ) ) ;
8789 console . log ( chalk . gray ( ' composer install' ) ) ;
8890 console . log ( chalk . gray ( ' npm install' ) ) ;
@@ -96,49 +98,75 @@ export async function createCommand(name, options) {
9698}
9799
98100async function createBasicFiles ( projectPath ) {
101+ // Resolve root-level shims directory regardless of running from src or dist
102+ const here = dirname ( fileURLToPath ( import . meta. url ) ) ;
103+ // here = .../dist/cli (when built) or .../src/cli (when dev)
104+ // package root is two levels up from dist/cli or src/cli
105+ const pkgRoot = join ( here , '..' , '..' ) ;
106+ const shimsDir = join ( pkgRoot , 'shims' ) ;
107+ const nmShimsDir = join ( process . cwd ( ) , 'node_modules' , '@gwack' , 'cli' , 'shims' ) ;
99108 // Create pages/index.vue from shim
100109 let indexPage ;
101110 try {
102- const shimPath = join ( import . meta . dirname , '../shims/ index-page.vue' ) ;
111+ let shimPath = join ( shimsDir , 'index-page.vue.tmpl ' ) ;
103112 if ( existsSync ( shimPath ) ) {
104113 indexPage = readFileSync ( shimPath , 'utf8' ) ;
105114 } else {
106- throw new Error ( 'Shim not found' ) ;
115+ // fallback to node_modules installation path
116+ shimPath = join ( nmShimsDir , 'index-page.vue.tmpl' ) ;
117+ if ( existsSync ( shimPath ) ) {
118+ indexPage = readFileSync ( shimPath , 'utf8' ) ;
119+ } else {
120+ throw new Error ( 'Shim not found' ) ;
121+ }
107122 }
108123 } catch ( error ) {
109124 console . error ( 'Could not read index page shim' ) ;
110125 }
111-
112- await writeFile ( join ( projectPath , 'pages/index.vue' ) , indexPage ) ;
126+ if ( typeof indexPage === 'string' ) {
127+ await writeFile ( join ( projectPath , 'pages/index.vue' ) , indexPage ) ;
128+ }
113129
114130 // gwack.config.js
115131 let config ;
116132 try {
117- const shimPath = join ( import . meta . dirname , '../shims/ gwack.config.js ' ) ;
133+ let shimPath = join ( shimsDir , 'gwack.config.tmpl ' ) ;
118134 if ( existsSync ( shimPath ) ) {
119135 config = readFileSync ( shimPath , 'utf8' ) ;
120136 } else {
121- throw new Error ( 'Shim not found' ) ;
137+ shimPath = join ( nmShimsDir , 'gwack.config.tmpl' ) ;
138+ if ( existsSync ( shimPath ) ) {
139+ config = readFileSync ( shimPath , 'utf8' ) ;
140+ } else {
141+ throw new Error ( 'Shim not found' ) ;
142+ }
122143 }
123144 } catch ( error ) {
124145 console . error ( 'Could not read config shim' ) ;
125146 }
126-
127- await writeFile ( join ( projectPath , 'gwack.config.js' ) , config ) ;
147+ if ( typeof config === 'string' ) {
148+ await writeFile ( join ( projectPath , 'gwack.config.js' ) , config ) ;
149+ }
128150
129151 // index.html
130152 let html ;
131153 try {
132- const shimPath = join ( import . meta . dirname , '../shims/ index.html' ) ;
154+ let shimPath = join ( shimsDir , 'index.html.tmpl ' ) ;
133155 if ( existsSync ( shimPath ) ) {
134156 html = readFileSync ( shimPath , 'utf8' ) ;
135157 } else {
136- throw new Error ( 'Shim not found' ) ;
158+ shimPath = join ( nmShimsDir , 'index.html.tmpl' ) ;
159+ if ( existsSync ( shimPath ) ) {
160+ html = readFileSync ( shimPath , 'utf8' ) ;
161+ } else {
162+ throw new Error ( 'Shim not found' ) ;
163+ }
137164 }
138165 } catch ( error ) {
139166 console . error ( 'Could not read HTML shim' ) ;
140167 return
141168 }
142-
143- await writeFile ( join ( projectPath , 'index.html' ) , html ) ;
169+ if ( typeof html === 'string' ) {
170+ await writeFile ( join ( projectPath , 'index.html' ) , html ) ;
171+ }
144172}
0 commit comments