codechart 0.1.2 → 0.1.3
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/dist/cli.js +48 -22
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -8,11 +8,27 @@ import path2 from "path";
|
|
|
8
8
|
import { Command } from "commander";
|
|
9
9
|
|
|
10
10
|
// src/config/load.ts
|
|
11
|
-
import { existsSync,
|
|
11
|
+
import { existsSync, realpathSync } from "fs";
|
|
12
12
|
import { homedir } from "os";
|
|
13
13
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "path";
|
|
14
14
|
import { parse } from "yaml";
|
|
15
15
|
|
|
16
|
+
// src/read-regular.ts
|
|
17
|
+
import { closeSync, constants, fstatSync, openSync, readSync } from "fs";
|
|
18
|
+
function readRegularFile(path3, accept) {
|
|
19
|
+
const fd = openSync(path3, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0));
|
|
20
|
+
try {
|
|
21
|
+
const st = fstatSync(fd);
|
|
22
|
+
if (!accept(st)) return void 0;
|
|
23
|
+
const buf = Buffer.alloc(st.size);
|
|
24
|
+
let n = 0;
|
|
25
|
+
for (let r = 1; r > 0 && n < buf.length; n += r) r = readSync(fd, buf, n, buf.length - n, null);
|
|
26
|
+
return buf.toString("utf8", 0, n);
|
|
27
|
+
} finally {
|
|
28
|
+
closeSync(fd);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
// src/config/builtin-rules.ts
|
|
17
33
|
var MAP_RULES = [
|
|
18
34
|
{
|
|
@@ -185,18 +201,25 @@ function findRepoRoot(cwd) {
|
|
|
185
201
|
}
|
|
186
202
|
}
|
|
187
203
|
function readConfigFile(file, root) {
|
|
188
|
-
|
|
189
|
-
|
|
204
|
+
let real;
|
|
205
|
+
try {
|
|
206
|
+
real = realpathSync(file);
|
|
207
|
+
} catch (e) {
|
|
208
|
+
if (e.code === "ENOENT") return void 0;
|
|
209
|
+
throw e;
|
|
210
|
+
}
|
|
190
211
|
if (root !== void 0) {
|
|
191
212
|
const rel = relative(realpathSync(root), real);
|
|
192
213
|
if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) throw new Error(`${file}: resolves outside the repo`);
|
|
193
214
|
}
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
215
|
+
const text = readRegularFile(real, (st) => {
|
|
216
|
+
if (!st.isFile()) throw new Error(`${file}: not a regular file`);
|
|
217
|
+
if (st.size > MAX_BYTES) throw new Error(`${file}: too large (over 1 MB)`);
|
|
218
|
+
return true;
|
|
219
|
+
});
|
|
197
220
|
let raw;
|
|
198
221
|
try {
|
|
199
|
-
raw = parse(
|
|
222
|
+
raw = parse(text);
|
|
200
223
|
} catch (e) {
|
|
201
224
|
throw new Error(`${file}: ${e.message.split("\n")[0]}`);
|
|
202
225
|
}
|
|
@@ -742,7 +765,7 @@ function packageFile(relative3) {
|
|
|
742
765
|
}
|
|
743
766
|
|
|
744
767
|
// src/commands/copy-file.ts
|
|
745
|
-
import { constants, copyFileSync, existsSync as existsSync4, lstatSync, mkdirSync, unlinkSync } from "fs";
|
|
768
|
+
import { constants as constants2, copyFileSync, existsSync as existsSync4, lstatSync, mkdirSync, unlinkSync } from "fs";
|
|
746
769
|
import { dirname as dirname3 } from "path";
|
|
747
770
|
var isSymlink = (path3) => lstatSync(path3, { throwIfNoEntry: false })?.isSymbolicLink() === true;
|
|
748
771
|
function copyFiles(pairs, force, opts) {
|
|
@@ -764,7 +787,7 @@ function copyFiles(pairs, force, opts) {
|
|
|
764
787
|
for (const { source, target } of pairs) {
|
|
765
788
|
mkdirSync(dirname3(target), { recursive: true });
|
|
766
789
|
if (force && isSymlink(target)) unlinkSync(target);
|
|
767
|
-
copyFileSync(source, target, force ? 0 :
|
|
790
|
+
copyFileSync(source, target, force ? 0 : constants2.COPYFILE_EXCL);
|
|
768
791
|
opts.stdout(`wrote ${target}
|
|
769
792
|
`);
|
|
770
793
|
}
|
|
@@ -965,12 +988,12 @@ function installSkillCommand(opts) {
|
|
|
965
988
|
}
|
|
966
989
|
|
|
967
990
|
// src/commands/render.ts
|
|
968
|
-
import { chmodSync, constants as
|
|
991
|
+
import { chmodSync, constants as constants3, closeSync as closeSync2, fstatSync as fstatSync2, lstatSync as lstatSync4, mkdirSync as mkdirSync2, openSync as openSync2, readSync as readSync2, writeFileSync } from "fs";
|
|
969
992
|
import { basename as basename2, dirname as dirname4, resolve as resolve6 } from "path";
|
|
970
993
|
import { Command as Command5, Option } from "commander";
|
|
971
994
|
|
|
972
995
|
// src/render/flow/anchors.ts
|
|
973
|
-
import {
|
|
996
|
+
import { realpathSync as realpathSync2 } from "fs";
|
|
974
997
|
import { isAbsolute as isAbsolute2, relative as relative2, resolve as resolve3, sep as sep2 } from "path";
|
|
975
998
|
var SNIPPET_MAX_LINES = 15;
|
|
976
999
|
var MAX_BYTES2 = 5 * 1024 * 1024;
|
|
@@ -1008,12 +1031,15 @@ function readLines(path3, root, realRoot, budget) {
|
|
|
1008
1031
|
if (escapes(rel)) return { error: `${path3} is outside the repo` };
|
|
1009
1032
|
if (inGit(rel)) return { error: `${path3} is inside .git` };
|
|
1010
1033
|
try {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1034
|
+
let refused;
|
|
1035
|
+
const text = readRegularFile(real, (st) => {
|
|
1036
|
+
if (!st.isFile()) refused = { error: `${path3} is not a regular file` };
|
|
1037
|
+
else if (st.size > MAX_BYTES2) refused = { error: `${path3} is too large (over 5 MB)` };
|
|
1038
|
+
else if (st.size > budget.left) refused = overBudget(path3);
|
|
1039
|
+
else budget.left -= st.size;
|
|
1040
|
+
return refused === void 0;
|
|
1041
|
+
});
|
|
1042
|
+
return refused ?? toLines(text);
|
|
1017
1043
|
} catch (e) {
|
|
1018
1044
|
return { error: `${path3} cannot be read (${e.code})` };
|
|
1019
1045
|
}
|
|
@@ -2508,16 +2534,16 @@ async function readStdin() {
|
|
|
2508
2534
|
return Buffer.concat(chunks).toString("utf8");
|
|
2509
2535
|
}
|
|
2510
2536
|
function readSpecFile(file) {
|
|
2511
|
-
const fd =
|
|
2537
|
+
const fd = openSync2(file, constants3.O_RDONLY | (constants3.O_NONBLOCK ?? 0));
|
|
2512
2538
|
try {
|
|
2513
|
-
if (!
|
|
2539
|
+
if (!fstatSync2(fd).isFile()) throw new Error("not a regular file");
|
|
2514
2540
|
const buf = Buffer.alloc(MAX_SPEC_BYTES + 1);
|
|
2515
2541
|
let n = 0;
|
|
2516
|
-
for (let r = 1; r > 0 && n < buf.length; n += r) r =
|
|
2542
|
+
for (let r = 1; r > 0 && n < buf.length; n += r) r = readSync2(fd, buf, n, buf.length - n, null);
|
|
2517
2543
|
if (n > MAX_SPEC_BYTES) throw new Error(TOO_BIG);
|
|
2518
2544
|
return buf.toString("utf8", 0, n);
|
|
2519
2545
|
} finally {
|
|
2520
|
-
|
|
2546
|
+
closeSync2(fd);
|
|
2521
2547
|
}
|
|
2522
2548
|
}
|
|
2523
2549
|
function specOutput(config, ruleNames, diagram) {
|
|
@@ -2658,7 +2684,7 @@ function buildProgram(opts = {}) {
|
|
|
2658
2684
|
const stdout = opts.stdout ?? ((s) => process.stdout.write(s));
|
|
2659
2685
|
const rawStderr = opts.stderr ?? ((s) => process.stderr.write(s));
|
|
2660
2686
|
const stderr = (s) => rawStderr(sanitizeText(s));
|
|
2661
|
-
const program = new Command7().name("codechart").version("0.1.
|
|
2687
|
+
const program = new Command7().name("codechart").version("0.1.3");
|
|
2662
2688
|
const cmdOpts = { ...opts, stdout, stderr };
|
|
2663
2689
|
program.addCommand(checkCommand(cmdOpts));
|
|
2664
2690
|
program.addCommand(renderCommand(cmdOpts));
|