[ci] format

This commit is contained in:
natemoo-re 2023-08-14 19:25:29 +00:00 committed by astrobot-houston
parent 44cf30a252
commit 1e9902177d
5 changed files with 41 additions and 36 deletions

View file

@ -1,8 +1,8 @@
import { color } from '@astrojs/cli-kit'; import { color } from '@astrojs/cli-kit';
import { shell } from '../shell.js';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { error, info, spinner, title } from '../messages.js'; import { error, info, spinner, title } from '../messages.js';
import { shell } from '../shell.js';
import type { Context } from './context'; import type { Context } from './context';
export async function dependencies( export async function dependencies(

View file

@ -3,8 +3,8 @@ import path from 'node:path';
import type { Context } from './context'; import type { Context } from './context';
import { color } from '@astrojs/cli-kit'; import { color } from '@astrojs/cli-kit';
import { shell } from '../shell.js';
import { error, info, spinner, title } from '../messages.js'; import { error, info, spinner, title } from '../messages.js';
import { shell } from '../shell.js';
export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun'>) { export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun'>) {
if (fs.existsSync(path.join(ctx.cwd, '.git'))) { if (fs.existsSync(path.join(ctx.cwd, '.git'))) {

View file

@ -1,11 +1,11 @@
/* eslint no-console: 'off' */ /* eslint no-console: 'off' */
import { color, say as houston, label, spinner as load } from '@astrojs/cli-kit'; import { color, say as houston, label, spinner as load } from '@astrojs/cli-kit';
import { align, sleep } from '@astrojs/cli-kit/utils'; import { align, sleep } from '@astrojs/cli-kit/utils';
import { shell } from './shell.js';
import fetch from 'node-fetch-native'; import fetch from 'node-fetch-native';
import { exec } from 'node:child_process'; import { exec } from 'node:child_process';
import stripAnsi from 'strip-ansi'; import stripAnsi from 'strip-ansi';
import detectPackageManager from 'which-pm-runs'; import detectPackageManager from 'which-pm-runs';
import { shell } from './shell.js';
// Users might lack access to the global npm registry, this function // Users might lack access to the global npm registry, this function
// checks the user's project type and will return the proper npm registry // checks the user's project type and will return the proper npm registry

View file

@ -3,42 +3,47 @@
import type { StdioOptions } from 'node:child_process'; import type { StdioOptions } from 'node:child_process';
import type { Readable } from 'node:stream'; import type { Readable } from 'node:stream';
import { text as textFromStream } from 'node:stream/consumers';
import { spawn } from 'node:child_process'; import { spawn } from 'node:child_process';
import { text as textFromStream } from 'node:stream/consumers';
import { setTimeout as sleep } from 'node:timers/promises'; import { setTimeout as sleep } from 'node:timers/promises';
export interface ExecaOptions { export interface ExecaOptions {
cwd?: string | URL; cwd?: string | URL;
stdio?: StdioOptions; stdio?: StdioOptions;
timeout?: number; timeout?: number;
} }
export interface Output { export interface Output {
stdout: string; stdout: string;
stderr: string; stderr: string;
exitCode: number; exitCode: number;
} }
const text = (stream: NodeJS.ReadableStream | Readable | null) => stream ? textFromStream(stream).then(t => t.trimEnd()) : ''; const text = (stream: NodeJS.ReadableStream | Readable | null) =>
stream ? textFromStream(stream).then((t) => t.trimEnd()) : '';
export async function shell(command: string, flags: string[], opts: ExecaOptions = {}): Promise<Output> { export async function shell(
const controller = opts.timeout ? new AbortController() : undefined; command: string,
const child = spawn(command, flags, { flags: string[],
cwd: opts.cwd, opts: ExecaOptions = {}
shell: true, ): Promise<Output> {
stdio: opts.stdio, const controller = opts.timeout ? new AbortController() : undefined;
signal: controller?.signal const child = spawn(command, flags, {
}) cwd: opts.cwd,
const stdout = await text(child.stdout); shell: true,
const stderr = await text(child.stderr); stdio: opts.stdio,
if (opts.timeout) { signal: controller?.signal,
sleep(opts.timeout).then(() => { });
controller!.abort(); const stdout = await text(child.stdout);
throw { stdout, stderr, exitCode: 1 } const stderr = await text(child.stderr);
}) if (opts.timeout) {
} sleep(opts.timeout).then(() => {
await new Promise((resolve) => child.on('exit', resolve)) controller!.abort();
const { exitCode } = child; throw { stdout, stderr, exitCode: 1 };
if (exitCode !== 0) { });
throw { stdout, stderr, exitCode }; }
} await new Promise((resolve) => child.on('exit', resolve));
return { stdout, stderr, exitCode } const { exitCode } = child;
if (exitCode !== 0) {
throw { stdout, stderr, exitCode };
}
return { stdout, stderr, exitCode };
} }

View file

@ -37,7 +37,7 @@ describe('git initialized', () => {
before(async () => { before(async () => {
await mkdir(dir, { recursive: true }); await mkdir(dir, { recursive: true });
await writeFile(new URL('./git.json', dir), '{}', { encoding: 'utf8' }); await writeFile(new URL('./git.json', dir), '{}', { encoding: 'utf8' });
}) });
it('already initialized', async () => { it('already initialized', async () => {
const context = { const context = {
@ -53,5 +53,5 @@ describe('git initialized', () => {
after(() => { after(() => {
rmSync(dir, { recursive: true, force: true }); rmSync(dir, { recursive: true, force: true });
}) });
}) });