Gruntfile.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*eslint-env node*/
  2. 'use strict';
  3. const fs = require('fs');
  4. const path = require('path');
  5. const semver = require('semver');
  6. const liveEditor = require('@gfxfundamentals/live-editor');
  7. const liveEditorPath = path.dirname(require.resolve('@gfxfundamentals/live-editor'));
  8. module.exports = function(grunt) {
  9. require('load-grunt-tasks')(grunt);
  10. const s_ignoreRE = /\.(md|py|sh|enc)$/i;
  11. function noMds(filename) {
  12. return !s_ignoreRE.test(filename);
  13. }
  14. const s_isMdRE = /\.md$/i;
  15. function mdsOnly(filename) {
  16. return s_isMdRE.test(filename);
  17. }
  18. function notFolder(filename) {
  19. return !fs.statSync(filename).isDirectory();
  20. }
  21. function noMdsNoFolders(filename) {
  22. return noMds(filename) && notFolder(filename);
  23. }
  24. grunt.initConfig({
  25. eslint: {
  26. lib: {
  27. src: [
  28. 'threejs/resources/*.js',
  29. ],
  30. },
  31. support: {
  32. src: [
  33. 'Gruntfile.js',
  34. 'build/js/build.js',
  35. ],
  36. },
  37. examples: {
  38. src: [
  39. 'threejs/*.html',
  40. 'threejs/lessons/resources/*.js',
  41. '!threejs/lessons/resources/prettify.js',
  42. 'threejs/lessons/resources/*.html',
  43. ],
  44. },
  45. },
  46. copy: {
  47. main: {
  48. files: [
  49. { expand: false, src: '*', dest: 'out/', filter: noMdsNoFolders, },
  50. { expand: true, cwd: `${liveEditor.monacoEditor}/`, src: 'min/**', dest: 'out/monaco-editor/', nonull: true, },
  51. { expand: true, cwd: `${liveEditorPath}/src/`, src: '**', dest: 'out/threejs/resources/', nonull: true, },
  52. { expand: true, src: 'threejs/**', dest: 'out/', filter: noMds, },
  53. { expand: true, src: '3rdparty/**', dest: 'out/', },
  54. ],
  55. },
  56. },
  57. clean: [
  58. 'out/**/*',
  59. ],
  60. buildlesson: {
  61. main: {
  62. files: [],
  63. },
  64. },
  65. watch: {
  66. main: {
  67. files: [
  68. 'threejs/**',
  69. '3rdparty/**',
  70. ],
  71. tasks: ['copy'],
  72. options: {
  73. spawn: false,
  74. },
  75. },
  76. lessons: {
  77. files: [
  78. 'threejs/lessons/**/threejs*.md',
  79. ],
  80. tasks: ['buildlesson'],
  81. options: {
  82. spawn: false,
  83. },
  84. },
  85. },
  86. });
  87. let changedFiles = {};
  88. const onChange = grunt.util._.debounce(function() {
  89. grunt.config('copy.main.files', Object.keys(changedFiles).filter(noMds).map((file) => {
  90. return {
  91. src: file,
  92. dest: 'out/',
  93. };
  94. }));
  95. grunt.config('buildlesson.main.files', Object.keys(changedFiles).filter(mdsOnly).map((file) => {
  96. return {
  97. src: file,
  98. };
  99. }));
  100. changedFiles = {};
  101. }, 200);
  102. grunt.event.on('watch', function(action, filepath) {
  103. changedFiles[filepath] = action;
  104. onChange();
  105. });
  106. const buildSettings = {
  107. outDir: 'out',
  108. baseUrl: 'http://threejsfundamentals.org',
  109. rootFolder: 'threejs',
  110. lessonGrep: 'threejs*.md',
  111. siteName: 'ThreeJSFundamentals',
  112. siteThumbnail: 'threejsfundamentals.jpg', // in rootFolder/lessons/resources
  113. templatePath: 'build/templates',
  114. };
  115. // just the hackiest way to get this working.
  116. grunt.registerMultiTask('buildlesson', 'build a lesson', function() {
  117. const filenames = new Set();
  118. this.files.forEach((files) => {
  119. files.src.forEach((filename) => {
  120. filenames.add(filename);
  121. });
  122. });
  123. const buildStuff = require('@gfxfundamentals/lesson-builder');
  124. const settings = Object.assign({}, buildSettings, {
  125. filenames,
  126. });
  127. const finish = this.async();
  128. buildStuff(settings).finally(finish);
  129. });
  130. grunt.registerTask('buildlessons', function() {
  131. const buildStuff = require('@gfxfundamentals/lesson-builder');
  132. const finish = this.async();
  133. buildStuff(buildSettings).finally(finish);
  134. });
  135. grunt.task.registerMultiTask('fixthreepaths', 'fix three paths', function() {
  136. const options = this.options({});
  137. const oldVersionRE = new RegExp(`/${options.oldVersionStr}/`, 'g');
  138. const newVersionReplacement = `/${options.newVersionStr}/`;
  139. this.files.forEach((files) => {
  140. files.src.forEach((filename) => {
  141. const oldContent = fs.readFileSync(filename, {encoding: 'utf8'});
  142. const newContent = oldContent.replace(oldVersionRE, newVersionReplacement);
  143. if (oldContent !== newContent) {
  144. grunt.log.writeln(`updating ${filename} to ${options.newVersionStr}`);
  145. fs.writeFileSync(filename, newContent);
  146. }
  147. });
  148. });
  149. });
  150. grunt.registerTask('bumpthree', function() {
  151. const lessonInfo = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
  152. const oldVersion = lessonInfo.threejsfundamentals.threeVersion;
  153. const oldVersionStr = `r${oldVersion}`;
  154. const threePath = path.join(__dirname, '..', 'three.js');
  155. const threeInfo = JSON.parse(fs.readFileSync(path.join(threePath, 'package.json'), {encoding: 'utf8'}));
  156. const newVersion = semver.minor(threeInfo.version);
  157. const newVersionStr = `r${newVersion}`;
  158. const basePath = path.join('threejs', 'resources', 'threejs', newVersionStr);
  159. grunt.config.merge({
  160. copy: {
  161. threejs: {
  162. files: [
  163. { expand: true, cwd: `${threePath}/build/`, src: 'three.js', dest: `${basePath}/`, },
  164. { expand: true, cwd: `${threePath}/build/`, src: 'three.min.js', dest: `${basePath}/`, },
  165. { expand: true, cwd: `${threePath}/examples/js/`, src: '**', dest: `${basePath}/js/`, },
  166. ],
  167. },
  168. },
  169. fixthreepaths: {
  170. options: {
  171. oldVersionStr,
  172. newVersionStr,
  173. },
  174. src: [
  175. 'threejs/**/*.html',
  176. 'threejs/**/*.md',
  177. 'threejs/**/*.js',
  178. '!threejs/resources/threejs/**',
  179. ],
  180. },
  181. });
  182. lessonInfo.threejsfundamentals.threeVersion = newVersion;
  183. fs.writeFileSync('package.json', JSON.stringify(lessonInfo, null, 2));
  184. grunt.task.run(['copy:threejs', 'fixthreepaths']);
  185. });
  186. grunt.registerTask('build', ['clean', 'copy:main', 'buildlessons']);
  187. grunt.registerTask('buildwatch', ['build', 'watch']);
  188. grunt.registerTask('default', ['eslint', 'build']);
  189. };
粤ICP备19079148号