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 | const child_process = require('child_process');
|
18 | const path = require('path');
|
19 | const fs = require('fs');
|
20 | const { promisify } = require('util');
|
21 |
|
22 | const exec = promisify(child_process.exec);
|
23 | const fsAccess = promisify(fs.access);
|
24 |
|
25 | const fileExists = async (filePath) =>
|
26 | fsAccess(filePath)
|
27 | .then(() => true)
|
28 | .catch(() => false);
|
29 | /*
|
30 |
|
31 | * Now Puppeteer is built with TypeScript, we need to ensure that
|
32 | * locally we have the generated output before trying to install.
|
33 | *
|
34 | * For users installing puppeteer this is fine, they will have the
|
35 | * generated lib/ directory as we ship it when we publish to npm.
|
36 | *
|
37 | * However, if you're cloning the repo to contribute, you won't have the
|
38 | * generated lib/ directory so this script checks if we need to run
|
39 | * TypeScript first to ensure the output exists and is in the right
|
40 | * place.
|
41 | */
|
42 | async function compileTypeScript() {
|
43 | return exec('npm run tsc').catch((error) => {
|
44 | console.error('Error running TypeScript', error);
|
45 | process.exit(1);
|
46 | });
|
47 | }
|
48 |
|
49 | async function compileTypeScriptIfRequired() {
|
50 | const libPath = path.join(__dirname, 'lib');
|
51 | const libExists = await fileExists(libPath);
|
52 | if (libExists) return;
|
53 |
|
54 | console.log('Puppeteer:', 'Compiling TypeScript...');
|
55 | await compileTypeScript();
|
56 | }
|
57 |
|
58 | // It's being run as node typescript-if-required.js, not require('..')
|
59 | if (require.main === module) compileTypeScriptIfRequired();
|
60 |
|
61 | module.exports = compileTypeScriptIfRequired;
|