11import fs from 'fs' ;
22import path from 'path' ;
3- import { type Mount } from './mounts' ;
3+ import type { Mount } from './mounts' ;
44import {
55 type X2jOptions ,
66 type XmlBuilderOptions ,
@@ -10,15 +10,15 @@ import {
1010import JSONC from 'jsonc-parser' ;
1111
1212/**
13- * Create a symlink to temp dir for the Playground CLI .
13+ * Create a symlink to a tempory directory .
1414 *
1515 * The symlink is created to access the system temp dir
1616 * inside the current debugging directory.
1717 *
1818 * @param nativeDirPath The system temp dir path.
1919 * @param symlinkPath The symlink name.
2020 */
21- export async function createPlaygroundCliTempDirSymlink (
21+ export async function createTempDirSymlink (
2222 nativeDirPath : string ,
2323 symlinkPath : string ,
2424 platform : string
@@ -34,11 +34,11 @@ export async function createPlaygroundCliTempDirSymlink(
3434}
3535
3636/**
37- * Remove the temp dir symlink if it exists.
37+ * Remove the given temporary directory symlink if it exists.
3838 *
3939 * @param symlinkPath The symlink path.
4040 */
41- export async function removePlaygroundCliTempDirSymlink ( symlinkPath : string ) {
41+ export async function removeTempDirSymlink ( symlinkPath : string ) {
4242 try {
4343 const stats = fs . lstatSync ( symlinkPath ) ;
4444 if ( stats . isSymbolicLink ( ) ) {
@@ -52,7 +52,7 @@ export async function removePlaygroundCliTempDirSymlink(symlinkPath: string) {
5252/**
5353 * Filters out mounts that are not in the current working directory
5454 *
55- * @param mounts The Playground CLI mount options .
55+ * @param mounts The mounts list .
5656 */
5757function filterLocalMounts ( cwd : string , mounts : Mount [ ] ) {
5858 return mounts . filter ( ( mount ) => {
@@ -91,7 +91,7 @@ export type IDEConfig = {
9191 /**
9292 * The mounts to consider for debugger path mapping.
9393 */
94- mounts : Mount [ ] ;
94+ mounts ? : Mount [ ] ;
9595 /**
9696 * The IDE key to use for the debug configuration. Defaults to 'PLAYGROUNDCLI'.
9797 */
@@ -137,7 +137,7 @@ type VSCodeConfigNode = {
137137 type : string ;
138138 request : string ;
139139 port : number ;
140- pathMappings : VSCodeConfigMetaData ;
140+ pathMappings ? : VSCodeConfigMetaData ;
141141} ;
142142
143143const xmlParserOptions : X2jOptions = {
@@ -170,7 +170,7 @@ export type PhpStormConfigOptions = {
170170 host : string ;
171171 port : number ;
172172 projectDir : string ;
173- mappings : Mount [ ] ;
173+ mappings ? : Mount [ ] ;
174174 ideKey : string ;
175175} ;
176176
@@ -201,19 +201,7 @@ export function updatePhpStormConfig(
201201
202202 // Create the server element with path mappings
203203 const serverElement : PhpStormConfigNode = {
204- server : [
205- {
206- path_mappings : mappings . map ( ( mapping ) => ( {
207- mapping : [ ] ,
208- ':@' : {
209- 'local-root' : `$PROJECT_DIR$/${ toPosixPath (
210- path . relative ( options . projectDir , mapping . hostPath )
211- ) } `,
212- 'remote-root' : mapping . vfsPath ,
213- } ,
214- } ) ) ,
215- } ,
216- ] ,
204+ server : [ { } ] ,
217205 ':@' : {
218206 name,
219207 // NOTE: PhpStorm quirk: Xdebug only works when the full URL (including port)
@@ -224,6 +212,18 @@ export function updatePhpStormConfig(
224212 } ,
225213 } ;
226214
215+ if ( mappings && mappings . length ) {
216+ serverElement . server ! [ 0 ] . path_mappings = mappings . map ( ( mapping ) => ( {
217+ mapping : [ ] ,
218+ ':@' : {
219+ 'local-root' : `$PROJECT_DIR$/${ toPosixPath (
220+ path . relative ( options . projectDir , mapping . hostPath )
221+ ) } `,
222+ 'remote-root' : mapping . vfsPath ,
223+ } ,
224+ } ) ) ;
225+ }
226+
227227 // Find or create project element
228228 let projectElement = config ?. find ( ( c : PhpStormConfigNode ) => ! ! c ?. project ) ;
229229 if ( projectElement ) {
@@ -364,7 +364,7 @@ export function updatePhpStormConfig(
364364export type VSCodeConfigOptions = {
365365 name : string ;
366366 workspaceDir : string ;
367- mappings : Mount [ ] ;
367+ mappings ? : Mount [ ] ;
368368} ;
369369
370370/**
@@ -408,7 +408,7 @@ export function updateVSCodeConfig(
408408
409409 // Check if configuration already exists
410410 const configurationIndex = configurationsNode ?. children ?. findIndex (
411- ( child ) => JSONC . findNodeAtLocation ( child , [ 'name' ] ) ?. value === name
411+ ( child : any ) => JSONC . findNodeAtLocation ( child , [ 'name' ] ) ?. value === name
412412 ) ;
413413
414414 // Only add configuration if it doesn't exist
@@ -418,14 +418,17 @@ export function updateVSCodeConfig(
418418 type : 'php' ,
419419 request : 'launch' ,
420420 port : 9003 ,
421- pathMappings : mappings . reduce ( ( acc , mount ) => {
422- acc [ mount . vfsPath ] = `\${workspaceFolder}/${ toPosixPath (
423- path . relative ( options . workspaceDir , mount . hostPath )
424- ) } `;
425- return acc ;
426- } , { } as VSCodeConfigMetaData ) ,
427421 } ;
428422
423+ if ( mappings && mappings . length ) {
424+ configuration . pathMappings = mappings . reduce ( ( acc , mount ) => {
425+ acc [ mount . vfsPath ] = `\${workspaceFolder}/${ toPosixPath (
426+ path . relative ( options . workspaceDir , mount . hostPath )
427+ ) } `;
428+ return acc ;
429+ } , { } as VSCodeConfigMetaData ) ;
430+ }
431+
429432 // Get the current length to append at the end
430433 const currentLength = configurationsNode ?. children ?. length || 0 ;
431434
@@ -452,7 +455,7 @@ export function updateVSCodeConfig(
452455 * Implement necessary parameters and path mappings in IDE configuration files.
453456 *
454457 * @param name The configuration name.
455- * @param mounts The Playground CLI mount options.
458+ * @param mounts The mounts options.
456459 */
457460export async function addXdebugIDEConfig ( {
458461 name,
@@ -461,9 +464,9 @@ export async function addXdebugIDEConfig({
461464 port,
462465 cwd,
463466 mounts,
464- ideKey = 'PLAYGROUNDCLI ' ,
467+ ideKey = 'PHPWASMCLI ' ,
465468} : IDEConfig ) {
466- const mappings = filterLocalMounts ( cwd , mounts ) ;
469+ const mappings = mounts ? filterLocalMounts ( cwd , mounts ) : [ ] ;
467470 const modifiedConfig : string [ ] = [ ] ;
468471
469472 // PHPstorm
@@ -625,7 +628,7 @@ export async function clearXdebugIDEConfig(name: string, cwd: string) {
625628 ] ) ;
626629
627630 const configurationIndex = configurationsNode ?. children ?. findIndex (
628- ( child ) => JSONC . findNodeAtLocation ( child , [ 'name' ] ) ?. value === name
631+ ( child : any ) => JSONC . findNodeAtLocation ( child , [ 'name' ] ) ?. value === name
629632 ) ;
630633
631634 if ( configurationIndex !== undefined && configurationIndex >= 0 ) {
@@ -681,8 +684,8 @@ function jsoncApplyEdits(content: string, edits: JSONC.Edit[]) {
681684 ( edit ) => `At ${ edit . offset } :${ edit . length } - (${ edit . content } )`
682685 ) ;
683686 throw new Error (
684- `VS Code configuration file (.vscode/launch.json) is not valid a JSONC after Playground CLI modifications. This is likely ` +
685- `a Playground CLI bug. Please report it at https://github.com/WordPress/wordpress-playground/issues and include the contents ` +
687+ `VS Code configuration file (.vscode/launch.json) is not valid a JSONC after CLI modifications. This is likely ` +
688+ `a CLI bug. Please report it at https://github.com/WordPress/wordpress-playground/issues and include the contents ` +
686689 `of your ".vscode/launch.json" file. \n\n Applied edits: ${ formattedEdits . join (
687690 '\n'
688691 ) } \n\n The errors are: ${ formattedErrors . join ( '\n' ) } `
0 commit comments