@jackwener/opencli 0.1.1 → 0.1.2

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 (103) 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 +2 -1
  6. package/dist/browser.js +9 -1
  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/pipeline/executor.d.ts +9 -0
  30. package/dist/pipeline/executor.js +88 -0
  31. package/dist/pipeline/index.d.ts +5 -0
  32. package/dist/pipeline/index.js +5 -0
  33. package/dist/pipeline/steps/browser.d.ts +12 -0
  34. package/dist/pipeline/steps/browser.js +68 -0
  35. package/dist/pipeline/steps/fetch.d.ts +5 -0
  36. package/dist/pipeline/steps/fetch.js +50 -0
  37. package/dist/pipeline/steps/intercept.d.ts +5 -0
  38. package/dist/pipeline/steps/intercept.js +75 -0
  39. package/dist/pipeline/steps/tap.d.ts +12 -0
  40. package/dist/pipeline/steps/tap.js +130 -0
  41. package/dist/pipeline/steps/transform.d.ts +8 -0
  42. package/dist/pipeline/steps/transform.js +53 -0
  43. package/dist/pipeline/template.d.ts +16 -0
  44. package/dist/pipeline/template.js +115 -0
  45. package/dist/pipeline/template.test.d.ts +4 -0
  46. package/dist/pipeline/template.test.js +102 -0
  47. package/dist/pipeline/transform.test.d.ts +4 -0
  48. package/dist/pipeline/transform.test.js +90 -0
  49. package/dist/pipeline.d.ts +5 -7
  50. package/dist/pipeline.js +5 -549
  51. package/dist/registry.d.ts +3 -2
  52. package/dist/runtime.d.ts +2 -1
  53. package/dist/types.d.ts +27 -0
  54. package/dist/types.js +7 -0
  55. package/package.json +6 -3
  56. package/src/bilibili.ts +9 -7
  57. package/src/browser.ts +8 -2
  58. package/src/cascade.ts +3 -2
  59. package/src/clis/bbc/news.ts +42 -0
  60. package/src/clis/boss/search.ts +47 -0
  61. package/src/clis/ctrip/search.ts +62 -0
  62. package/src/clis/index.ts +24 -0
  63. package/src/clis/reuters/search.ts +52 -0
  64. package/src/clis/smzdm/search.ts +66 -0
  65. package/src/clis/weibo/hot.ts +41 -0
  66. package/src/clis/yahoo-finance/quote.ts +74 -0
  67. package/src/clis/youtube/search.ts +60 -0
  68. package/src/engine.ts +2 -1
  69. package/src/explore.ts +1 -1
  70. package/src/generate.ts +3 -1
  71. package/src/main.ts +7 -5
  72. package/src/pipeline/executor.ts +98 -0
  73. package/src/pipeline/index.ts +6 -0
  74. package/src/pipeline/steps/browser.ts +67 -0
  75. package/src/pipeline/steps/fetch.ts +60 -0
  76. package/src/pipeline/steps/intercept.ts +78 -0
  77. package/src/pipeline/steps/tap.ts +137 -0
  78. package/src/pipeline/steps/transform.ts +50 -0
  79. package/src/pipeline/template.test.ts +107 -0
  80. package/src/pipeline/template.ts +101 -0
  81. package/src/pipeline/transform.test.ts +107 -0
  82. package/src/pipeline.ts +5 -529
  83. package/src/registry.ts +4 -2
  84. package/src/runtime.ts +3 -1
  85. package/src/types.ts +23 -0
  86. package/vitest.config.ts +7 -0
  87. package/dist/clis/github/search.js +0 -20
  88. package/dist/clis/github/trending.yaml +0 -58
  89. package/dist/promote.d.ts +0 -1
  90. package/dist/promote.js +0 -3
  91. package/dist/register.d.ts +0 -2
  92. package/dist/register.js +0 -2
  93. package/dist/scaffold.d.ts +0 -2
  94. package/dist/scaffold.js +0 -2
  95. package/dist/smoke.d.ts +0 -2
  96. package/dist/smoke.js +0 -2
  97. package/src/clis/github/search.ts +0 -21
  98. package/src/clis/github/trending.yaml +0 -58
  99. package/src/promote.ts +0 -3
  100. package/src/register.ts +0 -2
  101. package/src/scaffold.ts +0 -2
  102. package/src/smoke.ts +0 -2
  103. /package/dist/clis/{github/search.d.ts → bbc/news.d.ts} +0 -0
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Pipeline steps: data transforms — select, map, filter, sort, limit.
3
+ */
4
+ import { render, evalExpr } from '../template.js';
5
+ export async function stepSelect(_page, params, data, args) {
6
+ const pathStr = String(render(params, { args, data }));
7
+ if (data && typeof data === 'object') {
8
+ let current = data;
9
+ for (const part of pathStr.split('.')) {
10
+ if (current && typeof current === 'object' && !Array.isArray(current))
11
+ current = current[part];
12
+ else if (Array.isArray(current) && /^\d+$/.test(part))
13
+ current = current[parseInt(part, 10)];
14
+ else
15
+ return null;
16
+ }
17
+ return current;
18
+ }
19
+ return data;
20
+ }
21
+ export async function stepMap(_page, params, data, args) {
22
+ if (!data || typeof data !== 'object')
23
+ return data;
24
+ let items = Array.isArray(data) ? data : [data];
25
+ if (!Array.isArray(data) && typeof data === 'object' && 'data' in data)
26
+ items = data.data;
27
+ const result = [];
28
+ for (let i = 0; i < items.length; i++) {
29
+ const item = items[i];
30
+ const row = {};
31
+ for (const [key, template] of Object.entries(params))
32
+ row[key] = render(template, { args, data, item, index: i });
33
+ result.push(row);
34
+ }
35
+ return result;
36
+ }
37
+ export async function stepFilter(_page, params, data, args) {
38
+ if (!Array.isArray(data))
39
+ return data;
40
+ return data.filter((item, i) => evalExpr(String(params), { args, item, index: i }));
41
+ }
42
+ export async function stepSort(_page, params, data, _args) {
43
+ if (!Array.isArray(data))
44
+ return data;
45
+ const key = typeof params === 'object' ? (params.by ?? '') : String(params);
46
+ const reverse = typeof params === 'object' ? params.order === 'desc' : false;
47
+ return [...data].sort((a, b) => { const va = a[key] ?? ''; const vb = b[key] ?? ''; const cmp = va < vb ? -1 : va > vb ? 1 : 0; return reverse ? -cmp : cmp; });
48
+ }
49
+ export async function stepLimit(_page, params, data, args) {
50
+ if (!Array.isArray(data))
51
+ return data;
52
+ return data.slice(0, Number(render(params, { args, data })));
53
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Pipeline template engine: ${{ ... }} expression rendering.
3
+ */
4
+ export interface RenderContext {
5
+ args?: Record<string, any>;
6
+ data?: any;
7
+ item?: any;
8
+ index?: number;
9
+ }
10
+ export declare function render(template: any, ctx: RenderContext): any;
11
+ export declare function evalExpr(expr: string, ctx: RenderContext): any;
12
+ export declare function resolvePath(pathStr: string, ctx: RenderContext): any;
13
+ /**
14
+ * Normalize JavaScript source for browser evaluate() calls.
15
+ */
16
+ export declare function normalizeEvaluateSource(source: string): string;
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Pipeline template engine: ${{ ... }} expression rendering.
3
+ */
4
+ export function render(template, ctx) {
5
+ if (typeof template !== 'string')
6
+ return template;
7
+ const fullMatch = template.match(/^\$\{\{\s*(.*?)\s*\}\}$/);
8
+ if (fullMatch)
9
+ return evalExpr(fullMatch[1].trim(), ctx);
10
+ return template.replace(/\$\{\{\s*(.*?)\s*\}\}/g, (_m, expr) => String(evalExpr(expr.trim(), ctx)));
11
+ }
12
+ export function evalExpr(expr, ctx) {
13
+ const args = ctx.args ?? {};
14
+ const item = ctx.item ?? {};
15
+ const data = ctx.data;
16
+ const index = ctx.index ?? 0;
17
+ // Default filter: args.limit | default(20)
18
+ if (expr.includes('|') && expr.includes('default(')) {
19
+ const [mainExpr, rest] = expr.split('|', 2);
20
+ const defaultMatch = rest.match(/default\((.+?)\)/);
21
+ const defaultVal = defaultMatch ? defaultMatch[1] : null;
22
+ const result = resolvePath(mainExpr.trim(), { args, item, data, index });
23
+ if (result === null || result === undefined) {
24
+ if (defaultVal !== null) {
25
+ const intVal = parseInt(defaultVal, 10);
26
+ if (!isNaN(intVal) && String(intVal) === defaultVal.trim())
27
+ return intVal;
28
+ return defaultVal.replace(/^['"]|['"]$/g, '');
29
+ }
30
+ }
31
+ return result;
32
+ }
33
+ // Arithmetic: index + 1
34
+ const arithMatch = expr.match(/^([\w][\w.]*)\s*([+\-*/])\s*(\d+)$/);
35
+ if (arithMatch) {
36
+ const [, varName, op, numStr] = arithMatch;
37
+ const val = resolvePath(varName, { args, item, data, index });
38
+ if (val !== null && val !== undefined) {
39
+ const numVal = Number(val);
40
+ const num = Number(numStr);
41
+ if (!isNaN(numVal)) {
42
+ switch (op) {
43
+ case '+': return numVal + num;
44
+ case '-': return numVal - num;
45
+ case '*': return numVal * num;
46
+ case '/': return num !== 0 ? numVal / num : 0;
47
+ }
48
+ }
49
+ }
50
+ }
51
+ // JS-like fallback expression: item.tweetCount || 'N/A'
52
+ const orMatch = expr.match(/^(.+?)\s*\|\|\s*(.+)$/);
53
+ if (orMatch) {
54
+ const left = evalExpr(orMatch[1].trim(), ctx);
55
+ if (left)
56
+ return left;
57
+ const right = orMatch[2].trim();
58
+ return right.replace(/^['"]|['"]$/g, '');
59
+ }
60
+ return resolvePath(expr, { args, item, data, index });
61
+ }
62
+ export function resolvePath(pathStr, ctx) {
63
+ const args = ctx.args ?? {};
64
+ const item = ctx.item ?? {};
65
+ const data = ctx.data;
66
+ const index = ctx.index ?? 0;
67
+ const parts = pathStr.split('.');
68
+ const rootName = parts[0];
69
+ let obj;
70
+ let rest;
71
+ if (rootName === 'args') {
72
+ obj = args;
73
+ rest = parts.slice(1);
74
+ }
75
+ else if (rootName === 'item') {
76
+ obj = item;
77
+ rest = parts.slice(1);
78
+ }
79
+ else if (rootName === 'data') {
80
+ obj = data;
81
+ rest = parts.slice(1);
82
+ }
83
+ else if (rootName === 'index')
84
+ return index;
85
+ else {
86
+ obj = item;
87
+ rest = parts;
88
+ }
89
+ for (const part of rest) {
90
+ if (obj && typeof obj === 'object' && !Array.isArray(obj))
91
+ obj = obj[part];
92
+ else if (Array.isArray(obj) && /^\d+$/.test(part))
93
+ obj = obj[parseInt(part, 10)];
94
+ else
95
+ return null;
96
+ }
97
+ return obj;
98
+ }
99
+ /**
100
+ * Normalize JavaScript source for browser evaluate() calls.
101
+ */
102
+ export function normalizeEvaluateSource(source) {
103
+ const stripped = source.trim();
104
+ if (!stripped)
105
+ return '() => undefined';
106
+ if (stripped.startsWith('(') && stripped.endsWith(')()'))
107
+ return `() => (${stripped})`;
108
+ if (/^(async\s+)?\([^)]*\)\s*=>/.test(stripped))
109
+ return stripped;
110
+ if (/^(async\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=>/.test(stripped))
111
+ return stripped;
112
+ if (stripped.startsWith('function ') || stripped.startsWith('async function '))
113
+ return stripped;
114
+ return `() => (${stripped})`;
115
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tests for the pipeline template engine: render, evalExpr, resolvePath.
3
+ */
4
+ export {};
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Tests for the pipeline template engine: render, evalExpr, resolvePath.
3
+ */
4
+ import { describe, it, expect } from 'vitest';
5
+ import { render, evalExpr, resolvePath, normalizeEvaluateSource } from './template.js';
6
+ describe('resolvePath', () => {
7
+ it('resolves args path', () => {
8
+ expect(resolvePath('args.limit', { args: { limit: 20 } })).toBe(20);
9
+ });
10
+ it('resolves nested args path', () => {
11
+ expect(resolvePath('args.query.keyword', { args: { query: { keyword: 'test' } } })).toBe('test');
12
+ });
13
+ it('resolves item path', () => {
14
+ expect(resolvePath('item.title', { item: { title: 'Hello' } })).toBe('Hello');
15
+ });
16
+ it('resolves implicit item path (no prefix)', () => {
17
+ expect(resolvePath('title', { item: { title: 'World' } })).toBe('World');
18
+ });
19
+ it('resolves index', () => {
20
+ expect(resolvePath('index', { index: 5 })).toBe(5);
21
+ });
22
+ it('resolves data path', () => {
23
+ expect(resolvePath('data.items', { data: { items: [1, 2, 3] } })).toEqual([1, 2, 3]);
24
+ });
25
+ it('returns null for missing path', () => {
26
+ expect(resolvePath('args.missing', { args: {} })).toBeUndefined();
27
+ });
28
+ it('resolves array index', () => {
29
+ expect(resolvePath('data.0', { data: ['a', 'b'] })).toBe('a');
30
+ });
31
+ });
32
+ describe('evalExpr', () => {
33
+ it('evaluates default filter', () => {
34
+ expect(evalExpr('args.limit | default(20)', { args: {} })).toBe(20);
35
+ });
36
+ it('uses actual value over default', () => {
37
+ expect(evalExpr('args.limit | default(20)', { args: { limit: 10 } })).toBe(10);
38
+ });
39
+ it('evaluates string default', () => {
40
+ expect(evalExpr("args.name | default('unknown')", { args: {} })).toBe('unknown');
41
+ });
42
+ it('evaluates arithmetic: index + 1', () => {
43
+ expect(evalExpr('index + 1', { index: 0 })).toBe(1);
44
+ });
45
+ it('evaluates arithmetic: index * 2', () => {
46
+ expect(evalExpr('index * 2', { index: 5 })).toBe(10);
47
+ });
48
+ it('evaluates || fallback', () => {
49
+ expect(evalExpr("item.name || 'N/A'", { item: {} })).toBe('N/A');
50
+ });
51
+ it('evaluates || with truthy left', () => {
52
+ expect(evalExpr("item.name || 'N/A'", { item: { name: 'Alice' } })).toBe('Alice');
53
+ });
54
+ it('resolves simple path', () => {
55
+ expect(evalExpr('item.title', { item: { title: 'Test' } })).toBe('Test');
56
+ });
57
+ });
58
+ describe('render', () => {
59
+ it('renders full expression', () => {
60
+ expect(render('${{ args.limit }}', { args: { limit: 30 } })).toBe(30);
61
+ });
62
+ it('renders inline expression in string', () => {
63
+ expect(render('Hello ${{ item.name }}!', { item: { name: 'World' } })).toBe('Hello World!');
64
+ });
65
+ it('renders multiple inline expressions', () => {
66
+ expect(render('${{ item.first }}-${{ item.second }}', { item: { first: 'X', second: 'Y' } })).toBe('X-Y');
67
+ });
68
+ it('returns non-string values as-is', () => {
69
+ expect(render(42, {})).toBe(42);
70
+ expect(render(null, {})).toBeNull();
71
+ expect(render(undefined, {})).toBeUndefined();
72
+ });
73
+ it('returns full expression result as native type', () => {
74
+ expect(render('${{ args.list }}', { args: { list: [1, 2, 3] } })).toEqual([1, 2, 3]);
75
+ });
76
+ it('renders URL template', () => {
77
+ expect(render('https://api.example.com/search?q=${{ args.keyword }}', { args: { keyword: 'test' } })).toBe('https://api.example.com/search?q=test');
78
+ });
79
+ });
80
+ describe('normalizeEvaluateSource', () => {
81
+ it('wraps bare expression', () => {
82
+ expect(normalizeEvaluateSource('document.title')).toBe('() => (document.title)');
83
+ });
84
+ it('passes through arrow function', () => {
85
+ expect(normalizeEvaluateSource('() => 42')).toBe('() => 42');
86
+ });
87
+ it('passes through async arrow function', () => {
88
+ const src = 'async () => { return 1; }';
89
+ expect(normalizeEvaluateSource(src)).toBe(src);
90
+ });
91
+ it('passes through named function', () => {
92
+ const src = 'function foo() { return 1; }';
93
+ expect(normalizeEvaluateSource(src)).toBe(src);
94
+ });
95
+ it('wraps IIFE pattern', () => {
96
+ const src = '(async () => { return 1; })()';
97
+ expect(normalizeEvaluateSource(src)).toBe(`() => (${src})`);
98
+ });
99
+ it('handles empty string', () => {
100
+ expect(normalizeEvaluateSource('')).toBe('() => undefined');
101
+ });
102
+ });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tests for pipeline transform steps: select, map, filter, sort, limit.
3
+ */
4
+ export {};
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Tests for pipeline transform steps: select, map, filter, sort, limit.
3
+ */
4
+ import { describe, it, expect } from 'vitest';
5
+ import { stepSelect, stepMap, stepFilter, stepSort, stepLimit } from './steps/transform.js';
6
+ const SAMPLE_DATA = [
7
+ { title: 'Alpha', score: 10, author: 'Alice' },
8
+ { title: 'Beta', score: 30, author: 'Bob' },
9
+ { title: 'Gamma', score: 20, author: 'Charlie' },
10
+ ];
11
+ describe('stepSelect', () => {
12
+ it('selects nested path', async () => {
13
+ const data = { result: { items: [1, 2, 3] } };
14
+ const result = await stepSelect(null, 'result.items', data, {});
15
+ expect(result).toEqual([1, 2, 3]);
16
+ });
17
+ it('selects array by index', async () => {
18
+ const data = { list: ['a', 'b', 'c'] };
19
+ const result = await stepSelect(null, 'list.1', data, {});
20
+ expect(result).toBe('b');
21
+ });
22
+ it('returns null for missing path', async () => {
23
+ const result = await stepSelect(null, 'missing.path', { foo: 1 }, {});
24
+ expect(result).toBeNull();
25
+ });
26
+ it('returns data as-is for non-object', async () => {
27
+ const result = await stepSelect(null, 'foo', 'string-data', {});
28
+ expect(result).toBe('string-data');
29
+ });
30
+ });
31
+ describe('stepMap', () => {
32
+ it('maps array items', async () => {
33
+ const result = await stepMap(null, {
34
+ name: '${{ item.title }}',
35
+ rank: '${{ index + 1 }}',
36
+ }, SAMPLE_DATA, {});
37
+ expect(result).toEqual([
38
+ { name: 'Alpha', rank: 1 },
39
+ { name: 'Beta', rank: 2 },
40
+ { name: 'Gamma', rank: 3 },
41
+ ]);
42
+ });
43
+ it('handles single object', async () => {
44
+ const result = await stepMap(null, {
45
+ name: '${{ item.title }}',
46
+ }, { title: 'Solo' }, {});
47
+ expect(result).toEqual([{ name: 'Solo' }]);
48
+ });
49
+ it('returns null/undefined as-is', async () => {
50
+ expect(await stepMap(null, { x: '${{ item.x }}' }, null, {})).toBeNull();
51
+ });
52
+ });
53
+ describe('stepFilter', () => {
54
+ it('filters by expression', async () => {
55
+ const result = await stepFilter(null, 'item.score', SAMPLE_DATA, {});
56
+ expect(result).toHaveLength(3); // all truthy
57
+ });
58
+ it('returns non-array as-is', async () => {
59
+ const result = await stepFilter(null, 'item.x', 'not-array', {});
60
+ expect(result).toBe('not-array');
61
+ });
62
+ });
63
+ describe('stepSort', () => {
64
+ it('sorts ascending by key', async () => {
65
+ const result = await stepSort(null, 'score', SAMPLE_DATA, {});
66
+ expect(result.map((r) => r.title)).toEqual(['Alpha', 'Gamma', 'Beta']);
67
+ });
68
+ it('sorts descending', async () => {
69
+ const result = await stepSort(null, { by: 'score', order: 'desc' }, SAMPLE_DATA, {});
70
+ expect(result.map((r) => r.title)).toEqual(['Beta', 'Gamma', 'Alpha']);
71
+ });
72
+ it('does not mutate original', async () => {
73
+ const original = [...SAMPLE_DATA];
74
+ await stepSort(null, 'score', SAMPLE_DATA, {});
75
+ expect(SAMPLE_DATA).toEqual(original);
76
+ });
77
+ });
78
+ describe('stepLimit', () => {
79
+ it('limits array to N items', async () => {
80
+ const result = await stepLimit(null, '2', SAMPLE_DATA, {});
81
+ expect(result).toHaveLength(2);
82
+ });
83
+ it('limits using template expression', async () => {
84
+ const result = await stepLimit(null, '${{ args.limit }}', SAMPLE_DATA, { limit: 1 });
85
+ expect(result).toHaveLength(1);
86
+ });
87
+ it('returns non-array as-is', async () => {
88
+ expect(await stepLimit(null, '5', 'string', {})).toBe('string');
89
+ });
90
+ });
@@ -1,9 +1,7 @@
1
1
  /**
2
- * YAML pipeline executor.
3
- * Steps: fetch, navigate, evaluate, map, filter, sort, limit, select, snapshot, click, type, wait, press, intercept.
2
+ * YAML pipeline executor — re-exports from modular pipeline system.
3
+ *
4
+ * This file exists for backward compatibility. All logic has been
5
+ * refactored into src/pipeline/ with modular step handlers.
4
6
  */
5
- export interface PipelineContext {
6
- args?: Record<string, any>;
7
- debug?: boolean;
8
- }
9
- export declare function executePipeline(page: any, pipeline: any[], ctx?: PipelineContext): Promise<any>;
7
+ export { executePipeline, type PipelineContext } from './pipeline/index.js';