Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /**
  2. * Gulpfile.js
  3. * Symlinks node_module dependencies into any directory. This
  4. * file works on Windows and Linux machines by recursively
  5. * symlinking the files within a node module instead of symlinking
  6. * just the directory. This is vitally important as Editor's on Windows
  7. * do not understand Junctions and therefor will delete all the children
  8. * files first (i.e. the original files from the original directory), a
  9. * disasterous situation, especially if this is used with npm link.
  10. */
  11.  
  12. var gulp = require('gulp'),
  13. clean = require('gulp-clean'),
  14. path = require('path'),
  15. fs = require('graceful-fs'),
  16. source = require('vinyl-source-stream'),
  17. through2 = require('through2'),
  18. prepareWrite = require('vinyl-fs/lib/prepareWrite'),
  19.  
  20. // Project package.json
  21. PACKAGE_JSON = JSON.parse (fs.readFileSync ('./package.json')),
  22.  
  23. // Project Structure
  24. APP = {
  25. SRC: {
  26. BASE: "./src/main/webapp/js",
  27. DEST: "./src/main/webapp/dist",
  28. GLOBS: [
  29. "**/*.js"
  30. ],
  31. FILTER: () => APP.SRC.GLOBS.map(glob => path.join(APP.SRC.BASE, glob))
  32. },
  33. TESTS: {
  34. BASE: "./src/test/js",
  35. DEST: "./src/test/dist",
  36. GLOBS: [
  37. "**/*.js"
  38. ],
  39. FILTER: () => APP.TESTS.GLOBS.map(glob => path.join(APP.TESTS.BASE, glob))
  40. },
  41. DEPS: {
  42. BASE: "./node_modules",
  43. DEST: "./src/main/webapp/lib",
  44. GLOBS: Object.keys (PACKAGE_JSON.dependencies).map(dep => dep + "/**/*"),
  45. FILTER: () => APP.DEPS.GLOBS.map (glob => path.join(APP.DEPS.BASE, glob))
  46. }
  47. };
  48.  
  49. gulp.task ('default', ['build:deps', 'build:src']);
  50.  
  51. // Link the dependencies to the lib directory instead of requiring a copy.
  52. gulp.task ('build:deps', function () {
  53. return gulp.src(APP.DEPS.FILTER(), { base: APP.DEPS.BASE})
  54. .pipe(symlink(APP.DEPS.DEST, {}));
  55. });
  56.  
  57. // Just an example. Babel would go here.
  58. gulp.task ('build:src', function () {
  59. return gulp.src(APP.SRC.FILTER(), { base: APP.SRC.BASE})
  60. .pipe(gulp.dest(APP.SRC.DEST));
  61. });
  62.  
  63. // Clean project
  64. gulp.task('build:clean', function () {
  65. return gulp.src(Object.keys(APP).map(key => APP[key]["DEST"]).filter(Boolean))
  66. .pipe(clean());
  67. });
  68.  
  69. /**
  70. * Create a special symlink stream processor which will
  71. * use junctions instead of dir on Windows. As the 'dir' symlink
  72. * on Windows is all but impossible to create as a User with Admin rights.
  73. */
  74. function symlink(outFolder, opt) {
  75. function linkFile(file, enc, cb) {
  76. var srcPath = file.path;
  77. var symType = file.isDirectory() ? ((process.platform === 'win32') ? 'junction' : 'dir') : 'file';
  78. prepareWrite(outFolder, file, opt, function(err, writePath) {
  79. if (err) {
  80. return cb(err);
  81. }
  82. fs.symlink(srcPath, writePath, symType, function(err) {
  83. if (err && err.code !== 'EEXIST') {
  84. return cb(err);
  85. }
  86. cb(null, file);
  87. });
  88.  
  89. });
  90. }
  91. var stream = through2.obj(opt, linkFile);
  92. stream.resume();
  93. return stream;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement