| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const child_process = require('child_process'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2021-01-08 05:26:34 +08:00
										 |  |  | const chokidar = require('chokidar'); | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const steps = []; | 
					
						
							|  |  |  | const onChanges = []; | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | const copyFiles = []; | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const watchMode = process.argv.slice(2).includes('--watch'); | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | const lintMode = process.argv.slice(2).includes('--lint'); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | const ROOT = path.join(__dirname, '..', '..'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function filePath(relative) { | 
					
						
							|  |  |  |   return path.join(ROOT, ...relative.split('/')); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function runWatch() { | 
					
						
							|  |  |  |   function runOnChanges(paths, nodeFile) { | 
					
						
							| 
									
										
										
										
											2021-01-08 05:26:34 +08:00
										 |  |  |     nodeFile = filePath(nodeFile); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |     function callback() { | 
					
						
							| 
									
										
										
										
											2021-01-08 05:26:34 +08:00
										 |  |  |       child_process.spawnSync('node', [nodeFile], { stdio: 'inherit' }); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-08 05:26:34 +08:00
										 |  |  |     chokidar.watch([...paths, nodeFile].map(filePath)).on('all', callback); | 
					
						
							|  |  |  |     callback(); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const spawns = []; | 
					
						
							|  |  |  |   for (const step of steps) | 
					
						
							| 
									
										
										
										
											2021-01-09 08:15:05 +08:00
										 |  |  |     spawns.push(child_process.spawn(step.command, step.args, { stdio: 'inherit', shell: step.shell, env: { | 
					
						
							|  |  |  |       ...process.env, | 
					
						
							|  |  |  |       ...step.env, | 
					
						
							|  |  |  |     } })); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |   process.on('exit', () => spawns.forEach(s => s.kill())); | 
					
						
							|  |  |  |   for (const onChange of onChanges) | 
					
						
							|  |  |  |     runOnChanges(onChange.inputs, onChange.script); | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  |   for (const {files, from, to, ignored} of copyFiles) { | 
					
						
							|  |  |  |     const watcher = chokidar.watch([filePath(files)], {ignored}); | 
					
						
							|  |  |  |     watcher.on('all', (event, file) => { | 
					
						
							|  |  |  |       copyFile(file, from, to); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | async function runBuild() { | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |   function runStep(command, args, shell) { | 
					
						
							|  |  |  |     const out = child_process.spawnSync(command, args, { stdio: 'inherit', shell }); | 
					
						
							|  |  |  |     if (out.status) | 
					
						
							|  |  |  |       process.exit(out.status); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const step of steps) | 
					
						
							|  |  |  |     runStep(step.command, step.args, step.shell); | 
					
						
							|  |  |  |   for (const onChange of onChanges) { | 
					
						
							|  |  |  |     if (!onChange.committed) | 
					
						
							|  |  |  |       runStep('node', [filePath(onChange.script)], false); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  |   for (const {files, from, to, ignored} of copyFiles) { | 
					
						
							|  |  |  |     const watcher = chokidar.watch([filePath(files)], { | 
					
						
							|  |  |  |       ignored | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     watcher.on('add', file => { | 
					
						
							|  |  |  |       copyFile(file, from, to); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await new Promise(x => watcher.once('ready', x)); | 
					
						
							|  |  |  |     watcher.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function copyFile(file, from, to) { | 
					
						
							|  |  |  |   const destination = path.resolve(filePath(to), path.relative(filePath(from), file)); | 
					
						
							|  |  |  |   fs.mkdirSync(path.dirname(destination), { recursive: true }); | 
					
						
							|  |  |  |   fs.copyFileSync(file, destination); | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Build injected scripts.
 | 
					
						
							|  |  |  | const webPackFiles = [ | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  |   'src/server/injected/webpack.config.js', | 
					
						
							| 
									
										
										
										
											2021-01-29 01:33:20 +08:00
										 |  |  |   'src/web/traceViewer/webpack.config.js', | 
					
						
							|  |  |  |   'src/web/recorder/webpack.config.js', | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | ]; | 
					
						
							|  |  |  | for (const file of webPackFiles) { | 
					
						
							|  |  |  |   steps.push({ | 
					
						
							|  |  |  |     command: 'npx', | 
					
						
							| 
									
										
										
										
											2021-01-09 08:15:05 +08:00
										 |  |  |     args: ['webpack', '--config', filePath(file), ...(watchMode ? ['--watch', '--silent'] : [])], | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |     shell: true, | 
					
						
							| 
									
										
										
										
											2021-01-09 08:15:05 +08:00
										 |  |  |     env: { | 
					
						
							|  |  |  |       NODE_ENV: watchMode ? 'development' : 'production' | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | // Run Babel.
 | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | steps.push({ | 
					
						
							|  |  |  |   command: 'npx', | 
					
						
							| 
									
										
										
										
											2021-07-23 02:29:36 +08:00
										 |  |  |   args: ['babel', ...(watchMode ? ['-w', '--source-maps'] : []), '--extensions', '.ts', '--out-dir', filePath('./lib/'), filePath('./src/')], | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |   shell: true, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Generate channels.
 | 
					
						
							|  |  |  | onChanges.push({ | 
					
						
							|  |  |  |   committed: false, | 
					
						
							|  |  |  |   inputs: [ | 
					
						
							|  |  |  |     'src/protocol/protocol.yml' | 
					
						
							|  |  |  |   ], | 
					
						
							|  |  |  |   script: 'utils/generate_channels.js', | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Generate types.
 | 
					
						
							|  |  |  | onChanges.push({ | 
					
						
							|  |  |  |   committed: false, | 
					
						
							|  |  |  |   inputs: [ | 
					
						
							| 
									
										
										
										
											2021-01-08 05:26:34 +08:00
										 |  |  |     'docs/src/api/', | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |     'utils/generate_types/overrides.d.ts', | 
					
						
							|  |  |  |     'utils/generate_types/exported.json', | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  |     'src/server/chromium/protocol.d.ts', | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  |     'src/trace/traceTypes.ts', | 
					
						
							|  |  |  |   ], | 
					
						
							|  |  |  |   script: 'utils/generate_types/index.js', | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-02 08:05:14 +08:00
										 |  |  | // The recorder and trace viewer have an app_icon.png that needs to be copied.
 | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | copyFiles.push({ | 
					
						
							| 
									
										
										
										
											2021-07-02 08:05:14 +08:00
										 |  |  |   files: 'src/server/chromium/*.png', | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  |   from: 'src', | 
					
						
							|  |  |  |   to: 'lib', | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Babel doesn't touch JS files, so copy them manually.
 | 
					
						
							|  |  |  | // For example: diff_match_patch.js
 | 
					
						
							|  |  |  | copyFiles.push({ | 
					
						
							|  |  |  |   files: 'src/**/*.js', | 
					
						
							|  |  |  |   from: 'src', | 
					
						
							|  |  |  |   to: 'lib', | 
					
						
							|  |  |  |   ignored: ['**/.eslintrc.js', '**/*webpack.config.js', '**/injected/**/*'] | 
					
						
							| 
									
										
										
										
											2021-02-01 08:37:13 +08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 09:01:48 +08:00
										 |  |  | // Sometimes we require JSON files that babel ignores.
 | 
					
						
							|  |  |  | // For example, deviceDescriptorsSource.json
 | 
					
						
							|  |  |  | copyFiles.push({ | 
					
						
							|  |  |  |   files: 'src/**/*.json', | 
					
						
							|  |  |  |   ignored: ['**/injected/**/*'], | 
					
						
							|  |  |  |   from: 'src', | 
					
						
							|  |  |  |   to: 'lib', | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (lintMode) { | 
					
						
							|  |  |  |   // Run TypeScript for type chekcing.
 | 
					
						
							|  |  |  |   steps.push({ | 
					
						
							|  |  |  |     command: 'npx', | 
					
						
							|  |  |  |     args: ['tsc', ...(watchMode ? ['-w'] : []), '-p', filePath('.')], | 
					
						
							|  |  |  |     shell: true, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 04:41:17 +08:00
										 |  |  | watchMode ? runWatch() : runBuild(); |