11#!/usr/bin/env node
22
3- const hyperquest = require ( 'hyperzip' ) ( require ( 'hyperdirect' ) )
3+ const hyperquest = require ( 'hyperquest' )
44 , bl = require ( 'bl' )
55 , fs = require ( 'fs' )
66 , path = require ( 'path' )
7- , cheerio = require ( 'cheerio' )
8-
7+ , tar = require ( 'tar-fs' )
8+ , gunzip = require ( 'gunzip-maybe' )
9+ , babel = require ( 'babel-core' )
10+ , glob = require ( 'glob' )
11+ , pump = require ( 'pump' )
12+ , rimraf = require ( 'rimraf' )
13+ , encoding = 'utf8'
14+ , urlRegex = / ^ h t t p s ? : \/ \/ /
15+ , nodeVersion = process . argv [ 2 ]
16+ , nodeVersionRegexString = '\\d+\\.\\d+\\.\\d+'
17+ , usageVersionRegex = RegExp ( '^' + nodeVersionRegexString + '$' )
18+ , readmeVersionRegex =
19+ RegExp ( '((?:Node-core )|(?:https\:\/\/nodejs\.org\/dist\/)v)' + nodeVersionRegexString , 'g' )
20+
21+ , readmePath = path . join ( __dirname , '..' , 'README.md' )
922 , files = require ( './files' )
1023 , testReplace = require ( './test-replacements' )
1124
12- , srcurlpfx = ' https://raw.github.com/joyent/node/v' + process . argv [ 2 ] + '-release/'
13- , libsrcurl = srcurlpfx + 'lib/'
14- , testsrcurl = srcurlpfx + 'test/simple/'
15- , testlisturl = 'https://github.com/joyent/node/tree/v' + process . argv [ 2 ] + '-release/ test/simple'
16- , libourroot = path . join ( __dirname , '../' )
17- , testourroot = path . join ( __dirname , '../test/simple /' )
25+ , downloadurl = ` https://nodejs.org/dist/v ${ nodeVersion } /node-v ${ nodeVersion } .tar.gz`
26+ , src = path . join ( __dirname , `node-v ${ nodeVersion } ` )
27+ , libsrcurl = path . join ( src , 'lib/' )
28+ , testsrcurl = path . join ( src , ' test/parallel/' )
29+ , libourroot = path . join ( __dirname , '../lib/ ' )
30+ , testourroot = path . join ( __dirname , '../test/parallel /' )
1831
1932
20- function processFile ( url , out , replacements ) {
21- hyperquest ( url ) . pipe ( bl ( function ( err , data ) {
22- if ( err )
23- throw err
33+ if ( ! usageVersionRegex . test ( nodeVersion ) ) {
34+ console . error ( 'Usage: build.js xx.yy.zz' )
35+ return process . exit ( 1 ) ;
36+ }
37+
38+ // `inputLoc`: URL or local path.
39+ function processFile ( inputLoc , out , replacements ) {
40+ var file = fs . createReadStream ( inputLoc , encoding )
2441
42+ file . pipe ( bl ( function ( err , data ) {
43+ if ( err ) throw err
44+
45+ console . log ( 'Processing' , inputLoc )
2546 data = data . toString ( )
2647 replacements . forEach ( function ( replacement ) {
27- data = data . replace . apply ( data , replacement )
48+ const regexp = replacement [ 0 ]
49+ var arg2 = replacement [ 1 ]
50+ if ( typeof arg2 === 'function' )
51+ arg2 = arg2 . bind ( data )
52+ data = data . replace ( regexp , arg2 )
2853 } )
29-
30- fs . writeFile ( out , data , 'utf8' , function ( err ) {
31- if ( err )
32- throw err
54+ if ( inputLoc . slice ( - 3 ) === '.js' ) {
55+ const transformed = babel . transform ( data , {
56+ plugins : [
57+ 'transform-es2015-parameters' ,
58+ 'transform-es2015-arrow-functions' ,
59+ 'transform-es2015-block-scoping' ,
60+ 'transform-es2015-template-literals' ,
61+ 'transform-es2015-shorthand-properties' ,
62+ 'transform-es2015-for-of' ,
63+ 'transform-es2015-destructuring'
64+ ]
65+ } )
66+ data = transformed . code
67+ }
68+ fs . writeFile ( out , data , encoding , function ( err ) {
69+ if ( err ) throw err
3370
3471 console . log ( 'Wrote' , out )
3572 } )
3673 } ) )
3774}
38-
75+ function deleteOldTests ( ) {
76+ const files = fs . readdirSync ( path . join ( __dirname , '..' , 'test' , 'parallel' ) ) ;
77+ for ( let file of files ) {
78+ let name = path . join ( __dirname , '..' , 'test' , 'parallel' , file ) ;
79+ console . log ( 'Removing' , name ) ;
80+ fs . unlinkSync ( name ) ;
81+ }
82+ }
3983function processLibFile ( file ) {
4084 var replacements = files [ file ]
4185 , url = libsrcurl + file
42- , out = path . join ( libourroot , replacements . out || file )
86+ , out = path . join ( libourroot , file )
4387
4488 processFile ( url , out , replacements )
4589}
@@ -56,39 +100,68 @@ function processTestFile (file) {
56100 processFile ( url , out , replacements )
57101}
58102
103+ //--------------------------------------------------------------------
104+ // Download the release from nodejs.org
105+ console . log ( `Downloading ${ downloadurl } ` )
106+ pump (
107+ hyperquest ( downloadurl ) ,
108+ gunzip ( ) ,
109+ tar . extract ( __dirname ) ,
110+ function ( err ) {
111+ if ( err ) {
112+ throw err
113+ }
59114
60- if ( ! / 0 \. 1 \d \. \d + / . test ( process . argv [ 2 ] ) ) {
61- console . log ( 'Usage: build.js <node version>' )
62- return process . exit ( - 1 )
63- }
64115
116+ //--------------------------------------------------------------------
117+ // Grab & process files in ../lib/
65118
66- //--------------------------------------------------------------------
67- // Grab & process files in ../lib/
119+ Object . keys ( files ) . forEach ( processLibFile )
68120
69- Object . keys ( files ) . forEach ( processLibFile )
70121
71- //--------------------------------------------------------------------
72- // Discover, grab and process all test-string-decoder* files on joyent/node
122+ //--------------------------------------------------------------------
123+ // Discover, grab and process all test-stream* files on the given release
124+
125+ glob ( path . join ( testsrcurl , 'test-string-decoder*.js' ) , function ( err , list ) {
126+ if ( err ) {
127+ throw err
128+ }
73129
74- hyperquest ( testlisturl ) . pipe ( bl ( function ( err , data ) {
75- if ( err )
76- throw err
130+ list . forEach ( function ( file ) {
131+ file = path . basename ( file )
132+ processTestFile ( file )
133+ } )
134+ } )
77135
78- var $ = cheerio . load ( data . toString ( ) )
79136
80- $ ( 'table.files .js-directory-link' ) . each ( function ( ) {
81- var file = $ ( this ) . text ( )
82- if ( / ^ t e s t - s t r i n g - d e c o d e r / . test ( file ) || file == 'common.js' )
83- processTestFile ( file )
84- } )
85- } ) )
137+ //--------------------------------------------------------------------
138+ // Grab the nodejs/node test/common.js
86139
87- //--------------------------------------------------------------------
88- // Grab the joyent/node test/common.js
140+ processFile (
141+ testsrcurl . replace ( / p a r a l l e l \/ $ / , 'common.js' )
142+ , path . join ( testourroot , '../common.js' )
143+ , testReplace [ 'common.js' ]
144+ )
89145
90- processFile (
91- testsrcurl + '../common.js'
92- , path . join ( testourroot , '../common.js' )
93- , testReplace [ 'common.js' ]
94- )
146+ //--------------------------------------------------------------------
147+ // Update Node version in README
148+
149+ processFile ( readmePath , readmePath , [
150+ [ readmeVersionRegex , "$1" + nodeVersion ]
151+ ] )
152+ }
153+ )
154+
155+ // delete the current contents of test/parallel so if node removes any tests
156+ // they are removed here
157+ deleteOldTests ( ) ;
158+
159+ process . once ( 'beforeExit' , function ( ) {
160+ rimraf ( src , function ( err ) {
161+ if ( err ) {
162+ throw err
163+ }
164+
165+ console . log ( 'Removed' , src )
166+ } )
167+ } )
0 commit comments