Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
},
{
type: 'input',
name: 'projectName',
message: 'What would you like to name the project?',
validate: input => !!input.length,
},
{
type: 'input',
name: 'projectExternalUrl',
message: "Where is the project's code hosted?",
default: 'https://github.com//',
},
]);
const api = new ApiClient({rootURL: responses.serverBaseUrl || options.serverBaseUrl});
const project = await api.createProject({
name: responses.projectName,
externalUrl: responses.projectExternalUrl,
slug: '', // this property is dynamically generated server-side
});
const token = project.token;
process.stdout.write(`Created project ${project.name} (${project.id})!\n`);
process.stdout.write(`Use token ${log.bold}${token}${log.reset} to connect.\n`);
}
async function main() {
const rootURL = process.env.LHCI_ROOT_URL;
if (!rootURL) throw new Error(`Missing LHCI_ROOT_URL environment variable`);
const client = new ApiClient({rootURL});
const projects = await client.getProjects();
const project = projects.find(project => project.name.includes('Viewer')) || projects[0];
const builds = await client.getBuilds(project.id);
const build = builds.find(build => build.branch.includes('test_1')) || builds[0];
process.stdout.write(
[
new URL(`/app`, rootURL),
new URL(`/app/projects/${project.slug}`, rootURL),
new URL(`/app/projects/${project.slug}/compare/${_.shortId(build.id)}`, rootURL),
].join('\n')
);
process.exit(0);
}
async function runLHCITarget(options) {
if (!options.token) throw new Error('Must provide token for LHCI target');
const api = new ApiClient({rootURL: options.serverBaseUrl, extraHeaders: options.extraHeaders});
const project = await api.findProjectByToken(options.token);
if (!project) {
throw new Error('Could not find active project with provided token');
}
const hash = getCurrentHash();
const branch = getCurrentBranch();
const ancestorHash =
branch === 'master' ? getAncestorHashForMaster() : getAncestorHashForBranch();
const build = await api.createBuild({
projectId: project.id,
lifecycle: 'unsealed',
hash,
branch,
test: async ({serverBaseUrl = '', token = ''}) => {
const client = new ApiClient({rootURL: serverBaseUrl});
const project = await client.findProjectByToken(token);
if (!project) return true;
const builds = await client.getBuilds(project.id, {
branch: getCurrentBranch(),
hash: getCurrentHash(),
});
return builds.length === 0;
},
},
* `[loadingState, data]: [LoadingState, TData | undefined]`
*
* Conventions:
*
* 1. `loadingState === 'loaded'` if and only if `data !== undefined`.
* Hooks that violate this convention automatically redirect to the project selector via
* logic in `async-loader.jsx`.
* 2. A request is sent if and only if all of a hook's parameters are defined
* (i.e. `arguments.every(arg => arg !== undefined)`).
* 3. A request is reissued whenever the parameters have changed.
*/
import {useState, useEffect, useMemo} from 'preact/hooks';
import ApiClient from '@lhci/utils/src/api-client.js';
export const api = new ApiClient({
rootURL: window.location.origin,
URL: window.URL,
fetch: window.fetch.bind(window),
});
/** @typedef {'loading'|'error'|'loaded'} LoadingState */
/**
* @template {keyof StrictOmit} T
* @param {T} apiMethod
* @param {Parameters|undefined} apiParameters
* @return {[LoadingState, UnPromisify> | undefined]}
*/
function useApiData(apiMethod, apiParameters) {
const [loadingState, setLoadingState] = useState(/** @type {LoadingState} */ ('loading'));
const [apiData, setApiData] = useState(/** @type {any} */ (undefined));
test: async ({serverBaseUrl = ''}) =>
(await new ApiClient({rootURL: serverBaseUrl}).getVersion()).length > 0,
},
test: async ({serverBaseUrl = '', token = ''}) => {
const client = new ApiClient({rootURL: serverBaseUrl});
const project = await client.findProjectByToken(token);
return Boolean(project);
},
},