Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

link.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict'
  2. const u = require('universalify').fromCallback
  3. const path = require('path')
  4. const fs = require('graceful-fs')
  5. const mkdir = require('../mkdirs')
  6. const pathExists = require('../path-exists').pathExists
  7. function createLink (srcpath, dstpath, callback) {
  8. function makeLink (srcpath, dstpath) {
  9. fs.link(srcpath, dstpath, err => {
  10. if (err) return callback(err)
  11. callback(null)
  12. })
  13. }
  14. pathExists(dstpath, (err, destinationExists) => {
  15. if (err) return callback(err)
  16. if (destinationExists) return callback(null)
  17. fs.lstat(srcpath, (err) => {
  18. if (err) {
  19. err.message = err.message.replace('lstat', 'ensureLink')
  20. return callback(err)
  21. }
  22. const dir = path.dirname(dstpath)
  23. pathExists(dir, (err, dirExists) => {
  24. if (err) return callback(err)
  25. if (dirExists) return makeLink(srcpath, dstpath)
  26. mkdir.mkdirs(dir, err => {
  27. if (err) return callback(err)
  28. makeLink(srcpath, dstpath)
  29. })
  30. })
  31. })
  32. })
  33. }
  34. function createLinkSync (srcpath, dstpath) {
  35. const destinationExists = fs.existsSync(dstpath)
  36. if (destinationExists) return undefined
  37. try {
  38. fs.lstatSync(srcpath)
  39. } catch (err) {
  40. err.message = err.message.replace('lstat', 'ensureLink')
  41. throw err
  42. }
  43. const dir = path.dirname(dstpath)
  44. const dirExists = fs.existsSync(dir)
  45. if (dirExists) return fs.linkSync(srcpath, dstpath)
  46. mkdir.mkdirsSync(dir)
  47. return fs.linkSync(srcpath, dstpath)
  48. }
  49. module.exports = {
  50. createLink: u(createLink),
  51. createLinkSync
  52. }