1+ #!/usr/bin/env node
2+
3+ const hyperquest = require ( 'hyperzip' ) ( require ( 'hyperdirect' ) )
4+ , bl = require ( 'bl' )
5+ , fs = require ( 'fs' )
6+ , path = require ( 'path' )
7+ , cheerio = require ( 'cheerio' )
8+
9+ , files = require ( './files' )
10+ , testReplace = require ( './test-replacements' )
11+
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/' )
18+
19+
20+ function processFile ( url , out , replacements ) {
21+ hyperquest ( url ) . pipe ( bl ( function ( err , data ) {
22+ if ( err )
23+ throw err
24+
25+ data = data . toString ( )
26+ replacements . forEach ( function ( replacement ) {
27+ data = data . replace . apply ( data , replacement )
28+ } )
29+
30+ fs . writeFile ( out , data , 'utf8' , function ( err ) {
31+ if ( err )
32+ throw err
33+
34+ console . log ( 'Wrote' , out )
35+ } )
36+ } ) )
37+ }
38+
39+ function processLibFile ( file ) {
40+ var replacements = files [ file ]
41+ , url = libsrcurl + file
42+ , out = path . join ( libourroot , replacements . out || file )
43+
44+ processFile ( url , out , replacements )
45+ }
46+
47+
48+ function processTestFile ( file ) {
49+ var replacements = testReplace . all
50+ , url = testsrcurl + file
51+ , out = path . join ( testourroot , file )
52+
53+ if ( testReplace [ file ] )
54+ replacements = replacements . concat ( testReplace [ file ] )
55+
56+ processFile ( url , out , replacements )
57+ }
58+
59+
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+
65+
66+ //--------------------------------------------------------------------
67+ // Grab & process files in ../lib/
68+
69+ Object . keys ( files ) . forEach ( processLibFile )
70+
71+ //--------------------------------------------------------------------
72+ // Discover, grab and process all test-string-decoder* files on joyent/node
73+
74+ hyperquest ( testlisturl ) . pipe ( bl ( function ( err , data ) {
75+ if ( err )
76+ throw err
77+
78+ var $ = cheerio . load ( data . toString ( ) )
79+
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+ } ) )
86+
87+ //--------------------------------------------------------------------
88+ // Grab the joyent/node test/common.js
89+
90+ processFile (
91+ testsrcurl + '../common.js'
92+ , path . join ( testourroot , '../common.js' )
93+ , testReplace [ 'common.js' ]
94+ )
0 commit comments