11#!/usr/bin/env node
22
3- var fs = require ( 'fs' ) ;
4- var path = require ( 'path' ) ;
5- var querystring = require ( 'querystring' ) ;
6- var child_process = require ( 'child_process' ) ;
3+ const fs = require ( 'node:fs' ) ;
4+ const path = require ( 'node:path' ) ;
5+ const child_process = require ( 'node:child_process' ) ;
6+ const https = require ( 'node:https' ) ;
7+ const { text } = require ( 'node:stream/consumers' ) ;
78
8- var browserify = path . resolve ( path . join ( 'node_modules' , '.bin' , 'browserify' ) ) ;
9- var webpack = path . resolve ( path . join ( 'node_modules' , '.bin' , 'webpack' ) ) ;
10- var coffee = path . resolve ( path . join ( 'node_modules' , '.bin' , 'coffee' ) ) ;
9+ const browserify = path . resolve ( path . join ( 'node_modules' , '.bin' , 'browserify' ) ) ;
10+ const webpack = path . resolve ( path . join ( 'node_modules' , '.bin' , 'webpack' ) ) ;
11+ const coffee = path . resolve ( path . join ( 'node_modules' , '.bin' , 'coffee' ) ) ;
1112
1213function run ( command , callback ) {
1314 console . log ( command ) ;
@@ -16,90 +17,90 @@ function run(command, callback) {
1617
1718// Use browserify to package up source-map-support.js
1819fs . writeFileSync ( '.temp.js' , 'sourceMapSupport = require("./source-map-support");' ) ;
19- run ( browserify + ' .temp.js' , function ( error , stdout ) {
20+
21+ run ( browserify + ' .temp.js' , ( error , stdout ) => {
2022 if ( error ) throw error ;
2123
2224 // Wrap the code so it works both as a normal <script> module and as an AMD module
23- var header = [
25+ const header = [
2426 '/*' ,
2527 ' * Support for source maps in V8 stack traces' ,
2628 ' * https://github.com/evanw/node-source-map-support' ,
2729 ' */' ,
2830 ] . join ( '\n' ) ;
29- var code = [
31+
32+ const code = [
3033 '(this["define"] || function(name, callback) { this["sourceMapSupport"] = callback(); })("browser-source-map-support", function(sourceMapSupport) {' ,
3134 stdout . replace ( / \b b y t e \b / g, 'bite' ) . replace ( new RegExp ( __dirname + '/' , 'g' ) , '' ) . replace ( / @ l i c e n s e / g, 'license' ) ,
3235 'return sourceMapSupport});' ,
3336 ] . join ( '\n' ) ;
3437
3538 // Use the online Google Closure Compiler service for minification
36- var body = Buffer . from ( querystring . stringify ( {
39+ const body = new URLSearchParams ( {
3740 compilation_level : 'SIMPLE_OPTIMIZATIONS' ,
3841 output_info : 'compiled_code' ,
3942 output_format : 'text' ,
4043 js_code : code
41- } ) ) ;
44+ } ) ;
45+
46+ const buffer = new TextEncoder ( ) . encode ( body . toString ( ) )
47+
4248 console . log ( 'making request to google closure compiler' ) ;
43- var https = require ( 'https' ) ;
44- var request = https . request ( {
49+
50+ const request = https . request ( {
4551 method : 'POST' ,
4652 host : 'closure-compiler.appspot.com' ,
4753 path : '/compile' ,
4854 headers : {
49- 'content-length' : body . length ,
55+ 'content-length' : buffer . byteLength ,
5056 'content-type' : 'application/x-www-form-urlencoded'
5157 } ,
5258 } ) ;
53- request . end ( body ) ;
54- request . on ( 'response' , function ( response ) {
55- if ( response . statusCode !== 200 ) {
56- response . pipe ( process . stderr ) ;
57- response . on ( 'end' , function ( ) {
58- throw new Error ( 'failed to post to closure compiler' ) ;
59- } ) ;
60- return ;
61- }
6259
63- var data = [ ] ;
64- response . on ( 'data' , function ( chunk ) {
65- data . push ( chunk ) ;
66- } ) ;
67- response . on ( 'end' , function ( ) {
68- var stdout = Buffer . concat ( data ) ;
69- var code = header + '\n' + stdout ;
60+ request . once ( 'response' , response => {
61+ text ( response ) . then ( stdout => {
7062 fs . unlinkSync ( '.temp.js' ) ;
63+
64+ if ( response . statusCode !== 200 ) {
65+ console . error ( stdout ) ;
66+ throw new Error ( 'failed to post to closure compiler' ) ;
67+ }
68+
69+ const code = header + '\n' + stdout ;
7170 fs . writeFileSync ( 'browser-source-map-support.js' , code ) ;
7271 fs . writeFileSync ( 'amd-test/browser-source-map-support.js' , code ) ;
7372 } ) ;
7473 } ) ;
74+
75+ request . end ( buffer ) ;
7576} ) ;
7677
7778// Build the AMD test
78- run ( coffee + ' --map --compile amd-test/script.coffee' , function ( error ) {
79+ run ( coffee + ' --map --compile amd-test/script.coffee' , error => {
7980 if ( error ) throw error ;
8081} ) ;
8182
8283// Build the browserify test
83- run ( coffee + ' --map --compile browserify-test/script.coffee' , function ( error ) {
84+ run ( coffee + ' --map --compile browserify-test/script.coffee' , error => {
8485 if ( error ) throw error ;
85- run ( browserify + ' --debug browserify-test/script.js > browserify-test/compiled.js' , function ( error ) {
86+ run ( browserify + ' --debug browserify-test/script.js > browserify-test/compiled.js' , error => {
8687 if ( error ) throw error ;
8788 } )
8889} ) ;
8990
9091// Build the browser test
91- run ( coffee + ' --map --compile browser-test/script.coffee' , function ( error ) {
92+ run ( coffee + ' --map --compile browser-test/script.coffee' , error => {
9293 if ( error ) throw error ;
9394} ) ;
9495
9596// Build the header test
96- run ( coffee + ' --map --compile header-test/script.coffee' , function ( error ) {
97+ run ( coffee + ' --map --compile header-test/script.coffee' , error => {
9798 if ( error ) throw error ;
98- var contents = fs . readFileSync ( 'header-test/script.js' , 'utf8' ) ;
99+ const contents = fs . readFileSync ( 'header-test/script.js' , 'utf8' ) ;
99100 fs . writeFileSync ( 'header-test/script.js' , contents . replace ( / \/ \/ # s o u r c e M a p p i n g U R L = .* / g, '' ) )
100101} ) ;
101102
102103// Build the webpack test
103- child_process . exec ( webpack , { cwd : 'webpack-test' } , function ( error ) {
104+ child_process . exec ( webpack , { cwd : 'webpack-test' } , error => {
104105 if ( error ) throw error ;
105106} ) ;
0 commit comments