-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtransforms.ts
116 lines (102 loc) · 2.61 KB
/
transforms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import path from 'path';
import fs, { readFileSync } from 'fs';
import chalk from 'chalk';
import { cypressStringifyChromeRecording } from '../main.js';
const __dirname = path.resolve(path.dirname('.'));
// cli flags
type Flags = {
force?: boolean;
dry?: boolean;
path?: string;
print?: boolean;
};
interface FileToExport {
stringifiedFile: string;
testName: string;
outputFolder: string;
outputPath: string;
}
async function exportFileToFolder({
stringifiedFile,
testName,
outputFolder,
outputPath,
}: FileToExport) {
fs.writeFile(
path.join(outputFolder, `/${testName}.cy.js`),
stringifiedFile as string,
(err: any) => {
if (!err) {
console.log(
chalk.green(
`\n${testName}.json exported to ${outputPath}/${testName}.spec.js\n`
)
);
}
if (err) {
if (err?.path?.includes('cypress/integration')) {
const outputFolder = path.join(__dirname, 'cypress/e2e');
const outputPath = 'cypress/e2e';
exportFileToFolder({
stringifiedFile,
testName,
outputFolder,
outputPath,
});
} else {
console.log(
chalk.yellow(
`\nThere was an issue writing the output to ${outputPath}. Please check that it exists and try again.`
)
);
}
}
}
);
}
export async function runTransforms({
files,
outputPath,
flags,
}: {
files: string[];
outputPath: string;
flags: Flags;
}): Promise<Promise<string | void>[] | undefined> {
const transformPath = path.join(__dirname, '/dist/main.js');
const outputFolder = path.join(__dirname, outputPath);
const { dry, print } = flags;
const args = ['-t', transformPath].concat(files);
if (dry) {
args.push('--dry');
}
if (print) {
args.push('--print');
}
return files.map(async (file) => {
console.log(chalk.green(`Running Cypress Chrome Recorder on ${file}\n`));
const recordingContent = readFileSync(`${file}`, 'utf8');
const stringifiedFile = await cypressStringifyChromeRecording(
recordingContent
);
if (!stringifiedFile) {
return;
}
const fileName = file.split('/').pop();
const testName = fileName ? fileName.replace('.json', '') : undefined;
if (dry) {
console.log(stringifiedFile);
} else if (!testName) {
console.log(
chalk.red('No file or folder was found to export. Please try again.')
);
} else {
exportFileToFolder({
stringifiedFile,
testName,
outputFolder,
outputPath,
});
}
});
}