generated from mineclover/effect-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-cli.cjs
More file actions
executable file
·265 lines (229 loc) · 6.75 KB
/
test-cli.cjs
File metadata and controls
executable file
·265 lines (229 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const { spawn } = require('child_process');
const CONFIG_DIR = path.join(os.homedir(), '.protocol-registry');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
// Ensure config directory exists
function ensureConfigDir() {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
console.log('Configuration directory created');
}
}
// Get config
function getConfig() {
ensureConfigDir();
if (!fs.existsSync(CONFIG_FILE)) {
const defaultConfig = {
projects: {},
version: "1.0.0",
lastUpdated: new Date().toISOString()
};
fs.writeFileSync(CONFIG_FILE, JSON.stringify(defaultConfig, null, 2));
return defaultConfig;
}
return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
}
// Save config
function saveConfig(config) {
config.lastUpdated = new Date().toISOString();
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
// Commands
function install() {
console.log('Installing file opener protocol...');
ensureConfigDir();
console.log('Protocol registered successfully');
console.log('Configuration directory: ' + CONFIG_DIR);
}
function add(projectName, projectPath) {
if (!projectName || !projectPath) {
console.log('Missing required arguments');
console.log('Usage: fopen add <project> <path>');
return;
}
if (!fs.existsSync(projectPath)) {
console.log('Path does not exist');
return;
}
const config = getConfig();
config.projects[projectName] = path.resolve(projectPath);
saveConfig(config);
console.log(`Project '${projectName}' added successfully`);
}
function list() {
const config = getConfig();
const projects = config.projects;
const count = Object.keys(projects).length;
if (count === 0) {
console.log('No projects configured');
} else {
console.log('Configured projects:');
for (const [name, projectPath] of Object.entries(projects)) {
console.log(` ${name} -> ${projectPath}`);
}
}
}
function remove(projectName) {
if (!projectName) {
console.log('Missing required project name');
console.log('Usage: fopen remove <project>');
return;
}
const config = getConfig();
if (config.projects[projectName]) {
delete config.projects[projectName];
saveConfig(config);
console.log(`Project '${projectName}' removed successfully`);
} else {
console.log(`Project '${projectName}' not found`);
}
}
function uninstall() {
console.log('Uninstalling file opener protocol...');
console.log('Protocol unregistered successfully');
}
// File opening functionality
function openFile(filePath) {
const platform = process.platform;
let command, args;
switch (platform) {
case 'darwin': // macOS
command = 'open';
args = [filePath];
break;
case 'win32': // Windows
command = 'start';
args = ['', filePath];
break;
case 'linux': // Linux
command = 'xdg-open';
args = [filePath];
break;
default:
console.log(`Unsupported platform: ${platform}`);
return;
}
const child = spawn(command, args, {
detached: true,
stdio: 'ignore'
});
child.on('error', (error) => {
console.log(`Failed to open file: ${error.message}`);
});
child.on('spawn', () => {
console.log(`File opened successfully: ${filePath}`);
child.unref();
});
}
// Parse URL and open file
function handleUrl(url) {
console.log(`Processing URL: ${url}`);
try {
const parsedUrl = new URL(url);
if (parsedUrl.protocol !== 'fileopener:') {
console.log(`Invalid protocol: ${parsedUrl.protocol}. Expected 'fileopener:'`);
return;
}
const project = parsedUrl.hostname;
if (!project) {
console.log('Project name is required in URL');
return;
}
let filePath;
// Check for legacy query parameter format
const queryPath = parsedUrl.searchParams.get('path');
if (queryPath) {
// Legacy format: fileopener://project?path=file/path
filePath = decodeURIComponent(queryPath);
} else {
// Modern format: fileopener://project/file/path
filePath = parsedUrl.pathname.slice(1); // Remove leading slash
if (!filePath) {
console.log('File path is required in URL');
return;
}
filePath = decodeURIComponent(filePath);
}
console.log(`Project: ${project}, File: ${filePath}`);
// Get project path from config
const config = getConfig();
const projectPath = config.projects[project];
if (!projectPath) {
console.log(`Project '${project}' not found in configuration`);
console.log('Available projects:');
for (const [name, path] of Object.entries(config.projects)) {
console.log(` ${name} -> ${path}`);
}
return;
}
// Resolve full file path
const fullPath = path.resolve(path.join(projectPath, filePath));
// Security check: ensure the resolved path is within the project directory
const normalizedProjectPath = path.resolve(projectPath);
if (!fullPath.startsWith(normalizedProjectPath)) {
console.log('Path traversal detected - access denied for security reasons');
return;
}
// Check if file exists
if (!fs.existsSync(fullPath)) {
console.log(`File not found: ${fullPath}`);
return;
}
// Open the file
openFile(fullPath);
} catch (error) {
console.log(`Failed to parse URL: ${error.message}`);
}
}
// Open config file
function openConfig() {
if (!fs.existsSync(CONFIG_FILE)) {
console.log('Config file does not exist. Run "fopen install" first.');
return;
}
console.log(`Opening config file: ${CONFIG_FILE}`);
openFile(CONFIG_FILE);
}
// Main CLI logic
const args = process.argv.slice(2);
const command = args[0];
switch (command) {
case 'install':
install();
break;
case 'add':
add(args[1], args[2]);
break;
case 'list':
list();
break;
case 'remove':
remove(args[1]);
break;
case 'uninstall':
uninstall();
break;
case 'open':
if (!args[1]) {
console.log('Missing required URL');
console.log('Usage: fopen open <fileopener://url>');
} else {
handleUrl(args[1]);
}
break;
case 'config':
openConfig();
break;
default:
console.log('File opener CLI - Use one of the subcommands:');
console.log(' install - Register the fileopener:// protocol');
console.log(' add - Add a project alias');
console.log(' list - List all configured projects');
console.log(' remove - Remove a project alias');
console.log(' uninstall - Unregister the protocol');
console.log(' open - Open a file using fileopener:// URL');
console.log(' config - Open the configuration file');
}