[ci] format

This commit is contained in:
natemoo-re 2023-08-16 19:39:52 +00:00 committed by astrobot-houston
parent e6e1de4f08
commit e0d1439f2a
4 changed files with 58 additions and 56 deletions

View file

@ -1,12 +1,14 @@
import type { Context } from './context'; import type { Context } from './context';
import dns from 'node:dns/promises';
import { color } from '@astrojs/cli-kit'; 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 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<Context, 'version' | 'dryRun' | 'template' | 'ref' | 'exit'>) { export async function verify(
ctx: Pick<Context, 'version' | 'dryRun' | 'template' | 'ref' | 'exit'>
) {
if (!ctx.dryRun) { if (!ctx.dryRun) {
const online = await isOnline(); const online = await isOnline();
if (!online) { if (!online) {
@ -30,24 +32,27 @@ export async function verify(ctx: Pick<Context, 'version' | 'dryRun' | 'template
} }
function isOnline(): Promise<boolean> { function isOnline(): Promise<boolean> {
return dns.lookup('github.com').then(() => true, () => false); return dns.lookup('github.com').then(
() => true,
() => false
);
} }
async function verifyTemplate(tmpl: string, ref?: string) { async function verifyTemplate(tmpl: string, ref?: string) {
const target = getTemplateTarget(tmpl, ref); const target = getTemplateTarget(tmpl, ref);
const { repo, subdir, ref: branch } = parseGitURI(target.replace('github:', '')); const { repo, subdir, ref: branch } = parseGitURI(target.replace('github:', ''));
const url = new URL(`/repos/${repo}/contents${subdir}?ref=${branch}`, 'https://api.github.com/') const url = new URL(`/repos/${repo}/contents${subdir}?ref=${branch}`, 'https://api.github.com/');
let res = await fetch(url.toString(), { let res = await fetch(url.toString(), {
headers: { headers: {
"Accept": "application/vnd.github+json", Accept: 'application/vnd.github+json',
"X-GitHub-Api-Version": "2022-11-28" 'X-GitHub-Api-Version': '2022-11-28',
} },
}) });
// If users hit a ratelimit, fallback to the GitHub website // If users hit a ratelimit, fallback to the GitHub website
if (res.status === 403) { if (res.status === 403) {
res = await fetch(`https://github.com/${repo}/tree/${branch}${subdir}`) res = await fetch(`https://github.com/${repo}/tree/${branch}${subdir}`);
} }
return res.status === 200; return res.status === 200;
@ -75,15 +80,14 @@ async function verifyTemplate(tmpl: string, ref?: string) {
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // 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 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
const GIT_RE = const GIT_RE = /^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.-]+)?/;
/^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.-]+)?/;
function parseGitURI(input: string) { function parseGitURI(input: string) {
const m = input.match(GIT_RE)?.groups; const m = input.match(GIT_RE)?.groups;
if (!m) throw new Error(`Unable to parse "${input}"`); if (!m) throw new Error(`Unable to parse "${input}"`);
return { return {
repo: m.repo, repo: m.repo,
subdir: m.subdir || "/", subdir: m.subdir || '/',
ref: m.ref ? m.ref.slice(1) : "main", ref: m.ref ? m.ref.slice(1) : 'main',
}; };
} }

View file

@ -3,12 +3,12 @@ import { getContext } from './actions/context.js';
import { dependencies } from './actions/dependencies.js'; import { dependencies } from './actions/dependencies.js';
import { git } from './actions/git.js'; import { git } from './actions/git.js';
import { help } from './actions/help.js'; import { help } from './actions/help.js';
import { verify } from './actions/verify.js';
import { intro } from './actions/intro.js'; import { intro } from './actions/intro.js';
import { next } from './actions/next-steps.js'; import { next } from './actions/next-steps.js';
import { projectName } from './actions/project-name.js'; import { projectName } from './actions/project-name.js';
import { template } from './actions/template.js'; import { template } from './actions/template.js';
import { setupTypeScript, typescript } from './actions/typescript.js'; import { setupTypeScript, typescript } from './actions/typescript.js';
import { verify } from './actions/verify.js';
import { setStdout } from './messages.js'; import { setStdout } from './messages.js';
const exit = () => process.exit(0); const exit = () => process.exit(0);
@ -53,7 +53,6 @@ export {
dependencies, dependencies,
getContext, getContext,
git, git,
verify,
intro, intro,
next, next,
projectName, projectName,
@ -61,4 +60,5 @@ export {
setupTypeScript, setupTypeScript,
template, template,
typescript, typescript,
verify,
}; };

View file

@ -99,9 +99,7 @@ export const banner = async (version: string) =>
); );
export const bannerAbort = () => export const bannerAbort = () =>
log( log(`\n${label('astro', color.bgRed)} ${color.bold('Launch sequence aborted.')}`);
`\n${label('astro', color.bgRed)} ${color.bold('Launch sequence aborted.')}`
);
export const info = async (prefix: string, text: string) => { export const info = async (prefix: string, text: string) => {
await sleep(100); await sleep(100);

View file

@ -7,12 +7,12 @@ describe('verify', () => {
const fixture = setup(); const fixture = setup();
const exit = (code) => { const exit = (code) => {
throw code; throw code;
} };
it('basics', async () => { it('basics', async () => {
const context = { template: 'basics', exit }; const context = { template: 'basics', exit };
await verify(context); 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 () => { it('missing', async () => {
@ -30,12 +30,12 @@ describe('verify', () => {
it('starlight', async () => { it('starlight', async () => {
const context = { template: 'starlight', exit }; const context = { template: 'starlight', exit };
await verify(context); 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 () => { it('starlight/tailwind', async () => {
const context = { template: 'starlight/tailwind', exit }; const context = { template: 'starlight/tailwind', exit };
await verify(context); 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');
}); });
}); });