index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file at
  6. * https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
  7. *
  8. * Modified by Yuxi Evan You
  9. */
  10. const fs = require('fs')
  11. const os = require('os')
  12. const path = require('path')
  13. const colors = require('picocolors')
  14. const childProcess = require('child_process')
  15. const guessEditor = require('./guess')
  16. const getArgumentsForPosition = require('./get-args')
  17. function wrapErrorCallback (cb) {
  18. return (fileName, errorMessage) => {
  19. console.log()
  20. console.log(
  21. colors.red('Could not open ' + path.basename(fileName) + ' in the editor.')
  22. )
  23. if (errorMessage) {
  24. if (errorMessage[errorMessage.length - 1] !== '.') {
  25. errorMessage += '.'
  26. }
  27. console.log(
  28. colors.red('The editor process exited with an error: ' + errorMessage)
  29. )
  30. }
  31. console.log()
  32. if (cb) cb(fileName, errorMessage)
  33. }
  34. }
  35. function isTerminalEditor (editor) {
  36. switch (editor) {
  37. case 'vim':
  38. case 'emacs':
  39. case 'nano':
  40. return true
  41. }
  42. return false
  43. }
  44. const positionRE = /:(\d+)(:(\d+))?$/
  45. function parseFile (file) {
  46. // support `file://` protocol
  47. if (file.startsWith('file://')) {
  48. file = require('url').fileURLToPath(file)
  49. }
  50. const fileName = file.replace(positionRE, '')
  51. const match = file.match(positionRE)
  52. const lineNumber = match && match[1]
  53. const columnNumber = match && match[3]
  54. return {
  55. fileName,
  56. lineNumber,
  57. columnNumber
  58. }
  59. }
  60. let _childProcess = null
  61. function launchEditor (file, specifiedEditor, onErrorCallback) {
  62. const parsed = parseFile(file)
  63. let { fileName } = parsed
  64. const { lineNumber, columnNumber } = parsed
  65. if (!fs.existsSync(fileName)) {
  66. return
  67. }
  68. if (typeof specifiedEditor === 'function') {
  69. onErrorCallback = specifiedEditor
  70. specifiedEditor = undefined
  71. }
  72. onErrorCallback = wrapErrorCallback(onErrorCallback)
  73. const [editor, ...args] = guessEditor(specifiedEditor)
  74. if (!editor) {
  75. onErrorCallback(fileName, null)
  76. return
  77. }
  78. if (
  79. process.platform === 'linux' &&
  80. fileName.startsWith('/mnt/') &&
  81. /Microsoft/i.test(os.release())
  82. ) {
  83. // Assume WSL / "Bash on Ubuntu on Windows" is being used, and
  84. // that the file exists on the Windows file system.
  85. // `os.release()` is "4.4.0-43-Microsoft" in the current release
  86. // build of WSL, see: https://github.com/Microsoft/BashOnWindows/issues/423#issuecomment-221627364
  87. // When a Windows editor is specified, interop functionality can
  88. // handle the path translation, but only if a relative path is used.
  89. fileName = path.relative('', fileName)
  90. }
  91. if (lineNumber) {
  92. const extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber)
  93. args.push.apply(args, extraArgs)
  94. } else {
  95. args.push(fileName)
  96. }
  97. if (_childProcess && isTerminalEditor(editor)) {
  98. // There's an existing editor process already and it's attached
  99. // to the terminal, so go kill it. Otherwise two separate editor
  100. // instances attach to the stdin/stdout which gets confusing.
  101. _childProcess.kill('SIGKILL')
  102. }
  103. if (process.platform === 'win32') {
  104. // On Windows, we need to use `exec` with the `shell: true` option,
  105. // and some more sanitization is required.
  106. // However, CMD.exe on Windows is vulnerable to RCE attacks given a file name of the
  107. // form "C:\Users\myusername\Downloads\& curl 172.21.93.52".
  108. // `create-react-app` used a safe file name pattern to validate user-provided file names:
  109. // - https://github.com/facebook/create-react-app/pull/4866
  110. // - https://github.com/facebook/create-react-app/pull/5431
  111. // But that's not a viable solution for this package because
  112. // it's depended on by so many meta frameworks that heavily rely on
  113. // special characters in file names for filesystem-based routing.
  114. // We need to at least:
  115. // - Support `+` because it's used in SvelteKit and Vike
  116. // - Support `$` because it's used in Remix
  117. // - Support `(` and `)` because they are used in Analog, SolidStart, and Vike
  118. // - Support `@` because it's used in Vike
  119. // - Support `[` and `]` because they are widely used for [slug]
  120. // So here we choose to use `^` to escape special characters instead.
  121. // According to https://ss64.com/nt/syntax-esc.html,
  122. // we can use `^` to escape `&`, `<`, `>`, `|`, `%`, and `^`
  123. // I'm not sure if we have to escape all of these, but let's do it anyway
  124. function escapeCmdArgs (cmdArgs) {
  125. return cmdArgs.replace(/([&|<>,;=^])/g, '^$1')
  126. }
  127. // Need to double quote the editor path in case it contains spaces;
  128. // If the fileName contains spaces, we also need to double quote it in the arguments
  129. // However, there's a case that it's concatenated with line number and column number
  130. // which is separated by `:`. We need to double quote the whole string in this case.
  131. // Also, if the string contains the escape character `^`, it needs to be quoted, too.
  132. function doubleQuoteIfNeeded(str) {
  133. if (str.includes('^')) {
  134. // If a string includes an escaped character, not only does it need to be quoted,
  135. // but the quotes need to be escaped too.
  136. return `^"${str}^"`
  137. } else if (str.includes(' ')) {
  138. return `"${str}"`
  139. }
  140. return str
  141. }
  142. const launchCommand = [editor, ...args.map(escapeCmdArgs)]
  143. .map(doubleQuoteIfNeeded)
  144. .join(' ')
  145. _childProcess = childProcess.exec(launchCommand, {
  146. stdio: 'inherit',
  147. shell: true
  148. })
  149. } else {
  150. _childProcess = childProcess.spawn(editor, args, { stdio: 'inherit' })
  151. }
  152. _childProcess.on('exit', function (errorCode) {
  153. _childProcess = null
  154. if (errorCode) {
  155. onErrorCallback(fileName, '(code ' + errorCode + ')')
  156. }
  157. })
  158. _childProcess.on('error', function (error) {
  159. let { code, message } = error
  160. if ('ENOENT' === code) {
  161. message = `${message} ('${editor}' command does not exist in 'PATH')`
  162. }
  163. onErrorCallback(fileName, message);
  164. })
  165. }
  166. module.exports = launchEditor