puppeteer/packages/browsers/src/launcher.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-02-13 15:10:54 +00:00
/**
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import os from 'os';
import path from 'path';
2023-02-13 15:10:54 +00:00
import {
Browser,
BrowserPlatform,
executablePathByBrowser,
} from './browsers/browsers.js';
import {CacheStructure} from './CacheStructure.js';
import {detectBrowserPlatform} from './detectPlatform.js';
2023-02-13 15:10:54 +00:00
/**
* @public
*/
export interface Options {
/**
* Root path to the storage directory.
*/
cacheDir: string;
2023-02-13 15:10:54 +00:00
/**
* Determines which platform the browser will be suited for.
*
* @defaultValue Auto-detected.
*/
platform?: BrowserPlatform;
/**
* Determines which browser to fetch.
*/
browser: Browser;
/**
* Determines which revision to dowloand. Revision should uniquely identify
* binaries and they are used for caching.
*/
revision: string;
}
export function computeExecutablePath(options: Options): string {
options.platform ??= detectBrowserPlatform();
2023-02-13 15:10:54 +00:00
if (!options.platform) {
throw new Error(
`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`
);
}
const installationDir = new CacheStructure(options.cacheDir).installationDir(
options.browser,
2023-02-13 15:10:54 +00:00
options.platform,
options.revision
);
return path.join(
installationDir,
executablePathByBrowser[options.browser](options.platform, options.revision)
2023-02-13 15:10:54 +00:00
);
}