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