File size: 6,512 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node

import { exec, spawnSync } from 'node:child_process';
import { EventEmitter } from 'node:events';
import { writeFile, copyFile } from 'node:fs/promises';
import path from 'node:path';
import chokidar from 'chokidar';
import runAll from 'npm-run-all';
import treeKill from 'tree-kill';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

// When we run a build which has more than 10 build tasks in parallel (like ETK),
// Node shows a warning. We just need to increase the number of allowed listeners
// to handle larger numbers of parallel tasks.
EventEmitter.setMaxListeners( 30 );

// Default NODE_ENV is development unless manually set to production.
if ( process.env.NODE_ENV == null ) {
	process.env.NODE_ENV = 'development';
}
if ( process.env.BROWSERSLIST_ENV == null ) {
	process.env.BROWSERSLIST_ENV = 'wpcom';
}
if ( process.env.DISABLE_DUPLICATE_PACKAGE_CHECK == null ) {
	process.env.DISABLE_DUPLICATE_PACKAGE_CHECK = true;
}

const { argv } = yargs( hideBin( process.argv ) ).options( {
	sync: { type: 'boolean', default: false, implies: 'remotePath' },
	remotePath: { type: 'string' },
	localPath: {
		type: 'string',
		required: true,
		coerce: ( distPath ) => ( distPath ? `${ process.cwd() }/${ distPath }/` : undefined ),
	},
	verbose: { type: 'boolean', default: false, alias: 'v' },
	watch: { type: 'boolean', default: process.env.NODE_ENV === 'development', alias: 'w' },
} );
const VERBOSE = argv.verbose;

try {
	await runBuilder( argv );
} catch ( e ) {
	// treeKill doesn't really work in CI and also isn't necessary.
	if ( process.env.IS_CI !== 'true' ) {
		const { pid } = process;
		if ( VERBOSE ) {
			console.log( `Removing children of PID: ${ pid }` );
		}
		treeKill( pid );
	}
	showTips( e.tasks );
	console.error( e.message );
}

async function runBuilder( args ) {
	const { sync, localPath, remotePath, watch } = args;

	if ( VERBOSE ) {
		console.log( JSON.stringify( args ) );
	}

	const runOpts = {
		stdout: process.stdout,
		stderr: process.stderr,
		printLabel: true,
		parallel: true,
	};
	console.log( 'Cleaning...' );
	await runAll( [ 'clean' ], runOpts );

	console.log( 'Starting webpack...' );
	await Promise.all( [
		runAll( [ `build:*${ watch ? ' --watch' : '' }` ], runOpts ).then( () => {
			console.log( 'Build completed!' );
			const translate = runAll( 'translate', runOpts ).catch( () => {} );
			translate.then( () => {
				if ( ! watch && sync ) {
					// In non-watch + sync mode, we sync only once after the build has finished.
					setupRemoteSync( localPath, remotePath );
				}
			} );
		} ),
		// In watch + sync mode, we start watching to sync while the webpack build is happening.
		watch && sync && setupRemoteSync( localPath, remotePath, true ),
	] );

	// Create build_meta.json and copy the README as needed for the production
	// artifact. We do it here rather than in webpack, because several apps use
	// multiple simultaneous webpack processes. As a result, some apps don't have
	// one true config we could add this to. Thankfully, every calypso app does
	// use this builder script, making it a good place to put it.
	if ( ! watch ) {
		await copyMetaFiles( localPath );
	}
}

/**
 * Sets up remote syncing. In watch mode, schedules syncs to the remote after changes
 * have stopped happening and existing syncs have stopped. In non-watch mode, does
 * a single sync. Rejects if any errors happen during rsync. Resolves in non-watch
 * mode after a full sync. Is otherwise pending until the user kills the process.
 */
function setupRemoteSync( localPath, remotePath, shouldWatch = false ) {
	const remoteHost = process.env.WPCOM_SANDBOX || 'wpcom-sandbox';

	return new Promise( ( resolve, reject ) => {
		let rsync = null;
		const debouncedSync = debouncer( () => {
			if ( VERBOSE ) {
				console.log( 'Attempting sync...' );
			}
			if ( rsync ) {
				// Kill any existing rsync attempt.
				rsync.kill( 'SIGINT' );
			}
			rsync = exec(
				`rsync -ahz --exclude=".*" ${ localPath } ${ remoteHost }:${ remotePath }`,
				( err ) => {
					rsync = null;
					// err.signal is null on macOS, so use error code 20 in that case.
					const wasRsyncCancelled = err && ( err.signal === 'SIGINT' || err.code === 20 );
					if ( err && ! wasRsyncCancelled ) {
						// If there's an error unrelated to cancellation, reject and abort.
						reject( err );
						return;
					} else if ( wasRsyncCancelled ) {
						if ( VERBOSE ) {
							console.log( 'Restarting sync.' );
						}
						return;
					}

					// A full sync was completed.
					console.log( 'Sync to sandbox completed.' );
					if ( ! shouldWatch ) {
						// We only needed to sync once, so we can resolve the sync promise.
						resolve();
					}
				}
			);
		} );

		if ( shouldWatch ) {
			chokidar.watch( localPath ).on( 'all', debouncedSync );
		} else {
			debouncedSync();
		}
	} );
}

/**
 * A debouncer that calls `cb` after it has not been called for at least 1s.
 */
function debouncer( cb ) {
	let timeout = null;
	return () => {
		// Each time the debounced function is called, cancel the current schedule
		// and re-schedule it for +1s.
		clearTimeout( timeout );
		timeout = setTimeout( cb, 1000 );
	};
}

function showTips( tasks ) {
	if ( ! Array.isArray( tasks ) ) {
		return;
	}

	const numFailed = tasks.reduce( ( total, { code } ) => total + ( code ? 1 : 0 ), 0 );
	if ( numFailed === tasks.length ) {
		console.log(
			'Since all builds failed, there is likely an issue with webpack or node modules.'
		);
		return;
	}
}

function git( cmd ) {
	return spawnSync( 'git', cmd.split( ' ' ), {
		encoding: 'utf8',
	} ).stdout.replace( '\n', '' );
}

async function copyMetaFiles( archiveDir ) {
	const buildNumber = process.env.build_number;

	// Use commit hash from the environment if available. In TeamCity, that reflects
	// the GitHub data -- the local data may be different.
	const commitHash = process.env.commit_sha ?? git( 'rev-parse HEAD' );
	// Calypso repo short sha is currently at 11 characters.
	const cacheBuster = commitHash.slice( 0, 11 );

	const buildMeta = {
		build_number: buildNumber ?? 'dev',
		cache_buster: cacheBuster,
		commit_hash: commitHash,
		commit_url: `https://github.com/Automattic/wp-calypso/commit/${ commitHash }`,
	};

	await writeFile(
		path.join( archiveDir, 'build_meta.json' ),
		JSON.stringify( buildMeta, null, 2 )
	);

	// Copy README.md to the root of the archive.
	await copyFile( path.join( process.cwd(), 'README.md' ), path.join( archiveDir, 'README.md' ) );
}