diff --git a/packages/create-astro/src/actions/verify.ts b/packages/create-astro/src/actions/verify.ts index 220c794d6..73359142b 100644 --- a/packages/create-astro/src/actions/verify.ts +++ b/packages/create-astro/src/actions/verify.ts @@ -1,56 +1,61 @@ import type { Context } from './context'; -import dns from 'node:dns/promises'; import { color } from '@astrojs/cli-kit'; -import { getTemplateTarget } from "./template.js"; -import { error, log, info, bannerAbort } from '../messages.js'; import fetch from 'node-fetch-native'; +import dns from 'node:dns/promises'; +import { bannerAbort, error, info, log } from '../messages.js'; +import { getTemplateTarget } from './template.js'; -export async function verify(ctx: Pick) { - if (!ctx.dryRun) { - const online = await isOnline(); - if (!online) { - bannerAbort(); - log(''); - error('error', `Unable to connect to the internet.`); - ctx.exit(1); - } - } +export async function verify( + ctx: Pick +) { + if (!ctx.dryRun) { + const online = await isOnline(); + if (!online) { + bannerAbort(); + log(''); + error('error', `Unable to connect to the internet.`); + ctx.exit(1); + } + } - if (ctx.template) { - const ok = await verifyTemplate(ctx.template, ctx.ref); - if (!ok) { - bannerAbort(); - log(''); - error('error', `Template ${color.reset(ctx.template)} ${color.dim('could not be found!')}`); - await info('check', 'https://astro.build/examples'); - ctx.exit(1); - } - } + if (ctx.template) { + const ok = await verifyTemplate(ctx.template, ctx.ref); + if (!ok) { + bannerAbort(); + log(''); + error('error', `Template ${color.reset(ctx.template)} ${color.dim('could not be found!')}`); + await info('check', 'https://astro.build/examples'); + ctx.exit(1); + } + } } function isOnline(): Promise { - return dns.lookup('github.com').then(() => true, () => false); + return dns.lookup('github.com').then( + () => true, + () => false + ); } async function verifyTemplate(tmpl: string, ref?: string) { - const target = getTemplateTarget(tmpl, ref); - const { repo, subdir, ref: branch } = parseGitURI(target.replace('github:', '')); - const url = new URL(`/repos/${repo}/contents${subdir}?ref=${branch}`, 'https://api.github.com/') + const target = getTemplateTarget(tmpl, ref); + const { repo, subdir, ref: branch } = parseGitURI(target.replace('github:', '')); + const url = new URL(`/repos/${repo}/contents${subdir}?ref=${branch}`, 'https://api.github.com/'); - let res = await fetch(url.toString(), { - headers: { - "Accept": "application/vnd.github+json", - "X-GitHub-Api-Version": "2022-11-28" - } - }) + let res = await fetch(url.toString(), { + headers: { + Accept: 'application/vnd.github+json', + 'X-GitHub-Api-Version': '2022-11-28', + }, + }); - // If users hit a ratelimit, fallback to the GitHub website - if (res.status === 403) { - res = await fetch(`https://github.com/${repo}/tree/${branch}${subdir}`) - } + // If users hit a ratelimit, fallback to the GitHub website + if (res.status === 403) { + res = await fetch(`https://github.com/${repo}/tree/${branch}${subdir}`); + } - return res.status === 200; + return res.status === 200; } // Adapted from https://github.com/unjs/giget/blob/main/src/_utils.ts @@ -75,15 +80,14 @@ async function verifyTemplate(tmpl: string, ref?: string) { // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -const GIT_RE = - /^(?[\w.-]+\/[\w.-]+)(?[^#]+)?(?#[\w.-]+)?/; +const GIT_RE = /^(?[\w.-]+\/[\w.-]+)(?[^#]+)?(?#[\w.-]+)?/; function parseGitURI(input: string) { - const m = input.match(GIT_RE)?.groups; - if (!m) throw new Error(`Unable to parse "${input}"`); - return { - repo: m.repo, - subdir: m.subdir || "/", - ref: m.ref ? m.ref.slice(1) : "main", - }; + const m = input.match(GIT_RE)?.groups; + if (!m) throw new Error(`Unable to parse "${input}"`); + return { + repo: m.repo, + subdir: m.subdir || '/', + ref: m.ref ? m.ref.slice(1) : 'main', + }; } diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index d23163c7e..a33e11bff 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -3,12 +3,12 @@ import { getContext } from './actions/context.js'; import { dependencies } from './actions/dependencies.js'; import { git } from './actions/git.js'; import { help } from './actions/help.js'; -import { verify } from './actions/verify.js'; import { intro } from './actions/intro.js'; import { next } from './actions/next-steps.js'; import { projectName } from './actions/project-name.js'; import { template } from './actions/template.js'; import { setupTypeScript, typescript } from './actions/typescript.js'; +import { verify } from './actions/verify.js'; import { setStdout } from './messages.js'; const exit = () => process.exit(0); @@ -53,7 +53,6 @@ export { dependencies, getContext, git, - verify, intro, next, projectName, @@ -61,4 +60,5 @@ export { setupTypeScript, template, typescript, + verify, }; diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index 7a4ec5885..fbf276794 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -99,9 +99,7 @@ export const banner = async (version: string) => ); export const bannerAbort = () => - log( - `\n${label('astro', color.bgRed)} ${color.bold('Launch sequence aborted.')}` - ); + log(`\n${label('astro', color.bgRed)} ${color.bold('Launch sequence aborted.')}`); export const info = async (prefix: string, text: string) => { await sleep(100); diff --git a/packages/create-astro/test/verify.test.js b/packages/create-astro/test/verify.test.js index 6b1ab1344..ecfaba727 100644 --- a/packages/create-astro/test/verify.test.js +++ b/packages/create-astro/test/verify.test.js @@ -7,12 +7,12 @@ describe('verify', () => { const fixture = setup(); const exit = (code) => { throw code; - } + }; it('basics', async () => { const context = { template: 'basics', exit }; await verify(context); - expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages') + expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages'); }); it('missing', async () => { @@ -30,12 +30,12 @@ describe('verify', () => { it('starlight', async () => { const context = { template: 'starlight', exit }; await verify(context); - expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages') + expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages'); }); it('starlight/tailwind', async () => { const context = { template: 'starlight/tailwind', exit }; await verify(context); - expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages') + expect(fixture.messages().length).to.equal(0, 'Did not expect `verify` to log any messages'); }); });