padrone 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +330 -0
- package/index.d.mts +384 -0
- package/index.mjs +1246 -0
- package/index.mjs.map +1 -0
- package/package.json +64 -0
- package/src/colorizer.ts +41 -0
- package/src/create.ts +559 -0
- package/src/formatter.ts +499 -0
- package/src/help.ts +227 -0
- package/src/index.ts +22 -0
- package/src/options.ts +290 -0
- package/src/parse.ts +222 -0
- package/src/type-utils.ts +99 -0
- package/src/types.ts +313 -0
- package/src/utils.ts +131 -0
- package/src/zod.d.ts +5 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { AnyPadroneCommand } from './types';
|
|
2
|
+
|
|
3
|
+
export function getRootCommand(cmd: AnyPadroneCommand): AnyPadroneCommand {
|
|
4
|
+
let current = cmd;
|
|
5
|
+
while (current.parent) current = current.parent;
|
|
6
|
+
return current;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Attempts to get the version from various sources:
|
|
11
|
+
* 1. Explicit version set on the command
|
|
12
|
+
* 2. npm_package_version environment variable (set by npm/yarn/pnpm when running scripts)
|
|
13
|
+
* 3. package.json in current or parent directories
|
|
14
|
+
* @param explicitVersion - Version explicitly set via .version()
|
|
15
|
+
* @returns The version string or '0.0.0' if not found
|
|
16
|
+
*/
|
|
17
|
+
export function getVersion(explicitVersion?: string): string {
|
|
18
|
+
// 1. Use explicit version if provided
|
|
19
|
+
if (explicitVersion) return explicitVersion;
|
|
20
|
+
|
|
21
|
+
// 2. Check npm_package_version env var (set by npm/yarn/pnpm during script execution)
|
|
22
|
+
if (typeof process !== 'undefined' && process.env?.npm_package_version) {
|
|
23
|
+
return process.env.npm_package_version;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 3. Try to read from package.json
|
|
27
|
+
if (typeof process !== 'undefined') {
|
|
28
|
+
try {
|
|
29
|
+
const fs = require('node:fs');
|
|
30
|
+
const path = require('node:path');
|
|
31
|
+
let dir = process.cwd();
|
|
32
|
+
|
|
33
|
+
// Walk up the directory tree looking for package.json
|
|
34
|
+
for (let i = 0; i < 10; i++) {
|
|
35
|
+
const pkgPath = path.join(dir, 'package.json');
|
|
36
|
+
if (fs.existsSync(pkgPath)) {
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
38
|
+
if (pkg.version) return pkg.version;
|
|
39
|
+
}
|
|
40
|
+
const parentDir = path.dirname(dir);
|
|
41
|
+
if (parentDir === dir) break; // Reached root
|
|
42
|
+
dir = parentDir;
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
// Ignore errors (e.g., fs not available in browser)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return '0.0.0';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Loads and parses a config file from the given path.
|
|
54
|
+
* Supports JSON, JSONC (JSON with comments), and attempts to parse other formats.
|
|
55
|
+
* @param configPath - Path to the config file
|
|
56
|
+
* @returns Parsed config data or undefined if loading fails
|
|
57
|
+
*/
|
|
58
|
+
export function loadConfigFile(configPath: string): Record<string, unknown> | undefined {
|
|
59
|
+
if (typeof process === 'undefined') return undefined;
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const fs = require('node:fs');
|
|
63
|
+
const path = require('node:path');
|
|
64
|
+
|
|
65
|
+
// Resolve to absolute path
|
|
66
|
+
const absolutePath = path.isAbsolute(configPath) ? configPath : path.resolve(process.cwd(), configPath);
|
|
67
|
+
|
|
68
|
+
if (!fs.existsSync(absolutePath)) {
|
|
69
|
+
console.error(`Config file not found: ${absolutePath}`);
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const getContent = () => fs.readFileSync(absolutePath, 'utf-8');
|
|
74
|
+
const ext = path.extname(absolutePath).toLowerCase();
|
|
75
|
+
|
|
76
|
+
if (ext === '.yaml' || ext === '.yml') {
|
|
77
|
+
return Bun.YAML.parse(getContent()) as any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (ext === '.toml') {
|
|
81
|
+
return Bun.TOML.parse(getContent()) as any;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (ext === '.json') {
|
|
85
|
+
return JSON.parse(getContent());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (ext === '.js' || ext === '.cjs' || ext === '.mjs' || ext === '.ts' || ext === '.cts' || ext === '.mts') {
|
|
89
|
+
// For JS files, require them
|
|
90
|
+
return require(absolutePath);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// For unknown extensions, try to parse as JSON
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(getContent());
|
|
96
|
+
} catch {
|
|
97
|
+
console.error(`Unable to parse config file: ${absolutePath}`);
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error(`Error loading config file: ${error}`);
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Searches for a config file from a list of possible file names.
|
|
108
|
+
* Searches in the current working directory.
|
|
109
|
+
* @param configFiles - Array of possible config file names to search for
|
|
110
|
+
* @returns The path to the first found config file, or undefined if none found
|
|
111
|
+
*/
|
|
112
|
+
export function findConfigFile(configFiles: string[]): string | undefined {
|
|
113
|
+
if (typeof process === 'undefined' || !configFiles?.length) return undefined;
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const fs = require('node:fs');
|
|
117
|
+
const path = require('node:path');
|
|
118
|
+
const cwd = process.cwd();
|
|
119
|
+
|
|
120
|
+
for (const configFile of configFiles) {
|
|
121
|
+
const configPath = path.isAbsolute(configFile) ? configFile : path.resolve(cwd, configFile);
|
|
122
|
+
if (fs.existsSync(configPath)) {
|
|
123
|
+
return configPath;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} catch {
|
|
127
|
+
// Ignore errors (e.g., fs not available in browser)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|