chore: move code to src/ and emit with TypeScript (#5568)

This updates our `tsconfig.json` so it emits our JavaScript files as
well as type checking them. We compile into `./lib` which we then ship
in our npm package. The source code has moved from `./lib` into `./src`.

Because the `src/` directory is exclusively JS files, this change is a
no-op in terms of code functionality but is the first step towards being
able to replace `src/X.js` with `src/X.ts` in a way that allows us to
migrate incrementally.

The `lib` directory is gitignored, and the `src` directory is
npmignored. On `npm publish` we will now run `npm run tsc` in order to
generate the outputted code.
This commit is contained in:
Jack Franklin 2020-04-02 15:25:19 +01:00 committed by GitHub
parent c82b5560f0
commit 7a2a41f208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 15214 additions and 23 deletions

View File

@ -7,3 +7,4 @@ node6/*
node6-test/*
node6-testrunner/*
experimental/
lib/

2
.gitignore vendored
View File

@ -12,6 +12,6 @@
package-lock.json
yarn.lock
/node6
/lib/protocol.d.ts
/utils/browser/puppeteer-web.js
/index.d.ts
/lib

View File

@ -43,3 +43,6 @@ experimental
# exclude types, see https://github.com/puppeteer/puppeteer/issues/3878
/index.d.ts
# don't expose src/ as we ship the generated code in lib/
/src

View File

@ -14,6 +14,45 @@
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const {promisify} = require('util');
const fsAccess = promisify(fs.access);
const exec = promisify(child_process.exec);
const fileExists = async filePath => fsAccess(filePath).then(() => true).catch(() => false);
/*
* Now Puppeteer is built with TypeScript, we need to ensure that
* locally we have the generated output before trying to install.
*
* For users installing puppeteer this is fine, they will have the
* generated lib/ directory as we ship it when we publish to npm.
*
* However, if you're cloning the repo to contribute, you won't have the
* generated lib/ directory so this script checks if we need to run
* TypeScript first to ensure the output exists and is in the right
* place.
*/
async function compileTypeScript() {
return exec('npm run tsc').catch(err => {
console.error('Error running TypeScript', err);
process.exit(1);
});
}
async function ensureLibDirectoryExists() {
const libPath = path.join(__dirname, 'lib');
const libExists = await fileExists(libPath);
if (libExists) return;
logPolitely('Compiling TypeScript before install...');
await compileTypeScript();
}
/**
* This file is part of public API.
*
@ -29,6 +68,8 @@ const supportedProducts = {
};
async function download() {
await ensureLibDirectoryExists();
const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host || process.env.npm_package_config_puppeteer_download_host;
const puppeteer = require('./index');
const product = process.env.PUPPETEER_PRODUCT || process.env.npm_config_puppeteer_product || process.env.npm_package_config_puppeteer_product || 'chrome';
@ -53,7 +94,6 @@ async function download() {
// Do nothing if the revision is already downloaded.
if (revisionInfo.local) {
generateProtocolTypesIfNecessary(false /* updated */, product);
logPolitely(`${supportedProducts[product]} is already in ${revisionInfo.folderPath}; skipping download.`);
return;
}
@ -78,7 +118,7 @@ async function download() {
logPolitely(`${supportedProducts[product]} (${revisionInfo.revision}) downloaded to ${revisionInfo.folderPath}`);
localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
Promise.all([...cleanupOldVersions, generateProtocolTypesIfNecessary(true /* updated */, product)]);
Promise.all([...cleanupOldVersions]);
}
/**
@ -118,18 +158,6 @@ async function download() {
return `${Math.round(mb * 10) / 10} Mb`;
}
function generateProtocolTypesIfNecessary(updated, product) {
if (product !== 'chrome')
return;
const fs = require('fs');
const path = require('path');
if (!fs.existsSync(path.join(__dirname, 'utils', 'protocol-types-generator')))
return;
if (!updated && fs.existsSync(path.join(__dirname, 'lib', 'protocol.d.ts')))
return;
return require('./utils/protocol-types-generator');
}
function getFirefoxNightlyVersion(host) {
const https = require('https');
const promise = new Promise((resolve, reject) => {

View File

@ -16,16 +16,19 @@
"funit": "PUPPETEER_PRODUCT=firefox node test/test.js",
"debug-unit": "node --inspect-brk test/test.js",
"test-doclint": "node utils/doclint/check_public_api/test/test.js && node utils/doclint/preprocessor/test.js",
"test": "npm run lint --silent && npm run coverage && npm run test-doclint && npm run test-types && node utils/testrunner/test/test.js",
"test": "npm run tsc && npm run lint --silent && npm run coverage && npm run test-doclint && npm run test-types && node utils/testrunner/test/test.js",
"prepublishOnly": "npm run tsc",
"dev-install": "npm run tsc && node install.js",
"install": "node install.js",
"lint": "([ \"$CI\" = true ] && eslint --quiet -f codeframe . || eslint .) && npm run tsc && npm run doc",
"doc": "node utils/doclint/cli.js",
"coverage": "cross-env COVERAGE=true npm run unit",
"tsc": "tsc --version && tsc -p .",
"tsc": "tsc --version && tsc -p . && cp src/protocol.d.ts lib/ && cp src/externs.d.ts lib/",
"apply-next-version": "node utils/apply_next_version.js",
"bundle": "npx browserify -r ./index.js:puppeteer -o utils/browser/puppeteer-web.js",
"test-types": "node utils/doclint/generate_types && tsc --version && tsc -p utils/doclint/generate_types/test/",
"unit-bundle": "node utils/browser/test.js"
"unit-bundle": "node utils/browser/test.js",
"update-protocol-d-ts": "node utils/protocol-types-generator"
},
"author": "The Chromium Authors",
"license": "Apache-2.0",

View File

@ -30,7 +30,6 @@ const {Worker: PuppeteerWorker} = require('./Worker');
const {createJSHandle} = require('./JSHandle');
const {Accessibility} = require('./Accessibility');
const {TimeoutSettings} = require('./TimeoutSettings');
const writeFileAsync = helper.promisify(fs.writeFile);
class Page extends EventEmitter {

15157
src/protocol.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,11 @@
{
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"checkJs": true,
"outDir": "./lib",
"target": "ESNext"
},
"include": [
"lib"
"src"
]
}

View File

@ -72,12 +72,12 @@ declare global {
export default Protocol;
`;
const outputPath = path.join(__dirname, '..', '..', 'lib', 'protocol.d.ts');
const outputPath = path.join(__dirname, '..', '..', 'src', 'protocol.d.ts');
require('fs').writeFileSync(outputPath, output);
console.log(`Wrote protocol.d.ts for ${version} to ${path.relative(process.cwd(), outputPath)}`);
console.log(`You should commit the changes.`);
});
/**
* @typedef {Object} Property
* @property {string=} $ref