Skip to content

Commit

Permalink
Merge branch 'feature/reg-api' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayevans committed Jul 11, 2024
2 parents a0f48ad + 11367ce commit ca1a6e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pnpm start https://raw.githubusercontent.com/scffld-dev/cli/develop/examples/sim
pnpm start github:scffld-dev/cli/examples/simple --name="My Github Component"
pnpm start github:scffld-dev/cli/examples/simple@main --name="My Github Component"
pnpm start reg:parcel-web-app --overwrite --outputDirectory=./demo-src/reg-app/
pnpm start reg:react-hook --name=foo --overwrite --outputDirectory=./demo-src/reg-app/
pnpm start show github:scffld-dev/cli/examples/simple
```

Expand Down
28 changes: 28 additions & 0 deletions src/cli/loadTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'node:fs';
import path from 'node:path';
import ora, { Ora } from 'ora';

const REG_PROXY_TIMEOUT = 600;

export const loadTemplate = async (
templateName: string,
options: { quiet?: boolean } = {}
Expand Down Expand Up @@ -48,18 +50,44 @@ export const loadTemplate = async (

const parts = templateName.replace('reg:', '').split('/');

// Try via TemplateProxy API with very short timeout
try {
const proxyUrl = `https://scffld-api.azurewebsites.net/TemplateProxy/${parts.join(
'/'
)}${revision !== 'HEAD' ? `/${revision}` : ''}`;
// console.info(`\nFetching from proxy: ${proxyUrl}`);
const response = await fetch(proxyUrl, {
signal: AbortSignal.timeout(REG_PROXY_TIMEOUT),
});
if (response.ok && response.body) {
templateContent = await response.text();
if (templateContent && templateContent.indexOf('---') !== -1) {
if (fetchSpinner !== undefined && !quiet) {
fetchSpinner.succeed();
}
// console.info(`\nGot template content from '${proxyUrl}'`);
return templateContent;
}
}
} catch (e) {
// Catch errors & fallback to direct GitHub access
// console.warn('\nError from proxy:', e);
}
// console.warn('\nSomething broke in the proxy, fallback to GH');
url = `https://raw.githubusercontent.com/scffld-dev/website/${revision}/templates/${parts.join(
'/'
)}${templateName.endsWith('.md') ? '' : '.md'}`;
}

// TODO: Handle errors
const response = await fetch(url);
templateContent = await response.text();
if (fetchSpinner !== undefined && !quiet) {
fetchSpinner.succeed();
}
} else {
// Local template
// TODO: Handle errors
templateContent = fs
.readFileSync(
path.resolve(templateName + (templateName.endsWith('.md') ? '' : '.md'))
Expand Down

0 comments on commit ca1a6e3

Please sign in to comment.