socketsupply / socket

A cross-platform runtime for Web developers to build desktop & mobile apps for any OS using any frontend library.
https://socketsupply.co/guides
Other
1.6k stars 75 forks source link

fix(npm index.js): convert file url to actual path #959

Closed jstarpl closed 5 months ago

jstarpl commented 5 months ago

This PR fixes an issue that happens, at least on Windows, when the user has a space character in their Username.

The symptom:

> ssc init
node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'C:\Users\Jan%20Starzak\AppData\Roaming\nvm\v20.11.1\node_modules\@socketsupply\socket\node_modules\@socketsupply\socket-win32-x64\bin\ssc-platform.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

On Windows (but maybe on other platforms too), file URLs can contain spaces which will be encoded as %20. When treating these URL strings as paths, the OS will claim it doesn't recognize these paths because it won't URL-decode them automatically. The ENOENT here is caused by the un-url'ed cwd property passed into spawn. Therefore, the import.meta.url needs to be converted to an actual filePath before all the other path processing can be done to it.