UNPKG

2.05 kBJavaScriptView Raw
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
17const child_process = require('child_process');
18const path = require('path');
19const fs = require('fs');
20const { promisify } = require('util');
21
22const exec = promisify(child_process.exec);
23const fsAccess = promisify(fs.access);
24
25const 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 */
42async function compileTypeScript() {
43 return exec('npm run tsc').catch((error) => {
44 console.error('Error running TypeScript', error);
45 process.exit(1);
46 });
47}
48
49async 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('..')
59if (require.main === module) compileTypeScriptIfRequired();
60
61module.exports = compileTypeScriptIfRequired;