build.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Builds node_modules three and three-math
  3. *
  4. * Expects a single command line argument that is the build version in the format 0.54.4-dev
  5. *
  6. * @author bhouston / http://exocortex.com
  7. */
  8. var fs = require( "fs" );
  9. var cp = require('child_process');
  10. var commandLineArguments = process.argv.splice(2);
  11. var outputRootDir = "./node_modules";
  12. var inputDir = "../../build";
  13. var readmeFileName = "../../README.md";
  14. var headerFileName = "./header.js";
  15. var footerFileName = "./footer.js";
  16. if( commandLineArguments.length == 0 ) {
  17. throw new Error( "build version must be specified as a command line argument (e.g. 0.54.3-dev)");
  18. }
  19. var buildVersion = commandLineArguments[0];
  20. var concateFiles = function ( inputFileNames, outputFileName ) {
  21. var buffer = [];
  22. for ( var i = 0; i < inputFileNames.length; i++ ) {
  23. buffer.push(
  24. fs.readFileSync( inputFileNames[i], 'utf8' )
  25. );
  26. }
  27. var combinedContents = buffer.join("");
  28. fs.writeFileSync( outputFileName, combinedContents, 'utf8' );
  29. }
  30. var createTemplatedFile = function ( templateFileName, replaceSet, outputFileName ) {
  31. var templateContents = fs.readFileSync( templateFileName, 'utf8' );
  32. for( var token in replaceSet ) {
  33. templateContents = templateContents.replace( "%"+token+"%", replaceSet[token] );
  34. }
  35. fs.writeFileSync( outputFileName, templateContents, 'utf8' );
  36. }
  37. var copyFile = function( sourceFileName, destinationFileName ) {
  38. var contents = fs.readFileSync( sourceFileName, 'utf8' );
  39. fs.writeFileSync( destinationFileName, contents, 'utf8' );
  40. }
  41. var buildModule = function ( name, version ) {
  42. if( ! fs.existsSync( outputRootDir ) ) {
  43. fs.mkdirSync( outputRootDir );
  44. }
  45. var outputModuleDir = outputRootDir + "/" + name;
  46. if( ! fs.existsSync( outputModuleDir ) ) {
  47. fs.mkdirSync( outputModuleDir );
  48. }
  49. // make directory moduleDir
  50. var inputRawFileName = inputDir + "/" + name + ".js";
  51. var outputRawFileName = outputModuleDir + "/" + name + ".js";
  52. concateFiles( [ headerFileName, inputRawFileName, footerFileName ], outputRawFileName );
  53. var inputMinifiedFileName = inputDir + "/" + name + ".min.js";
  54. var outputMinifiedFileName = outputModuleDir + "/" + name + ".min.js";
  55. concateFiles( [ headerFileName, inputMinifiedFileName, footerFileName ], outputMinifiedFileName );
  56. var templatePackageFileName = "./" + name + ".package.json";
  57. var replaceSet = {
  58. "VERSION": buildVersion
  59. };
  60. var outputPackageFileName = outputModuleDir + "/package.json";
  61. createTemplatedFile( templatePackageFileName, replaceSet, outputPackageFileName );
  62. var outputReadmeFileName = outputModuleDir + "/README.md";
  63. copyFile( readmeFileName, outputReadmeFileName );
  64. }
  65. var cmdExe, args;
  66. if (process.platform === 'win32' || process.platform === 'win64') {
  67. cmdExe = "cmd.exe";
  68. args = [ "/c", "build_all.bat" ];
  69. } else {
  70. cmdExe = './build_all.sh';
  71. args = [];
  72. }
  73. var opts = { "cwd": "../build" };
  74. var buildAll = cp.spawn( cmdExe, args, opts );
  75. buildAll.stdout.on('data', function (data) {
  76. console.log('stdout: ' + data);
  77. });
  78. buildAll.stderr.on('data', function (data) {
  79. console.log('stderr: ' + data);
  80. });
  81. buildAll.on( 'exit', function ( exitCode ) {
  82. console.log( "exitCode: " + exitCode );
  83. buildModule( "three" );
  84. buildModule( "three-math" );
  85. });