dependencies.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. const fs = require('fs')
  2. const path = require('path')
  3. const LRU = require('lru-cache')
  4. const { chalk, semver } = require('@vue/cli-shared-utils')
  5. // Connectors
  6. const cwd = require('./cwd')
  7. const folders = require('./folders')
  8. const progress = require('./progress')
  9. const logs = require('./logs')
  10. // Context
  11. const getContext = require('../context')
  12. // Utils
  13. const { isPlugin, resolveModule } = require('@vue/cli-shared-utils')
  14. const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
  15. const PackageManager = require('@vue/cli/lib/util/ProjectPackageManager')
  16. const { resolveModuleRoot } = require('../util/resolve-path')
  17. const { notify } = require('../util/notification')
  18. const { log } = require('../util/logger')
  19. const PROGRESS_ID = 'dependency-installation'
  20. const CLI_SERVICE = '@vue/cli-service'
  21. // Caches
  22. const metadataCache = new LRU({
  23. max: 200,
  24. maxAge: 1000 * 60 * 30 // 30 min.
  25. })
  26. // Local
  27. let dependencies
  28. function list (file, context) {
  29. const pkg = folders.readPackage(file, context)
  30. dependencies = []
  31. dependencies = dependencies.concat(
  32. findDependencies(pkg.devDependencies || {}, 'devDependencies', file, context)
  33. )
  34. dependencies = dependencies.concat(
  35. findDependencies(pkg.dependencies || {}, 'dependencies', file, context)
  36. )
  37. return dependencies
  38. }
  39. function findOne (id, context) {
  40. return dependencies.find(
  41. p => p.id === id
  42. )
  43. }
  44. function findDependencies (deps, type, file, context) {
  45. return Object.keys(deps).filter(
  46. id => !isPlugin(id) && id !== CLI_SERVICE
  47. ).map(
  48. id => ({
  49. id,
  50. versionRange: deps[id],
  51. installed: isInstalled({ id, file }),
  52. website: getLink({ id, file }, context),
  53. type,
  54. baseFir: file
  55. })
  56. )
  57. }
  58. function getPath ({ id, file = cwd.get() }) {
  59. const filePath = resolveModule(path.join(id, 'package.json'), file)
  60. if (!filePath) return
  61. return resolveModuleRoot(filePath, id)
  62. }
  63. function isInstalled ({ id, file = cwd.get() }) {
  64. const resolvedPath = getPath({ id, file })
  65. return resolvedPath && fs.existsSync(resolvedPath)
  66. }
  67. function readPackage ({ id, file }, context) {
  68. try {
  69. return folders.readPackage(getPath({ id, file }), context)
  70. } catch (e) {
  71. console.log(e)
  72. }
  73. return {}
  74. }
  75. function invalidatePackage ({ id, file }, context) {
  76. return folders.invalidatePackage(getPath({ id, file }), context)
  77. }
  78. async function getMetadata (id, context) {
  79. let metadata = metadataCache.get(id)
  80. if (metadata) {
  81. return metadata
  82. }
  83. try {
  84. metadata = await (new PackageManager({ context: cwd.get() })).getMetadata(id)
  85. } catch (e) {
  86. // No connection?
  87. }
  88. if (metadata) {
  89. metadataCache.set(id, metadata)
  90. return metadata
  91. } else {
  92. log('Dependencies', chalk.yellow('Can\'t load metadata'), id)
  93. }
  94. }
  95. async function getVersion ({ id, installed, versionRange, baseDir }, context) {
  96. let current
  97. // Is local dep
  98. const localPath = getLocalPath(id, context)
  99. // Read module package.json
  100. if (installed) {
  101. const pkg = readPackage({ id, file: baseDir }, context)
  102. current = pkg.version
  103. } else {
  104. current = null
  105. }
  106. // Metadata
  107. let latest, wanted
  108. const metadata = await getMetadata(id, context)
  109. if (metadata) {
  110. latest = metadata['dist-tags'].latest
  111. const versions = Array.isArray(metadata.versions) ? metadata.versions : Object.keys(metadata.versions)
  112. wanted = semver.maxSatisfying(versions, versionRange)
  113. }
  114. if (!latest) latest = current
  115. if (!wanted) wanted = current
  116. return {
  117. current,
  118. latest,
  119. wanted,
  120. range: versionRange,
  121. localPath
  122. }
  123. }
  124. function getLocalPath (id, context) {
  125. const projects = require('./projects')
  126. const projectPkg = folders.readPackage(projects.getCurrent(context).path, context, true)
  127. const deps = Object.assign(
  128. {},
  129. projectPkg.dependencies || {},
  130. projectPkg.devDependencies || {}
  131. )
  132. const range = deps[id]
  133. if (range && range.match(/^file:/)) {
  134. const localPath = range.substr('file:'.length)
  135. return path.resolve(cwd.get(), localPath)
  136. }
  137. return null
  138. }
  139. async function getDescription ({ id }, context) {
  140. const metadata = await getMetadata(id, context)
  141. if (metadata) {
  142. return metadata.description
  143. }
  144. return null
  145. }
  146. function getLink ({ id, file }, context) {
  147. const pkg = readPackage({ id, file }, context)
  148. return pkg.homepage ||
  149. (pkg.repository && pkg.repository.url) ||
  150. `https://www.npmjs.com/package/${id.replace('/', '%2F')}`
  151. }
  152. function install ({ id, type, range }, context) {
  153. return progress.wrap(PROGRESS_ID, context, async setProgress => {
  154. setProgress({
  155. status: 'dependency-install',
  156. args: [id]
  157. })
  158. let arg
  159. if (range) {
  160. arg = `${id}@${range}`
  161. } else {
  162. arg = id
  163. }
  164. const pm = new PackageManager({ context: cwd.get() })
  165. await pm.add(arg, {
  166. tilde: !range && isPlugin(id),
  167. dev: type === 'devDependencies'
  168. })
  169. logs.add({
  170. message: `Dependency ${id} installed`,
  171. type: 'info'
  172. }, context)
  173. notify({
  174. title: 'Dependency installed',
  175. message: `Dependency ${id} successfully installed`,
  176. icon: 'done'
  177. })
  178. list(cwd.get(), context)
  179. return findOne(id, context)
  180. })
  181. }
  182. function uninstall ({ id }, context) {
  183. return progress.wrap(PROGRESS_ID, context, async setProgress => {
  184. setProgress({
  185. status: 'dependency-uninstall',
  186. args: [id]
  187. })
  188. const dep = findOne(id, context)
  189. const pm = new PackageManager({ context: cwd.get() })
  190. await pm.remove(id)
  191. logs.add({
  192. message: `Dependency ${id} uninstalled`,
  193. type: 'info'
  194. }, context)
  195. notify({
  196. title: 'Dependency uninstalled',
  197. message: `Dependency ${id} successfully uninstalled`,
  198. icon: 'done'
  199. })
  200. return dep
  201. })
  202. }
  203. function update ({ id }, context) {
  204. return progress.wrap(PROGRESS_ID, context, async setProgress => {
  205. setProgress({
  206. status: 'dependency-update',
  207. args: [id]
  208. })
  209. const dep = findOne(id, context)
  210. const { current, wanted } = await getVersion(dep, context)
  211. const pm = new PackageManager({ context: cwd.get() })
  212. await pm.upgrade(id)
  213. logs.add({
  214. message: `Dependency ${id} updated from ${current} to ${wanted}`,
  215. type: 'info'
  216. }, context)
  217. notify({
  218. title: 'Dependency updated',
  219. message: `Dependency ${id} was successfully updated`,
  220. icon: 'done'
  221. })
  222. invalidatePackage({ id }, context)
  223. return findOne(id)
  224. })
  225. }
  226. function updateAll (context) {
  227. return progress.wrap(PROGRESS_ID, context, async setProgress => {
  228. const deps = list(cwd.get(), context)
  229. const updatedDeps = []
  230. for (const dep of deps) {
  231. const version = await getVersion(dep, context)
  232. if (version.current !== version.wanted) {
  233. updatedDeps.push(dep)
  234. invalidatePackage({ id: dep.id }, context)
  235. }
  236. }
  237. if (!updatedDeps.length) {
  238. notify({
  239. title: 'No updates available',
  240. message: 'No dependency to update in the version ranges declared in package.json',
  241. icon: 'done'
  242. })
  243. return []
  244. }
  245. setProgress({
  246. status: 'dependencies-update',
  247. args: [updatedDeps.length]
  248. })
  249. const pm = new PackageManager({ context: cwd.get() })
  250. await pm.upgrade(updatedDeps.map(p => p.id).join(' '))
  251. notify({
  252. title: 'Dependencies updated',
  253. message: `${updatedDeps.length} dependencies were successfully updated`,
  254. icon: 'done'
  255. })
  256. return updatedDeps
  257. })
  258. }
  259. function setup (context) {
  260. // Package installation progress events
  261. installProgress.on('progress', value => {
  262. if (progress.get(PROGRESS_ID)) {
  263. progress.set({ id: PROGRESS_ID, progress: value }, context)
  264. }
  265. })
  266. installProgress.on('log', message => {
  267. if (progress.get(PROGRESS_ID)) {
  268. progress.set({ id: PROGRESS_ID, info: message }, context)
  269. }
  270. })
  271. }
  272. setup(getContext())
  273. module.exports = {
  274. list,
  275. findOne,
  276. getPath,
  277. getMetadata,
  278. getLink,
  279. getDescription,
  280. getVersion,
  281. install,
  282. uninstall,
  283. update,
  284. updateAll,
  285. invalidatePackage
  286. }