[ci] format
This commit is contained in:
parent
e6e1de4f08
commit
e0d1439f2a
4 changed files with 58 additions and 56 deletions
|
@ -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<Context, 'version' | 'dryRun' | 'template' | 'ref' | 'exit'>) {
|
||||
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<Context, 'version' | 'dryRun' | 'template' | 'ref' | 'exit'>
|
||||
) {
|
||||
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<boolean> {
|
||||
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 =
|
||||
/^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.-]+)?/;
|
||||
const GIT_RE = /^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\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',
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue