cc4-embedded-system 3.1.3 → 3.1.5

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.
Files changed (2) hide show
  1. package/dist/gui.js +82 -19
  2. package/package.json +2 -2
package/dist/gui.js CHANGED
@@ -64,7 +64,7 @@ import path from 'node:path';
64
64
  import { fileURLToPath } from 'node:url';
65
65
  import { runMakeFsData } from './makefsdata.js';
66
66
  import { getPackageVersion } from './utils.js';
67
- import { execSync } from 'node:child_process';
67
+ import { execFile } from 'node:child_process';
68
68
  import os from 'node:os';
69
69
  // prevent socket errors
70
70
  process.on('uncaughtException', (err) => {
@@ -98,6 +98,53 @@ const cancelShutdown = () => {
98
98
  // console.log('🛡️ Shutdown cancelled (keep alive)');
99
99
  }
100
100
  };
101
+ const buildPowerShellArgs = (script) => {
102
+ const encodedCommand = Buffer.from(script, 'utf16le').toString('base64');
103
+ return [
104
+ '-NoProfile',
105
+ '-STA',
106
+ '-EncodedCommand',
107
+ encodedCommand
108
+ ];
109
+ };
110
+ const runDialogCommand = (command, args) => {
111
+ return new Promise((resolve, reject) => {
112
+ execFile(command, args, {
113
+ windowsHide: false,
114
+ maxBuffer: 1024 * 1024
115
+ }, (error, stdout, stderr) => {
116
+ if (error) {
117
+ reject(new Error(stderr.trim() || error.message));
118
+ return;
119
+ }
120
+ resolve(stdout.trim());
121
+ });
122
+ });
123
+ };
124
+ const buildWindowsDialogScript = (dialogLines) => {
125
+ return [
126
+ 'Add-Type -AssemblyName System.Windows.Forms',
127
+ 'Add-Type -AssemblyName System.Drawing',
128
+ '$screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea',
129
+ '$owner = New-Object System.Windows.Forms.Form',
130
+ '$owner.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual',
131
+ '$owner.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None',
132
+ '$owner.ShowInTaskbar = $false',
133
+ '$owner.TopMost = $true',
134
+ '$owner.Width = 1',
135
+ '$owner.Height = 1',
136
+ '$owner.Left = $screen.Left + [int]($screen.Width / 2)',
137
+ '$owner.Top = $screen.Top + [int]($screen.Height / 2)',
138
+ '$owner.Opacity = 0',
139
+ '$null = $owner.Show()',
140
+ '$null = $owner.Activate()',
141
+ '$owner.BringToFront()',
142
+ '[System.Windows.Forms.Application]::DoEvents()',
143
+ ...dialogLines,
144
+ '$owner.Close()',
145
+ '$owner.Dispose()'
146
+ ].join('\n');
147
+ };
101
148
  app.get('/api/version', (_req, res) => {
102
149
  cancelShutdown();
103
150
  res.json({ version: getPackageVersion() });
@@ -173,30 +220,46 @@ app.post('/api/change-port', (req, res) => {
173
220
  }
174
221
  }, 100);
175
222
  });
176
- app.get('/api/browse', (req, res) => {
223
+ app.get('/api/browse', async (req, res) => {
224
+ cancelShutdown();
177
225
  const isDir = req.query.type === 'dir';
178
226
  const platform = os.platform();
179
- let command = '';
180
227
  try {
181
- if (platform === 'win32') { // Windows: Powershell + OpenFileDialog
182
- if (isDir)
183
- command = `powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.OpenFileDialog; $f.CheckFileExists = $false; $f.CheckPathExists = $true; $f.FileName = 'Select Folder'; $f.ValidateNames = $false; if($f.ShowDialog() -eq 'OK'){ Split-Path $f.FileName }"`;
184
- else
185
- command = `powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.OpenFileDialog; $f.Filter = 'C Files (*.c)|*.c|All Files (*.*)|*.*'; if($f.ShowDialog() -eq 'OK'){ $f.FileName }"`;
228
+ let result = '';
229
+ if (platform === 'win32') {
230
+ const script = isDir
231
+ ? buildWindowsDialogScript([
232
+ '$dialog = New-Object System.Windows.Forms.FolderBrowserDialog',
233
+ '$dialog.Description = "Select Input Directory"',
234
+ 'if ($dialog.ShowDialog($owner) -eq [System.Windows.Forms.DialogResult]::OK) {',
235
+ ' $dialog.SelectedPath',
236
+ '}'
237
+ ])
238
+ : buildWindowsDialogScript([
239
+ '$dialog = New-Object System.Windows.Forms.SaveFileDialog',
240
+ '$dialog.Filter = "C Files (*.c)|*.c|All Files (*.*)|*.*"',
241
+ '$dialog.DefaultExt = "c"',
242
+ '$dialog.AddExtension = $true',
243
+ '$dialog.OverwritePrompt = $true',
244
+ 'if ($dialog.ShowDialog($owner) -eq [System.Windows.Forms.DialogResult]::OK) {',
245
+ ' $dialog.FileName',
246
+ '}'
247
+ ]);
248
+ result = await runDialogCommand('powershell.exe', buildPowerShellArgs(script));
186
249
  }
187
- else if (platform === 'darwin') { // macOS: darwin
188
- if (isDir)
189
- command = `osascript -e 'POSIX path of (choose folder with prompt "Select Input Directory")'`;
190
- else
191
- command = `osascript -e 'POSIX path of (choose file with prompt "Select Output File")'`;
250
+ else if (platform === 'darwin') {
251
+ result = await runDialogCommand('osascript', [
252
+ '-e',
253
+ isDir
254
+ ? 'POSIX path of (choose folder with prompt "Select Input Directory")'
255
+ : 'POSIX path of (choose file name with prompt "Select Output File")'
256
+ ]);
192
257
  }
193
- else { // Linux: Zenity
194
- if (isDir)
195
- command = `zenity --file-selection --directory --title="Select Input Directory"`;
196
- else
197
- command = `zenity --file-selection --title="Select Output File"`;
258
+ else {
259
+ result = await runDialogCommand('zenity', isDir
260
+ ? ['--file-selection', '--directory', '--title=Select Input Directory']
261
+ : ['--file-selection', '--save', '--confirm-overwrite', '--title=Select Output File', '--filename=fsdata.c']);
198
262
  }
199
- const result = execSync(command).toString().trim();
200
263
  res.json({ success: true, path: result || null });
201
264
  }
202
265
  catch (e) {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "cc4-embedded-system",
3
- "version": "3.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "A modern typescript version of makefsdata based on html-minifier-next and lwIP v1.3.1",
5
5
  "bin": {
6
- "cc4es": "./dist/gui.js"
6
+ "cc4es": "dist/gui.js"
7
7
  },
8
8
  "type": "module",
9
9
  "scripts": {