aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/famous-dolls-grin.md6
-rw-r--r--.changeset/wild-trainers-matter.md6
-rw-r--r--packages/astro/package.json4
-rw-r--r--packages/astro/src/cli/check/index.ts2
-rw-r--r--packages/astro/src/cli/db/index.ts22
-rw-r--r--packages/astro/src/cli/flags.ts8
-rw-r--r--packages/astro/src/cli/index.ts37
-rw-r--r--packages/create-astro/src/actions/context.ts91
-rw-r--r--packages/db/package.json2
-rw-r--r--packages/db/src/core/cli/commands/execute/index.ts4
-rw-r--r--packages/db/src/core/cli/commands/login/index.ts4
-rw-r--r--packages/db/src/core/cli/commands/push/index.ts4
-rw-r--r--packages/db/src/core/cli/commands/shell/index.ts4
-rw-r--r--packages/db/src/core/cli/commands/verify/index.ts4
-rw-r--r--packages/db/src/core/cli/index.ts4
-rw-r--r--packages/db/src/core/cli/types.ts4
-rw-r--r--packages/db/src/core/integration/index.ts6
-rw-r--r--packages/upgrade/src/actions/context.ts28
-rw-r--r--packages/upgrade/test/context.test.js4
-rw-r--r--pnpm-lock.yaml19
20 files changed, 131 insertions, 132 deletions
diff --git a/.changeset/famous-dolls-grin.md b/.changeset/famous-dolls-grin.md
new file mode 100644
index 000000000..0070ee43b
--- /dev/null
+++ b/.changeset/famous-dolls-grin.md
@@ -0,0 +1,6 @@
+---
+'create-astro': patch
+'@astrojs/db': patch
+---
+
+Reverts back to `arg` package for CLI argument parsing
diff --git a/.changeset/wild-trainers-matter.md b/.changeset/wild-trainers-matter.md
new file mode 100644
index 000000000..daf3cd9e5
--- /dev/null
+++ b/.changeset/wild-trainers-matter.md
@@ -0,0 +1,6 @@
+---
+'astro': patch
+'@astrojs/db': patch
+---
+
+Reverts back to `yargs-parser` package for CLI argument parsing
diff --git a/packages/astro/package.json b/packages/astro/package.json
index 2e7657517..6da1e12e7 100644
--- a/packages/astro/package.json
+++ b/packages/astro/package.json
@@ -133,8 +133,8 @@
"@babel/plugin-transform-react-jsx": "^7.25.2",
"@babel/traverse": "^7.25.3",
"@babel/types": "^7.25.2",
- "@rollup/pluginutils": "^5.1.0",
"@oslojs/encoding": "^0.4.1",
+ "@rollup/pluginutils": "^5.1.0",
"@types/babel__core": "^7.20.5",
"@types/cookie": "^0.6.0",
"acorn": "^8.12.1",
@@ -186,6 +186,7 @@
"vitefu": "^0.2.5",
"which-pm": "^3.0.0",
"xxhash-wasm": "^1.0.2",
+ "yargs-parser": "^21.1.1",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.2",
"zod-to-ts": "^1.2.0"
@@ -212,6 +213,7 @@
"@types/micromatch": "^4.0.9",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
+ "@types/yargs-parser": "^21.0.3",
"astro-scripts": "workspace:*",
"cheerio": "1.0.0",
"eol": "^0.9.1",
diff --git a/packages/astro/src/cli/check/index.ts b/packages/astro/src/cli/check/index.ts
index 9a354c8e0..c93e3b2f4 100644
--- a/packages/astro/src/cli/check/index.ts
+++ b/packages/astro/src/cli/check/index.ts
@@ -8,7 +8,7 @@ export async function check(flags: Flags) {
const logger = createLoggerFromFlags(flags);
const getPackageOpts = {
skipAsk: !!flags.yes || !!flags.y,
- cwd: typeof flags.root == 'string' ? flags.root : undefined,
+ cwd: flags.root,
};
const checkPackage = await getPackage<typeof import('@astrojs/check')>(
'@astrojs/check',
diff --git a/packages/astro/src/cli/db/index.ts b/packages/astro/src/cli/db/index.ts
index c6be7411b..ae97e498f 100644
--- a/packages/astro/src/cli/db/index.ts
+++ b/packages/astro/src/cli/db/index.ts
@@ -1,25 +1,20 @@
+import type { Arguments } from 'yargs-parser';
import type { AstroConfig } from '../../@types/astro.js';
import { resolveConfig } from '../../core/config/config.js';
import { apply as applyPolyfill } from '../../core/polyfill.js';
-import { type Flags, createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js';
+import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js';
import { getPackage } from '../install-package.js';
-interface YargsArguments {
- _: Array<string | number>;
- '--'?: Array<string | number>;
- [argName: string]: any;
-}
-
type DBPackage = {
- cli: (args: { flags: YargsArguments; config: AstroConfig }) => unknown;
+ cli: (args: { flags: Arguments; config: AstroConfig }) => unknown;
};
-export async function db({ positionals, flags }: { positionals: string[]; flags: Flags }) {
+export async function db({ flags }: { flags: Arguments }) {
applyPolyfill();
const logger = createLoggerFromFlags(flags);
const getPackageOpts = {
skipAsk: !!flags.yes || !!flags.y,
- cwd: typeof flags.root == 'string' ? flags.root : undefined,
+ cwd: flags.root,
};
const dbPackage = await getPackage<DBPackage>('@astrojs/db', logger, getPackageOpts, []);
@@ -35,10 +30,5 @@ export async function db({ positionals, flags }: { positionals: string[]; flags:
const inlineConfig = flagsToAstroInlineConfig(flags);
const { astroConfig } = await resolveConfig(inlineConfig, 'build');
- const yargsArgs: YargsArguments = {
- _: positionals,
- ...flags,
- };
-
- await cli({ flags: yargsArgs, config: astroConfig });
+ await cli({ flags, config: astroConfig });
}
diff --git a/packages/astro/src/cli/flags.ts b/packages/astro/src/cli/flags.ts
index 59dfbf00a..9d57c0316 100644
--- a/packages/astro/src/cli/flags.ts
+++ b/packages/astro/src/cli/flags.ts
@@ -1,10 +1,10 @@
-import type { parseArgs } from 'node:util';
+import type { Arguments } from 'yargs-parser';
import type { AstroInlineConfig } from '../@types/astro.js';
import { type LogOptions, Logger } from '../core/logger/core.js';
import { nodeLogDestination } from '../core/logger/node.js';
-export type ParsedArgsResult = ReturnType<typeof parseArgs>;
-export type Flags = ParsedArgsResult['values'];
+// Alias for now, but allows easier migration to node's `parseArgs` in the future.
+export type Flags = Arguments;
export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
return {
@@ -20,7 +20,7 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
base: typeof flags.base === 'string' ? flags.base : undefined,
outDir: typeof flags.outDir === 'string' ? flags.outDir : undefined,
server: {
- port: typeof flags.port === 'string' ? Number(flags.port) : undefined,
+ port: typeof flags.port === 'number' ? flags.port : undefined,
host:
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
open:
diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts
index b3a819e58..c767569fd 100644
--- a/packages/astro/src/cli/index.ts
+++ b/packages/astro/src/cli/index.ts
@@ -1,8 +1,7 @@
-import { parseArgs } from 'node:util';
/* eslint-disable no-console */
import * as colors from 'kleur/colors';
+import yargs from 'yargs-parser';
import { ASTRO_VERSION } from '../core/constants.js';
-import type { ParsedArgsResult } from './flags.js';
type CLICommand =
| 'help'
@@ -66,9 +65,9 @@ function printVersion() {
}
/** Determine which command the user requested */
-function resolveCommand(args: ParsedArgsResult): CLICommand {
- const cmd = args.positionals[2] as string;
- if (args.values.version) return 'version';
+function resolveCommand(flags: yargs.Arguments): CLICommand {
+ const cmd = flags._[2] as string;
+ if (flags.version) return 'version';
const supportedCommands = new Set([
'add',
@@ -98,9 +97,7 @@ function resolveCommand(args: ParsedArgsResult): CLICommand {
* NOTE: This function provides no error handling, so be sure
* to present user-friendly error output where the fn is called.
**/
-async function runCommand(cmd: string, args: ParsedArgsResult) {
- const flags = args.values;
-
+async function runCommand(cmd: string, flags: yargs.Arguments) {
// These commands can run directly without parsing the user config.
switch (cmd) {
case 'help':
@@ -123,7 +120,7 @@ async function runCommand(cmd: string, args: ParsedArgsResult) {
// Do not track session start, since the user may be trying to enable,
// disable, or modify telemetry settings.
const { update } = await import('./telemetry/index.js');
- const subcommand = args.positionals[3];
+ const subcommand = flags._[3]?.toString();
await update(subcommand, { flags });
return;
}
@@ -134,7 +131,7 @@ async function runCommand(cmd: string, args: ParsedArgsResult) {
}
case 'preferences': {
const { preferences } = await import('./preferences/index.js');
- const [subcommand, key, value] = args.positionals.slice(3);
+ const [subcommand, key, value] = flags._.slice(3).map((v) => v.toString());
const exitCode = await preferences(subcommand, key, value, { flags });
return process.exit(exitCode);
}
@@ -154,7 +151,7 @@ async function runCommand(cmd: string, args: ParsedArgsResult) {
switch (cmd) {
case 'add': {
const { add } = await import('./add/index.js');
- const packages = args.positionals.slice(3);
+ const packages = flags._.slice(3) as string[];
await add(packages, { flags });
return;
}
@@ -164,7 +161,7 @@ async function runCommand(cmd: string, args: ParsedArgsResult) {
case 'link':
case 'init': {
const { db } = await import('./db/index.js');
- await db({ positionals: args.positionals, flags });
+ await db({ flags });
return;
}
case 'dev': {
@@ -205,20 +202,10 @@ async function runCommand(cmd: string, args: ParsedArgsResult) {
/** The primary CLI action */
export async function cli(argv: string[]) {
- const args = parseArgs({
- args: argv,
- allowPositionals: true,
- strict: false,
- options: {
- global: { type: 'boolean', short: 'g' },
- host: { type: 'string' }, // Can be boolean too, which is covered by `strict: false`
- open: { type: 'string' }, // Can be boolean too, which is covered by `strict: false`
- // TODO: Add more flags here
- },
- });
- const cmd = resolveCommand(args);
+ const flags = yargs(argv, { boolean: ['global'], alias: { g: 'global' } });
+ const cmd = resolveCommand(flags);
try {
- await runCommand(cmd, args);
+ await runCommand(cmd, flags);
} catch (err) {
const { throwAndExit } = await import('./throw-and-exit.js');
await throwAndExit(cmd, err);
diff --git a/packages/create-astro/src/actions/context.ts b/packages/create-astro/src/actions/context.ts
index bf1b522bd..83a13eda7 100644
--- a/packages/create-astro/src/actions/context.ts
+++ b/packages/create-astro/src/actions/context.ts
@@ -1,7 +1,7 @@
import os from 'node:os';
-import { parseArgs } from 'node:util';
import { type Task, prompt } from '@astrojs/cli-kit';
import { random } from '@astrojs/cli-kit/utils';
+import arg from 'arg';
import getSeasonalData from '../data/seasonal.js';
import { getName, getVersion } from '../messages.js';
@@ -33,44 +33,47 @@ export interface Context {
}
export async function getContext(argv: string[]): Promise<Context> {
- const args = parseArgs({
- args: argv,
- allowPositionals: true,
- strict: false,
- options: {
- template: { type: 'string' },
- ref: { type: 'string' },
- yes: { type: 'boolean', short: 'y' },
- no: { type: 'boolean', short: 'n' },
- install: { type: 'boolean' },
- 'no-install': { type: 'boolean' },
- git: { type: 'boolean' },
- 'no-git': { type: 'boolean' },
- typescript: { type: 'string' },
- 'skip-houston': { type: 'boolean' },
- 'dry-run': { type: 'boolean' },
- help: { type: 'boolean', short: 'h' },
- fancy: { type: 'boolean' },
+ const flags = arg(
+ {
+ '--template': String,
+ '--ref': String,
+ '--yes': Boolean,
+ '--no': Boolean,
+ '--install': Boolean,
+ '--no-install': Boolean,
+ '--git': Boolean,
+ '--no-git': Boolean,
+ '--typescript': String,
+ '--skip-houston': Boolean,
+ '--dry-run': Boolean,
+ '--help': Boolean,
+ '--fancy': Boolean,
+
+ '-y': '--yes',
+ '-n': '--no',
+ '-h': '--help',
},
- });
+ { argv, permissive: true },
+ );
const packageManager = detectPackageManager() ?? 'npm';
- const projectName = args.positionals[0];
+ let cwd = flags['_'][0];
let {
- help,
- template,
- no,
- yes,
- install,
- 'no-install': noInstall,
- git,
- 'no-git': noGit,
- typescript,
- fancy,
- 'skip-houston': skipHouston,
- 'dry-run': dryRun,
- ref,
- } = args.values;
+ '--help': help = false,
+ '--template': template,
+ '--no': no,
+ '--yes': yes,
+ '--install': install,
+ '--no-install': noInstall,
+ '--git': git,
+ '--no-git': noGit,
+ '--typescript': typescript,
+ '--fancy': fancy,
+ '--skip-houston': skipHouston,
+ '--dry-run': dryRun,
+ '--ref': ref,
+ } = flags;
+ let projectName = cwd;
if (no) {
yes = false;
@@ -79,26 +82,10 @@ export async function getContext(argv: string[]): Promise<Context> {
if (typescript == undefined) typescript = 'strict';
}
- skipHouston = typeof skipHouston == 'boolean' ? skipHouston : undefined;
skipHouston =
((os.platform() === 'win32' && !fancy) || skipHouston) ??
[yes, no, install, git, typescript].some((v) => v !== undefined);
- // We use `strict: false` in `parseArgs` to allow unknown options, but Node also
- // simply doesn't guarantee the types anymore, so we need to validate ourselves :(
- help = !!help;
- template = typeof template == 'string' ? template : undefined;
- no = !!no;
- yes = !!yes;
- install = !!install;
- noInstall = !!noInstall;
- git = !!git;
- noGit = !!noGit;
- typescript = typeof typescript == 'string' ? typescript : undefined;
- fancy = !!fancy;
- dryRun = !!dryRun;
- ref = typeof ref == 'string' ? ref : undefined;
-
const { messages, hats, ties } = getSeasonalData({ fancy });
const context: Context = {
@@ -120,7 +107,7 @@ export async function getContext(argv: string[]): Promise<Context> {
install: install ?? (noInstall ? false : undefined),
git: git ?? (noGit ? false : undefined),
typescript,
- cwd: projectName,
+ cwd,
exit(code) {
process.exit(code);
},
diff --git a/packages/db/package.json b/packages/db/package.json
index b92e47fe6..adc6fde64 100644
--- a/packages/db/package.json
+++ b/packages/db/package.json
@@ -81,11 +81,13 @@
"ora": "^8.0.1",
"prompts": "^2.4.2",
"strip-ansi": "^7.1.0",
+ "yargs-parser": "^21.1.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/deep-diff": "^1.0.5",
"@types/prompts": "^2.4.9",
+ "@types/yargs-parser": "^21.0.3",
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"cheerio": "1.0.0",
diff --git a/packages/db/src/core/cli/commands/execute/index.ts b/packages/db/src/core/cli/commands/execute/index.ts
index 03b3d00ad..c6c11cdbb 100644
--- a/packages/db/src/core/cli/commands/execute/index.ts
+++ b/packages/db/src/core/cli/commands/execute/index.ts
@@ -3,6 +3,7 @@ import { getManagedAppTokenOrExit } from '@astrojs/studio';
import { LibsqlError } from '@libsql/client';
import type { AstroConfig } from 'astro';
import { green } from 'kleur/colors';
+import type { Arguments } from 'yargs-parser';
import {
EXEC_DEFAULT_EXPORT_ERROR,
EXEC_ERROR,
@@ -15,7 +16,6 @@ import {
} from '../../../integration/vite-plugin-db.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
import type { DBConfig } from '../../../types.js';
-import type { YargsArguments } from '../../types.js';
export async function cmd({
astroConfig,
@@ -24,7 +24,7 @@ export async function cmd({
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
- flags: YargsArguments;
+ flags: Arguments;
}) {
const filePath = flags._[4];
if (typeof filePath !== 'string') {
diff --git a/packages/db/src/core/cli/commands/login/index.ts b/packages/db/src/core/cli/commands/login/index.ts
index 251d61e05..61f7e0275 100644
--- a/packages/db/src/core/cli/commands/login/index.ts
+++ b/packages/db/src/core/cli/commands/login/index.ts
@@ -7,8 +7,8 @@ import { cyan } from 'kleur/colors';
import open from 'open';
import ora from 'ora';
import prompt from 'prompts';
+import type { Arguments } from 'yargs-parser';
import type { DBConfig } from '../../../types.js';
-import type { YargsArguments } from '../../types.js';
const isWebContainer =
// Stackblitz heuristic
@@ -21,7 +21,7 @@ export async function cmd({
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
- flags: YargsArguments;
+ flags: Arguments;
}) {
let session = flags.session;
diff --git a/packages/db/src/core/cli/commands/push/index.ts b/packages/db/src/core/cli/commands/push/index.ts
index 237f7f6ea..ecd101ece 100644
--- a/packages/db/src/core/cli/commands/push/index.ts
+++ b/packages/db/src/core/cli/commands/push/index.ts
@@ -1,6 +1,7 @@
import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { AstroConfig } from 'astro';
import prompts from 'prompts';
+import type { Arguments } from 'yargs-parser';
import { safeFetch } from '../../../../runtime/utils.js';
import { MIGRATION_VERSION } from '../../../consts.js';
import { type DBConfig, type DBSnapshot } from '../../../types.js';
@@ -12,7 +13,6 @@ import {
getMigrationQueries,
getProductionCurrentSnapshot,
} from '../../migration-queries.js';
-import type { YargsArguments } from '../../types.js';
export async function cmd({
dbConfig,
@@ -20,7 +20,7 @@ export async function cmd({
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
- flags: YargsArguments;
+ flags: Arguments;
}) {
const isDryRun = flags.dryRun;
const isForceReset = flags.forceReset;
diff --git a/packages/db/src/core/cli/commands/shell/index.ts b/packages/db/src/core/cli/commands/shell/index.ts
index 5a6bcaa53..e0a1a6086 100644
--- a/packages/db/src/core/cli/commands/shell/index.ts
+++ b/packages/db/src/core/cli/commands/shell/index.ts
@@ -1,6 +1,7 @@
import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { AstroConfig } from 'astro';
import { sql } from 'drizzle-orm';
+import type { Arguments } from 'yargs-parser';
import {
createLocalDatabaseClient,
createRemoteDatabaseClient,
@@ -10,7 +11,6 @@ import { DB_PATH } from '../../../consts.js';
import { SHELL_QUERY_MISSING_ERROR } from '../../../errors.js';
import type { DBConfigInput } from '../../../types.js';
import { getAstroEnv, getRemoteDatabaseUrl } from '../../../utils.js';
-import type { YargsArguments } from '../../types.js';
export async function cmd({
flags,
@@ -18,7 +18,7 @@ export async function cmd({
}: {
dbConfig: DBConfigInput;
astroConfig: AstroConfig;
- flags: YargsArguments;
+ flags: Arguments;
}) {
const query = flags.query;
if (!query) {
diff --git a/packages/db/src/core/cli/commands/verify/index.ts b/packages/db/src/core/cli/commands/verify/index.ts
index 081c8ae3f..950fb6615 100644
--- a/packages/db/src/core/cli/commands/verify/index.ts
+++ b/packages/db/src/core/cli/commands/verify/index.ts
@@ -1,5 +1,6 @@
import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { AstroConfig } from 'astro';
+import type { Arguments } from 'yargs-parser';
import type { DBConfig } from '../../../types.js';
import {
createCurrentSnapshot,
@@ -8,7 +9,6 @@ import {
getMigrationQueries,
getProductionCurrentSnapshot,
} from '../../migration-queries.js';
-import type { YargsArguments } from '../../types.js';
export async function cmd({
dbConfig,
@@ -16,7 +16,7 @@ export async function cmd({
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
- flags: YargsArguments;
+ flags: Arguments;
}) {
const isJson = flags.json;
const appToken = await getManagedAppTokenOrExit(flags.token);
diff --git a/packages/db/src/core/cli/index.ts b/packages/db/src/core/cli/index.ts
index 5d9aa10e9..531b016a6 100644
--- a/packages/db/src/core/cli/index.ts
+++ b/packages/db/src/core/cli/index.ts
@@ -1,13 +1,13 @@
import type { AstroConfig } from 'astro';
+import type { Arguments } from 'yargs-parser';
import { resolveDbConfig } from '../load-file.js';
import { printHelp } from './print-help.js';
-import type { YargsArguments } from './types.js';
export async function cli({
flags,
config: astroConfig,
}: {
- flags: YargsArguments;
+ flags: Arguments;
config: AstroConfig;
}) {
const args = flags._ as string[];
diff --git a/packages/db/src/core/cli/types.ts b/packages/db/src/core/cli/types.ts
index 9d8aad56b..4294c3fb0 100644
--- a/packages/db/src/core/cli/types.ts
+++ b/packages/db/src/core/cli/types.ts
@@ -1,6 +1,4 @@
-// Copy of `yargs-parser` `Arguments` type. We don't use `yargs-parser`
-// in runtime anymore, but our exposed API still relies on this shape.
-export interface YargsArguments {
+export interface Arguments {
_: Array<string | number>;
'--'?: Array<string | number>;
[argName: string]: any;
diff --git a/packages/db/src/core/integration/index.ts b/packages/db/src/core/integration/index.ts
index c096d08af..da03e71f2 100644
--- a/packages/db/src/core/integration/index.ts
+++ b/packages/db/src/core/integration/index.ts
@@ -2,7 +2,6 @@ import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
-import { parseArgs } from 'node:util';
import { type ManagedAppToken, getManagedAppTokenOrExit } from '@astrojs/studio';
import { LibsqlError } from '@libsql/client';
import type { AstroConfig, AstroIntegration } from 'astro';
@@ -15,6 +14,7 @@ import {
loadEnv,
mergeConfig,
} from 'vite';
+import parseArgs from 'yargs-parser';
import { AstroDbError } from '../../runtime/utils.js';
import { CONFIG_FILE_NAMES, DB_PATH } from '../consts.js';
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js';
@@ -71,8 +71,8 @@ function astroDBIntegration(): AstroIntegration {
if (command === 'preview') return;
let dbPlugin: VitePlugin | undefined = undefined;
- const args = parseArgs({ strict: false });
- connectToStudio = !!process.env.ASTRO_INTERNAL_TEST_REMOTE || !!args.values.remote;
+ const args = parseArgs(process.argv.slice(3));
+ connectToStudio = process.env.ASTRO_INTERNAL_TEST_REMOTE || args['remote'];
if (connectToStudio) {
appToken = await getManagedAppTokenOrExit();
diff --git a/packages/upgrade/src/actions/context.ts b/packages/upgrade/src/actions/context.ts
index cd9028e85..2103a5327 100644
--- a/packages/upgrade/src/actions/context.ts
+++ b/packages/upgrade/src/actions/context.ts
@@ -1,13 +1,13 @@
import { pathToFileURL } from 'node:url';
-import { parseArgs } from 'node:util';
import { prompt } from '@astrojs/cli-kit';
+import arg from 'arg';
import detectPackageManager from 'preferred-pm';
export interface Context {
help: boolean;
prompt: typeof prompt;
version: string;
- dryRun: boolean;
+ dryRun?: boolean;
cwd: URL;
stdin?: typeof process.stdin;
stdout?: typeof process.stdout;
@@ -28,20 +28,22 @@ export interface PackageInfo {
}
export async function getContext(argv: string[]): Promise<Context> {
- const args = parseArgs({
- args: argv,
- allowPositionals: true,
- strict: false,
- options: {
- 'dry-run': { type: 'boolean' },
- help: { type: 'boolean', short: 'h' },
+ const flags = arg(
+ {
+ '--dry-run': Boolean,
+ '--help': Boolean,
+
+ '-h': '--help',
},
- });
+ { argv, permissive: true },
+ );
const packageManager = (await detectPackageManager(process.cwd()))?.name ?? 'npm';
- const version = args.positionals[0] ?? 'latest';
- const help = !!args.values.help;
- const dryRun = !!args.values['dry-run'];
+ const {
+ _: [version = 'latest'] = [],
+ '--help': help = false,
+ '--dry-run': dryRun,
+ } = flags;
return {
help,
diff --git a/packages/upgrade/test/context.test.js b/packages/upgrade/test/context.test.js
index 5ba484740..bbc887c2a 100644
--- a/packages/upgrade/test/context.test.js
+++ b/packages/upgrade/test/context.test.js
@@ -6,12 +6,12 @@ describe('context', () => {
it('no arguments', async () => {
const ctx = await getContext([]);
assert.equal(ctx.version, 'latest');
- assert.equal(ctx.dryRun, false);
+ assert.equal(ctx.dryRun, undefined);
});
it('tag', async () => {
const ctx = await getContext(['beta']);
assert.equal(ctx.version, 'beta');
- assert.equal(ctx.dryRun, false);
+ assert.equal(ctx.dryRun, undefined);
});
it('dry run', async () => {
const ctx = await getContext(['--dry-run']);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 5c902e108..48b1f14e6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -744,6 +744,9 @@ importers:
xxhash-wasm:
specifier: ^1.0.2
version: 1.0.2
+ yargs-parser:
+ specifier: ^21.1.1
+ version: 21.1.1
zod:
specifier: ^3.23.8
version: 3.23.8
@@ -812,6 +815,9 @@ importers:
'@types/semver':
specifier: ^7.5.8
version: 7.5.8
+ '@types/yargs-parser':
+ specifier: ^21.0.3
+ version: 21.0.3
astro-scripts:
specifier: workspace:*
version: link:../../scripts
@@ -4306,6 +4312,9 @@ importers:
strip-ansi:
specifier: ^7.1.0
version: 7.1.0
+ yargs-parser:
+ specifier: ^21.1.1
+ version: 21.1.1
zod:
specifier: ^3.23.8
version: 3.23.8
@@ -4316,6 +4325,9 @@ importers:
'@types/prompts':
specifier: ^2.4.9
version: 2.4.9
+ '@types/yargs-parser':
+ specifier: ^21.0.3
+ version: 21.0.3
astro:
specifier: workspace:*
version: link:../astro
@@ -7598,6 +7610,9 @@ packages:
'@types/xml2js@0.4.14':
resolution: {integrity: sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==}
+ '@types/yargs-parser@21.0.3':
+ resolution: {integrity: sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==}
+
'@typescript-eslint/eslint-plugin@8.0.1':
resolution: {integrity: sha512-5g3Y7GDFsJAnY4Yhvk8sZtFfV6YNF2caLzjrRPUBzewjPCaj0yokePB4LJSobyCzGMzjZZYFbwuzbfDHlimXbQ==}
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
@@ -9479,10 +9494,12 @@ packages:
libsql@0.3.19:
resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==}
+ cpu: [x64, arm64, wasm32]
os: [darwin, linux, win32]
libsql@0.4.1:
resolution: {integrity: sha512-qZlR9Yu1zMBeLChzkE/cKfoKV3Esp9cn9Vx5Zirn4AVhDWPcjYhKwbtJcMuHehgk3mH+fJr9qW+3vesBWbQpBg==}
+ cpu: [x64, arm64, wasm32]
os: [darwin, linux, win32]
lilconfig@2.1.0:
@@ -13557,6 +13574,8 @@ snapshots:
dependencies:
'@types/node': 18.19.31
+ '@types/yargs-parser@21.0.3': {}
+
'@typescript-eslint/eslint-plugin@8.0.1(@typescript-eslint/parser@8.0.1(eslint@9.9.0(jiti@1.21.0))(typescript@5.5.4))(eslint@9.9.0(jiti@1.21.0))(typescript@5.5.4)':
dependencies:
'@eslint-community/regexpp': 4.11.0