update.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const fs = require('fs');
  2. const path = require('path');
  3. const glob = require('glob');
  4. const ncp = require('ncp');
  5. function promisify(func, ...partialArgs) {
  6. return function (...args) {
  7. return new Promise((resolve, reject) => {
  8. func(...args, ...partialArgs, (err, data) => {
  9. err ? reject(err) : resolve(data);
  10. });
  11. });
  12. };
  13. }
  14. const getFiles = (glob => {
  15. return function (pattern, cwd='.') {
  16. return glob(pattern, {nodir: true, cwd});
  17. };
  18. })(promisify(glob));
  19. const readdir = promisify(fs.readdir);
  20. const stat = promisify(fs.stat);
  21. const copy = (copy => {
  22. return function (src, dest) {
  23. console.log(`Copy ${src} => ${dest}`);
  24. return copy(src, dest);
  25. };
  26. })(promisify(ncp));
  27. const MOD_DIR = 'node_modules';
  28. const LIB_DIR = 'src/public/lib';
  29. const mappings = {
  30. CodeMirror: 'codemirror',
  31. 'define.js': 'define-commonjs',
  32. 'vue.min.js': 'vue/dist',
  33. };
  34. function updateFile(dest, src) {
  35. const srcPath = path.join(MOD_DIR, src);
  36. return stat(srcPath)
  37. .then(res => res.isDirectory() ? path.join(srcPath, dest) : srcPath)
  38. .then(srcPath => copy(srcPath, path.join(LIB_DIR, dest)));
  39. }
  40. function updateDir(dest, src) {
  41. return getFiles('**', path.join(LIB_DIR, dest))
  42. .then(files => Promise.all(files.map(file => (
  43. copy(path.join(MOD_DIR, src, file), path.join(LIB_DIR, dest, file))
  44. ))));
  45. }
  46. function update(dest, src) {
  47. return stat(path.join(LIB_DIR, dest))
  48. .then(res => res.isFile() ? updateFile(dest, src) : updateDir(dest, src));
  49. }
  50. Promise.all(Object.keys(mappings).map(key => update(key, mappings[key])))
  51. .catch(err => console.log(err));