nodewise 1.0.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/src/config.js ADDED
@@ -0,0 +1,167 @@
1
+ /**
2
+ * config.js
3
+ *
4
+ * Manages nodewise configuration.
5
+ * Handles reading, writing, and validating the nodewise.config.json file.
6
+ */
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ const CONFIG_FILE = 'nodewise.config.json';
12
+
13
+ /**
14
+ * Get the configuration file path
15
+ * Looks in current working directory
16
+ */
17
+ function getConfigPath() {
18
+ return path.join(process.cwd(), CONFIG_FILE);
19
+ }
20
+
21
+ /**
22
+ * Check if config file exists
23
+ */
24
+ function configExists() {
25
+ return fs.existsSync(getConfigPath());
26
+ }
27
+
28
+ /**
29
+ * Load configuration from file
30
+ */
31
+ function loadConfig() {
32
+ const configPath = getConfigPath();
33
+
34
+ if (!fs.existsSync(configPath)) {
35
+ return null;
36
+ }
37
+
38
+ try {
39
+ const data = fs.readFileSync(configPath, 'utf8');
40
+ return JSON.parse(data);
41
+ } catch (error) {
42
+ throw new Error(`Failed to parse config file: ${error.message}`);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Save configuration to file
48
+ */
49
+ function saveConfig(config) {
50
+ const configPath = getConfigPath();
51
+ const data = JSON.stringify(config, null, 2);
52
+
53
+ try {
54
+ fs.writeFileSync(configPath, data, 'utf8');
55
+ return true;
56
+ } catch (error) {
57
+ throw new Error(`Failed to save config file: ${error.message}`);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Get default configuration template
63
+ */
64
+ function getDefaultConfig() {
65
+ return {
66
+ mode: 'normal',
67
+ gemini: {
68
+ endpoint: '',
69
+ apiKey: ''
70
+ },
71
+ autoRestart: true,
72
+ ignorePatterns: ['node_modules', '.git', '.env'],
73
+ timeout: 60000 // 60 seconds
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Validate configuration
79
+ */
80
+ function validateConfig(config) {
81
+ if (!config.mode) {
82
+ throw new Error('Config must have a "mode" field (normal or gemini)');
83
+ }
84
+
85
+ if (config.mode !== 'normal' && config.mode !== 'gemini') {
86
+ throw new Error('Mode must be either "normal" or "gemini"');
87
+ }
88
+
89
+ if (config.mode === 'gemini') {
90
+ if (!config.gemini || !config.gemini.apiKey) {
91
+ throw new Error('Gemini mode requires API key in config');
92
+ }
93
+ }
94
+
95
+ return true;
96
+ }
97
+
98
+ /**
99
+ * Merge partial config with defaults
100
+ */
101
+ function mergeWithDefaults(partialConfig) {
102
+ const defaults = getDefaultConfig();
103
+ return {
104
+ ...defaults,
105
+ ...partialConfig,
106
+ gemini: {
107
+ ...defaults.gemini,
108
+ ...partialConfig.gemini
109
+ }
110
+ };
111
+ }
112
+
113
+ /**
114
+ * Create initial configuration
115
+ */
116
+ function createConfig(mode, geminiConfig = {}) {
117
+ const config = getDefaultConfig();
118
+ config.mode = mode;
119
+
120
+ if (mode === 'gemini') {
121
+ config.gemini = {
122
+ endpoint: geminiConfig.endpoint || '',
123
+ apiKey: geminiConfig.apiKey || ''
124
+ };
125
+ }
126
+
127
+ validateConfig(config);
128
+ saveConfig(config);
129
+ return config;
130
+ }
131
+
132
+ /**
133
+ * Update existing configuration
134
+ */
135
+ function updateConfig(updates) {
136
+ let config = loadConfig();
137
+
138
+ if (!config) {
139
+ config = getDefaultConfig();
140
+ }
141
+
142
+ config = {
143
+ ...config,
144
+ ...updates,
145
+ gemini: {
146
+ ...config.gemini,
147
+ ...updates.gemini
148
+ }
149
+ };
150
+
151
+ validateConfig(config);
152
+ saveConfig(config);
153
+ return config;
154
+ }
155
+
156
+ module.exports = {
157
+ getConfigPath,
158
+ configExists,
159
+ loadConfig,
160
+ saveConfig,
161
+ getDefaultConfig,
162
+ validateConfig,
163
+ mergeWithDefaults,
164
+ createConfig,
165
+ updateConfig,
166
+ CONFIG_FILE
167
+ };