1
1
#!/usr/bin/env node
2
2
3
- const hyperquest = require ( 'hyperzip' ) ( require ( 'hyperdirect' ) )
3
+ const hyperquest = require ( 'hyperquest' )
4
4
, bl = require ( 'bl' )
5
5
, fs = require ( 'fs' )
6
6
, 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' )
9
22
, files = require ( './files' )
10
23
, testReplace = require ( './test-replacements' )
11
24
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 /' )
18
31
19
32
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 )
24
41
42
+ file . pipe ( bl ( function ( err , data ) {
43
+ if ( err ) throw err
44
+
45
+ console . log ( 'Processing' , inputLoc )
25
46
data = data . toString ( )
26
47
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 )
28
53
} )
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
33
70
34
71
console . log ( 'Wrote' , out )
35
72
} )
36
73
} ) )
37
74
}
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
+ }
39
83
function processLibFile ( file ) {
40
84
var replacements = files [ file ]
41
85
, url = libsrcurl + file
42
- , out = path . join ( libourroot , replacements . out || file )
86
+ , out = path . join ( libourroot , file )
43
87
44
88
processFile ( url , out , replacements )
45
89
}
@@ -56,39 +100,68 @@ function processTestFile (file) {
56
100
processFile ( url , out , replacements )
57
101
}
58
102
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
+ }
59
114
60
- if ( ! / 0 \. 1 \d \. \d + / . test ( process . argv [ 2 ] ) ) {
61
- console . log ( 'Usage: build.js <node version>' )
62
- return process . exit ( - 1 )
63
- }
64
115
116
+ //--------------------------------------------------------------------
117
+ // Grab & process files in ../lib/
65
118
66
- //--------------------------------------------------------------------
67
- // Grab & process files in ../lib/
119
+ Object . keys ( files ) . forEach ( processLibFile )
68
120
69
- Object . keys ( files ) . forEach ( processLibFile )
70
121
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
+ }
73
129
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
+ } )
77
135
78
- var $ = cheerio . load ( data . toString ( ) )
79
136
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
86
139
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
+ )
89
145
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