@@ -13,6 +13,7 @@ const {
1313 renameSync,
1414 lstatSync,
1515 rmdirSync,
16+ readlinkSync,
1617} = require ( 'fs' ) ;
1718const { join } = require ( 'path' ) ;
1819
@@ -173,19 +174,33 @@ async function run() {
173174 const links = argv . links . split ( ',' ) ;
174175
175176 links . forEach ( ( link ) => {
176- if ( existsSync ( join ( cacheDir , link ) ) ) {
177- const stats = lstatSync ( join ( cacheDir , link ) ) ;
178- if ( ! stats . isSymbolicLink ( ) ) {
179- rmdirSync ( join ( cacheDir , link ) , {
177+ let source , destination ;
178+
179+ if ( link . includes ( ':' ) ) {
180+ let split = link . split ( ':' ) ;
181+ source = split [ 0 ] ;
182+ destination = split [ 1 ] ;
183+ } else {
184+ source = destination = link ;
185+ }
186+
187+ const destiantionPath = join ( cacheDir , destination ) ;
188+ const sourcePath = join ( process . cwd ( ) , source ) ;
189+
190+ if ( existsSync ( destiantionPath ) ) {
191+ const stats = lstatSync ( destiantionPath ) ;
192+
193+ if ( ! stats . isSymbolicLink ( ) || readlinkSync ( destiantionPath ) !== sourcePath ) {
194+ rmdirSync ( destiantionPath , {
180195 recursive : true ,
181196 force : true ,
182197 } ) ;
183198 }
184199 }
185200
186- if ( ! existsSync ( join ( cacheDir , link ) ) ) {
187- console . log ( `linking ${ link } 🤖` ) ;
188- symlinkSync ( join ( process . cwd ( ) , link ) , join ( cacheDir , link ) ) ;
201+ if ( ! existsSync ( destiantionPath ) ) {
202+ console . log ( `linking ${ source } -> ${ destination } 🤖` ) ;
203+ symlinkSync ( sourcePath , destiantionPath ) ;
189204 }
190205 } ) ;
191206 }
0 commit comments