mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-04 21:04:37 +08:00
ref: use commander for arguments parsing
This commit is contained in:
parent
a2dbecb606
commit
4d4e2a8324
@ -97,6 +97,7 @@
|
||||
},
|
||||
"packageManager": "pnpm@9.1.2",
|
||||
"dependencies": {
|
||||
"commander": "^13.1.0",
|
||||
"tsx": "^4.19.3",
|
||||
"yaml": "^2.4.5"
|
||||
}
|
||||
|
||||
@ -38,6 +38,7 @@
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@stackframe/stack-shared": "workspace:*",
|
||||
"commander": "^13.1.0",
|
||||
"inquirer": "^9.2.19",
|
||||
"open": "^10.1.0"
|
||||
},
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
import * as child_process from "child_process";
|
||||
import { Command } from "commander";
|
||||
import * as fs from "fs";
|
||||
import inquirer from "inquirer";
|
||||
import open from "open";
|
||||
import * as path from "path";
|
||||
import packageJson from '../package.json';
|
||||
|
||||
const jsLikeFileExtensions: string[] = [
|
||||
"mtsx",
|
||||
@ -19,6 +21,43 @@ const jsLikeFileExtensions: string[] = [
|
||||
"js",
|
||||
];
|
||||
|
||||
// Setup command line parsing
|
||||
const program = new Command();
|
||||
program
|
||||
.name(packageJson.name)
|
||||
.description("Stack Auth Initialization Tool")
|
||||
.version(packageJson.version)
|
||||
.argument("[project-path]", "Path to your project")
|
||||
.usage(`[project-path] [options]`)
|
||||
.option("--dry-run", "Run without making any changes")
|
||||
.option("--neon", "Use Neon database")
|
||||
.option("--js", "Initialize for JavaScript project")
|
||||
.option("--next", "Initialize for Next.js project")
|
||||
.option("--npm", "Use npm as package manager")
|
||||
.option("--yarn", "Use yarn as package manager")
|
||||
.option("--pnpm", "Use pnpm as package manager")
|
||||
.option("--bun", "Use bun as package manager")
|
||||
.option("--client", "Initialize client-side only")
|
||||
.option("--server", "Initialize server-side only")
|
||||
.option("--no-browser", "Don't open browser for environment variable setup")
|
||||
.addHelpText('after', `
|
||||
For more information, please visit https://docs.stack-auth.com/getting-started/setup`);
|
||||
|
||||
program.parse();
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
// Keep existing variables but assign from Commander
|
||||
let savedProjectPath: string | undefined = program.args[0] || undefined;
|
||||
const isDryRun: boolean = options.dryRun || false;
|
||||
const isNeon: boolean = options.neon || false;
|
||||
const typeFromArgs: string | undefined = options.js ? "js" : options.next ? "next" : undefined;
|
||||
const packageManagerFromArgs: string | undefined = options.npm ? "npm" : options.yarn ? "yarn" : options.pnpm ? "pnpm" : options.bun ? "bun" : undefined;
|
||||
const isClient: boolean = options.client || false;
|
||||
const isServer: boolean = options.server || false;
|
||||
const noBrowser: boolean = !options.browser;
|
||||
const showHelp: boolean = false; // Handled by Commander automatically
|
||||
|
||||
class UserError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
@ -26,17 +65,6 @@ class UserError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
let savedProjectPath: string | undefined = process.argv[2] || undefined;
|
||||
|
||||
const isDryRun: boolean = process.argv.includes("--dry-run");
|
||||
const isNeon: boolean = process.argv.includes("--neon");
|
||||
const typeFromArgs: string | undefined = ["js", "next"].find(s => process.argv.includes(`--${s}`));
|
||||
const packageManagerFromArgs: string | undefined = ["npm", "yarn", "pnpm", "bun"].find(s => process.argv.includes(`--${s}`));
|
||||
const isClient: boolean = process.argv.includes("--client");
|
||||
const isServer: boolean = process.argv.includes("--server");
|
||||
const noBrowser: boolean = process.argv.includes("--no-browser");
|
||||
const showHelp: boolean = process.argv.includes("--help") || process.argv.includes("-h");
|
||||
|
||||
type Ansis = {
|
||||
red: string,
|
||||
blue: string,
|
||||
@ -85,32 +113,6 @@ const nextSteps: string[] = [
|
||||
];
|
||||
|
||||
async function main(): Promise<void> {
|
||||
// Check if help flag is provided
|
||||
if (showHelp) {
|
||||
console.log(`
|
||||
Stack Auth Initialization Tool
|
||||
|
||||
Usage: npx init-stack [project-path] [options]
|
||||
|
||||
Options:
|
||||
--help, -h Show this help message
|
||||
--dry-run Run without making any changes
|
||||
--neon Use Neon database
|
||||
--js Initialize for JavaScript project
|
||||
--next Initialize for Next.js project
|
||||
--npm Use npm as package manager
|
||||
--yarn Use yarn as package manager
|
||||
--pnpm Use pnpm as package manager
|
||||
--bun Use bun as package manager
|
||||
--client Initialize client-side only
|
||||
--server Initialize server-side only
|
||||
--no-browser Don't open browser for environment variable setup
|
||||
|
||||
For more information, please visit https://docs.stack-auth.com/getting-started/setup
|
||||
`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Welcome message
|
||||
console.log();
|
||||
console.log(`
|
||||
|
||||
@ -17,6 +17,9 @@ importers:
|
||||
|
||||
.:
|
||||
dependencies:
|
||||
commander:
|
||||
specifier: ^13.1.0
|
||||
version: 13.1.0
|
||||
tsx:
|
||||
specifier: ^4.19.3
|
||||
version: 4.19.3
|
||||
@ -828,6 +831,9 @@ importers:
|
||||
'@stackframe/stack-shared':
|
||||
specifier: workspace:*
|
||||
version: link:../stack-shared
|
||||
commander:
|
||||
specifier: ^13.1.0
|
||||
version: 13.1.0
|
||||
inquirer:
|
||||
specifier: ^9.2.19
|
||||
version: 9.2.23
|
||||
@ -7311,6 +7317,10 @@ packages:
|
||||
resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==}
|
||||
engines: {node: '>=18'}
|
||||
|
||||
commander@13.1.0:
|
||||
resolution: {integrity: sha512-/rFeCpNJQbhSZjGVwO9RFV3xPqbnERS8MmIQzCtD/zl6gpJuV/bMLuN92oG3F7d8oDEHHRrujSXNUr8fpjntKw==}
|
||||
engines: {node: '>=18'}
|
||||
|
||||
commander@2.20.3:
|
||||
resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==}
|
||||
|
||||
@ -18481,6 +18491,8 @@ snapshots:
|
||||
|
||||
commander@12.1.0: {}
|
||||
|
||||
commander@13.1.0: {}
|
||||
|
||||
commander@2.20.3: {}
|
||||
|
||||
commander@4.1.1: {}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user