Browse Source

add bumpthree grunt task

Gregg Tavares 6 years ago
parent
commit
b2ed7e9aa9
1 changed files with 57 additions and 1 deletions
  1. 57 1
      Gruntfile.js

+ 57 - 1
Gruntfile.js

@@ -3,6 +3,8 @@
 'use strict';
 
 const fs = require('fs');
+const path = require('path');
+const semver = require('semver');
 
 module.exports = function(grunt) {
 
@@ -66,7 +68,61 @@ module.exports = function(grunt) {
     }).done();
   });
 
-  grunt.registerTask('build', ['clean', 'copy', 'buildlessons']);
+  grunt.task.registerMultiTask('fixthreepaths', 'fix three paths', function() {
+    const options = this.options({});
+    const oldVersionRE = new RegExp(`/${options.oldVersionStr}/`, 'g');
+    const newVersionReplacement = `/${options.newVersionStr}/`;
+    this.files.forEach((files) => {
+      files.src.forEach((filename) => {
+        const oldContent = fs.readFileSync(filename, {encoding: 'utf8'});
+        const newContent = oldContent.replace(oldVersionRE, newVersionReplacement);
+        if (oldContent !== newContent) {
+          grunt.log.writeln(`updating ${filename} to ${options.newVersionStr}`);
+          fs.writeFileSync(filename, newContent);
+        }
+      });
+    });
+  });
+
+  grunt.registerTask('bumpthree', function() {
+    const lessonInfo = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
+    const oldVersion = lessonInfo.threejsfundamentals.threeVersion;
+    const oldVersionStr = `r${oldVersion}`;
+    const threePath = path.join(__dirname, '..', 'three.js');
+    const threeInfo = JSON.parse(fs.readFileSync(path.join(threePath, 'package.json'), {encoding: 'utf8'}));
+    const newVersion = semver.minor(threeInfo.version);
+    const newVersionStr = `r${newVersion}`;
+    const basePath = path.join('threejs', 'resources', 'threejs', newVersionStr);
+    grunt.config.merge({
+      copy: {
+        threejs: {
+          files: [
+            { expand: true, cwd: `${threePath}/build/`, src: 'three.js', dest: `${basePath}/`, },
+            { expand: true, cwd: `${threePath}/build/`, src: 'three.min.js', dest: `${basePath}/`, },
+            { expand: true,  cwd: `${threePath}/examples/js/`, src: '**', dest: `${basePath}/js/`, },
+          ],
+        },
+      },
+      fixthreepaths: {
+        options: {
+          oldVersionStr,
+          newVersionStr,
+        },
+        src: [
+          'threejs/**/*.html',
+          'threejs/**/*.md',
+          'threejs/**/*.js',
+          '!threejs/resources/threejs/**',
+        ],
+      },
+    });
+
+    lessonInfo.threejsfundamentals.threeVersion = newVersion;
+    fs.writeFileSync('package.json', JSON.stringify(lessonInfo, null, 2));
+    grunt.task.run(['copy:threejs', 'fixthreepaths']);
+  });
+
+  grunt.registerTask('build', ['clean', 'copy:main', 'buildlessons']);
 
   grunt.registerTask('default', ['eslint', 'build']);
 };