@@ -12,11 +12,9 @@ import fs from 'node:fs/promises';
1212import os from 'node:os' ;
1313import path from 'node:path' ;
1414
15- import { codifySpawn } from '../../../utils/codify-spawn.js' ;
1615import { FileUtils } from '../../../utils/file-utils.js' ;
1716import { untildify } from '../../../utils/untildify.js' ;
1817import Schema from './path-schema.json' ;
19- import { Utils } from '../../../utils/index.js' ;
2018
2119export interface PathConfig extends StringIndexedObject {
2220 path : string ;
@@ -53,23 +51,27 @@ export class PathResource extends Resource<PathConfig> {
5351 const $ = getPty ( ) ;
5452
5553 const { data : existingPaths } = await $ . spawnSafe ( 'echo $PATH' )
56- if ( parameters . path && ( existingPaths . includes ( parameters . path ) || existingPaths . includes ( untildify ( parameters . path ) ) ) ) {
54+ if ( parameters . path !== undefined && ( existingPaths . includes ( parameters . path ) || existingPaths . includes ( untildify ( parameters . path ) ) ) ) {
5755 return parameters ;
5856 }
5957
60- if ( parameters . paths ) {
58+ // MacOS defines system paths in /etc/paths and inside the /etc/paths.d folders
59+ const systemPaths = ( await fs . readFile ( '/etc/paths' , 'utf8' ) )
60+ . split ( / \n / )
61+ . filter ( Boolean ) ;
6162
62- // Only add the paths that are found on the system
63- const existingPathsSplit = new Set ( existingPaths . split ( ':' )
63+ for ( const pathFile of await fs . readdir ( '/etc/paths.d' ) ) {
64+ systemPaths . push ( ...( await fs . readFile ( path . join ( '/etc/paths.d' , pathFile ) , 'utf8' ) )
65+ . split ( / \n / )
6466 . filter ( Boolean )
65- . map ( ( l ) => path . resolve ( l . trim ( ) ) ) )
67+ ) ;
68+ }
6669
67- const foundPaths = parameters . paths . filter ( ( p ) => existingPathsSplit . has ( p ) ) ;
68- if ( foundPaths . length === 0 ) {
69- return null ;
70- }
70+ const userPaths = existingPaths . split ( ':' )
71+ . filter ( ( p ) => ! systemPaths . includes ( p ) )
7172
72- return { paths : foundPaths , prepend : parameters . prepend } ;
73+ if ( parameters . paths !== undefined ) {
74+ return { paths : userPaths , prepend : parameters . prepend } ;
7375 }
7476
7577 return null ;
0 commit comments