Gruntfile.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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({
  58. outDir: 'out',
  59. baseUrl: 'http://threejsfundamentals.org',
  60. rootFolder: 'threejs',
  61. lessonGrep: 'threejs*.md',
  62. siteName: 'ThreeJSFundamentals',
  63. siteThumbnail: 'threejsfundamentals.jpg', // in rootFolder/lessons/resources
  64. }).then(function() {
  65. finish();
  66. }).done();
  67. });
  68. grunt.task.registerMultiTask('fixthreepaths', 'fix three paths', function() {
  69. const options = this.options({});
  70. const oldVersionRE = new RegExp(`/${options.oldVersionStr}/`, 'g');
  71. const newVersionReplacement = `/${options.newVersionStr}/`;
  72. this.files.forEach((files) => {
  73. files.src.forEach((filename) => {
  74. const oldContent = fs.readFileSync(filename, {encoding: 'utf8'});
  75. const newContent = oldContent.replace(oldVersionRE, newVersionReplacement);
  76. if (oldContent !== newContent) {
  77. grunt.log.writeln(`updating ${filename} to ${options.newVersionStr}`);
  78. fs.writeFileSync(filename, newContent);
  79. }
  80. });
  81. });
  82. });
  83. grunt.registerTask('bumpthree', function() {
  84. const lessonInfo = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
  85. const oldVersion = lessonInfo.threejsfundamentals.threeVersion;
  86. const oldVersionStr = `r${oldVersion}`;
  87. const threePath = path.join(__dirname, '..', 'three.js');
  88. const threeInfo = JSON.parse(fs.readFileSync(path.join(threePath, 'package.json'), {encoding: 'utf8'}));
  89. const newVersion = semver.minor(threeInfo.version);
  90. const newVersionStr = `r${newVersion}`;
  91. const basePath = path.join('threejs', 'resources', 'threejs', newVersionStr);
  92. grunt.config.merge({
  93. copy: {
  94. threejs: {
  95. files: [
  96. { expand: true, cwd: `${threePath}/build/`, src: 'three.js', dest: `${basePath}/`, },
  97. { expand: true, cwd: `${threePath}/build/`, src: 'three.min.js', dest: `${basePath}/`, },
  98. { expand: true, cwd: `${threePath}/examples/js/`, src: '**', dest: `${basePath}/js/`, },
  99. ],
  100. },
  101. },
  102. fixthreepaths: {
  103. options: {
  104. oldVersionStr,
  105. newVersionStr,
  106. },
  107. src: [
  108. 'threejs/**/*.html',
  109. 'threejs/**/*.md',
  110. 'threejs/**/*.js',
  111. '!threejs/resources/threejs/**',
  112. ],
  113. },
  114. });
  115. lessonInfo.threejsfundamentals.threeVersion = newVersion;
  116. fs.writeFileSync('package.json', JSON.stringify(lessonInfo, null, 2));
  117. grunt.task.run(['copy:threejs', 'fixthreepaths']);
  118. });
  119. grunt.registerTask('build', ['clean', 'copy:main', 'buildlessons']);
  120. grunt.registerTask('default', ['eslint', 'build']);
  121. };
粤ICP备19079148号