scripting-cli 1.3.0 → 1.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/README.md CHANGED
@@ -112,12 +112,40 @@ The default file is JSON so the npx flow stays zero-install. TypeScript and Java
112
112
  "port": 3000,
113
113
  "autoOpen": true,
114
114
  "generateTsConfig": true, // set to false if you manage tsconfig.json yourself
115
- "logLevel": "info" // "silent" | "error" | "warn" | "info" | "debug"
115
+ "logLevel": "info", // "silent" | "error" | "warn" | "info" | "debug"
116
+ "ignore": ["*.log", "dist", "assets/large.mp4"] // extra paths to exclude from sync
116
117
  }
117
118
  ```
118
119
 
119
120
  CLI flags take precedence over the config file.
120
121
 
122
+ ### Excluding files from sync (`ignore`)
123
+
124
+ Synchronization covers **all** files in a script directory — source code, plus
125
+ images, fonts, and any other assets — in both directions (app ⇄ desktop).
126
+ Text files sync with change tracking; everything else is transferred as raw bytes.
127
+
128
+ Dotfiles/dot-directories (such as `.git`, `.vscode`, `.DS_Store`) and `node_modules`
129
+ are **always excluded** by default. Use `ignore` to exclude additional files:
130
+
131
+ ```jsonc
132
+ {
133
+ "ignore": [
134
+ "*.log", // extension glob — matches any file ending in .log
135
+ "dist", // name — matches any file or directory named "dist" at any depth
136
+ "assets/big.mp4" // path — matches this file relative to the script root
137
+ ]
138
+ }
139
+ ```
140
+
141
+ Each entry is matched as one of three forms:
142
+
143
+ - `*.ext` — matches files by extension.
144
+ - a plain name (no `/`) — matches any path segment (file or directory) with that name.
145
+ - a path containing `/` — matches that script-relative path (exact or as a directory prefix).
146
+
147
+ `ignore` applies to both sync directions.
148
+
121
149
  ### Type completion (TypeScript config)
122
150
 
123
151
  To get autocomplete in `scripting.config.ts`, install scripting-cli as a dev dependency:
package/dist/config.js CHANGED
@@ -29,6 +29,7 @@ const DEFAULTS = {
29
29
  autoOpen: true,
30
30
  generateTsConfig: true,
31
31
  logLevel: 'info',
32
+ ignore: [],
32
33
  };
33
34
  function findConfigFile(cwd = process.cwd()) {
34
35
  for (const name of CONFIG_FILENAMES) {
@@ -78,6 +79,7 @@ function mergeConfig(base, override) {
78
79
  autoOpen: override.autoOpen ?? base.autoOpen,
79
80
  generateTsConfig: override.generateTsConfig ?? base.generateTsConfig,
80
81
  logLevel: override.logLevel ?? base.logLevel,
82
+ ignore: override.ignore ?? base.ignore,
81
83
  };
82
84
  }
83
85
  function resolveDefaults(config) {
@@ -89,6 +91,7 @@ function resolveDefaults(config) {
89
91
  autoOpen: config.autoOpen ?? DEFAULTS.autoOpen,
90
92
  generateTsConfig: config.generateTsConfig ?? DEFAULTS.generateTsConfig,
91
93
  logLevel: config.logLevel ?? DEFAULTS.logLevel,
94
+ ignore: config.ignore ?? DEFAULTS.ignore,
92
95
  };
93
96
  }
94
97
  async function resolveConfig(opts) {
@@ -15,6 +15,7 @@ export declare class Controller {
15
15
  handleSyncScriptFromClient: (data: {
16
16
  [globalDtsFileName]: string;
17
17
  [scriptingDtsFileName]: string;
18
+ extraDtsFiles?: Record<string, string>;
18
19
  scriptName: string;
19
20
  scriptFiles: Record<string, string>;
20
21
  }, ack: (result: {
@@ -24,6 +25,7 @@ export declare class Controller {
24
25
  handleSyncScriptFromServer: (data: {
25
26
  "global.d.ts": string;
26
27
  "scripting.d.ts": string;
28
+ extraDtsFiles?: Record<string, string>;
27
29
  scriptName: string;
28
30
  }, ack: (result: {
29
31
  error?: string;
@@ -50,6 +52,14 @@ export declare class Controller {
50
52
  error?: string;
51
53
  content?: string;
52
54
  }) => void) => Promise<void>;
55
+ handlePutFileData: (data: {
56
+ scriptName: string;
57
+ relativePath: string;
58
+ data: Buffer;
59
+ }, ack: (result: {
60
+ error?: string;
61
+ success?: boolean;
62
+ }) => void) => Promise<void>;
53
63
  handleGetFilePath: (data: {
54
64
  scriptName: string;
55
65
  relativePath: string;
@@ -53,11 +53,11 @@ class Controller {
53
53
  createWatcher(scriptName) {
54
54
  const scriptDir = (0, util_1.getScriptPath)(scriptName);
55
55
  this.watcher = chokidar_1.default.watch(scriptDir, {
56
- ignored: /(^|[\/\\])\../,
56
+ ignored: (filePath) => (0, util_1.isIgnoredPath)((0, util_1.getRelativePath)(scriptDir, filePath), this.config.ignore),
57
57
  persistent: true,
58
58
  });
59
59
  const updateHandler = (filePath) => {
60
- if (filePath.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
60
+ if (!(0, util_1.isBinaryFile)(filePath)) {
61
61
  // send file content md5 hash to client
62
62
  // const content = fs.readFileSync(filePath, 'utf-8');
63
63
  fs_1.default.readFile(filePath, 'utf-8', (err, content) => {
@@ -116,11 +116,15 @@ class Controller {
116
116
  await (0, util_1.writeDtsFiles)({
117
117
  [const_1.globalDtsFileName]: data[const_1.globalDtsFileName],
118
118
  [const_1.scriptingDtsFileName]: data[const_1.scriptingDtsFileName],
119
+ ...(data.extraDtsFiles ?? {}),
119
120
  });
120
121
  this.scriptName = data.scriptName;
121
122
  const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
122
123
  (0, util_1.ensureDirectoryExistence)(scriptDir);
123
124
  await Promise.all(Object.entries(data.scriptFiles).map(async ([filename, content]) => {
125
+ if ((0, util_1.isIgnoredPath)(filename, this.config.ignore)) {
126
+ return;
127
+ }
124
128
  const filePath = path_1.default.join(scriptDir, filename);
125
129
  (0, util_1.ensureDirectoryExistence)(filePath);
126
130
  await fs_1.default.promises.writeFile(filePath, content);
@@ -163,6 +167,7 @@ class Controller {
163
167
  await (0, util_1.writeDtsFiles)({
164
168
  [const_1.globalDtsFileName]: data[const_1.globalDtsFileName],
165
169
  [const_1.scriptingDtsFileName]: data[const_1.scriptingDtsFileName],
170
+ ...(data.extraDtsFiles ?? {}),
166
171
  });
167
172
  const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
168
173
  if (!fs_1.default.existsSync(scriptDir)) {
@@ -176,15 +181,15 @@ class Controller {
176
181
  const readDir = async (dir) => {
177
182
  const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
178
183
  await Promise.all(files.map(async (file) => {
179
- // ignore .git and .vscode directories
180
- if (file.name.startsWith('.git') || file.name.startsWith('.vscode')) {
184
+ const filePath = path_1.default.join(dir, file.name);
185
+ const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
186
+ // Skip default-ignored (dotfiles, node_modules) and user-configured paths.
187
+ if ((0, util_1.isIgnoredPath)(relativePath, this.config.ignore)) {
181
188
  return;
182
189
  }
183
- // only read js, jsx, ts, tsx, json, md, txt files
190
+ // Classify by content: text files sync with hash, others as binary.
184
191
  if (file.isFile()) {
185
- const filePath = path_1.default.join(dir, file.name);
186
- const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
187
- if (file.name.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
192
+ if (!(0, util_1.isBinaryFile)(filePath)) {
188
193
  const content = await fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
189
194
  console.log(chalk_1.default.red(`Error reading file: ${err}`));
190
195
  });
@@ -274,6 +279,45 @@ class Controller {
274
279
  });
275
280
  }
276
281
  };
282
+ handlePutFileData = async (data, ack) => {
283
+ if (this.scriptName !== data.scriptName) {
284
+ ack({
285
+ error: `Script ${data.scriptName} is not initialized`,
286
+ });
287
+ return;
288
+ }
289
+ if ((0, util_1.isIgnoredPath)(data.relativePath, this.config.ignore)) {
290
+ ack({
291
+ error: `Ignored file path: ${data.relativePath}`,
292
+ });
293
+ return;
294
+ }
295
+ const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
296
+ const filePath = path_1.default.join(scriptDir, data.relativePath);
297
+ // Prevent path traversal: the resolved path must stay inside scriptDir.
298
+ const resolved = path_1.default.resolve(filePath);
299
+ if (resolved !== path_1.default.resolve(scriptDir) &&
300
+ !resolved.startsWith(path_1.default.resolve(scriptDir) + path_1.default.sep)) {
301
+ ack({
302
+ error: `Invalid file path: ${data.relativePath}`,
303
+ });
304
+ return;
305
+ }
306
+ try {
307
+ (0, util_1.ensureDirectoryExistence)(filePath);
308
+ await fs_1.default.promises.writeFile(filePath, data.data);
309
+ console.log(`[${chalk_1.default.bold.blue(data.scriptName)}] File uploaded: ${data.relativePath}`);
310
+ ack({
311
+ success: true,
312
+ });
313
+ }
314
+ catch (e) {
315
+ console.log(chalk_1.default.red(`Error writing uploaded file: ${e}`));
316
+ ack({
317
+ error: `Failed to write uploaded file.\n${e}`,
318
+ });
319
+ }
320
+ };
277
321
  handleGetFilePath = async (data, ack) => {
278
322
  if (this.scriptName !== data.scriptName) {
279
323
  ack({
@@ -71,6 +71,44 @@ const getFileData = (req, res) => {
71
71
  res.sendFile(result.filePath);
72
72
  });
73
73
  };
74
+ const putFileData = (req, res) => {
75
+ const { socketId, scriptName, relativePath, } = req.query;
76
+ if (!scriptName || !relativePath) {
77
+ res.json({
78
+ error: "Missing parameters: scriptName or relativePath."
79
+ });
80
+ return;
81
+ }
82
+ if (!Buffer.isBuffer(req.body)) {
83
+ res.json({
84
+ error: "Missing file data in request body."
85
+ });
86
+ return;
87
+ }
88
+ const ctrl = controller_1.Controller.get(socketId);
89
+ if (!ctrl) {
90
+ console.log(chalk_1.default.red(`Socket not found: ${socketId}`));
91
+ res.json({
92
+ error: "Socket not found."
93
+ });
94
+ return;
95
+ }
96
+ ctrl.handlePutFileData({
97
+ scriptName,
98
+ relativePath,
99
+ data: req.body,
100
+ }, (result) => {
101
+ if (result.error) {
102
+ res.json({
103
+ error: result.error
104
+ });
105
+ return;
106
+ }
107
+ res.json({
108
+ success: true
109
+ });
110
+ });
111
+ };
74
112
  const syncScriptFromClient = (req, res) => {
75
113
  const params = req.body;
76
114
  const socketId = params.socketId;
@@ -121,6 +159,7 @@ function initHttpRouter(app) {
121
159
  app.use(express_1.default.urlencoded({ extended: true }));
122
160
  app.get("/getFileContent", getFileContent);
123
161
  app.get("/getFileData", getFileData);
162
+ app.post("/putFileData", express_1.default.raw({ type: 'application/octet-stream', limit: '100mb' }), putFileData);
124
163
  app.post("/syncScriptFromClient", syncScriptFromClient);
125
164
  app.post("/syncScriptFromServer", syncScriptFromServer);
126
165
  }
package/dist/types.d.ts CHANGED
@@ -8,6 +8,8 @@ export interface ScriptingConfig {
8
8
  autoOpen?: boolean;
9
9
  generateTsConfig?: boolean;
10
10
  logLevel?: LogLevel;
11
+ /** Extra script-relative paths, directory/file names, or `*.ext` globs to exclude from syncing. */
12
+ ignore?: string[];
11
13
  }
12
14
  export type ResolvedConfig = Required<Omit<ScriptingConfig, 'editorCommand' | 'editorArgs'>> & {
13
15
  editorCommand?: string;
package/dist/util.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function md5(content: string): string;
2
+ export declare function isBinaryFile(filePath: string): boolean;
2
3
  export declare function getPath(filename: string): string;
3
4
  export declare function getScriptPath(filename?: string): string;
4
5
  export declare function createTsConfig(): void;
@@ -6,4 +7,5 @@ export declare function ensureScriptsDirectory(): void;
6
7
  export declare function ensureDirectoryExistence(filePath: string): void;
7
8
  export declare function writeDtsFiles(files: Record<string, string>): Promise<void>;
8
9
  export declare function getRelativePath(from: string, to: string): string;
10
+ export declare function isIgnoredPath(relativePath: string, patterns?: string[]): boolean;
9
11
  export declare function migrateOldFiles(): Promise<void>;
package/dist/util.js CHANGED
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.md5 = md5;
7
+ exports.isBinaryFile = isBinaryFile;
7
8
  exports.getPath = getPath;
8
9
  exports.getScriptPath = getScriptPath;
9
10
  exports.createTsConfig = createTsConfig;
@@ -11,6 +12,7 @@ exports.ensureScriptsDirectory = ensureScriptsDirectory;
11
12
  exports.ensureDirectoryExistence = ensureDirectoryExistence;
12
13
  exports.writeDtsFiles = writeDtsFiles;
13
14
  exports.getRelativePath = getRelativePath;
15
+ exports.isIgnoredPath = isIgnoredPath;
14
16
  exports.migrateOldFiles = migrateOldFiles;
15
17
  const path_1 = __importDefault(require("path"));
16
18
  const crypto_1 = __importDefault(require("crypto"));
@@ -23,6 +25,68 @@ function md5(content) {
23
25
  .update(content)
24
26
  .digest('hex');
25
27
  }
28
+ // Drop an incomplete trailing UTF-8 multibyte sequence so a text file whose
29
+ // character happens to straddle the read boundary isn't misjudged as binary.
30
+ function trimIncompleteUtf8Tail(chunk) {
31
+ let i = chunk.length - 1;
32
+ let cont = 0;
33
+ // Walk back over continuation bytes (10xxxxxx).
34
+ while (i >= 0 && (chunk[i] & 0xc0) === 0x80) {
35
+ cont++;
36
+ i--;
37
+ }
38
+ if (i < 0)
39
+ return chunk;
40
+ const lead = chunk[i];
41
+ let seqLen;
42
+ if ((lead & 0x80) === 0x00)
43
+ seqLen = 1; // ASCII
44
+ else if ((lead & 0xe0) === 0xc0)
45
+ seqLen = 2;
46
+ else if ((lead & 0xf0) === 0xe0)
47
+ seqLen = 3;
48
+ else if ((lead & 0xf8) === 0xf0)
49
+ seqLen = 4;
50
+ else
51
+ return chunk; // invalid lead — let the decoder flag it
52
+ // Fewer bytes than the sequence needs → it's cut off; trim it.
53
+ if (cont + 1 < seqLen) {
54
+ return chunk.subarray(0, i);
55
+ }
56
+ return chunk;
57
+ }
58
+ // Detect whether a file is binary by content instead of extension.
59
+ // Reads the first 1024 bytes: a NUL byte or non-UTF-8 content means binary.
60
+ // Conceptually mirrors FileManagerService.isBinaryFileSync on the iOS side.
61
+ function isBinaryFile(filePath) {
62
+ let fd = null;
63
+ try {
64
+ fd = fs_1.default.openSync(filePath, 'r');
65
+ const buffer = Buffer.alloc(1024);
66
+ const bytesRead = fs_1.default.readSync(fd, buffer, 0, 1024, 0);
67
+ const chunk = buffer.subarray(0, bytesRead);
68
+ if (chunk.includes(0)) {
69
+ return true;
70
+ }
71
+ try {
72
+ new TextDecoder('utf-8', { fatal: true }).decode(trimIncompleteUtf8Tail(chunk));
73
+ return false;
74
+ }
75
+ catch {
76
+ return true;
77
+ }
78
+ }
79
+ catch (err) {
80
+ console.log(chalk_1.default.red(`Failed to inspect file: ${filePath}, ${err}`));
81
+ // On failure, fall back to treating it as text (previous default behavior).
82
+ return false;
83
+ }
84
+ finally {
85
+ if (fd != null) {
86
+ fs_1.default.closeSync(fd);
87
+ }
88
+ }
89
+ }
26
90
  function getPath(filename) {
27
91
  return path_1.default.join(process.cwd(), filename);
28
92
  }
@@ -59,8 +123,11 @@ function createTsConfig() {
59
123
  }
60
124
  },
61
125
  "include": [
62
- "./dts/${const_1.globalDtsFileName}",
126
+ "./dts/*.d.ts",
63
127
  "scripts"
128
+ ],
129
+ "exclude": [
130
+ "./dts/${const_1.scriptingDtsFileName}"
64
131
  ]
65
132
  }`;
66
133
  const filePath = getPath('tsconfig.json');
@@ -68,20 +135,35 @@ function createTsConfig() {
68
135
  if (!fs_1.default.existsSync(filePath)) {
69
136
  fs_1.default.writeFileSync(filePath, tsconfigContent);
70
137
  console.log(chalk_1.default.green('tsconfig.json created.'));
138
+ return;
139
+ }
140
+ // Migrate an existing tsconfig to the current include/exclude shape:
141
+ // - all ambient dts are picked up via the "./dts/*.d.ts" glob (so newly synced
142
+ // node.d.ts / web-fetch.d.ts / safari-ext.d.ts are covered automatically),
143
+ // - scripting.d.ts is excluded from the root program (it stays importable via
144
+ // the "scripting" paths mapping). Existing custom entries are preserved.
145
+ const tsconfig = JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
146
+ const include = Array.isArray(tsconfig.include) ? [...tsconfig.include] : [];
147
+ const exclude = Array.isArray(tsconfig.exclude) ? [...tsconfig.exclude] : [];
148
+ // Drop the stale explicit global entry now covered by the glob.
149
+ let nextInclude = include.filter((x) => x !== `./dts/${const_1.globalDtsFileName}`);
150
+ if (!nextInclude.includes('./dts/*.d.ts'))
151
+ nextInclude.unshift('./dts/*.d.ts');
152
+ if (!nextInclude.includes('scripts'))
153
+ nextInclude.push('scripts');
154
+ const nextExclude = exclude.includes(`./dts/${const_1.scriptingDtsFileName}`)
155
+ ? exclude
156
+ : [...exclude, `./dts/${const_1.scriptingDtsFileName}`];
157
+ const includeChanged = JSON.stringify(nextInclude) !== JSON.stringify(include);
158
+ const excludeChanged = JSON.stringify(nextExclude) !== JSON.stringify(exclude);
159
+ if (includeChanged || excludeChanged) {
160
+ tsconfig.include = nextInclude;
161
+ tsconfig.exclude = nextExclude;
162
+ fs_1.default.writeFileSync(filePath, JSON.stringify(tsconfig, null, 2));
163
+ console.log(chalk_1.default.yellow('tsconfig.json updated to sync all ambient type declarations.'));
71
164
  }
72
165
  else {
73
- const tsconfig = JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
74
- if (tsconfig.include && !tsconfig.include.includes('scripts')) {
75
- tsconfig.include = [
76
- `./dts/${const_1.globalDtsFileName}`,
77
- "scripts"
78
- ];
79
- fs_1.default.writeFileSync(filePath, JSON.stringify(tsconfig, null, 2));
80
- console.log(chalk_1.default.yellow('tsconfig.json updated to include scripts directory.'));
81
- }
82
- else {
83
- console.log(chalk_1.default.gray('tsconfig.json already exists.'));
84
- }
166
+ console.log(chalk_1.default.gray('tsconfig.json already exists.'));
85
167
  }
86
168
  }
87
169
  function ensureScriptsDirectory() {
@@ -102,6 +184,14 @@ function ensureDirectoryExistence(filePath) {
102
184
  }
103
185
  async function writeDtsFiles(files) {
104
186
  await Promise.all(Object.entries(files).map(async ([filename, content]) => {
187
+ // These names arrive over the wire; only allow a plain `.d.ts` basename so
188
+ // a crafted name can't escape the dts/ directory.
189
+ if (typeof content !== 'string' ||
190
+ filename !== path_1.default.basename(filename) ||
191
+ !filename.endsWith('.d.ts')) {
192
+ console.log(chalk_1.default.red(`Skipped writing dts file with unexpected name: ${filename}`));
193
+ return;
194
+ }
105
195
  const filePath = getPath(`dts/${filename}`);
106
196
  ensureDirectoryExistence(filePath);
107
197
  await fs_1.default
@@ -116,6 +206,43 @@ function getRelativePath(from, to) {
116
206
  const relativePath = path_1.default.relative(from, to);
117
207
  return relativePath.replaceAll("\\", "/");
118
208
  }
209
+ // Whether a script-relative path should be excluded from syncing.
210
+ // Always ignores dotfiles/dot-directories and node_modules; `patterns` adds
211
+ // user-configured excludes. Kept in sync with the iOS default ignore logic.
212
+ function isIgnoredPath(relativePath, patterns = []) {
213
+ const normalized = relativePath.replaceAll("\\", "/");
214
+ const segments = normalized.split("/").filter(Boolean);
215
+ // Default ignores.
216
+ for (const seg of segments) {
217
+ if (seg.startsWith(".") || seg === "node_modules") {
218
+ return true;
219
+ }
220
+ }
221
+ // User-configured patterns.
222
+ const basename = segments[segments.length - 1] ?? "";
223
+ for (const raw of patterns) {
224
+ const pattern = raw.trim();
225
+ if (!pattern)
226
+ continue;
227
+ if (pattern.startsWith("*.")) {
228
+ // Extension match, e.g. "*.log".
229
+ if (basename.endsWith(pattern.slice(1)))
230
+ return true;
231
+ }
232
+ else if (pattern.includes("/")) {
233
+ // Path match (relative), prefix or exact.
234
+ const p = pattern.replace(/^\/+|\/+$/g, "");
235
+ if (normalized === p || normalized.startsWith(p + "/"))
236
+ return true;
237
+ }
238
+ else {
239
+ // Directory or file name match on any segment.
240
+ if (segments.includes(pattern))
241
+ return true;
242
+ }
243
+ }
244
+ return false;
245
+ }
119
246
  async function migrateOldFiles() {
120
247
  try {
121
248
  const list = await fs_1.default.promises.readdir(process.cwd());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scripting-cli",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "This plugin integrates the desktop editor synchronization feature of the Scripting app.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",