configorama 0.4.9 → 0.5.0

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/cli.js ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs')
4
+ const minimist = require('minimist')
5
+ const Configorama = require('./lib/main')
6
+
7
+ // Parse command line arguments
8
+ const argv = minimist(process.argv.slice(2), {
9
+ string: ['output', 'o', 'format', 'f'],
10
+ boolean: ['help', 'h', 'version', 'v', 'debug', 'd', 'allow-unknown', 'allow-undefined'],
11
+ alias: {
12
+ h: 'help',
13
+ v: 'version',
14
+ o: 'output',
15
+ f: 'format',
16
+ d: 'debug'
17
+ },
18
+ default: {
19
+ format: 'json'
20
+ }
21
+ })
22
+
23
+ // Show help
24
+ if (argv.help) {
25
+ console.log(`
26
+ Configorama - Variable resolution for configuration files
27
+
28
+ Usage:
29
+ configorama [options] <file>
30
+
31
+ Options:
32
+ -h, --help Show this help message
33
+ -v, --version Show version number
34
+ -o, --output <file> Write output to file instead of stdout
35
+ -f, --format <format> Output format: json, yaml, or js (default: json)
36
+ -d, --debug Enable debug mode
37
+ --allow-unknown Allow unknown variables to pass through
38
+ --allow-undefined Allow undefined values in the final output
39
+
40
+ Examples:
41
+ configorama config.yml
42
+ configorama --format yaml config.json
43
+ configorama --output resolved.json config.yml
44
+ configorama --allow-unknown config.toml
45
+ `)
46
+ process.exit(0)
47
+ }
48
+
49
+ // Show version
50
+ if (argv.version) {
51
+ const packageJson = require('./package.json')
52
+ console.log(`Configorama v${packageJson.version}`)
53
+ process.exit(0)
54
+ }
55
+
56
+ // Check for input file
57
+ const inputFile = argv._[0]
58
+ if (!inputFile) {
59
+ console.error('Error: No input file specified')
60
+ console.error('Run with --help for usage information')
61
+ process.exit(1)
62
+ }
63
+
64
+ // Check if file exists
65
+ if (!fs.existsSync(inputFile)) {
66
+ console.error(`Error: File not found: ${inputFile}`)
67
+ process.exit(1)
68
+ }
69
+
70
+ // Set options for Configorama
71
+ const options = {
72
+ allowUnknownVars: argv['allow-unknown'] || false,
73
+ allowUndefinedValues: argv['allow-undefined'] || false,
74
+ dynamicArgs: argv
75
+ }
76
+
77
+ // Create Configorama instance
78
+ const configorama = new Configorama(inputFile, options)
79
+
80
+ // Process the configuration
81
+ configorama.init(argv)
82
+ .then((config) => {
83
+ let output
84
+
85
+ // Format the output
86
+ switch (argv.format.toLowerCase()) {
87
+ case 'yaml':
88
+ case 'yml':
89
+ const YAML = require('./lib/parsers/yaml')
90
+ output = YAML.dump(config)
91
+ break
92
+ case 'js':
93
+ case 'javascript':
94
+ output = `module.exports = ${JSON.stringify(config, null, 2)}`
95
+ break
96
+ case 'json':
97
+ default:
98
+ output = JSON.stringify(config, null, 2)
99
+ }
100
+
101
+ // Write to file or stdout
102
+ if (argv.output) {
103
+ fs.writeFileSync(argv.output, output)
104
+ console.log(`Configuration written to ${argv.output}`)
105
+ } else {
106
+ console.log(output)
107
+ }
108
+ })
109
+ .catch((error) => {
110
+ console.error(`Error processing configuration: ${inputFile}`)
111
+ console.error(error.message)
112
+ if (argv.debug) {
113
+ console.error(error.stack)
114
+ }
115
+ process.exit(1)
116
+ })