gitsheets 1.0.5 → 1.1.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/dist/cli/formats.d.ts +30 -0
- package/dist/cli/formats.d.ts.map +1 -0
- package/dist/cli/formats.js +145 -0
- package/dist/cli/formats.js.map +1 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +572 -40
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/path-template/index.d.ts +12 -0
- package/dist/path-template/index.d.ts.map +1 -1
- package/dist/path-template/index.js +50 -0
- package/dist/path-template/index.js.map +1 -1
- package/dist/repository.d.ts +9 -0
- package/dist/repository.d.ts.map +1 -1
- package/dist/repository.js +13 -4
- package/dist/repository.js.map +1 -1
- package/dist/sheet.d.ts +91 -0
- package/dist/sheet.d.ts.map +1 -1
- package/dist/sheet.js +371 -17
- package/dist/sheet.js.map +1 -1
- package/dist/transaction.d.ts +7 -3
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +6 -3
- package/dist/transaction.js.map +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RecordLike } from '../path-template/index.js';
|
|
2
|
+
export type InputFormat = 'json' | 'toml' | 'csv';
|
|
3
|
+
export type OutputFormat = 'json' | 'toml' | 'csv' | 'tsv';
|
|
4
|
+
/** Map a file extension (without the dot) to an input format. */
|
|
5
|
+
export declare function inferInputFormat(input: string | undefined): InputFormat;
|
|
6
|
+
/**
|
|
7
|
+
* Parse input text into an array of records, given the format.
|
|
8
|
+
*
|
|
9
|
+
* - JSON: a single object, an array, or JSONL (one object per line).
|
|
10
|
+
* - TOML: either a single record (the document maps to one record), an array
|
|
11
|
+
* of records under `[[records]]`, or any top-level table where every value
|
|
12
|
+
* is itself a table (each becomes a record keyed by table name).
|
|
13
|
+
* - CSV: the first row is the header. Each subsequent row is one record.
|
|
14
|
+
* Numeric strings stay as strings — type coercion belongs in the consumer
|
|
15
|
+
* schema, not in CSV parsing.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseRecords(text: string, format: InputFormat): RecordLike[];
|
|
18
|
+
/**
|
|
19
|
+
* Stringify a single record into one piece of output text for the given
|
|
20
|
+
* format. For CSV / TSV, the caller drives header emission; this function
|
|
21
|
+
* formats one row only.
|
|
22
|
+
*/
|
|
23
|
+
export declare function stringifyRecord_text(record: RecordLike, format: OutputFormat, fields?: readonly string[]): string;
|
|
24
|
+
/**
|
|
25
|
+
* Emit a CSV / TSV header row from a list of column names.
|
|
26
|
+
*/
|
|
27
|
+
export declare function csvHeader(columns: readonly string[], format: 'csv' | 'tsv'): string;
|
|
28
|
+
export declare function validateInputFormat(s: string | undefined): InputFormat | undefined;
|
|
29
|
+
export declare function validateOutputFormat(s: string | undefined): OutputFormat | undefined;
|
|
30
|
+
//# sourceMappingURL=formats.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../../src/cli/formats.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;AAClD,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;AAE3D,iEAAiE;AACjE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,CAMvE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,UAAU,EAAE,CAoD5E;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,GACzB,MAAM,CAWR;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAKnF;AA2BD,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,CAIlF;AAED,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,YAAY,GAAG,SAAS,CAIpF"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// Input/output format adapters for the CLI (#145).
|
|
2
|
+
//
|
|
3
|
+
// `upsert` accepts JSON | TOML | CSV input.
|
|
4
|
+
// `query` and `read` produce JSON | TOML | CSV | TSV output.
|
|
5
|
+
// Encoding (`--encoding`) controls how on-disk inputs are decoded.
|
|
6
|
+
import { parse as csvParse } from 'csv-parse/sync';
|
|
7
|
+
import { stringify as csvStringify } from 'csv-stringify/sync';
|
|
8
|
+
import { stringifyRecord, parseToml } from '../toml.js';
|
|
9
|
+
import { RECORD_PATH_KEY, RECORD_SHEET_KEY } from '../sheet.js';
|
|
10
|
+
/** Map a file extension (without the dot) to an input format. */
|
|
11
|
+
export function inferInputFormat(input) {
|
|
12
|
+
if (!input || input === '-')
|
|
13
|
+
return 'json';
|
|
14
|
+
const lower = input.toLowerCase();
|
|
15
|
+
if (lower.endsWith('.toml'))
|
|
16
|
+
return 'toml';
|
|
17
|
+
if (lower.endsWith('.csv'))
|
|
18
|
+
return 'csv';
|
|
19
|
+
return 'json';
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse input text into an array of records, given the format.
|
|
23
|
+
*
|
|
24
|
+
* - JSON: a single object, an array, or JSONL (one object per line).
|
|
25
|
+
* - TOML: either a single record (the document maps to one record), an array
|
|
26
|
+
* of records under `[[records]]`, or any top-level table where every value
|
|
27
|
+
* is itself a table (each becomes a record keyed by table name).
|
|
28
|
+
* - CSV: the first row is the header. Each subsequent row is one record.
|
|
29
|
+
* Numeric strings stay as strings — type coercion belongs in the consumer
|
|
30
|
+
* schema, not in CSV parsing.
|
|
31
|
+
*/
|
|
32
|
+
export function parseRecords(text, format) {
|
|
33
|
+
const trimmed = text.trim();
|
|
34
|
+
if (!trimmed)
|
|
35
|
+
return [];
|
|
36
|
+
if (format === 'json') {
|
|
37
|
+
if (trimmed.startsWith('[')) {
|
|
38
|
+
const arr = JSON.parse(trimmed);
|
|
39
|
+
if (!Array.isArray(arr))
|
|
40
|
+
throw new Error('expected JSON array of records');
|
|
41
|
+
return arr;
|
|
42
|
+
}
|
|
43
|
+
if (trimmed.startsWith('{')) {
|
|
44
|
+
return [JSON.parse(trimmed)];
|
|
45
|
+
}
|
|
46
|
+
// JSONL fallback
|
|
47
|
+
return trimmed
|
|
48
|
+
.split('\n')
|
|
49
|
+
.filter((line) => line.trim().length > 0)
|
|
50
|
+
.map((line) => JSON.parse(line));
|
|
51
|
+
}
|
|
52
|
+
if (format === 'toml') {
|
|
53
|
+
const parsed = parseToml(text);
|
|
54
|
+
// [[records]] convention: an array of tables under the key "records"
|
|
55
|
+
if ('records' in parsed &&
|
|
56
|
+
Array.isArray(parsed.records)) {
|
|
57
|
+
return parsed.records;
|
|
58
|
+
}
|
|
59
|
+
// If every top-level value is a table, treat each as a record
|
|
60
|
+
const entries = Object.entries(parsed);
|
|
61
|
+
const allTables = entries.length > 0 &&
|
|
62
|
+
entries.every(([, v]) => typeof v === 'object' && v !== null && !Array.isArray(v) && !(v instanceof Date));
|
|
63
|
+
if (allTables) {
|
|
64
|
+
return entries.map(([, v]) => v);
|
|
65
|
+
}
|
|
66
|
+
// Otherwise, treat the document as a single record.
|
|
67
|
+
return [parsed];
|
|
68
|
+
}
|
|
69
|
+
// CSV
|
|
70
|
+
const rows = csvParse(text, {
|
|
71
|
+
columns: true,
|
|
72
|
+
skip_empty_lines: true,
|
|
73
|
+
relax_quotes: true,
|
|
74
|
+
trim: true,
|
|
75
|
+
});
|
|
76
|
+
return rows.map((row) => ({ ...row }));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Stringify a single record into one piece of output text for the given
|
|
80
|
+
* format. For CSV / TSV, the caller drives header emission; this function
|
|
81
|
+
* formats one row only.
|
|
82
|
+
*/
|
|
83
|
+
export function stringifyRecord_text(record, format, fields) {
|
|
84
|
+
const projected = projectRecord(record, fields);
|
|
85
|
+
if (format === 'json')
|
|
86
|
+
return `${JSON.stringify(projected)}\n`;
|
|
87
|
+
if (format === 'toml')
|
|
88
|
+
return stringifyRecord(stripGitsheetsSymbols(projected));
|
|
89
|
+
if (format === 'csv' || format === 'tsv') {
|
|
90
|
+
return csvStringify([projected], {
|
|
91
|
+
header: false,
|
|
92
|
+
delimiter: format === 'tsv' ? '\t' : ',',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`unsupported output format: ${String(format)}`);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Emit a CSV / TSV header row from a list of column names.
|
|
99
|
+
*/
|
|
100
|
+
export function csvHeader(columns, format) {
|
|
101
|
+
return csvStringify([columns], {
|
|
102
|
+
header: false,
|
|
103
|
+
delimiter: format === 'tsv' ? '\t' : ',',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Project a record to a subset of fields (in given order). When `fields` is
|
|
108
|
+
* undefined the record is returned with symbol keys stripped but otherwise
|
|
109
|
+
* unchanged.
|
|
110
|
+
*/
|
|
111
|
+
function projectRecord(record, fields) {
|
|
112
|
+
const cleaned = stripGitsheetsSymbols(record);
|
|
113
|
+
if (!fields)
|
|
114
|
+
return cleaned;
|
|
115
|
+
const out = {};
|
|
116
|
+
for (const field of fields) {
|
|
117
|
+
if (field in cleaned)
|
|
118
|
+
out[field] = cleaned[field];
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
function stripGitsheetsSymbols(record) {
|
|
123
|
+
// Object spread retains string-keyed enumerable props, drops symbols.
|
|
124
|
+
// Defensive: also delete the well-known symbols in case a future change
|
|
125
|
+
// produces them in non-spread ways.
|
|
126
|
+
const out = { ...record };
|
|
127
|
+
delete out[RECORD_PATH_KEY];
|
|
128
|
+
delete out[RECORD_SHEET_KEY];
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
export function validateInputFormat(s) {
|
|
132
|
+
if (s === undefined)
|
|
133
|
+
return undefined;
|
|
134
|
+
if (s === 'json' || s === 'toml' || s === 'csv')
|
|
135
|
+
return s;
|
|
136
|
+
throw new Error(`--format must be one of: json, toml, csv (got "${s}")`);
|
|
137
|
+
}
|
|
138
|
+
export function validateOutputFormat(s) {
|
|
139
|
+
if (s === undefined)
|
|
140
|
+
return undefined;
|
|
141
|
+
if (s === 'json' || s === 'toml' || s === 'csv' || s === 'tsv')
|
|
142
|
+
return s;
|
|
143
|
+
throw new Error(`--format must be one of: json, toml, csv, tsv (got "${s}")`);
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=formats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/cli/formats.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,4CAA4C;AAC5C,6DAA6D;AAC7D,mEAAmE;AAEnE,OAAO,EAAE,KAAK,IAAI,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,SAAS,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAMhE,iEAAiE;AACjE,MAAM,UAAU,gBAAgB,CAAC,KAAyB;IACxD,IAAI,CAAC,KAAK,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,MAAM,CAAC;IAC3C,MAAM,KAA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AAC;IACtC,OAAQ,GAA+B,CAAC,eAAe,CAAC,CAAC;IACzD,OAAQ,GAA+B,CAAC,gBAAgB,CAAC,CAAC;IAC1D,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,CAAqB;IACvD,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACtC,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,CAAC,CAAC;IAC1D,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,IAAI,CAAC,CAAC;AAC3E,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,CAAqB;IACxD,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACtC,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,CAAC,CAAC;IACzE,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,IAAI,CAAC,CAAC;AAChF,CAAC"}
|
package/dist/cli/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AA0vBA,wBAAsB,IAAI,CAAC,IAAI,GAAE,MAAM,EAA0B,GAAG,OAAO,CAAC,MAAM,CAAC,CA6MlF"}
|