Gruntfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*eslint-env node*/
  2. 'use strict';
  3. const fs = require('fs');
  4. const path = require('path');
  5. const semver = require('semver');
  6. module.exports = function(grunt) {
  7. require('load-grunt-tasks')(grunt);
  8. const s_ignoreRE = /\.(md|py|sh|enc)$/i;
  9. function noMds(filename) {
  10. return !s_ignoreRE.test(filename);
  11. }
  12. function notFolder(filename) {
  13. return !fs.statSync(filename).isDirectory();
  14. }
  15. function noMdsNoFolders(filename) {
  16. return noMds(filename) && notFolder(filename);
  17. }
  18. grunt.initConfig({
  19. eslint: {
  20. lib: {
  21. src: [
  22. 'threejs/resources/*.js',
  23. ],
  24. },
  25. support: {
  26. src: [
  27. 'Gruntfile.js',
  28. 'build/js/build.js',
  29. ],
  30. },
  31. examples: {
  32. src: [
  33. 'threejs/*.html',
  34. 'threejs/lessons/resources/*.js',
  35. '!threejs/lessons/resources/prettify.js',
  36. 'threejs/lessons/resources/*.html',
  37. ],
  38. },
  39. },
  40. copy: {
  41. main: {
  42. files: [
  43. { expand: false, src: '*', dest: 'out/', filter: noMdsNoFolders, },
  44. { expand: true, src: 'threejs/**', dest: 'out/', filter: noMds, },
  45. { expand: true, src: 'monaco-editor/**', dest: 'out/', },
  46. { expand: true, src: '3rdparty/**', dest: 'out/', },
  47. ],
  48. },
  49. },
  50. clean: [
  51. 'out/**/*',
  52. ],
  53. });
  54. grunt.registerTask('buildlessons', function() {
  55. const buildStuff = require('./build/js/build');
  56. const finish = this.async();
  57. buildStuff().then(function() {
  58. finish();
  59. }).done();
  60. });
  61. grunt.task.registerMultiTask('fixthreepaths', 'fix three paths', function() {
  62. const options = this.options({});
  63. const oldVersionRE = new RegExp(`/${options.oldVersionStr}/`, 'g');
  64. const newVersionReplacement = `/${options.newVersionStr}/`;
  65. this.files.forEach((files) => {
  66. files.src.forEach((filename) => {
  67. const oldContent = fs.readFileSync(filename, {encoding: 'utf8'});
  68. const newContent = oldContent.replace(oldVersionRE, newVersionReplacement);
  69. if (oldContent !== newContent) {
  70. grunt.log.writeln(`updating ${filename} to ${options.newVersionStr}`);
  71. fs.writeFileSync(filename, newContent);
  72. }
  73. });
  74. });
  75. });
  76. grunt.registerTask('bumpthree', function() {
  77. const lessonInfo = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
  78. const oldVersion = lessonInfo.threejsfundamentals.threeVersion;
  79. const oldVersionStr = `r${oldVersion}`;
  80. const threePath = path.join(__dirname, '..', 'three.js');
  81. const threeInfo = JSON.parse(fs.readFileSync(path.join(threePath, 'package.json'), {encoding: 'utf8'}));
  82. const newVersion = semver.minor(threeInfo.version);
  83. const newVersionStr = `r${newVersion}`;
  84. const basePath = path.join('threejs', 'resources', 'threejs', newVersionStr);
  85. grunt.config.merge({
  86. copy: {
  87. threejs: {
  88. files: [
  89. { expand: true, cwd: `${threePath}/build/`, src: 'three.js', dest: `${basePath}/`, },
  90. { expand: true, cwd: `${threePath}/build/`, src: 'three.min.js', dest: `${basePath}/`, },
  91. { expand: true, cwd: `${threePath}/examples/js/`, src: '**', dest: `${basePath}/js/`, },
  92. ],
  93. },
  94. },
  95. fixthreepaths: {
  96. options: {
  97. oldVersionStr,
  98. newVersionStr,
  99. },
  100. src: [
  101. 'threejs/**/*.html',
  102. 'threejs/**/*.md',
  103. 'threejs/**/*.js',
  104. '!threejs/resources/threejs/**',
  105. ],
  106. },
  107. });
  108. lessonInfo.threejsfundamentals.threeVersion = newVersion;
  109. fs.writeFileSync('package.json', JSON.stringify(lessonInfo, null, 2));
  110. grunt.task.run(['copy:threejs', 'fixthreepaths']);
  111. });
  112. grunt.registerTask('build', ['clean', 'copy:main', 'buildlessons']);
  113. grunt.registerTask('default', ['eslint', 'build']);
  114. };