|
1 |
| -import assert from 'node:assert'; |
2 | 1 | import { glob } from 'glob';
|
3 |
| -import minimist from 'minimist'; |
4 |
| -import { findOnlyItemInArgs, orderMattersParse } from './args'; |
5 | 2 | import 'dotenv/config';
|
6 | 3 | import { statSync } from 'node:fs';
|
7 | 4 | import { existsSync } from 'node:fs';
|
8 | 5 | import { join } from 'node:path';
|
9 |
| -import { dump } from 'js-yaml'; |
10 | 6 | import yargs from 'yargs/yargs';
|
11 |
| -import type { |
12 |
| - MidsceneYamlFlowItemAIAction, |
13 |
| - MidsceneYamlFlowItemAIAssert, |
14 |
| - MidsceneYamlFlowItemAIQuery, |
15 |
| - MidsceneYamlFlowItemAIWaitFor, |
16 |
| - MidsceneYamlFlowItemSleep, |
17 |
| - MidsceneYamlScript, |
18 |
| -} from './types'; |
19 |
| - |
20 |
| -const preferenceArgs = { |
21 |
| - url: 'url', |
22 |
| - serve: 'serve', |
23 |
| - headed: 'headed', |
24 |
| - viewportWidth: 'viewport-width', |
25 |
| - viewportHeight: 'viewport-height', |
26 |
| - viewportScale: 'viewport-scale', |
27 |
| - useragent: 'user-agent', |
28 |
| - output: 'output', |
29 |
| - cookie: 'cookie', |
30 |
| -}; |
31 |
| - |
32 |
| -const removedArgs = { |
33 |
| - action: 'action', |
34 |
| - assert: 'assert', |
35 |
| - query: 'query', |
36 |
| - waitFor: 'wait-for', |
37 |
| -}; |
38 |
| - |
39 |
| -const actionArgs = { |
40 |
| - ai: 'ai', |
41 |
| - aiAction: 'aiAction', |
42 |
| - aiAssert: 'aiAssert', |
43 |
| - aiQuery: 'aiQuery', |
44 |
| - aiWaitFor: 'aiWaitFor', |
45 |
| - sleep: 'sleep', |
46 |
| -}; |
47 | 7 |
|
48 | 8 | export const parseProcessArgs = async (): Promise<{
|
49 | 9 | path?: string;
|
@@ -85,93 +45,14 @@ Usage: $0 [options] <path-to-yaml-script-file-or-directory>`,
|
85 | 45 | };
|
86 | 46 | };
|
87 | 47 |
|
88 |
| -export const parseArgsIntoYamlScript = async ( |
89 |
| - input?: string[], |
90 |
| -): Promise<string> => { |
91 |
| - const args = minimist(input || process.argv); |
92 |
| - |
93 |
| - if (findOnlyItemInArgs(args, 'version')) { |
94 |
| - const versionFromPkgJson = require('../package.json').version; |
95 |
| - console.log(`@midscene/cli version ${versionFromPkgJson}`); |
96 |
| - process.exit(0); |
97 |
| - } |
98 |
| - |
99 |
| - // check if any deprecated args are used |
100 |
| - Object.keys(removedArgs).forEach((arg) => { |
101 |
| - if (findOnlyItemInArgs(args, arg)) { |
102 |
| - throw new Error( |
103 |
| - `Parameter ${arg} has been removed, use --aiAction --aiAssert --aiQuery --aiWaitFor instead.`, |
104 |
| - ); |
105 |
| - } |
106 |
| - }); |
107 |
| - |
108 |
| - // check each arg is either in the preferenceArgs or actionArgs |
109 |
| - Object.keys(args).forEach((arg) => { |
110 |
| - if (arg === '_') return; |
111 |
| - assert( |
112 |
| - Object.values(preferenceArgs).includes(arg) || |
113 |
| - Object.values(actionArgs).includes(arg), |
114 |
| - `Unknown argument: ${arg}`, |
115 |
| - ); |
116 |
| - }); |
117 |
| - |
118 |
| - const url = findOnlyItemInArgs(args, preferenceArgs.url) as |
119 |
| - | string |
120 |
| - | undefined; |
121 |
| - |
122 |
| - assert(url, 'url is required'); |
123 |
| - const script: MidsceneYamlScript = { |
124 |
| - target: { url }, |
125 |
| - flow: [], |
126 |
| - }; |
127 |
| - |
128 |
| - Object.entries(preferenceArgs).forEach(([key, value]) => { |
129 |
| - const argValue = findOnlyItemInArgs(args, value); |
130 |
| - if (argValue) { |
131 |
| - Object.assign(script.target, { |
132 |
| - [key]: argValue, |
133 |
| - }); |
134 |
| - } |
135 |
| - }); |
136 |
| - |
137 |
| - const orderedArgs = orderMattersParse(process.argv); |
138 |
| - for (const arg of orderedArgs) { |
139 |
| - const argName = arg.name; |
140 |
| - const argValue = arg.value; |
141 |
| - if (argName === actionArgs.ai || argName === actionArgs.aiAction) { |
142 |
| - script.flow.push({ |
143 |
| - aiAction: argValue, |
144 |
| - } as MidsceneYamlFlowItemAIAction); |
145 |
| - } else if (argName === actionArgs.aiAssert) { |
146 |
| - script.flow.push({ |
147 |
| - aiAssert: argValue, |
148 |
| - } as MidsceneYamlFlowItemAIAssert); |
149 |
| - } else if (argName === actionArgs.aiQuery) { |
150 |
| - script.flow.push({ |
151 |
| - aiQuery: argValue, |
152 |
| - } as MidsceneYamlFlowItemAIQuery); |
153 |
| - } else if (argName === actionArgs.aiWaitFor) { |
154 |
| - script.flow.push({ |
155 |
| - aiWaitFor: argValue, |
156 |
| - } as MidsceneYamlFlowItemAIWaitFor); |
157 |
| - } else if (argName === actionArgs.sleep) { |
158 |
| - script.flow.push({ |
159 |
| - sleep: argValue, |
160 |
| - } as MidsceneYamlFlowItemSleep); |
161 |
| - } |
162 |
| - } |
163 |
| - |
164 |
| - const yaml = dump(script); |
165 |
| - return yaml; |
166 |
| -}; |
167 |
| - |
168 | 48 | // match yml or yaml files
|
169 | 49 | export async function matchYamlFiles(fileGlob: string) {
|
170 | 50 | if (existsSync(fileGlob) && statSync(fileGlob).isDirectory()) {
|
171 | 51 | fileGlob = join(fileGlob, '**/*.{yml,yaml}');
|
172 | 52 | }
|
173 | 53 | const files = await glob(fileGlob, {
|
174 | 54 | nodir: true,
|
| 55 | + windowsPathsNoEscape: true, |
175 | 56 | });
|
176 | 57 | return files
|
177 | 58 | .filter((file) => file.endsWith('.yml') || file.endsWith('.yaml'))
|
|
0 commit comments