Skip to content

Commit

Permalink
feat: cypress 12.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed Dec 10, 2022
1 parent 79ef42e commit 7ecf9a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';

function pathExists(path: string) {
export function pathExists(path: string) {
try {
fs.statSync(path);
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/injected.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { copyFileSync } from 'fs';

const Mod = require('module');
const req = Mod.prototype.require;

// @ts-ignore
global.integrityCheck = function () {};

// @ts-ignore
if (global.snapshotResult?.customRequire?.exports) {
// @ts-ignore
Expand Down Expand Up @@ -45,3 +44,7 @@ Mod.prototype.require = function (...args) {
}
return req.call(this, ...args);
};

module.exports = function (entryPointPath: string, backupPath: string) {
copyFileSync(backupPath, entryPointPath);
};
8 changes: 5 additions & 3 deletions src/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export const FN_ID = 'cy2_injected';

export const instrumentCypressInit = (
code: string,
injectedModulePath: string
injectedModulePath: string,
entryPointPath: string,
backupPath: string
) => {
const normalizedPath = normalizePath(injectedModulePath);
const injectedFn = `
function ${FN_ID}() {
try { require('${normalizedPath}'); }
try { require('${normalizedPath}')("${entryPointPath}", "${backupPath}"); }
catch (e) {}
}`;

Expand Down Expand Up @@ -65,7 +67,7 @@ function replaceAST(ast: Program, injectedFnAst: Program) {
enter: function (node) {
if (
node.type == estraverse.Syntax.FunctionExpression &&
node.id.name === FN_ID
node.id?.name === FN_ID
) {
return injectedFnAst.body[0];
}
Expand Down
17 changes: 12 additions & 5 deletions src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import cp from 'child_process';
import fs from 'fs';
import { instrumentCypressInit } from './js';

import yaml from 'js-yaml';
import { getCypressCLIBinPath } from './bin-path';
import { debug } from './debug';
import { getConfigFilesPaths, getServerInitPath } from './discovery';
import { pathExists } from './fs';
import { instrumentCypressInit } from './js';

/**
* Patch Cypress init script
Expand All @@ -14,12 +14,19 @@ import { getConfigFilesPaths, getServerInitPath } from './discovery';
*/
export async function patchServerInit(injectedAbsolutePath: string) {
const serverInitPath = await getServerInitPath();

debug('Patching cypress entry point file: %s', serverInitPath);

const serverInitBackup = serverInitPath + '.bak';
if (!pathExists(serverInitBackup)) {
fs.copyFileSync(serverInitPath, serverInitBackup);
}
const doc = fs.readFileSync(serverInitPath, 'utf8');

const result = instrumentCypressInit(doc, injectedAbsolutePath);
const result = instrumentCypressInit(
doc,
injectedAbsolutePath,
serverInitPath,
serverInitBackup
);
fs.writeFileSync(serverInitPath, result);
}

Expand Down

0 comments on commit 7ecf9a2

Please sign in to comment.