1/**
2 * Copyright 2020 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * This script ensures that the pinned version of devtools-protocol in
19 * package.json is the right version for the current revision of Chromium that
20 * Puppeteer ships with.
21 *
22 * The devtools-protocol package publisher runs every hour and checks if there
23 * are protocol changes. If there are, it will be versioned with the revision
24 * number of the commit that last changed the .pdl files.
25 *
26 * Chromium branches/releases are figured out at a later point in time, so it's
27 * not true that each Chromium revision will have an exact matching revision
28 * version of devtools-protocol. To ensure we're using a devtools-protocol that
29 * is aligned with our revision, we want to find the largest package number
30 * that's <= the revision that Puppeteer is using.
31 *
32 * This script uses npm's `view` function to list all versions in a range and
33 * find the one closest to our Chromium revision.
34 */
35
36// eslint-disable-next-line import/extensions
37import { PUPPETEER_REVISIONS } from '../src/revisions';
38import { execSync } from 'child_process';
39
40import packageJson from '../package.json';
41
42const currentProtocolPackageInstalledVersion =
43  packageJson.dependencies['devtools-protocol'];
44
45/**
46 * Ensure that the devtools-protocol version is pinned.
47 */
48if (/^[^0-9]/.test(currentProtocolPackageInstalledVersion)) {
49  console.log(
50    `ERROR: devtools-protocol package is not pinned to a specific version.\n`
51  );
52  process.exit(1);
53}
54
55// find the right revision for our Chromium revision
56
57const command = `npm view "devtools-protocol@<=0.0.${PUPPETEER_REVISIONS.chromium}" version | tail -1`;
58
59console.log(
60  'Checking npm for devtools-protocol revisions:\n',
61  `'${command}'`,
62  '\n'
63);
64
65const output = execSync(command, {
66  encoding: 'utf8',
67});
68
69const bestRevisionFromNpm = output.split(' ')[1].replace(/'|\n/g, '');
70
71if (currentProtocolPackageInstalledVersion !== bestRevisionFromNpm) {
72  console.log(`ERROR: bad devtools-protocol revision detected:
73
74    Current Puppeteer Chromium revision: ${PUPPETEER_REVISIONS.chromium}
75    Current devtools-protocol version in package.json: ${currentProtocolPackageInstalledVersion}
76    Expected devtools-protocol version:                ${bestRevisionFromNpm}`);
77
78  process.exit(1);
79}
80
81console.log(
82  `Correct devtools-protocol version found (${bestRevisionFromNpm}).`
83);
84process.exit(0);
85