scripting-cli 1.3.0 → 1.4.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 +29 -1
- package/dist/config.js +3 -0
- package/dist/controller.d.ts +8 -0
- package/dist/controller.js +50 -8
- package/dist/http_router.js +39 -0
- package/dist/types.d.ts +2 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +101 -0
- package/package.json +1 -1
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"
|
|
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) {
|
package/dist/controller.d.ts
CHANGED
|
@@ -50,6 +50,14 @@ export declare class Controller {
|
|
|
50
50
|
error?: string;
|
|
51
51
|
content?: string;
|
|
52
52
|
}) => void) => Promise<void>;
|
|
53
|
+
handlePutFileData: (data: {
|
|
54
|
+
scriptName: string;
|
|
55
|
+
relativePath: string;
|
|
56
|
+
data: Buffer;
|
|
57
|
+
}, ack: (result: {
|
|
58
|
+
error?: string;
|
|
59
|
+
success?: boolean;
|
|
60
|
+
}) => void) => Promise<void>;
|
|
53
61
|
handleGetFilePath: (data: {
|
|
54
62
|
scriptName: string;
|
|
55
63
|
relativePath: string;
|
package/dist/controller.js
CHANGED
|
@@ -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 (
|
|
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) => {
|
|
@@ -121,6 +121,9 @@ class Controller {
|
|
|
121
121
|
const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
|
|
122
122
|
(0, util_1.ensureDirectoryExistence)(scriptDir);
|
|
123
123
|
await Promise.all(Object.entries(data.scriptFiles).map(async ([filename, content]) => {
|
|
124
|
+
if ((0, util_1.isIgnoredPath)(filename, this.config.ignore)) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
124
127
|
const filePath = path_1.default.join(scriptDir, filename);
|
|
125
128
|
(0, util_1.ensureDirectoryExistence)(filePath);
|
|
126
129
|
await fs_1.default.promises.writeFile(filePath, content);
|
|
@@ -176,15 +179,15 @@ class Controller {
|
|
|
176
179
|
const readDir = async (dir) => {
|
|
177
180
|
const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
|
|
178
181
|
await Promise.all(files.map(async (file) => {
|
|
179
|
-
|
|
180
|
-
|
|
182
|
+
const filePath = path_1.default.join(dir, file.name);
|
|
183
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
184
|
+
// Skip default-ignored (dotfiles, node_modules) and user-configured paths.
|
|
185
|
+
if ((0, util_1.isIgnoredPath)(relativePath, this.config.ignore)) {
|
|
181
186
|
return;
|
|
182
187
|
}
|
|
183
|
-
//
|
|
188
|
+
// Classify by content: text files sync with hash, others as binary.
|
|
184
189
|
if (file.isFile()) {
|
|
185
|
-
|
|
186
|
-
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
187
|
-
if (file.name.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
190
|
+
if (!(0, util_1.isBinaryFile)(filePath)) {
|
|
188
191
|
const content = await fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
189
192
|
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
190
193
|
});
|
|
@@ -274,6 +277,45 @@ class Controller {
|
|
|
274
277
|
});
|
|
275
278
|
}
|
|
276
279
|
};
|
|
280
|
+
handlePutFileData = async (data, ack) => {
|
|
281
|
+
if (this.scriptName !== data.scriptName) {
|
|
282
|
+
ack({
|
|
283
|
+
error: `Script ${data.scriptName} is not initialized`,
|
|
284
|
+
});
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if ((0, util_1.isIgnoredPath)(data.relativePath, this.config.ignore)) {
|
|
288
|
+
ack({
|
|
289
|
+
error: `Ignored file path: ${data.relativePath}`,
|
|
290
|
+
});
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
|
|
294
|
+
const filePath = path_1.default.join(scriptDir, data.relativePath);
|
|
295
|
+
// Prevent path traversal: the resolved path must stay inside scriptDir.
|
|
296
|
+
const resolved = path_1.default.resolve(filePath);
|
|
297
|
+
if (resolved !== path_1.default.resolve(scriptDir) &&
|
|
298
|
+
!resolved.startsWith(path_1.default.resolve(scriptDir) + path_1.default.sep)) {
|
|
299
|
+
ack({
|
|
300
|
+
error: `Invalid file path: ${data.relativePath}`,
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
(0, util_1.ensureDirectoryExistence)(filePath);
|
|
306
|
+
await fs_1.default.promises.writeFile(filePath, data.data);
|
|
307
|
+
console.log(`[${chalk_1.default.bold.blue(data.scriptName)}] File uploaded: ${data.relativePath}`);
|
|
308
|
+
ack({
|
|
309
|
+
success: true,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
console.log(chalk_1.default.red(`Error writing uploaded file: ${e}`));
|
|
314
|
+
ack({
|
|
315
|
+
error: `Failed to write uploaded file.\n${e}`,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
};
|
|
277
319
|
handleGetFilePath = async (data, ack) => {
|
|
278
320
|
if (this.scriptName !== data.scriptName) {
|
|
279
321
|
ack({
|
package/dist/http_router.js
CHANGED
|
@@ -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
|
}
|
|
@@ -116,6 +180,43 @@ function getRelativePath(from, to) {
|
|
|
116
180
|
const relativePath = path_1.default.relative(from, to);
|
|
117
181
|
return relativePath.replaceAll("\\", "/");
|
|
118
182
|
}
|
|
183
|
+
// Whether a script-relative path should be excluded from syncing.
|
|
184
|
+
// Always ignores dotfiles/dot-directories and node_modules; `patterns` adds
|
|
185
|
+
// user-configured excludes. Kept in sync with the iOS default ignore logic.
|
|
186
|
+
function isIgnoredPath(relativePath, patterns = []) {
|
|
187
|
+
const normalized = relativePath.replaceAll("\\", "/");
|
|
188
|
+
const segments = normalized.split("/").filter(Boolean);
|
|
189
|
+
// Default ignores.
|
|
190
|
+
for (const seg of segments) {
|
|
191
|
+
if (seg.startsWith(".") || seg === "node_modules") {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// User-configured patterns.
|
|
196
|
+
const basename = segments[segments.length - 1] ?? "";
|
|
197
|
+
for (const raw of patterns) {
|
|
198
|
+
const pattern = raw.trim();
|
|
199
|
+
if (!pattern)
|
|
200
|
+
continue;
|
|
201
|
+
if (pattern.startsWith("*.")) {
|
|
202
|
+
// Extension match, e.g. "*.log".
|
|
203
|
+
if (basename.endsWith(pattern.slice(1)))
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
else if (pattern.includes("/")) {
|
|
207
|
+
// Path match (relative), prefix or exact.
|
|
208
|
+
const p = pattern.replace(/^\/+|\/+$/g, "");
|
|
209
|
+
if (normalized === p || normalized.startsWith(p + "/"))
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
// Directory or file name match on any segment.
|
|
214
|
+
if (segments.includes(pattern))
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
119
220
|
async function migrateOldFiles() {
|
|
120
221
|
try {
|
|
121
222
|
const list = await fs_1.default.promises.readdir(process.cwd());
|