Skip to content

Commit

Permalink
Improved interactive update of emulator website
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 19, 2024
1 parent 21c9726 commit 29a0ccf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/tooling/piral-cli/src/apps/upgrade-pilet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'path';
import { LogLevels, NpmClientType } from '../types';
import { isInteractive } from '../external';
import {
installNpmPackage,
checkExistingDirectory,
Expand Down Expand Up @@ -99,7 +100,10 @@ export async function upgradePilet(baseDir = process.cwd(), options: UpgradePile
}

const npmClient = await determineNpmClient(root, defaultNpmClient);
const { apps, piletPackage } = await retrievePiletData(root);

// in case we run from a user's CLI we want to allow updating
const interactive = isInteractive();
const { apps, piletPackage } = await retrievePiletData(root, undefined, interactive);
const { devDependencies = {}, dependencies = {}, source } = piletPackage;

if (apps.length === 0) {
Expand Down
16 changes: 11 additions & 5 deletions src/tooling/piral-cli/src/common/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ async function loadPiralInstance(root: string, details?: PiralInstanceDetails):
return appPackage;
}

export async function findPiralInstance(proposedApp: string, rootDir: string, details?: PiralInstanceDetails) {
export async function findPiralInstance(
proposedApp: string,
rootDir: string,
details?: PiralInstanceDetails,
interactive = false,
) {
const path = findPackageRoot(proposedApp, rootDir);
const url = details?.url;

Expand All @@ -167,7 +172,7 @@ export async function findPiralInstance(proposedApp: string, rootDir: string, de

if (url) {
log('generalDebug_0003', `Updating the emulator from remote "${url}" ...`);
await updateFromEmulatorWebsite(root, url);
await updateFromEmulatorWebsite(root, url, interactive);
}

return await loadPiralInstance(root, details);
Expand All @@ -185,6 +190,7 @@ export async function findPiralInstances(
piletPackage: PiletPackageData,
piletDefinition: undefined | PiletDefinition,
rootDir: string,
interactive?: boolean,
) {
if (proposedApps) {
// do nothing
Expand All @@ -202,7 +208,7 @@ export async function findPiralInstances(
if (proposedApps.length > 0) {
return Promise.all(
proposedApps.map((proposedApp) =>
findPiralInstance(proposedApp, rootDir, piletDefinition?.piralInstances?.[proposedApp]),
findPiralInstance(proposedApp, rootDir, piletDefinition?.piralInstances?.[proposedApp], interactive),
),
);
}
Expand Down Expand Up @@ -776,13 +782,13 @@ export async function findPiletRoot(proposedRoot: string) {
return dirname(packageJsonPath);
}

export async function retrievePiletData(target: string, app?: string) {
export async function retrievePiletData(target: string, app?: string, interactive?: boolean) {
const piletJsonPath = await findFile(target, piletJson);
const proposedRoot = piletJsonPath ? dirname(piletJsonPath) : target;
const root = await findPiletRoot(proposedRoot);
const piletPackage = await readJson(root, packageJson);
const piletDefinition: PiletDefinition = piletJsonPath && (await readJson(proposedRoot, piletJson));
const appPackages = await findPiralInstances(app && [app], piletPackage, piletDefinition, root);
const appPackages = await findPiralInstances(app && [app], piletPackage, piletDefinition, root, interactive);
const apps: Array<AppDefinition> = [];

for (const appPackage of appPackages) {
Expand Down
33 changes: 15 additions & 18 deletions src/tooling/piral-cli/src/common/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import { ForceOverwrite } from './enums';
import { createDirectory, readJson, writeBinary } from './io';
import { writeJson } from './io';
import { progress, log } from './log';
import { axios } from '../external';
import { axios, isInteractive } from '../external';
import { EmulatorWebsiteManifestFiles, EmulatorWebsiteManifest } from '../types';

async function requestManifest(url: string, httpsAgent?: Agent) {
async function requestManifest(url: string, interactive: boolean, httpsAgent?: Agent) {
const opts = getAxiosOptions(url);

try {
return await axios.default.get(url, { ...opts, httpsAgent });
} catch (error) {
return await handleAxiosError(error, true, httpsAgent, async (mode, key) => {
return await handleAxiosError(error, interactive, httpsAgent, async (mode, key) => {
const headers = getAuthorizationHeaders(mode, key);

if (headers.authorization) {
await updateConfig('auth', {
[url]: {
mode: 'header',
key: 'authorization',
value: headers.authorization,
},
});
}

return await axios.default.get(url, { headers, httpsAgent });
await updateConfig('auth', {
[url]: {
mode: 'header',
key: 'authorization',
value: headers.authorization,
},
});
return await requestManifest(url, false, httpsAgent);
});
}
}
Expand Down Expand Up @@ -99,13 +95,13 @@ async function createEmulatorFiles(
await downloadEmulatorFiles(manifestUrl, appDir, emulatorJson.files, httpsAgent);
}

export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl: string) {
export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl: string, interactive: boolean) {
progress(`Updating emulator from %s ...`, manifestUrl);
const ca = await getCertificate();
const httpsAgent = ca ? new Agent({ ca }) : undefined;

try {
const response = await requestManifest(manifestUrl, httpsAgent);
const response = await requestManifest(manifestUrl, interactive, httpsAgent);
const nextEmulator: EmulatorWebsiteManifest = response.data;
const currentEmulator = await readJson(targetDir, packageJson);

Expand All @@ -128,7 +124,8 @@ export async function scaffoldFromEmulatorWebsite(rootDir: string, manifestUrl:
progress(`Downloading emulator from %s ...`, manifestUrl);
const ca = await getCertificate();
const httpsAgent = ca ? new Agent({ ca }) : undefined;
const response = await requestManifest(manifestUrl, httpsAgent);
const interactive = isInteractive();
const response = await requestManifest(manifestUrl, interactive, httpsAgent);
const emulatorJson: EmulatorWebsiteManifest = response.data;
const targetDir = resolve(rootDir, 'node_modules', emulatorJson.name);
const appDir = resolve(targetDir, 'app');
Expand Down

0 comments on commit 29a0ccf

Please sign in to comment.