Skip to content

Commit

Permalink
chore: move code to src/ and emit with TypeScript (#5568)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jackfranklin committed Apr 2, 2020
1 parent c82b556 commit 7a2a41f
Show file tree
Hide file tree
Showing 41 changed files with 15,214 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -7,3 +7,4 @@ node6/*
node6-test/*
node6-testrunner/*
experimental/
lib/
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -12,6 +12,6 @@
package-lock.json
yarn.lock
/node6
/lib/protocol.d.ts
/utils/browser/puppeteer-web.js
/index.d.ts
/lib
3 changes: 3 additions & 0 deletions .npmignore
Expand Up @@ -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
56 changes: 42 additions & 14 deletions install.js
Expand Up @@ -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.
*
Expand All @@ -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';
Expand All @@ -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;
}
Expand All @@ -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]);
}

/**
Expand Down Expand Up @@ -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) => {
Expand Down
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -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",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion lib/Page.js → src/Page.js
Expand Up @@ -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 {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7a2a41f

Please sign in to comment.