@jackwener/opencli 0.1.1 → 0.2.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.
Files changed (106) hide show
  1. package/README.md +9 -2
  2. package/README.zh-CN.md +9 -1
  3. package/SKILL.md +24 -0
  4. package/dist/bilibili.d.ts +6 -5
  5. package/dist/browser.d.ts +3 -1
  6. package/dist/browser.js +29 -2
  7. package/dist/cascade.d.ts +3 -2
  8. package/dist/clis/bbc/news.js +42 -0
  9. package/dist/clis/boss/search.d.ts +1 -0
  10. package/dist/clis/boss/search.js +47 -0
  11. package/dist/clis/ctrip/search.d.ts +1 -0
  12. package/dist/clis/ctrip/search.js +62 -0
  13. package/dist/clis/index.d.ts +8 -0
  14. package/dist/clis/index.js +16 -0
  15. package/dist/clis/reuters/search.d.ts +1 -0
  16. package/dist/clis/reuters/search.js +52 -0
  17. package/dist/clis/smzdm/search.d.ts +1 -0
  18. package/dist/clis/smzdm/search.js +66 -0
  19. package/dist/clis/weibo/hot.d.ts +1 -0
  20. package/dist/clis/weibo/hot.js +41 -0
  21. package/dist/clis/yahoo-finance/quote.d.ts +1 -0
  22. package/dist/clis/yahoo-finance/quote.js +74 -0
  23. package/dist/clis/youtube/search.d.ts +1 -0
  24. package/dist/clis/youtube/search.js +60 -0
  25. package/dist/engine.d.ts +2 -1
  26. package/dist/explore.js +1 -1
  27. package/dist/generate.js +2 -1
  28. package/dist/main.js +6 -4
  29. package/dist/output.d.ts +1 -1
  30. package/dist/output.js +12 -8
  31. package/dist/pipeline/executor.d.ts +9 -0
  32. package/dist/pipeline/executor.js +88 -0
  33. package/dist/pipeline/index.d.ts +5 -0
  34. package/dist/pipeline/index.js +5 -0
  35. package/dist/pipeline/steps/browser.d.ts +12 -0
  36. package/dist/pipeline/steps/browser.js +68 -0
  37. package/dist/pipeline/steps/fetch.d.ts +5 -0
  38. package/dist/pipeline/steps/fetch.js +50 -0
  39. package/dist/pipeline/steps/intercept.d.ts +5 -0
  40. package/dist/pipeline/steps/intercept.js +75 -0
  41. package/dist/pipeline/steps/tap.d.ts +12 -0
  42. package/dist/pipeline/steps/tap.js +130 -0
  43. package/dist/pipeline/steps/transform.d.ts +8 -0
  44. package/dist/pipeline/steps/transform.js +53 -0
  45. package/dist/pipeline/template.d.ts +16 -0
  46. package/dist/pipeline/template.js +172 -0
  47. package/dist/pipeline/template.test.d.ts +4 -0
  48. package/dist/pipeline/template.test.js +120 -0
  49. package/dist/pipeline/transform.test.d.ts +4 -0
  50. package/dist/pipeline/transform.test.js +90 -0
  51. package/dist/pipeline.d.ts +5 -7
  52. package/dist/pipeline.js +5 -549
  53. package/dist/registry.d.ts +3 -2
  54. package/dist/runtime.d.ts +2 -1
  55. package/dist/types.d.ts +27 -0
  56. package/dist/types.js +7 -0
  57. package/package.json +6 -3
  58. package/src/bilibili.ts +9 -7
  59. package/src/browser.ts +24 -3
  60. package/src/cascade.ts +3 -2
  61. package/src/clis/bbc/news.ts +42 -0
  62. package/src/clis/boss/search.ts +47 -0
  63. package/src/clis/ctrip/search.ts +62 -0
  64. package/src/clis/index.ts +24 -0
  65. package/src/clis/reuters/search.ts +52 -0
  66. package/src/clis/smzdm/search.ts +66 -0
  67. package/src/clis/weibo/hot.ts +41 -0
  68. package/src/clis/yahoo-finance/quote.ts +74 -0
  69. package/src/clis/youtube/search.ts +60 -0
  70. package/src/engine.ts +2 -1
  71. package/src/explore.ts +1 -1
  72. package/src/generate.ts +3 -1
  73. package/src/main.ts +7 -5
  74. package/src/output.ts +10 -6
  75. package/src/pipeline/executor.ts +98 -0
  76. package/src/pipeline/index.ts +6 -0
  77. package/src/pipeline/steps/browser.ts +67 -0
  78. package/src/pipeline/steps/fetch.ts +60 -0
  79. package/src/pipeline/steps/intercept.ts +78 -0
  80. package/src/pipeline/steps/tap.ts +137 -0
  81. package/src/pipeline/steps/transform.ts +50 -0
  82. package/src/pipeline/template.test.ts +125 -0
  83. package/src/pipeline/template.ts +157 -0
  84. package/src/pipeline/transform.test.ts +107 -0
  85. package/src/pipeline.ts +5 -529
  86. package/src/registry.ts +4 -2
  87. package/src/runtime.ts +3 -1
  88. package/src/types.ts +23 -0
  89. package/vitest.config.ts +7 -0
  90. package/dist/clis/github/search.js +0 -20
  91. package/dist/clis/github/trending.yaml +0 -58
  92. package/dist/promote.d.ts +0 -1
  93. package/dist/promote.js +0 -3
  94. package/dist/register.d.ts +0 -2
  95. package/dist/register.js +0 -2
  96. package/dist/scaffold.d.ts +0 -2
  97. package/dist/scaffold.js +0 -2
  98. package/dist/smoke.d.ts +0 -2
  99. package/dist/smoke.js +0 -2
  100. package/src/clis/github/search.ts +0 -21
  101. package/src/clis/github/trending.yaml +0 -58
  102. package/src/promote.ts +0 -3
  103. package/src/register.ts +0 -2
  104. package/src/scaffold.ts +0 -2
  105. package/src/smoke.ts +0 -2
  106. /package/dist/clis/{github/search.d.ts → bbc/news.d.ts} +0 -0
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Tests for the pipeline template engine: render, evalExpr, resolvePath.
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import { render, evalExpr, resolvePath, normalizeEvaluateSource } from './template.js';
7
+
8
+ describe('resolvePath', () => {
9
+ it('resolves args path', () => {
10
+ expect(resolvePath('args.limit', { args: { limit: 20 } })).toBe(20);
11
+ });
12
+ it('resolves nested args path', () => {
13
+ expect(resolvePath('args.query.keyword', { args: { query: { keyword: 'test' } } })).toBe('test');
14
+ });
15
+ it('resolves item path', () => {
16
+ expect(resolvePath('item.title', { item: { title: 'Hello' } })).toBe('Hello');
17
+ });
18
+ it('resolves implicit item path (no prefix)', () => {
19
+ expect(resolvePath('title', { item: { title: 'World' } })).toBe('World');
20
+ });
21
+ it('resolves index', () => {
22
+ expect(resolvePath('index', { index: 5 })).toBe(5);
23
+ });
24
+ it('resolves data path', () => {
25
+ expect(resolvePath('data.items', { data: { items: [1, 2, 3] } })).toEqual([1, 2, 3]);
26
+ });
27
+ it('returns null for missing path', () => {
28
+ expect(resolvePath('args.missing', { args: {} })).toBeUndefined();
29
+ });
30
+ it('resolves array index', () => {
31
+ expect(resolvePath('data.0', { data: ['a', 'b'] })).toBe('a');
32
+ });
33
+ });
34
+
35
+ describe('evalExpr', () => {
36
+ it('evaluates default filter', () => {
37
+ expect(evalExpr('args.limit | default(20)', { args: {} })).toBe(20);
38
+ });
39
+ it('uses actual value over default', () => {
40
+ expect(evalExpr('args.limit | default(20)', { args: { limit: 10 } })).toBe(10);
41
+ });
42
+ it('evaluates string default', () => {
43
+ expect(evalExpr("args.name | default('unknown')", { args: {} })).toBe('unknown');
44
+ });
45
+ it('evaluates arithmetic: index + 1', () => {
46
+ expect(evalExpr('index + 1', { index: 0 })).toBe(1);
47
+ });
48
+ it('evaluates arithmetic: index * 2', () => {
49
+ expect(evalExpr('index * 2', { index: 5 })).toBe(10);
50
+ });
51
+ it('evaluates || fallback', () => {
52
+ expect(evalExpr("item.name || 'N/A'", { item: {} })).toBe('N/A');
53
+ });
54
+ it('evaluates || with truthy left', () => {
55
+ expect(evalExpr("item.name || 'N/A'", { item: { name: 'Alice' } })).toBe('Alice');
56
+ });
57
+ it('resolves simple path', () => {
58
+ expect(evalExpr('item.title', { item: { title: 'Test' } })).toBe('Test');
59
+ });
60
+ it('applies join filter', () => {
61
+ expect(evalExpr('item.tags | join(,)', { item: { tags: ['a', 'b', 'c'] } })).toBe('a,b,c');
62
+ });
63
+ it('applies upper filter', () => {
64
+ expect(evalExpr('item.name | upper', { item: { name: 'hello' } })).toBe('HELLO');
65
+ });
66
+ it('applies lower filter', () => {
67
+ expect(evalExpr('item.name | lower', { item: { name: 'HELLO' } })).toBe('hello');
68
+ });
69
+ it('applies truncate filter', () => {
70
+ expect(evalExpr('item.text | truncate(5)', { item: { text: 'Hello World!' } })).toBe('Hello...');
71
+ });
72
+ it('chains filters', () => {
73
+ expect(evalExpr('item.name | upper | truncate(3)', { item: { name: 'hello' } })).toBe('HEL...');
74
+ });
75
+ it('applies length filter', () => {
76
+ expect(evalExpr('item.items | length', { item: { items: [1, 2, 3] } })).toBe(3);
77
+ });
78
+ });
79
+
80
+ describe('render', () => {
81
+ it('renders full expression', () => {
82
+ expect(render('${{ args.limit }}', { args: { limit: 30 } })).toBe(30);
83
+ });
84
+ it('renders inline expression in string', () => {
85
+ expect(render('Hello ${{ item.name }}!', { item: { name: 'World' } })).toBe('Hello World!');
86
+ });
87
+ it('renders multiple inline expressions', () => {
88
+ expect(render('${{ item.first }}-${{ item.second }}', { item: { first: 'X', second: 'Y' } })).toBe('X-Y');
89
+ });
90
+ it('returns non-string values as-is', () => {
91
+ expect(render(42, {})).toBe(42);
92
+ expect(render(null, {})).toBeNull();
93
+ expect(render(undefined, {})).toBeUndefined();
94
+ });
95
+ it('returns full expression result as native type', () => {
96
+ expect(render('${{ args.list }}', { args: { list: [1, 2, 3] } })).toEqual([1, 2, 3]);
97
+ });
98
+ it('renders URL template', () => {
99
+ expect(render('https://api.example.com/search?q=${{ args.keyword }}', { args: { keyword: 'test' } })).toBe('https://api.example.com/search?q=test');
100
+ });
101
+ });
102
+
103
+ describe('normalizeEvaluateSource', () => {
104
+ it('wraps bare expression', () => {
105
+ expect(normalizeEvaluateSource('document.title')).toBe('() => (document.title)');
106
+ });
107
+ it('passes through arrow function', () => {
108
+ expect(normalizeEvaluateSource('() => 42')).toBe('() => 42');
109
+ });
110
+ it('passes through async arrow function', () => {
111
+ const src = 'async () => { return 1; }';
112
+ expect(normalizeEvaluateSource(src)).toBe(src);
113
+ });
114
+ it('passes through named function', () => {
115
+ const src = 'function foo() { return 1; }';
116
+ expect(normalizeEvaluateSource(src)).toBe(src);
117
+ });
118
+ it('wraps IIFE pattern', () => {
119
+ const src = '(async () => { return 1; })()';
120
+ expect(normalizeEvaluateSource(src)).toBe(`() => (${src})`);
121
+ });
122
+ it('handles empty string', () => {
123
+ expect(normalizeEvaluateSource('')).toBe('() => undefined');
124
+ });
125
+ });
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Pipeline template engine: ${{ ... }} expression rendering.
3
+ */
4
+
5
+ export interface RenderContext {
6
+ args?: Record<string, any>;
7
+ data?: any;
8
+ item?: any;
9
+ index?: number;
10
+ }
11
+
12
+ export function render(template: any, ctx: RenderContext): any {
13
+ if (typeof template !== 'string') return template;
14
+ // Full expression: entire string is a single ${{ ... }}
15
+ // Use [^}] to prevent matching across }} boundaries (e.g. "${{ a }}-${{ b }}")
16
+ const fullMatch = template.match(/^\$\{\{\s*([^}]*(?:\}[^}][^}]*)*)\s*\}\}$/);
17
+ if (fullMatch && !template.includes('}}-') && !template.includes('}}${{')) return evalExpr(fullMatch[1].trim(), ctx);
18
+ // Check if the entire string is a single expression (no other text around it)
19
+ const singleExpr = template.match(/^\$\{\{\s*([\s\S]*?)\s*\}\}$/);
20
+ if (singleExpr) {
21
+ // Verify it's truly a single expression (no other ${{ inside)
22
+ const inner = singleExpr[1];
23
+ if (!inner.includes('${{')) return evalExpr(inner.trim(), ctx);
24
+ }
25
+ return template.replace(/\$\{\{\s*(.*?)\s*\}\}/g, (_m, expr) => String(evalExpr(expr.trim(), ctx)));
26
+ }
27
+
28
+ export function evalExpr(expr: string, ctx: RenderContext): any {
29
+ const args = ctx.args ?? {};
30
+ const item = ctx.item ?? {};
31
+ const data = ctx.data;
32
+ const index = ctx.index ?? 0;
33
+
34
+ // ── Pipe filters: expr | filter1(arg) | filter2 ──
35
+ // Supports: default(val), join(sep), upper, lower, truncate(n), trim, replace(old,new)
36
+ if (expr.includes('|') && !expr.includes('||')) {
37
+ const segments = expr.split('|').map(s => s.trim());
38
+ const mainExpr = segments[0];
39
+ let result = resolvePath(mainExpr, { args, item, data, index });
40
+ for (let i = 1; i < segments.length; i++) {
41
+ result = applyFilter(segments[i], result);
42
+ }
43
+ return result;
44
+ }
45
+
46
+ // Arithmetic: index + 1
47
+ const arithMatch = expr.match(/^([\w][\w.]*)\s*([+\-*/])\s*(\d+)$/);
48
+ if (arithMatch) {
49
+ const [, varName, op, numStr] = arithMatch;
50
+ const val = resolvePath(varName, { args, item, data, index });
51
+ if (val !== null && val !== undefined) {
52
+ const numVal = Number(val); const num = Number(numStr);
53
+ if (!isNaN(numVal)) {
54
+ switch (op) {
55
+ case '+': return numVal + num; case '-': return numVal - num;
56
+ case '*': return numVal * num; case '/': return num !== 0 ? numVal / num : 0;
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ // JS-like fallback expression: item.tweetCount || 'N/A'
63
+ const orMatch = expr.match(/^(.+?)\s*\|\|\s*(.+)$/);
64
+ if (orMatch) {
65
+ const left = evalExpr(orMatch[1].trim(), ctx);
66
+ if (left) return left;
67
+ const right = orMatch[2].trim();
68
+ return right.replace(/^['"]|['"]$/g, '');
69
+ }
70
+
71
+ return resolvePath(expr, { args, item, data, index });
72
+ }
73
+
74
+ /**
75
+ * Apply a named filter to a value.
76
+ * Supported filters:
77
+ * default(val), join(sep), upper, lower, truncate(n), trim,
78
+ * replace(old,new), keys, length, first, last
79
+ */
80
+ function applyFilter(filterExpr: string, value: any): any {
81
+ const match = filterExpr.match(/^(\w+)(?:\((.+)\))?$/);
82
+ if (!match) return value;
83
+ const [, name, rawArgs] = match;
84
+ const filterArg = rawArgs?.replace(/^['"]|['"]$/g, '') ?? '';
85
+
86
+ switch (name) {
87
+ case 'default': {
88
+ if (value === null || value === undefined || value === '') {
89
+ const intVal = parseInt(filterArg, 10);
90
+ if (!isNaN(intVal) && String(intVal) === filterArg.trim()) return intVal;
91
+ return filterArg;
92
+ }
93
+ return value;
94
+ }
95
+ case 'join':
96
+ return Array.isArray(value) ? value.join(filterArg || ', ') : value;
97
+ case 'upper':
98
+ return typeof value === 'string' ? value.toUpperCase() : value;
99
+ case 'lower':
100
+ return typeof value === 'string' ? value.toLowerCase() : value;
101
+ case 'trim':
102
+ return typeof value === 'string' ? value.trim() : value;
103
+ case 'truncate': {
104
+ const n = parseInt(filterArg, 10) || 50;
105
+ return typeof value === 'string' && value.length > n ? value.slice(0, n) + '...' : value;
106
+ }
107
+ case 'replace': {
108
+ if (typeof value !== 'string') return value;
109
+ const parts = rawArgs?.split(',').map(s => s.trim().replace(/^['"]|['"]$/g, '')) ?? [];
110
+ return parts.length >= 2 ? value.replaceAll(parts[0], parts[1]) : value;
111
+ }
112
+ case 'keys':
113
+ return value && typeof value === 'object' ? Object.keys(value) : value;
114
+ case 'length':
115
+ return Array.isArray(value) ? value.length : typeof value === 'string' ? value.length : value;
116
+ case 'first':
117
+ return Array.isArray(value) ? value[0] : value;
118
+ case 'last':
119
+ return Array.isArray(value) ? value[value.length - 1] : value;
120
+ default:
121
+ return value;
122
+ }
123
+ }
124
+
125
+ export function resolvePath(pathStr: string, ctx: RenderContext): any {
126
+ const args = ctx.args ?? {};
127
+ const item = ctx.item ?? {};
128
+ const data = ctx.data;
129
+ const index = ctx.index ?? 0;
130
+ const parts = pathStr.split('.');
131
+ const rootName = parts[0];
132
+ let obj: any; let rest: string[];
133
+ if (rootName === 'args') { obj = args; rest = parts.slice(1); }
134
+ else if (rootName === 'item') { obj = item; rest = parts.slice(1); }
135
+ else if (rootName === 'data') { obj = data; rest = parts.slice(1); }
136
+ else if (rootName === 'index') return index;
137
+ else { obj = item; rest = parts; }
138
+ for (const part of rest) {
139
+ if (obj && typeof obj === 'object' && !Array.isArray(obj)) obj = obj[part];
140
+ else if (Array.isArray(obj) && /^\d+$/.test(part)) obj = obj[parseInt(part, 10)];
141
+ else return null;
142
+ }
143
+ return obj;
144
+ }
145
+
146
+ /**
147
+ * Normalize JavaScript source for browser evaluate() calls.
148
+ */
149
+ export function normalizeEvaluateSource(source: string): string {
150
+ const stripped = source.trim();
151
+ if (!stripped) return '() => undefined';
152
+ if (stripped.startsWith('(') && stripped.endsWith(')()')) return `() => (${stripped})`;
153
+ if (/^(async\s+)?\([^)]*\)\s*=>/.test(stripped)) return stripped;
154
+ if (/^(async\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=>/.test(stripped)) return stripped;
155
+ if (stripped.startsWith('function ') || stripped.startsWith('async function ')) return stripped;
156
+ return `() => (${stripped})`;
157
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Tests for pipeline transform steps: select, map, filter, sort, limit.
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import { stepSelect, stepMap, stepFilter, stepSort, stepLimit } from './steps/transform.js';
7
+
8
+ const SAMPLE_DATA = [
9
+ { title: 'Alpha', score: 10, author: 'Alice' },
10
+ { title: 'Beta', score: 30, author: 'Bob' },
11
+ { title: 'Gamma', score: 20, author: 'Charlie' },
12
+ ];
13
+
14
+ describe('stepSelect', () => {
15
+ it('selects nested path', async () => {
16
+ const data = { result: { items: [1, 2, 3] } };
17
+ const result = await stepSelect(null, 'result.items', data, {});
18
+ expect(result).toEqual([1, 2, 3]);
19
+ });
20
+
21
+ it('selects array by index', async () => {
22
+ const data = { list: ['a', 'b', 'c'] };
23
+ const result = await stepSelect(null, 'list.1', data, {});
24
+ expect(result).toBe('b');
25
+ });
26
+
27
+ it('returns null for missing path', async () => {
28
+ const result = await stepSelect(null, 'missing.path', { foo: 1 }, {});
29
+ expect(result).toBeNull();
30
+ });
31
+
32
+ it('returns data as-is for non-object', async () => {
33
+ const result = await stepSelect(null, 'foo', 'string-data', {});
34
+ expect(result).toBe('string-data');
35
+ });
36
+ });
37
+
38
+ describe('stepMap', () => {
39
+ it('maps array items', async () => {
40
+ const result = await stepMap(null, {
41
+ name: '${{ item.title }}',
42
+ rank: '${{ index + 1 }}',
43
+ }, SAMPLE_DATA, {});
44
+ expect(result).toEqual([
45
+ { name: 'Alpha', rank: 1 },
46
+ { name: 'Beta', rank: 2 },
47
+ { name: 'Gamma', rank: 3 },
48
+ ]);
49
+ });
50
+
51
+ it('handles single object', async () => {
52
+ const result = await stepMap(null, {
53
+ name: '${{ item.title }}',
54
+ }, { title: 'Solo' }, {});
55
+ expect(result).toEqual([{ name: 'Solo' }]);
56
+ });
57
+
58
+ it('returns null/undefined as-is', async () => {
59
+ expect(await stepMap(null, { x: '${{ item.x }}' }, null, {})).toBeNull();
60
+ });
61
+ });
62
+
63
+ describe('stepFilter', () => {
64
+ it('filters by expression', async () => {
65
+ const result = await stepFilter(null, 'item.score', SAMPLE_DATA, {});
66
+ expect(result).toHaveLength(3); // all truthy
67
+ });
68
+
69
+ it('returns non-array as-is', async () => {
70
+ const result = await stepFilter(null, 'item.x', 'not-array', {});
71
+ expect(result).toBe('not-array');
72
+ });
73
+ });
74
+
75
+ describe('stepSort', () => {
76
+ it('sorts ascending by key', async () => {
77
+ const result = await stepSort(null, 'score', SAMPLE_DATA, {});
78
+ expect(result.map((r: any) => r.title)).toEqual(['Alpha', 'Gamma', 'Beta']);
79
+ });
80
+
81
+ it('sorts descending', async () => {
82
+ const result = await stepSort(null, { by: 'score', order: 'desc' }, SAMPLE_DATA, {});
83
+ expect(result.map((r: any) => r.title)).toEqual(['Beta', 'Gamma', 'Alpha']);
84
+ });
85
+
86
+ it('does not mutate original', async () => {
87
+ const original = [...SAMPLE_DATA];
88
+ await stepSort(null, 'score', SAMPLE_DATA, {});
89
+ expect(SAMPLE_DATA).toEqual(original);
90
+ });
91
+ });
92
+
93
+ describe('stepLimit', () => {
94
+ it('limits array to N items', async () => {
95
+ const result = await stepLimit(null, '2', SAMPLE_DATA, {});
96
+ expect(result).toHaveLength(2);
97
+ });
98
+
99
+ it('limits using template expression', async () => {
100
+ const result = await stepLimit(null, '${{ args.limit }}', SAMPLE_DATA, { limit: 1 });
101
+ expect(result).toHaveLength(1);
102
+ });
103
+
104
+ it('returns non-array as-is', async () => {
105
+ expect(await stepLimit(null, '5', 'string', {})).toBe('string');
106
+ });
107
+ });