Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(types): generate a d.ts file for protocol types (#2325)
This uses the `/json/protocol` endpoint to generate type definitions for the protocol. Currently it is lacking protocol events and commands, but I will add those later.
- Loading branch information
1 parent
35e34db
commit 8c54f41
Showing
10 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
package-lock.json | ||
yarn.lock | ||
/node6 | ||
/lib/protocol.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const puppeteer = require('../..'); | ||
puppeteer.launch({ | ||
pipe: false, | ||
executablePath: process.env.CHROME, | ||
args: ['--no-sandbox', '--disable-dev-shm-usage'] | ||
}).then(async browser => { | ||
const origin = browser.wsEndpoint().match(/ws:\/\/([0-9A-Za-z:\.]*)\//)[1]; | ||
const page = await browser.newPage(); | ||
await page.goto(`http://${origin}/json/protocol`); | ||
const json = JSON.parse(await page.evaluate(() => document.documentElement.innerText)); | ||
await browser.close(); | ||
const output = `// This is generated from /utils/protocol-types-generator/index.js | ||
declare global { | ||
module Protocol { | ||
${json.domains.map(domain => `${domain.description ? ` | ||
/** | ||
* ${domain.description} | ||
*/` : ''} | ||
export module ${domain.domain} { | ||
${(domain.types || []).map(type => `${type.description ? ` | ||
/** | ||
* ${type.description} | ||
*/` : ''}${type.properties ? ` | ||
export interface ${type.id} { | ||
${(type.properties || []).map(property => `${property.description ? ` | ||
/** | ||
* ${property.description} | ||
*/` : ''} | ||
${property.name}${property.optional ? '?' : ''}: ${typeOfProperty(property)}; | ||
`).join(``)} | ||
}` : ` | ||
export type ${type.id} = ${typeOfProperty(type)};`} | ||
`).join('')} | ||
} | ||
`).join('')} | ||
} | ||
} | ||
// empty export to keep file a module | ||
export {} | ||
`; | ||
require('fs').writeFileSync(require('path').join(__dirname, '..', '..', 'lib', 'protocol.d.ts'), output); | ||
}); | ||
|
||
function typeOfProperty(property) { | ||
if (property.$ref) return property.$ref; | ||
if (property.enum) return property.enum.map(value => JSON.stringify(value)).join('|'); | ||
switch (property.type) { | ||
case 'array': | ||
return typeOfProperty(property.items) + '[]'; | ||
case 'integer': | ||
return 'number'; | ||
} | ||
return property.type; | ||
} |