testeranto 0.110.0 → 0.112.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.
@@ -1,259 +1,24 @@
1
1
  import ansiC from "ansi-colors";
2
- import { watch } from "fs";
3
- import path from "path";
4
- import crypto from "node:crypto";
5
- import fs from "fs";
6
- import tsc from "tsc-prog";
7
- import { ESLint } from "eslint";
8
- import ts from "typescript";
9
2
  import readline from "readline";
10
3
  import { PM_Main } from "./PM/main";
11
- import { lintExitCodePather, lintPather, tscExitCodePather, tscPather, } from "./utils";
12
- console.log(ansiC.inverse("Press 'x' to shutdown forcefully."));
13
4
  readline.emitKeypressEvents(process.stdin);
14
5
  if (process.stdin.isTTY)
15
6
  process.stdin.setRawMode(true);
7
+ console.log(ansiC.inverse("Press 'x' to shutdown forcefully."));
16
8
  process.stdin.on("keypress", (str, key) => {
17
9
  if (key.name === "x") {
18
10
  console.log(ansiC.inverse("Shutting down forcefully..."));
19
11
  process.exit(-1);
20
12
  }
21
13
  });
22
- async function fileHash(filePath, algorithm = "md5") {
23
- return new Promise((resolve, reject) => {
24
- const hash = crypto.createHash(algorithm);
25
- const fileStream = fs.createReadStream(filePath);
26
- fileStream.on("data", (data) => {
27
- hash.update(data);
28
- });
29
- fileStream.on("end", () => {
30
- const fileHash = hash.digest("hex");
31
- resolve(fileHash);
32
- });
33
- fileStream.on("error", (error) => {
34
- reject(`Error reading file: ${error.message}`);
35
- });
36
- });
37
- }
38
- async function filesHash(files, algorithm = "md5") {
39
- return new Promise((resolve, reject) => {
40
- resolve(files.reduce(async (mm, f) => {
41
- return (await mm) + (await fileHash(f));
42
- }, Promise.resolve("")));
43
- });
44
- }
45
- const getRunnables = (tests, payload = {
46
- nodeEntryPoints: {},
47
- webEntryPoints: {},
48
- }) => {
49
- return tests.reduce((pt, cv, cndx, cry) => {
50
- if (cv[1] === "node") {
51
- pt.nodeEntryPoints[cv[0]] = path.resolve(`./docs/node/${cv[0].split(".").slice(0, -1).concat("mjs").join(".")}`);
52
- }
53
- else if (cv[1] === "web") {
54
- pt.webEntryPoints[cv[0]] = path.resolve(`./docs/web/${cv[0].split(".").slice(0, -1).concat("mjs").join(".")}`);
55
- }
56
- if (cv[3].length) {
57
- getRunnables(cv[3], payload);
58
- }
59
- return pt;
60
- }, payload);
61
- };
62
- const changes = {};
63
- const tscCheck = async ({ entrypoint, addableFiles, platform, }) => {
64
- console.log(ansiC.green(ansiC.inverse(`tsc < ${entrypoint}`)));
65
- const program = tsc.createProgramFromConfig({
66
- basePath: process.cwd(), // always required, used for relative paths
67
- configFilePath: "tsconfig.json", // config to inherit from (optional)
68
- compilerOptions: {
69
- rootDir: "src",
70
- outDir: tscPather(entrypoint, platform),
71
- // declaration: true,
72
- // skipLibCheck: true,
73
- noEmit: true,
74
- },
75
- include: addableFiles, //["src/**/*"],
76
- // exclude: ["**/*.test.ts", "**/*.spec.ts"],
77
- });
78
- const tscPath = tscPather(entrypoint, platform);
79
- let allDiagnostics = program.getSemanticDiagnostics();
80
- const d = [];
81
- allDiagnostics.forEach((diagnostic) => {
82
- if (diagnostic.file) {
83
- let { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
84
- let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
85
- d.push(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
86
- }
87
- else {
88
- d.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
89
- }
90
- });
91
- fs.writeFileSync(tscPath, d.join("\n"));
92
- fs.writeFileSync(tscExitCodePather(entrypoint, platform), d.length.toString());
93
- };
94
- const eslint = new ESLint();
95
- const formatter = await eslint.loadFormatter("./node_modules/testeranto/dist/prebuild/esbuildConfigs/eslint-formatter-testeranto.mjs");
96
- const eslintCheck = async (entrypoint, platform, addableFiles) => {
97
- console.log(ansiC.green(ansiC.inverse(`eslint < ${entrypoint}`)));
98
- const results = (await eslint.lintFiles(addableFiles))
99
- .filter((r) => r.messages.length)
100
- .filter((r) => {
101
- return r.messages[0].ruleId !== null;
102
- })
103
- .map((r) => {
104
- delete r.source;
105
- return r;
106
- });
107
- fs.writeFileSync(lintPather(entrypoint, platform), await formatter.format(results));
108
- fs.writeFileSync(lintExitCodePather(entrypoint, platform), results.length.toString());
109
- };
110
- const makePrompt = async (entryPoint, addableFiles, platform) => {
111
- const promptPath = path.join("./docs/", platform, entryPoint.split(".").slice(0, -1).join("."), `prompt.txt`);
112
- const testPaths = path.join("./docs/", platform, entryPoint.split(".").slice(0, -1).join("."), `tests.json`);
113
- const featuresPath = path.join("./docs/", platform, entryPoint.split(".").slice(0, -1).join("."), `featurePrompt.txt`);
114
- fs.writeFileSync(promptPath, `
115
- ${addableFiles
116
- .map((x) => {
117
- return `/add ${x}`;
118
- })
119
- .join("\n")}
120
-
121
- /read ${lintPather(entryPoint, platform)}
122
- /read ${tscPather(entryPoint, platform)}
123
- /read ${testPaths}
124
-
125
- /load ${featuresPath}
126
-
127
- /code Fix the failing tests described in ${testPaths}. Correct any type signature errors described in the files ${tscPather(entryPoint, platform)}. Implement any method which throws "Function not implemented. Resolve the lint errors described in ${lintPather(entryPoint, platform)}"
128
- `);
129
- };
130
- const metafileOutputs = async (platform) => {
131
- const metafile = JSON.parse(fs.readFileSync(`docs/${platform}/metafile.json`).toString()).metafile;
132
- if (!metafile)
133
- return;
134
- const outputs = metafile.outputs;
135
- Object.keys(outputs).forEach(async (k) => {
136
- const addableFiles = Object.keys(outputs[k].inputs).filter((i) => {
137
- if (!fs.existsSync(i))
138
- return false;
139
- if (i.startsWith("node_modules"))
140
- return false;
141
- return true;
142
- });
143
- const f = `${k.split(".").slice(0, -1).join(".")}/`;
144
- if (!fs.existsSync(f)) {
145
- fs.mkdirSync(f);
146
- }
147
- const entrypoint = outputs[k].entryPoint;
148
- if (entrypoint) {
149
- const changeDigest = await filesHash(addableFiles);
150
- if (changeDigest === changes[entrypoint]) {
151
- // skip
152
- }
153
- else {
154
- changes[entrypoint] = changeDigest;
155
- tscCheck({ platform, addableFiles, entrypoint });
156
- eslintCheck(entrypoint, platform, addableFiles);
157
- makePrompt(entrypoint, addableFiles, platform);
158
- }
159
- }
160
- });
161
- };
162
14
  import(process.cwd() + "/" + process.argv[2]).then(async (module) => {
163
15
  const rawConfig = module.default;
164
16
  const config = Object.assign(Object.assign({}, rawConfig), { buildDir: process.cwd() + "/" + rawConfig.outdir });
165
- let mode = config.devMode ? "DEV" : "PROD";
166
- const fileHashes = {};
167
- let pm = new PM_Main(config);
168
- console.log(ansiC.inverse(`Press 'q' to shutdown gracefully`));
17
+ const pm = new PM_Main(config);
18
+ pm.start();
169
19
  process.stdin.on("keypress", (str, key) => {
170
20
  if (key.name === "q") {
171
- console.log(ansiC.inverse("Testeranto-Run is shutting down gracefully..."));
172
- mode = "PROD";
173
- // onDone();
174
- nodeMetafileWatcher.close();
175
- webMetafileWatcher.close();
176
- pm.shutDown();
177
- }
178
- });
179
- metafileOutputs("node");
180
- const nodeMetafileWatcher = watch("docs/node/metafile.json", async (e, filename) => {
181
- console.log(ansiC.green(ansiC.inverse(`< ${e} ${filename} (node)`)));
182
- metafileOutputs("node");
183
- });
184
- metafileOutputs("web");
185
- const webMetafileWatcher = watch("docs/web/metafile.json", async (e, filename) => {
186
- console.log(ansiC.green(ansiC.inverse(`< ${e} ${filename} (web)`)));
187
- metafileOutputs("web");
188
- });
189
- await pm.startPuppeteer({
190
- slowMo: 1,
191
- // timeout: 1,
192
- waitForInitialPage: false,
193
- executablePath:
194
- // process.env.CHROMIUM_PATH || "/opt/homebrew/bin/chromium",
195
- "/opt/homebrew/bin/chromium",
196
- headless: true,
197
- dumpio: true,
198
- // timeout: 0,
199
- devtools: true,
200
- args: [
201
- "--auto-open-devtools-for-tabs",
202
- `--remote-debugging-port=3234`,
203
- // "--disable-features=IsolateOrigins,site-per-process",
204
- "--disable-site-isolation-trials",
205
- "--allow-insecure-localhost",
206
- "--allow-file-access-from-files",
207
- "--allow-running-insecure-content",
208
- "--disable-dev-shm-usage",
209
- "--disable-extensions",
210
- "--disable-gpu",
211
- "--disable-setuid-sandbox",
212
- "--disable-site-isolation-trials",
213
- "--disable-web-security",
214
- "--no-first-run",
215
- "--no-sandbox",
216
- "--no-startup-window",
217
- // "--no-zygote",
218
- "--reduce-security-for-testing",
219
- "--remote-allow-origins=*",
220
- "--unsafely-treat-insecure-origin-as-secure=*",
221
- // "--disable-features=IsolateOrigins",
222
- // "--remote-allow-origins=ws://localhost:3234",
223
- // "--single-process",
224
- // "--unsafely-treat-insecure-origin-as-secure",
225
- // "--unsafely-treat-insecure-origin-as-secure=ws://192.168.0.101:3234",
226
- // "--disk-cache-dir=/dev/null",
227
- // "--disk-cache-size=1",
228
- // "--start-maximized",
229
- ],
230
- }, ".");
231
- const { nodeEntryPoints, webEntryPoints } = getRunnables(config.tests);
232
- Object.entries(nodeEntryPoints).forEach(([k, outputFile]) => {
233
- pm.launchNode(k, outputFile);
234
- try {
235
- watch(outputFile, async (e, filename) => {
236
- const hash = await fileHash(outputFile);
237
- if (fileHashes[k] !== hash) {
238
- fileHashes[k] = hash;
239
- console.log(ansiC.green(ansiC.inverse(`< ${e} ${filename}`)));
240
- pm.launchNode(k, outputFile);
241
- }
242
- });
21
+ pm.stop();
243
22
  }
244
- catch (e) {
245
- console.error(e);
246
- }
247
- });
248
- Object.entries(webEntryPoints).forEach(([k, outputFile]) => {
249
- pm.launchWeb(k, outputFile);
250
- watch(outputFile, async (e, filename) => {
251
- const hash = await fileHash(outputFile);
252
- if (fileHashes[k] !== hash) {
253
- fileHashes[k] = hash;
254
- console.log(ansiC.green(ansiC.inverse(`< ${e} ${filename}`)));
255
- pm.launchWeb(k, outputFile);
256
- }
257
- });
258
23
  });
259
24
  });