Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

symlink.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict'
  2. const u = require('universalify').fromCallback
  3. const path = require('path')
  4. const fs = require('graceful-fs')
  5. const _mkdirs = require('../mkdirs')
  6. const mkdirs = _mkdirs.mkdirs
  7. const mkdirsSync = _mkdirs.mkdirsSync
  8. const _symlinkPaths = require('./symlink-paths')
  9. const symlinkPaths = _symlinkPaths.symlinkPaths
  10. const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
  11. const _symlinkType = require('./symlink-type')
  12. const symlinkType = _symlinkType.symlinkType
  13. const symlinkTypeSync = _symlinkType.symlinkTypeSync
  14. const pathExists = require('../path-exists').pathExists
  15. function createSymlink (srcpath, dstpath, type, callback) {
  16. callback = (typeof type === 'function') ? type : callback
  17. type = (typeof type === 'function') ? false : type
  18. pathExists(dstpath, (err, destinationExists) => {
  19. if (err) return callback(err)
  20. if (destinationExists) return callback(null)
  21. symlinkPaths(srcpath, dstpath, (err, relative) => {
  22. if (err) return callback(err)
  23. srcpath = relative.toDst
  24. symlinkType(relative.toCwd, type, (err, type) => {
  25. if (err) return callback(err)
  26. const dir = path.dirname(dstpath)
  27. pathExists(dir, (err, dirExists) => {
  28. if (err) return callback(err)
  29. if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
  30. mkdirs(dir, err => {
  31. if (err) return callback(err)
  32. fs.symlink(srcpath, dstpath, type, callback)
  33. })
  34. })
  35. })
  36. })
  37. })
  38. }
  39. function createSymlinkSync (srcpath, dstpath, type) {
  40. const destinationExists = fs.existsSync(dstpath)
  41. if (destinationExists) return undefined
  42. const relative = symlinkPathsSync(srcpath, dstpath)
  43. srcpath = relative.toDst
  44. type = symlinkTypeSync(relative.toCwd, type)
  45. const dir = path.dirname(dstpath)
  46. const exists = fs.existsSync(dir)
  47. if (exists) return fs.symlinkSync(srcpath, dstpath, type)
  48. mkdirsSync(dir)
  49. return fs.symlinkSync(srcpath, dstpath, type)
  50. }
  51. module.exports = {
  52. createSymlink: u(createSymlink),
  53. createSymlinkSync
  54. }