| 
									
										
										
										
											2019-11-19 10: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 23:03:09 +08:00
										 |  |  | //@ts-check
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 15:03:57 +08:00
										 |  |  | const playwright = require('../../'); | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | const path = require('path'); | 
					
						
							|  |  |  | const Source = require('./Source'); | 
					
						
							| 
									
										
										
										
											2020-04-24 10:52:06 +08:00
										 |  |  | const Message = require('./Message'); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | const { parseMd, renderMd, applyTemplates, clone } = require('./../parse_md'); | 
					
						
							| 
									
										
										
										
											2020-12-04 01:11:48 +08:00
										 |  |  | const { spawnSync } = require('child_process'); | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  | const preprocessor = require('./preprocessor'); | 
					
						
							| 
									
										
										
										
											2020-12-27 06:31:41 +08:00
										 |  |  | const mdBuilder = require('./check_public_api/MDBuilder'); | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | /** @typedef {import('./check_public_api/Documentation').MarkdownNode} MarkdownNode */ | 
					
						
							|  |  |  | /** @typedef {import('./check_public_api/Documentation').Type} Type */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | const PROJECT_DIR = path.join(__dirname, '..', '..'); | 
					
						
							|  |  |  | const VERSION = require(path.join(PROJECT_DIR, 'package.json')).version; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RED_COLOR = '\x1b[31m'; | 
					
						
							|  |  |  | const YELLOW_COLOR = '\x1b[33m'; | 
					
						
							|  |  |  | const RESET_COLOR = '\x1b[0m'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 01:21:56 +08:00
										 |  |  | run().catch(e => { | 
					
						
							|  |  |  |   console.error(e); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							|  |  |  | });; | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function run() { | 
					
						
							|  |  |  |   const startTime = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 10:05:35 +08:00
										 |  |  |   const api = await Source.readFile(path.join(PROJECT_DIR, 'docs', 'api.md')); | 
					
						
							|  |  |  |   const readme = await Source.readFile(path.join(PROJECT_DIR, 'README.md')); | 
					
						
							|  |  |  |   const binReadme = await Source.readFile(path.join(PROJECT_DIR, 'bin', 'README.md')); | 
					
						
							|  |  |  |   const contributing = await Source.readFile(path.join(PROJECT_DIR, 'CONTRIBUTING.md')); | 
					
						
							|  |  |  |   const docs = await Source.readdir(path.join(PROJECT_DIR, 'docs'), '.md'); | 
					
						
							|  |  |  |   const mdSources = [readme, binReadme, api, contributing, ...docs]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   /** @type {!Array<!Message>} */ | 
					
						
							|  |  |  |   const messages = []; | 
					
						
							|  |  |  |   let changedFiles = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |   const header = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-header.md')).toString(); | 
					
						
							|  |  |  |   const body = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-body.md')).toString(); | 
					
						
							|  |  |  |   const footer = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-footer.md')).toString(); | 
					
						
							|  |  |  |   const links = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-links.md')).toString(); | 
					
						
							|  |  |  |   const params = fs.readFileSync(path.join(PROJECT_DIR, 'docs-src', 'api-params.md')).toString(); | 
					
						
							|  |  |  |   const apiSpec = applyTemplates(parseMd(body), parseMd(params)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  |   // Produce api.md
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const comment = '<!-- THIS FILE IS NOW GENERATED -->'; | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |       const { outline } = mdBuilder(apiSpec, false); | 
					
						
							| 
									
										
										
										
											2020-12-27 06:31:41 +08:00
										 |  |  |       const signatures = outline.signatures; | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |       const result = []; | 
					
						
							|  |  |  |       for (const clazz of outline.classesArray) { | 
					
						
							|  |  |  |         // Iterate over classes, create header node.
 | 
					
						
							|  |  |  |         const classNode = { type: 'h3', text: `class: ${clazz.name}` }; | 
					
						
							|  |  |  |         const match = clazz.name.match(/(JS|CDP|[A-Z])(.*)/); | 
					
						
							|  |  |  |         const varName = match[1].toLocaleLowerCase() + match[2]; | 
					
						
							|  |  |  |         result.push(classNode); | 
					
						
							|  |  |  |         // Append link shortcut to resolve text like [Browser]
 | 
					
						
							|  |  |  |         result.push({ | 
					
						
							|  |  |  |           type: 'text', | 
					
						
							|  |  |  |           text: `[${clazz.name}]: #class-${clazz.name.toLowerCase()} "${clazz.name}"` | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         // Append class comments
 | 
					
						
							|  |  |  |         classNode.children = (clazz.spec || []).map(c => clone(c)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const member of clazz.membersArray) { | 
					
						
							|  |  |  |           // Iterate members
 | 
					
						
							|  |  |  |           const memberNode = { type: 'h4', children: [] }; | 
					
						
							|  |  |  |           if (member.kind === 'event') { | 
					
						
							|  |  |  |             memberNode.text = `${varName}.on('${member.name}')`; | 
					
						
							|  |  |  |           } else if (member.kind === 'property') { | 
					
						
							|  |  |  |             memberNode.text = `${varName}.${member.name}`; | 
					
						
							|  |  |  |           } else if (member.kind === 'method') { | 
					
						
							|  |  |  |             // Patch method signatures
 | 
					
						
							|  |  |  |             const signature = signatures.get(clazz.name + '.' + member.name); | 
					
						
							|  |  |  |             memberNode.text = `${varName}.${member.name}(${signature})`; | 
					
						
							|  |  |  |             for (const arg of member.argsArray) { | 
					
						
							|  |  |  |               if (arg.type) | 
					
						
							|  |  |  |                memberNode.children.push(renderProperty(`\`${arg.name}\``, arg.type, arg.spec)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           // Append type
 | 
					
						
							|  |  |  |           if (member.type && member.type.name !== 'void') { | 
					
						
							|  |  |  |             let name; | 
					
						
							|  |  |  |             switch (member.kind) { | 
					
						
							|  |  |  |               case 'event': name = 'type:'; break; | 
					
						
							|  |  |  |               case 'property': name = 'type:'; break; | 
					
						
							|  |  |  |               case 'method': name = 'returns:'; break; | 
					
						
							| 
									
										
										
										
											2020-12-05 10:05:35 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |             memberNode.children.push(renderProperty(name, member.type)); | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |           // Append member doc
 | 
					
						
							|  |  |  |           memberNode.children.push(...(member.spec || []).map(c => clone(c))); | 
					
						
							|  |  |  |           classNode.children.push(memberNode); | 
					
						
							| 
									
										
										
										
											2020-12-05 01:03:33 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 14:28:11 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |       result.push({ | 
					
						
							|  |  |  |         type: 'text', | 
					
						
							|  |  |  |         text: links | 
					
						
							|  |  |  |       });     | 
					
						
							|  |  |  |       api.setText([comment, header, renderMd(result, 10000), footer].join('\n')); | 
					
						
							| 
									
										
										
										
											2020-12-04 14:28:11 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 05:50:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   // Documentation checks.
 | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  |     const browserVersions = await getBrowserVersions(); | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  |     messages.push(...(await preprocessor.runCommands(mdSources, { | 
					
						
							|  |  |  |       libversion: VERSION, | 
					
						
							|  |  |  |       chromiumVersion: browserVersions.chromium, | 
					
						
							|  |  |  |       firefoxVersion: browserVersions.firefox, | 
					
						
							| 
									
										
										
										
											2020-12-22 10:09:55 +08:00
										 |  |  |       webkitVersion: browserVersions.webkit, | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 04:19:48 +08:00
										 |  |  |     messages.push(...preprocessor.autocorrectInvalidLinks(PROJECT_DIR, mdSources, getRepositoryFiles())); | 
					
						
							|  |  |  |     for (const source of mdSources.filter(source => source.hasUpdatedText())) | 
					
						
							|  |  |  |       messages.push(Message.warning(`WARN: updated ${source.projectPath()}`)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const checkPublicAPI = require('./check_public_api'); | 
					
						
							| 
									
										
										
										
											2020-12-27 06:31:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 04:19:48 +08:00
										 |  |  |     const jsSources = await Source.readdir(path.join(PROJECT_DIR, 'src', 'client'), '', []); | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  |     messages.push(...checkPublicAPI(apiSpec, jsSources)); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const source of mdSources) { | 
					
						
							|  |  |  |       if (!source.hasUpdatedText()) | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       await source.save(); | 
					
						
							|  |  |  |       changedFiles = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Report results.
 | 
					
						
							|  |  |  |   const errors = messages.filter(message => message.type === 'error'); | 
					
						
							|  |  |  |   if (errors.length) { | 
					
						
							|  |  |  |     console.log('DocLint Failures:'); | 
					
						
							|  |  |  |     for (let i = 0; i < errors.length; ++i) { | 
					
						
							|  |  |  |       let error = errors[i].text; | 
					
						
							|  |  |  |       error = error.split('\n').join('\n      '); | 
					
						
							|  |  |  |       console.log(`  ${i + 1}) ${RED_COLOR}${error}${RESET_COLOR}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const warnings = messages.filter(message => message.type === 'warning'); | 
					
						
							|  |  |  |   if (warnings.length) { | 
					
						
							|  |  |  |     console.log('DocLint Warnings:'); | 
					
						
							|  |  |  |     for (let i = 0; i < warnings.length; ++i) { | 
					
						
							|  |  |  |       let warning = warnings[i].text; | 
					
						
							|  |  |  |       warning = warning.split('\n').join('\n      '); | 
					
						
							|  |  |  |       console.log(`  ${i + 1}) ${YELLOW_COLOR}${warning}${RESET_COLOR}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   let clearExit = messages.length === 0; | 
					
						
							|  |  |  |   if (changedFiles) { | 
					
						
							|  |  |  |     if (clearExit) | 
					
						
							|  |  |  |       console.log(`${YELLOW_COLOR}Some files were updated.${RESET_COLOR}`); | 
					
						
							|  |  |  |     clearExit = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   console.log(`${errors.length} failures, ${warnings.length} warnings.`); | 
					
						
							|  |  |  |   const runningTime = Date.now() - startTime; | 
					
						
							|  |  |  |   console.log(`DocLint Finished in ${runningTime / 1000} seconds`); | 
					
						
							| 
									
										
										
										
											2020-12-05 04:19:48 +08:00
										 |  |  |   process.exit(clearExit ? 0 : 1); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-14 10:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  | async function getBrowserVersions() { | 
					
						
							| 
									
										
										
										
											2020-12-22 10: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-13 05:03:01 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-22 10:09:55 +08:00
										 |  |  |   await Promise.all(browsers.map(browser => browser.close())); | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2020-03-13 05:03:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-24 10:52:06 +08:00
										 |  |  | function getRepositoryFiles() { | 
					
						
							|  |  |  |   const out = spawnSync('git', ['ls-files'], {cwd: PROJECT_DIR}); | 
					
						
							| 
									
										
										
										
											2020-12-05 03:09:20 +08:00
										 |  |  |   const files = out.stdout.toString().trim().split('\n').filter(f => !f.startsWith('docs-src')); | 
					
						
							|  |  |  |   return files.map(file => path.join(PROJECT_DIR, file)); | 
					
						
							| 
									
										
										
										
											2020-04-24 10:52:06 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-28 23:03:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string} name | 
					
						
							|  |  |  |  * @param {Type} type | 
					
						
							|  |  |  |  * @param {MarkdownNode[]} [spec] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function renderProperty(name, type, spec) { | 
					
						
							|  |  |  |   let comment = ''; | 
					
						
							|  |  |  |   if (spec && spec.length) | 
					
						
							|  |  |  |     comment = spec[0].text; | 
					
						
							|  |  |  |   let children; | 
					
						
							|  |  |  |   if (type.properties && type.properties.length) | 
					
						
							|  |  |  |     children = type.properties.map(p => renderProperty(`\`${p.name}\``, p.type, p.spec)) | 
					
						
							|  |  |  |   else if (spec && spec.length > 1) | 
					
						
							|  |  |  |     children = spec.slice(1).map(s => clone(s)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const result = { | 
					
						
							|  |  |  |     type: 'li', | 
					
						
							|  |  |  |     liType: 'default', | 
					
						
							|  |  |  |     text: `${name} <${renderType(type.name)}>${comment ? ' ' + comment : ''}`, | 
					
						
							|  |  |  |     children | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string} type | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function renderType(type) { | 
					
						
							|  |  |  |   if (type.includes('"')) | 
					
						
							|  |  |  |     return type.replace(/,/g, '|').replace(/Array/, "[Array]").replace(/null/, "[null]").replace(/number/, "[number]"); | 
					
						
							|  |  |  |   const result = type.replace(/([\w]+)/g, '[$1]'); | 
					
						
							|  |  |  |   if (result === '[Promise]<[void]>') | 
					
						
							|  |  |  |     return '[Promise]'; | 
					
						
							|  |  |  |   return result.replace(/[(]/g, '\\(').replace(/[)]/g, '\\)'); | 
					
						
							|  |  |  | } |