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.
|
|
|
|
*/
|
|
|
|
|
2023-02-15 23:09:31 +00:00
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
|
|
|
|
2023-02-13 15:10:54 +00:00
|
|
|
import {
|
|
|
|
Browser,
|
|
|
|
BrowserPlatform,
|
|
|
|
executablePathByBrowser,
|
|
|
|
} from './browsers/browsers.js';
|
2023-02-15 19:41:22 +00:00
|
|
|
import {CacheStructure} from './CacheStructure.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {detectBrowserPlatform} from './detectPlatform.js';
|
2023-02-13 15:10:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface Options {
|
|
|
|
/**
|
|
|
|
* Root path to the storage directory.
|
|
|
|
*/
|
2023-02-15 19:41:22 +00:00
|
|
|
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 {
|
2023-02-15 19:41:22 +00:00
|
|
|
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()})`
|
|
|
|
);
|
|
|
|
}
|
2023-02-15 19:41:22 +00:00
|
|
|
const installationDir = new CacheStructure(options.cacheDir).installationDir(
|
|
|
|
options.browser,
|
2023-02-13 15:10:54 +00:00
|
|
|
options.platform,
|
2023-02-15 19:41:22 +00:00
|
|
|
options.revision
|
|
|
|
);
|
|
|
|
return path.join(
|
|
|
|
installationDir,
|
|
|
|
executablePathByBrowser[options.browser](options.platform, options.revision)
|
2023-02-13 15:10:54 +00:00
|
|
|
);
|
|
|
|
}
|