loadRemotePreset.js 963 B

123456789101112131415161718192021222324252627282930313233
  1. const fs = require('fs-extra')
  2. const loadPresetFromDir = require('./loadPresetFromDir')
  3. module.exports = async function loadRemotePreset (repository, clone) {
  4. const os = require('os')
  5. const path = require('path')
  6. const download = require('download-git-repo')
  7. const presetName = repository
  8. .replace(/((?:.git)?#.*)/, '')
  9. .split('/')
  10. .slice(-1)[0]
  11. // for direct urls, it's hard to get the correct project name,
  12. // but we need to at least make sure no special characters remaining
  13. .replace(/[:#]/g, '')
  14. const tmpdir = path.join(os.tmpdir(), 'vue-cli-presets', presetName)
  15. // clone will fail if tmpdir already exists
  16. // https://github.com/flipxfx/download-git-repo/issues/41
  17. if (clone) {
  18. await fs.remove(tmpdir)
  19. }
  20. await new Promise((resolve, reject) => {
  21. download(repository, tmpdir, { clone }, err => {
  22. if (err) return reject(err)
  23. resolve()
  24. })
  25. })
  26. return await loadPresetFromDir(tmpdir)
  27. }