11import { resolve as resolvePath } from 'node:path' ;
2+ import { cp , mkdir , rm } from 'node:fs/promises' ;
23import { fileURLToPath } from 'node:url' ;
34import type { PluginOption } from 'vite' ;
45import type { Reporter } from '@wpkernel/core/reporter' ;
@@ -14,6 +15,15 @@ interface DriverInstallerHelper {
1415
1516type PhpDriverInstallerFactory = ( ) => DriverInstallerHelper ;
1617
18+ async function copyPhpDriverDist ( outDir : string ) : Promise < void > {
19+ const source = resolvePath ( CLI_ROOT , '..' , 'php-driver' , 'dist' ) ;
20+ const target = resolvePath ( outDir , 'packages' , 'php-driver' , 'dist' ) ;
21+
22+ await rm ( target , { recursive : true , force : true } ) . catch ( ( ) => undefined ) ;
23+ await mkdir ( target , { recursive : true } ) ;
24+ await cp ( source , target , { recursive : true } ) ;
25+ }
26+
1727function resolveCliRoot ( ) : string {
1828 if ( typeof __dirname === 'string' ) {
1929 return __dirname ;
@@ -117,10 +127,16 @@ function createConsoleReporter(): Reporter {
117127
118128function phpDriverInstallerPlugin ( ) : PluginOption {
119129 let hasRun = false ;
130+ let resolvedOutDir = resolvePath ( CLI_ROOT , 'dist' ) ;
120131
121132 return {
122133 name : 'wpkernel-cli-php-driver-installer' ,
123134 apply : 'build' ,
135+ configResolved ( config ) {
136+ const root = config . root ?? CLI_ROOT ;
137+ const dir = config . build ?. outDir ?? 'dist' ;
138+ resolvedOutDir = resolvePath ( root , dir ) ;
139+ } ,
124140 async buildStart ( ) {
125141 if ( hasRun ) {
126142 return ;
@@ -147,6 +163,9 @@ function phpDriverInstallerPlugin(): PluginOption {
147163 undefined
148164 ) ;
149165 } ,
166+ async writeBundle ( ) {
167+ await copyPhpDriverDist ( resolvedOutDir ) ;
168+ } ,
150169 } ;
151170}
152171
0 commit comments