| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 07:03:09 -08:00
										 |  |  | //@ts-check
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 00:03:57 -07:00
										 |  |  | const playwright = require('../../'); | 
					
						
							| 
									
										
										
										
											2020-12-02 13:50:10 -08:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:00:04 -08:00
										 |  |  | const { parseApi } = require('./api_parser'); | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  | const missingDocs = require('./missingDocs'); | 
					
						
							| 
									
										
										
										
											2021-01-08 16:36:52 -08:00
										 |  |  | const md = require('../markdown'); | 
					
						
							| 
									
										
										
										
											2020-02-13 18:26:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 15:00:04 -08:00
										 |  |  | /** @typedef {import('./documentation').Type} Type */ | 
					
						
							| 
									
										
										
										
											2020-12-28 17:38:00 -08:00
										 |  |  | /** @typedef {import('../markdown').MarkdownNode} MarkdownNode */ | 
					
						
							| 
									
										
										
										
											2020-12-28 07:03:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | const PROJECT_DIR = path.join(__dirname, '..', '..'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  | const dirtyFiles = new Set(); | 
					
						
							| 
									
										
										
										
											2020-12-30 18:04:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 09:21:56 -08:00
										 |  |  | run().catch(e => { | 
					
						
							|  |  |  |   console.error(e); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							|  |  |  | });; | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function run() { | 
					
						
							| 
									
										
										
										
											2021-01-07 15:00:04 -08:00
										 |  |  |   const documentation = parseApi(path.join(PROJECT_DIR, 'docs', 'src', 'api')); | 
					
						
							| 
									
										
										
										
											2021-01-06 11:59:29 -08:00
										 |  |  |   // This validates member links.
 | 
					
						
							| 
									
										
										
										
											2021-01-07 15:00:04 -08:00
										 |  |  |   documentation.setLinkRenderer(() => undefined); | 
					
						
							|  |  |  |   documentation.filterForLanguage('js'); | 
					
						
							| 
									
										
										
										
											2020-12-02 13:50:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |   // Patch README.md
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |     const versions = await getBrowserVersions(); | 
					
						
							|  |  |  |     const params = new Map(); | 
					
						
							|  |  |  |     const { chromium, firefox, webkit } = versions; | 
					
						
							|  |  |  |     params.set('chromium-version', chromium); | 
					
						
							|  |  |  |     params.set('firefox-version', firefox); | 
					
						
							|  |  |  |     params.set('webkit-version', webkit); | 
					
						
							|  |  |  |     params.set('chromium-version-badge', `[](https://www.chromium.org/Home)`); | 
					
						
							|  |  |  |     params.set('firefox-version-badge', `[](https://www.mozilla.org/en-US/firefox/new/)`); | 
					
						
							|  |  |  |     params.set('webkit-version-badge', `[](https://webkit.org/)`); | 
					
						
							| 
									
										
										
										
											2020-12-30 18:04:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |     let content = fs.readFileSync(path.join(PROJECT_DIR, 'README.md')).toString(); | 
					
						
							|  |  |  |     content = content.replace(/<!-- GEN:([^ ]+) -->([^<]*)<!-- GEN:stop -->/ig, (match, p1) => { | 
					
						
							|  |  |  |       if (!params.has(p1)) { | 
					
						
							|  |  |  |         console.log(`ERROR: Invalid generate parameter "${p1}" in "${match}"`); | 
					
						
							|  |  |  |         process.exit(1); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return `<!-- GEN:${p1} -->${params.get(p1)}<!-- GEN:stop -->`; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     writeAssumeNoop(path.join(PROJECT_DIR, 'README.md'), content, dirtyFiles); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 16:36:52 -08:00
										 |  |  |   // Validate links
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     for (const file of fs.readdirSync(path.join(PROJECT_DIR, 'docs', 'src'))) { | 
					
						
							|  |  |  |       if (!file.endsWith('.md')) | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       const data = fs.readFileSync(path.join(PROJECT_DIR, 'docs', 'src', file)).toString(); | 
					
						
							|  |  |  |       documentation.renderLinksInText(md.parse(data)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |   // Check for missing docs
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const srcClient = path.join(PROJECT_DIR, 'src', 'client'); | 
					
						
							|  |  |  |     const sources = fs.readdirSync(srcClient).map(n => path.join(srcClient, n)); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:00:04 -08:00
										 |  |  |     const errors = missingDocs(documentation, sources, path.join(srcClient, 'api.ts')); | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |     if (errors.length) { | 
					
						
							|  |  |  |       console.log('============================'); | 
					
						
							|  |  |  |       console.log('ERROR: missing documentation:'); | 
					
						
							|  |  |  |       errors.forEach(e => console.log(e)); | 
					
						
							|  |  |  |       console.log('============================') | 
					
						
							|  |  |  |       process.exit(1); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |   if (dirtyFiles.size) { | 
					
						
							|  |  |  |     console.log('============================') | 
					
						
							|  |  |  |     console.log('ERROR: generated markdown files have changed, this is only error if happens in CI:'); | 
					
						
							|  |  |  |     [...dirtyFiles].forEach(f => console.log(f)); | 
					
						
							|  |  |  |     console.log('============================') | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |   process.exit(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string} name | 
					
						
							|  |  |  |  * @param {string} content | 
					
						
							|  |  |  |  * @param {Set<string>} dirtyFiles | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function writeAssumeNoop(name, content, dirtyFiles) { | 
					
						
							| 
									
										
										
										
											2021-01-03 08:47:29 -08:00
										 |  |  |   fs.mkdirSync(path.dirname(name), { recursive: true }); | 
					
						
							|  |  |  |   const oldContent = fs.existsSync(name) ? fs.readFileSync(name).toString() : ''; | 
					
						
							| 
									
										
										
										
											2021-01-01 15:17:27 -08:00
										 |  |  |   if (oldContent !== content) { | 
					
						
							|  |  |  |     fs.writeFileSync(name, content); | 
					
						
							|  |  |  |     dirtyFiles.add(name); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-13 18:26:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 14:03:01 -07:00
										 |  |  | async function getBrowserVersions() { | 
					
						
							| 
									
										
										
										
											2020-12-21 18:09:55 -08:00
										 |  |  |   const names = ['chromium', 'firefox', 'webkit']; | 
					
						
							|  |  |  |   const browsers = await Promise.all(names.map(name => playwright[name].launch())); | 
					
						
							|  |  |  |   const result = {}; | 
					
						
							|  |  |  |   for (let i = 0; i < names.length; i++) { | 
					
						
							|  |  |  |     result[names[i]] = browsers[i].version(); | 
					
						
							| 
									
										
										
										
											2020-03-12 14:03:01 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-21 18:09:55 -08:00
										 |  |  |   await Promise.all(browsers.map(browser => browser.close())); | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2020-03-12 14:03:01 -07:00
										 |  |  | } |