mjpic 1.0.2 → 1.0.4

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/api/cli.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from 'commander';
3
3
  import express from 'express';
4
+ import fs from 'fs';
4
5
  import path from 'path';
5
6
  import { fileURLToPath } from 'url';
6
7
  import open from 'open';
@@ -11,21 +12,15 @@ const __dirname = path.dirname(__filename);
11
12
 
12
13
  const program = new Command();
13
14
 
14
- program
15
- .name('mjpic')
16
- .description('敏捷图片 - 轻量级图片处理工具')
17
- .version('1.0.2')
18
- .option('-p, --port <number>', 'server port', '3000')
19
- .option('--host <string>', 'server host', 'localhost')
20
- .argument('[file]', 'image file to open')
21
- .action(async (file, options) => {
22
- const port = parseInt(options.port, 10);
23
- const host = options.host;
24
-
25
- if (file) {
26
- const absPath = path.resolve(file);
15
+ const startServer = async (options: { port: string; host: string }, file?: string) => {
16
+ const port = parseInt(options.port, 10);
17
+ const host = options.host;
18
+
19
+ if (file) {
20
+ const absPath = path.resolve(file);
21
+ if (fs.existsSync(absPath)) {
27
22
  console.log(`Opening file: ${absPath}`);
28
-
23
+
29
24
  app.get('/api/current-image', (req, res) => {
30
25
  res.json({ path: absPath });
31
26
  });
@@ -33,29 +28,61 @@ program
33
28
  app.get('/api/image-content', (req, res) => {
34
29
  res.sendFile(absPath);
35
30
  });
31
+ } else {
32
+ console.warn(`File not found: ${absPath}`);
36
33
  }
34
+ }
37
35
 
38
- // Serve static files
39
- // In production (dist), client is in ../client
40
- const clientPath = path.join(__dirname, '../client');
41
-
42
- app.use(express.static(clientPath));
36
+ const clientPath = path.join(__dirname, '../client');
37
+ app.use(express.static(clientPath));
43
38
 
44
- // API 404 handler - Ensure API requests don't fall through to index.html
45
- app.use('/api/*', (req, res) => {
46
- res.status(404).json({ success: false, error: 'API not found' });
47
- });
39
+ app.use('/api/*', (req, res) => {
40
+ res.status(404).json({ success: false, error: 'API not found' });
41
+ });
48
42
 
49
- // SPA fallback
50
- app.get('*', (req, res) => {
51
- res.sendFile(path.join(clientPath, 'index.html'));
52
- });
43
+ app.get('*', (req, res) => {
44
+ res.sendFile(path.join(clientPath, 'index.html'));
45
+ });
53
46
 
54
- app.listen(port, host, () => {
55
- const url = `http://${host}:${port}`;
56
- console.log(`敏捷图片 is running at ${url}`);
57
- open(url);
58
- });
47
+ app.listen(port, host, () => {
48
+ const url = `http://${host}:${port}`;
49
+ console.log(`敏捷图片 is running at ${url}`);
50
+ open(url);
59
51
  });
52
+ };
53
+
54
+ program
55
+ .name('mjpic')
56
+ .description('敏捷图片 - 轻量级图片处理工具')
57
+ .version('1.0.4');
58
+
59
+ const attachServeOptions = (cmd: Command) => {
60
+ cmd
61
+ .option('-p, --port <number>', 'server port', '3000')
62
+ .option('--host <string>', 'server host', 'localhost')
63
+ .argument('[file]', 'image file to open')
64
+ .action(async (file, options) => {
65
+ const opts = (options as any)?.opts?.() || options || {};
66
+ await startServer(opts, file);
67
+ });
68
+ };
69
+
70
+ attachServeOptions(
71
+ program
72
+ .command('start', { isDefault: true })
73
+ .description('Start mjpic server'),
74
+ );
75
+
76
+ attachServeOptions(
77
+ program
78
+ .command('dev')
79
+ .description('Start mjpic server (alias)'),
80
+ );
81
+
82
+ attachServeOptions(
83
+ program
84
+ .command('build')
85
+ .description('Start mjpic server (alias)'),
86
+ );
60
87
 
61
88
  program.parse();
package/dist/cli/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from 'commander';
3
3
  import express from 'express';
4
+ import fs from 'fs';
4
5
  import path from 'path';
5
6
  import { fileURLToPath } from 'url';
6
7
  import open from 'open';
@@ -8,35 +9,29 @@ import app from './app.js';
8
9
  const __filename = fileURLToPath(import.meta.url);
9
10
  const __dirname = path.dirname(__filename);
10
11
  const program = new Command();
11
- program
12
- .name('mjpic')
13
- .description('敏捷图片 - 轻量级图片处理工具')
14
- .version('1.0.2')
15
- .option('-p, --port <number>', 'server port', '3000')
16
- .option('--host <string>', 'server host', 'localhost')
17
- .argument('[file]', 'image file to open')
18
- .action(async (file, options) => {
12
+ const startServer = async (options, file) => {
19
13
  const port = parseInt(options.port, 10);
20
14
  const host = options.host;
21
15
  if (file) {
22
16
  const absPath = path.resolve(file);
23
- console.log(`Opening file: ${absPath}`);
24
- app.get('/api/current-image', (req, res) => {
25
- res.json({ path: absPath });
26
- });
27
- app.get('/api/image-content', (req, res) => {
28
- res.sendFile(absPath);
29
- });
17
+ if (fs.existsSync(absPath)) {
18
+ console.log(`Opening file: ${absPath}`);
19
+ app.get('/api/current-image', (req, res) => {
20
+ res.json({ path: absPath });
21
+ });
22
+ app.get('/api/image-content', (req, res) => {
23
+ res.sendFile(absPath);
24
+ });
25
+ }
26
+ else {
27
+ console.warn(`File not found: ${absPath}`);
28
+ }
30
29
  }
31
- // Serve static files
32
- // In production (dist), client is in ../client
33
30
  const clientPath = path.join(__dirname, '../client');
34
31
  app.use(express.static(clientPath));
35
- // API 404 handler - Ensure API requests don't fall through to index.html
36
32
  app.use('/api/*', (req, res) => {
37
33
  res.status(404).json({ success: false, error: 'API not found' });
38
34
  });
39
- // SPA fallback
40
35
  app.get('*', (req, res) => {
41
36
  res.sendFile(path.join(clientPath, 'index.html'));
42
37
  });
@@ -45,5 +40,28 @@ program
45
40
  console.log(`敏捷图片 is running at ${url}`);
46
41
  open(url);
47
42
  });
48
- });
43
+ };
44
+ program
45
+ .name('mjpic')
46
+ .description('敏捷图片 - 轻量级图片处理工具')
47
+ .version('1.0.4');
48
+ const attachServeOptions = (cmd) => {
49
+ cmd
50
+ .option('-p, --port <number>', 'server port', '3000')
51
+ .option('--host <string>', 'server host', 'localhost')
52
+ .argument('[file]', 'image file to open')
53
+ .action(async (file, options) => {
54
+ const opts = options?.opts?.() || options || {};
55
+ await startServer(opts, file);
56
+ });
57
+ };
58
+ attachServeOptions(program
59
+ .command('start', { isDefault: true })
60
+ .description('Start mjpic server'));
61
+ attachServeOptions(program
62
+ .command('dev')
63
+ .description('Start mjpic server (alias)'));
64
+ attachServeOptions(program
65
+ .command('build')
66
+ .description('Start mjpic server (alias)'));
49
67
  program.parse();