folders.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const path = require('path')
  2. const fs = require('fs-extra')
  3. const LRU = require('lru-cache')
  4. const winattr = require('@akryum/winattr')
  5. const hiddenPrefix = '.'
  6. const isPlatformWindows = process.platform.indexOf('win') === 0
  7. const pkgCache = new LRU({
  8. max: 500,
  9. maxAge: 1000 * 5
  10. })
  11. const cwd = require('./cwd')
  12. function isDirectory (file) {
  13. file = file.replace(/\\/g, path.sep)
  14. try {
  15. return fs.stat(file).then((x) => x.isDirectory())
  16. } catch (e) {
  17. if (process.env.VUE_APP_CLI_UI_DEBUG) console.warn(e.message)
  18. }
  19. return false
  20. }
  21. async function list (base, context) {
  22. let dir = base
  23. if (isPlatformWindows) {
  24. if (base.match(/^([A-Z]{1}:)$/)) {
  25. dir = path.join(base, '\\')
  26. }
  27. }
  28. const files = await fs.readdir(dir, 'utf8')
  29. const f = await Promise.all(
  30. files.map(async (file) => {
  31. const folderPath = path.join(base, file)
  32. const [directory, hidden] = await Promise.all([
  33. isDirectory(folderPath),
  34. isHidden(folderPath)
  35. ])
  36. if (!directory) {
  37. return null
  38. }
  39. return {
  40. path: folderPath,
  41. name: file,
  42. hidden
  43. }
  44. })
  45. )
  46. return f.filter((x) => !!x)
  47. }
  48. async function isHiddenWindows (file) {
  49. const windowsFile = file.replace(/\\/g, '\\\\')
  50. return new Promise((resolve, reject) => {
  51. winattr.get(windowsFile, (file, error) => {
  52. if (error) {
  53. return reject(error)
  54. }
  55. resolve(file)
  56. })
  57. }).then((x) => x.hidden)
  58. }
  59. async function isHidden (file) {
  60. try {
  61. const prefixed = path.basename(file).charAt(0) === hiddenPrefix
  62. const result = {
  63. unix: prefixed,
  64. windows: false
  65. }
  66. if (isPlatformWindows) {
  67. result.windows = await isHiddenWindows(file)
  68. }
  69. return (
  70. (!isPlatformWindows && result.unix) ||
  71. (isPlatformWindows && result.windows)
  72. )
  73. } catch (e) {
  74. if (process.env.VUE_APP_CLI_UI_DEBUG) {
  75. console.log('file:', file)
  76. console.error(e)
  77. }
  78. }
  79. }
  80. function generateFolder (file, context) {
  81. return {
  82. name: path.basename(file),
  83. path: file
  84. }
  85. }
  86. function getCurrent (args, context) {
  87. const base = cwd.get()
  88. return generateFolder(base, context)
  89. }
  90. function open (file, context) {
  91. cwd.set(file, context)
  92. return generateFolder(cwd.get(), context)
  93. }
  94. function openParent (file, context) {
  95. const newFile = path.dirname(file)
  96. cwd.set(newFile, context)
  97. return generateFolder(cwd.get(), context)
  98. }
  99. function isPackage (file, context) {
  100. try {
  101. return fs.existsSync(path.join(file, 'package.json'))
  102. } catch (e) {
  103. console.warn(e.message)
  104. }
  105. return false
  106. }
  107. function readPackage (file, context, force = false) {
  108. if (!force) {
  109. const cachedValue = pkgCache.get(file)
  110. if (cachedValue) {
  111. return cachedValue
  112. }
  113. }
  114. const pkgFile = path.join(file, 'package.json')
  115. if (fs.existsSync(pkgFile)) {
  116. const pkg = fs.readJsonSync(pkgFile)
  117. pkgCache.set(file, pkg)
  118. return pkg
  119. }
  120. }
  121. function writePackage ({ file, data }, context) {
  122. fs.outputJsonSync(path.join(file, 'package.json'), data, {
  123. spaces: 2
  124. })
  125. invalidatePackage(file, context)
  126. return true
  127. }
  128. function invalidatePackage (file, context) {
  129. pkgCache.del(file)
  130. return true
  131. }
  132. function isVueProject (file, context) {
  133. if (!isPackage(file)) return false
  134. try {
  135. const pkg = readPackage(file, context)
  136. return Object.keys(pkg.devDependencies || {}).includes('@vue/cli-service')
  137. } catch (e) {
  138. if (process.env.VUE_APP_CLI_UI_DEBUG) {
  139. console.log(e)
  140. }
  141. }
  142. return false
  143. }
  144. function listFavorite (context) {
  145. return context.db
  146. .get('foldersFavorite')
  147. .value()
  148. .map((file) => generateFolder(file.id, context))
  149. }
  150. function isFavorite (file, context) {
  151. return !!context.db.get('foldersFavorite').find({ id: file }).size().value()
  152. }
  153. function setFavorite ({ file, favorite }, context) {
  154. const collection = context.db.get('foldersFavorite')
  155. if (favorite) {
  156. collection.push({ id: file }).write()
  157. } else {
  158. collection.remove({ id: file }).write()
  159. }
  160. return generateFolder(file, context)
  161. }
  162. async function deleteFolder (file) {
  163. await fs.remove(file)
  164. }
  165. function createFolder (name, context) {
  166. const file = path.join(cwd.get(), name)
  167. fs.mkdirpSync(file)
  168. return generateFolder(file, context)
  169. }
  170. module.exports = {
  171. isDirectory,
  172. getCurrent,
  173. list,
  174. open,
  175. openParent,
  176. isPackage,
  177. readPackage,
  178. writePackage,
  179. invalidatePackage,
  180. isVueProject,
  181. isFavorite,
  182. listFavorite,
  183. setFavorite,
  184. delete: deleteFolder,
  185. create: createFolder
  186. }