@tywalk/pcf-helper 1.5.8 → 1.6.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.
- package/dist/bin/session.js +13 -13
- package/dist/package.json +1 -1
- package/dist/tasks/session-pcf.js +213 -166
- package/dist/util/commandUtil.js +34 -0
- package/package.json +1 -1
- package/types/tasks/session-pcf.d.ts +15 -1
- package/types/util/commandUtil.d.ts +6 -0
package/dist/bin/session.js
CHANGED
|
@@ -33,14 +33,11 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
return result;
|
|
34
34
|
};
|
|
35
35
|
})();
|
|
36
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
-
};
|
|
39
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
37
|
const task = __importStar(require("../tasks/session-pcf"));
|
|
41
38
|
const package_json_1 = require("../package.json");
|
|
42
|
-
const color_logger_1 = __importDefault(require("@tywalk/color-logger"));
|
|
43
39
|
const commander_1 = require("commander");
|
|
40
|
+
const commandUtil_1 = require("../util/commandUtil");
|
|
44
41
|
const program = new commander_1.Command();
|
|
45
42
|
program
|
|
46
43
|
.name('pcf-helper-session')
|
|
@@ -53,12 +50,15 @@ program
|
|
|
53
50
|
.option('-b, --bundle <path>', 'local bundle path')
|
|
54
51
|
.option('-c, --css <path>', 'local CSS path')
|
|
55
52
|
.option('-f, --config <path>', 'config file path', 'session.config.json')
|
|
56
|
-
.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
53
|
+
.option('-w, --watch', 'start pcf-scripts watch process')
|
|
54
|
+
.parse()
|
|
55
|
+
.action((options) => {
|
|
56
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
57
|
+
const { logger, tick } = (0, commandUtil_1.setupExecutionContext)(options);
|
|
58
|
+
logger.log('PCF Helper version', package_json_1.version);
|
|
59
|
+
const config = task.loadConfig(options.config);
|
|
60
|
+
// Priority: CLI args > config file > environment variables
|
|
61
|
+
const startWatch = (_b = (_a = options.watch) !== null && _a !== void 0 ? _a : config.startWatch) !== null && _b !== void 0 ? _b : false;
|
|
62
|
+
task.runSession((_c = options.url) !== null && _c !== void 0 ? _c : config.remoteEnvironmentUrl, (_d = options.script) !== null && _d !== void 0 ? _d : config.remoteScriptToIntercept, (_e = options.stylesheet) !== null && _e !== void 0 ? _e : config.remoteStylesheetToIntercept, (_f = options.bundle) !== null && _f !== void 0 ? _f : config.localBundlePath, (_g = options.css) !== null && _g !== void 0 ? _g : config.localCssPath, startWatch);
|
|
63
|
+
(0, commandUtil_1.handleResults)('session', logger, tick, 0);
|
|
64
|
+
});
|
package/dist/package.json
CHANGED
|
@@ -18,6 +18,7 @@ const color_logger_1 = __importDefault(require("@tywalk/color-logger"));
|
|
|
18
18
|
const path_1 = __importDefault(require("path"));
|
|
19
19
|
const fs_1 = __importDefault(require("fs"));
|
|
20
20
|
const playwright_1 = require("playwright");
|
|
21
|
+
const child_process_1 = require("child_process");
|
|
21
22
|
/**
|
|
22
23
|
* Loads configuration for the session task, supporting a combination of config file, environment variables, and CLI arguments.
|
|
23
24
|
* The priority order is: CLI arguments > environment variables > config file > defaults.
|
|
@@ -76,6 +77,8 @@ function loadConfig(config) {
|
|
|
76
77
|
remoteStylesheetToIntercept: remoteStylesheetToIntercept,
|
|
77
78
|
localCssPath: (_a = process.env.LOCAL_CSS_PATH) !== null && _a !== void 0 ? _a : fileConfig.localCssPath,
|
|
78
79
|
localBundlePath: (_b = process.env.LOCAL_BUNDLE_PATH) !== null && _b !== void 0 ? _b : fileConfig.localBundlePath,
|
|
80
|
+
startWatch: process.env.START_WATCH === 'true' ||
|
|
81
|
+
fileConfig.startWatch || false,
|
|
79
82
|
};
|
|
80
83
|
}
|
|
81
84
|
/**
|
|
@@ -87,183 +90,227 @@ function loadConfig(config) {
|
|
|
87
90
|
* @param remoteStylesheetToIntercept The full URL of the remote stylesheet to intercept (e.g., https://app.your-remote-environment.com/static/css/remote-control-styles.css).
|
|
88
91
|
* @param localBundlePath The local file path to the JavaScript bundle that should be served when the remote script URL is requested.
|
|
89
92
|
* @param localCssPath The local file path to the CSS file that should be served when the remote stylesheet URL is requested.
|
|
93
|
+
* @param startWatch Optional flag to start the session in watch mode. If true, the process will kick off "pcf-scripts start watch" in parallel to automatically rebuild the bundle on changes.
|
|
94
|
+
* @returns A promise that resolves when the session is set up and running. The session will continue to run until the process is exited, at which point it will clean up and save state.
|
|
90
95
|
*/
|
|
91
|
-
function runSession(remoteEnvironmentUrl, remoteScriptToIntercept, remoteStylesheetToIntercept, localBundlePath, localCssPath) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
color_logger_1.default.error('❌ Remote script URL to intercept is required. Please provide it via CLI, config file, or environment variable.');
|
|
98
|
-
process.exit(1);
|
|
99
|
-
}
|
|
100
|
-
if (!remoteStylesheetToIntercept) {
|
|
101
|
-
color_logger_1.default.error('❌ Remote stylesheet URL to intercept is required. Please provide it via CLI, config file, or environment variable.');
|
|
102
|
-
process.exit(1);
|
|
103
|
-
}
|
|
104
|
-
if (!localBundlePath) {
|
|
105
|
-
color_logger_1.default.error('❌ Local bundle path is required. Please provide it via CLI, config file, or environment variable.');
|
|
106
|
-
process.exit(1);
|
|
107
|
-
}
|
|
108
|
-
if (!localCssPath) {
|
|
109
|
-
color_logger_1.default.error('❌ Local CSS path is required. Please provide it via CLI, config file, or environment variable.');
|
|
110
|
-
process.exit(1);
|
|
111
|
-
}
|
|
112
|
-
const REMOTE_ENVIRONMENT_URL = remoteEnvironmentUrl;
|
|
113
|
-
const REMOTE_SCRIPT_TO_INTERCEPT = remoteScriptToIntercept;
|
|
114
|
-
const REMOTE_STYLESHEET_TO_INTERCEPT = remoteStylesheetToIntercept;
|
|
115
|
-
const LOCAL_BUNDLE_PATH = path_1.default.resolve(localBundlePath);
|
|
116
|
-
const LOCAL_CSS_PATH = path_1.default.resolve(localCssPath);
|
|
117
|
-
// Debug logging for URL construction
|
|
118
|
-
color_logger_1.default.debug('🔍 Debug - Final URLs:');
|
|
119
|
-
color_logger_1.default.debug(` Remote Environment: ${REMOTE_ENVIRONMENT_URL}`);
|
|
120
|
-
color_logger_1.default.debug(` Script to intercept: ${REMOTE_SCRIPT_TO_INTERCEPT}`);
|
|
121
|
-
color_logger_1.default.debug(` CSS to intercept: ${REMOTE_STYLESHEET_TO_INTERCEPT}`);
|
|
122
|
-
color_logger_1.default.debug(` Local bundle path: ${LOCAL_BUNDLE_PATH}`);
|
|
123
|
-
color_logger_1.default.debug(` Local CSS path: ${LOCAL_CSS_PATH}`);
|
|
124
|
-
color_logger_1.default.debug('');
|
|
125
|
-
// Path to store your session cookies
|
|
126
|
-
const AUTH_DIR = path_1.default.join(process.cwd(), '.auth');
|
|
127
|
-
const STATE_FILE = path_1.default.join(AUTH_DIR, 'state.json');
|
|
128
|
-
(() => __awaiter(this, void 0, void 0, function* () {
|
|
129
|
-
color_logger_1.default.log('🚀 Starting ephemeral browser session...');
|
|
130
|
-
// 1. Prepare context options (load session if it exists)
|
|
131
|
-
let contextOptions = {};
|
|
132
|
-
if (fs_1.default.existsSync(STATE_FILE)) {
|
|
133
|
-
color_logger_1.default.log('🔓 Loading previous login session...');
|
|
134
|
-
contextOptions.storageState = STATE_FILE;
|
|
96
|
+
function runSession(remoteEnvironmentUrl, remoteScriptToIntercept, remoteStylesheetToIntercept, localBundlePath, localCssPath, startWatch) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
if (!remoteEnvironmentUrl) {
|
|
100
|
+
color_logger_1.default.error('❌ Remote environment URL is required. Please provide it via CLI, config file, or environment variable.');
|
|
101
|
+
process.exit(1);
|
|
135
102
|
}
|
|
136
|
-
|
|
137
|
-
color_logger_1.default.
|
|
103
|
+
if (!remoteScriptToIntercept) {
|
|
104
|
+
color_logger_1.default.error('❌ Remote script URL to intercept is required. Please provide it via CLI, config file, or environment variable.');
|
|
105
|
+
process.exit(1);
|
|
138
106
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
107
|
+
if (!remoteStylesheetToIntercept) {
|
|
108
|
+
color_logger_1.default.error('❌ Remote stylesheet URL to intercept is required. Please provide it via CLI, config file, or environment variable.');
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
111
|
+
if (!localBundlePath) {
|
|
112
|
+
color_logger_1.default.error('❌ Local bundle path is required. Please provide it via CLI, config file, or environment variable.');
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
if (!localCssPath) {
|
|
116
|
+
color_logger_1.default.error('❌ Local CSS path is required. Please provide it via CLI, config file, or environment variable.');
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
const REMOTE_ENVIRONMENT_URL = remoteEnvironmentUrl;
|
|
120
|
+
const REMOTE_SCRIPT_TO_INTERCEPT = remoteScriptToIntercept;
|
|
121
|
+
const REMOTE_STYLESHEET_TO_INTERCEPT = remoteStylesheetToIntercept;
|
|
122
|
+
const LOCAL_BUNDLE_PATH = path_1.default.resolve(localBundlePath);
|
|
123
|
+
const LOCAL_CSS_PATH = path_1.default.resolve(localCssPath);
|
|
124
|
+
// Debug logging for URL construction
|
|
125
|
+
color_logger_1.default.debug('🔍 Debug - Final URLs:');
|
|
126
|
+
color_logger_1.default.debug(` Remote Environment: ${REMOTE_ENVIRONMENT_URL}`);
|
|
127
|
+
color_logger_1.default.debug(` Script to intercept: ${REMOTE_SCRIPT_TO_INTERCEPT}`);
|
|
128
|
+
color_logger_1.default.debug(` CSS to intercept: ${REMOTE_STYLESHEET_TO_INTERCEPT}`);
|
|
129
|
+
color_logger_1.default.debug(` Local bundle path: ${LOCAL_BUNDLE_PATH}`);
|
|
130
|
+
color_logger_1.default.debug(` Local CSS path: ${LOCAL_CSS_PATH}`);
|
|
131
|
+
color_logger_1.default.debug('');
|
|
132
|
+
// Path to store your session cookies
|
|
133
|
+
const AUTH_DIR = path_1.default.join(process.cwd(), '.auth');
|
|
134
|
+
const STATE_FILE = path_1.default.join(AUTH_DIR, 'state.json');
|
|
135
|
+
// Start watch process if requested
|
|
136
|
+
let watchProcess;
|
|
137
|
+
if (startWatch) {
|
|
138
|
+
color_logger_1.default.log('🔧 Starting pcf-scripts watch process...');
|
|
139
|
+
watchProcess = (0, child_process_1.spawn)('pcf-scripts', ['start', 'watch'], {
|
|
140
|
+
cwd: process.cwd(),
|
|
141
|
+
stdio: ['inherit', 'pipe', 'pipe'],
|
|
142
|
+
shell: true
|
|
143
|
+
});
|
|
144
|
+
(_a = watchProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
|
|
145
|
+
color_logger_1.default.log(`📦 [PCF Watch] ${data.toString().trim()}`);
|
|
146
|
+
});
|
|
147
|
+
(_b = watchProcess.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
|
|
148
|
+
color_logger_1.default.warn(`⚠️ [PCF Watch] ${data.toString().trim()}`);
|
|
149
|
+
});
|
|
150
|
+
watchProcess.on('exit', (code) => {
|
|
151
|
+
if (code !== null && code !== 0) {
|
|
152
|
+
color_logger_1.default.error(`❌ PCF watch process exited with code ${code}`);
|
|
152
153
|
}
|
|
153
154
|
else {
|
|
154
|
-
|
|
155
|
-
yield context.storageState({ path: STATE_FILE });
|
|
156
|
-
color_logger_1.default.log('🛑 Tearing down rules and session.');
|
|
157
|
-
yield browser.close();
|
|
155
|
+
color_logger_1.default.log('✅ PCF watch process ended');
|
|
158
156
|
}
|
|
159
|
-
}
|
|
160
|
-
catch (error) {
|
|
161
|
-
color_logger_1.default.debug('Error during cleanup:', error);
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
// Handle process exit signals
|
|
165
|
-
process.on('SIGINT', () => __awaiter(this, void 0, void 0, function* () {
|
|
166
|
-
yield cleanup('SIGINT');
|
|
167
|
-
process.exit(0);
|
|
168
|
-
}));
|
|
169
|
-
process.on('SIGTERM', () => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
yield cleanup('SIGTERM');
|
|
171
|
-
process.exit(0);
|
|
172
|
-
}));
|
|
173
|
-
process.on('beforeExit', () => __awaiter(this, void 0, void 0, function* () {
|
|
174
|
-
yield cleanup('beforeExit');
|
|
175
|
-
}));
|
|
176
|
-
// Handle uncaught exceptions
|
|
177
|
-
process.on('uncaughtException', (error) => __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
color_logger_1.default.error('Uncaught exception:', error);
|
|
179
|
-
yield cleanup('uncaughtException');
|
|
180
|
-
process.exit(1);
|
|
181
|
-
}));
|
|
182
|
-
process.on('unhandledRejection', (reason) => __awaiter(this, void 0, void 0, function* () {
|
|
183
|
-
color_logger_1.default.error('Unhandled promise rejection:', reason);
|
|
184
|
-
yield cleanup('unhandledRejection');
|
|
185
|
-
process.exit(1);
|
|
186
|
-
}));
|
|
187
|
-
// Handle browser disconnect
|
|
188
|
-
browser.on('disconnected', () => __awaiter(this, void 0, void 0, function* () {
|
|
189
|
-
color_logger_1.default.log('Browser disconnected');
|
|
190
|
-
yield cleanup('browser disconnected');
|
|
191
|
-
}));
|
|
192
|
-
// Handle context close (when all pages in context are closed)
|
|
193
|
-
context.on('close', () => __awaiter(this, void 0, void 0, function* () {
|
|
194
|
-
color_logger_1.default.log('Browser context closed');
|
|
195
|
-
yield cleanup('context closed');
|
|
196
|
-
process.exit(0);
|
|
197
|
-
}));
|
|
198
|
-
// 3. Programmatically apply your network interception rule with pattern matching
|
|
199
|
-
// Handle dynamic version segments in CRM URLs like /version?/webresources/...
|
|
200
|
-
const scriptPattern = REMOTE_SCRIPT_TO_INTERCEPT.replace(/^https?:\/\/[^\/]+/, '');
|
|
201
|
-
const stylesheetPattern = REMOTE_STYLESHEET_TO_INTERCEPT.replace(/^https?:\/\/[^\/]+/, '');
|
|
202
|
-
color_logger_1.default.debug(`📡 Setting up interception patterns:`);
|
|
203
|
-
color_logger_1.default.debug(` Script pattern: **${scriptPattern}`);
|
|
204
|
-
color_logger_1.default.debug(` CSS pattern: **${stylesheetPattern}`);
|
|
205
|
-
yield context.route(route => {
|
|
206
|
-
if (!route.href) {
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
// Match script URLs that end with the same path structure
|
|
210
|
-
return route.href.includes(scriptPattern);
|
|
211
|
-
}, (route) => __awaiter(this, void 0, void 0, function* () {
|
|
212
|
-
color_logger_1.default.log(`✅ Intercepted script request: ${route.request().url()}`);
|
|
213
|
-
color_logger_1.default.log(` Serving local file: ${LOCAL_BUNDLE_PATH}`);
|
|
214
|
-
route.fulfill({
|
|
215
|
-
status: 200,
|
|
216
|
-
contentType: 'application/javascript',
|
|
217
|
-
body: fs_1.default.readFileSync(LOCAL_BUNDLE_PATH)
|
|
218
157
|
});
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (!route.href) {
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
224
|
-
// Match CSS URLs that end with the same path structure
|
|
225
|
-
return route.href.includes(stylesheetPattern);
|
|
226
|
-
}, (route) => __awaiter(this, void 0, void 0, function* () {
|
|
227
|
-
color_logger_1.default.log(`✅ Intercepted CSS request: ${route.request().url()}`);
|
|
228
|
-
color_logger_1.default.log(` Serving local file: ${LOCAL_CSS_PATH}`);
|
|
229
|
-
route.fulfill({
|
|
230
|
-
status: 200,
|
|
231
|
-
contentType: 'text/css',
|
|
232
|
-
body: fs_1.default.readFileSync(LOCAL_CSS_PATH)
|
|
158
|
+
watchProcess.on('error', (error) => {
|
|
159
|
+
color_logger_1.default.error('❌ Failed to start PCF watch process:', error);
|
|
233
160
|
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
// This was the last page, trigger full cleanup
|
|
243
|
-
yield cleanup('last page closed');
|
|
244
|
-
process.exit(0);
|
|
161
|
+
}
|
|
162
|
+
yield (() => __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
color_logger_1.default.log('🚀 Starting ephemeral browser session...');
|
|
164
|
+
// 1. Prepare context options (load session if it exists)
|
|
165
|
+
let contextOptions = {};
|
|
166
|
+
if (fs_1.default.existsSync(STATE_FILE)) {
|
|
167
|
+
color_logger_1.default.log('🔓 Loading previous login session...');
|
|
168
|
+
contextOptions.storageState = STATE_FILE;
|
|
245
169
|
}
|
|
246
170
|
else {
|
|
247
|
-
color_logger_1.default.
|
|
171
|
+
color_logger_1.default.log('⚠️ No previous session found. You may need to log in.');
|
|
248
172
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
173
|
+
// 2. Launch browser and apply context
|
|
174
|
+
const browser = yield playwright_1.chromium.launch({ headless: false, args: ['--auto-open-devtools-for-tabs'] });
|
|
175
|
+
const context = yield browser.newContext(Object.assign(Object.assign({}, contextOptions), { viewport: null, serviceWorkers: 'block' }));
|
|
176
|
+
// Shared cleanup function to save state and close browser
|
|
177
|
+
const cleanup = (...args_1) => __awaiter(this, [...args_1], void 0, function* (reason = 'unknown') {
|
|
178
|
+
try {
|
|
179
|
+
color_logger_1.default.log(`💾 Saving session state (${reason})...`);
|
|
180
|
+
// Kill the watch process if it's running
|
|
181
|
+
if (watchProcess && !watchProcess.killed) {
|
|
182
|
+
color_logger_1.default.log('🛑 Terminating PCF watch process...');
|
|
183
|
+
watchProcess.kill('SIGTERM');
|
|
184
|
+
// Give it a chance to exit gracefully, then force kill if needed
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
if (watchProcess && !watchProcess.killed) {
|
|
187
|
+
color_logger_1.default.warn('⚠️ Force killing PCF watch process...');
|
|
188
|
+
watchProcess.kill('SIGKILL');
|
|
189
|
+
}
|
|
190
|
+
}, 2000);
|
|
261
191
|
}
|
|
262
|
-
|
|
263
|
-
|
|
192
|
+
// Ensure the .auth directory exists before saving
|
|
193
|
+
if (!fs_1.default.existsSync(AUTH_DIR)) {
|
|
194
|
+
fs_1.default.mkdirSync(AUTH_DIR, { recursive: true });
|
|
264
195
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
196
|
+
if (!browser.isConnected()) {
|
|
197
|
+
color_logger_1.default.log('Browser already disconnected.');
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
// Save the cookies and local storage to the JSON file
|
|
201
|
+
yield context.storageState({ path: STATE_FILE });
|
|
202
|
+
color_logger_1.default.log('🛑 Tearing down rules and session.');
|
|
203
|
+
yield browser.close();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
color_logger_1.default.debug('Error during cleanup:', error);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
// Handle process exit signals
|
|
211
|
+
process.on('SIGINT', () => __awaiter(this, void 0, void 0, function* () {
|
|
212
|
+
yield cleanup('SIGINT');
|
|
213
|
+
process.exit(0);
|
|
214
|
+
}));
|
|
215
|
+
process.on('SIGTERM', () => __awaiter(this, void 0, void 0, function* () {
|
|
216
|
+
yield cleanup('SIGTERM');
|
|
217
|
+
process.exit(0);
|
|
218
|
+
}));
|
|
219
|
+
process.on('beforeExit', () => __awaiter(this, void 0, void 0, function* () {
|
|
220
|
+
yield cleanup('beforeExit');
|
|
221
|
+
}));
|
|
222
|
+
// Handle uncaught exceptions
|
|
223
|
+
process.on('uncaughtException', (error) => __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
color_logger_1.default.error('Uncaught exception:', error);
|
|
225
|
+
yield cleanup('uncaughtException');
|
|
226
|
+
process.exit(1);
|
|
227
|
+
}));
|
|
228
|
+
process.on('unhandledRejection', (reason) => __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
color_logger_1.default.error('Unhandled promise rejection:', reason);
|
|
230
|
+
yield cleanup('unhandledRejection');
|
|
231
|
+
process.exit(1);
|
|
232
|
+
}));
|
|
233
|
+
// Handle browser disconnect
|
|
234
|
+
browser.on('disconnected', () => __awaiter(this, void 0, void 0, function* () {
|
|
235
|
+
color_logger_1.default.log('Browser disconnected');
|
|
236
|
+
yield cleanup('browser disconnected');
|
|
237
|
+
}));
|
|
238
|
+
// Handle context close (when all pages in context are closed)
|
|
239
|
+
context.on('close', () => __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
color_logger_1.default.log('Browser context closed');
|
|
241
|
+
yield cleanup('context closed');
|
|
242
|
+
process.exit(0);
|
|
243
|
+
}));
|
|
244
|
+
// 3. Programmatically apply your network interception rule with pattern matching
|
|
245
|
+
// Handle dynamic version segments in CRM URLs like /version?/webresources/...
|
|
246
|
+
const scriptPattern = REMOTE_SCRIPT_TO_INTERCEPT.replace(/^https?:\/\/[^\/]+/, '');
|
|
247
|
+
const stylesheetPattern = REMOTE_STYLESHEET_TO_INTERCEPT.replace(/^https?:\/\/[^\/]+/, '');
|
|
248
|
+
color_logger_1.default.debug(`📡 Setting up interception patterns:`);
|
|
249
|
+
color_logger_1.default.debug(` Script pattern: **${scriptPattern}`);
|
|
250
|
+
color_logger_1.default.debug(` CSS pattern: **${stylesheetPattern}`);
|
|
251
|
+
yield context.route(route => {
|
|
252
|
+
if (!route.href) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
// Match script URLs that end with the same path structure
|
|
256
|
+
return route.href.includes(scriptPattern);
|
|
257
|
+
}, (route) => __awaiter(this, void 0, void 0, function* () {
|
|
258
|
+
color_logger_1.default.log(`✅ Intercepted script request: ${route.request().url()}`);
|
|
259
|
+
color_logger_1.default.log(` Serving local file: ${LOCAL_BUNDLE_PATH}`);
|
|
260
|
+
route.fulfill({
|
|
261
|
+
status: 200,
|
|
262
|
+
contentType: 'application/javascript',
|
|
263
|
+
body: fs_1.default.readFileSync(LOCAL_BUNDLE_PATH)
|
|
264
|
+
});
|
|
265
|
+
}));
|
|
266
|
+
yield context.route(route => {
|
|
267
|
+
if (!route.href) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
// Match CSS URLs that end with the same path structure
|
|
271
|
+
return route.href.includes(stylesheetPattern);
|
|
272
|
+
}, (route) => __awaiter(this, void 0, void 0, function* () {
|
|
273
|
+
color_logger_1.default.log(`✅ Intercepted CSS request: ${route.request().url()}`);
|
|
274
|
+
color_logger_1.default.log(` Serving local file: ${LOCAL_CSS_PATH}`);
|
|
275
|
+
route.fulfill({
|
|
276
|
+
status: 200,
|
|
277
|
+
contentType: 'text/css',
|
|
278
|
+
body: fs_1.default.readFileSync(LOCAL_CSS_PATH)
|
|
279
|
+
});
|
|
280
|
+
}));
|
|
281
|
+
// 4. Open a new tab and navigate to your remote environment
|
|
282
|
+
const page = yield context.newPage();
|
|
283
|
+
yield page.goto(REMOTE_ENVIRONMENT_URL);
|
|
284
|
+
// 5. Clean up and save state when the page is closed (but others may still be open)
|
|
285
|
+
page.on('close', () => __awaiter(this, void 0, void 0, function* () {
|
|
286
|
+
const pages = context.pages();
|
|
287
|
+
if (pages.length <= 1) {
|
|
288
|
+
// This was the last page, trigger full cleanup
|
|
289
|
+
yield cleanup('last page closed');
|
|
290
|
+
process.exit(0);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
color_logger_1.default.debug(`Page closed, but ${pages.length - 1} pages still open. Keeping session alive.`);
|
|
294
|
+
}
|
|
295
|
+
}));
|
|
296
|
+
// 6. Watch the local bundle for changes and auto-reload
|
|
297
|
+
let reloadTimeout;
|
|
298
|
+
fs_1.default.watch(LOCAL_BUNDLE_PATH, (eventType) => {
|
|
299
|
+
if (eventType === 'change') {
|
|
300
|
+
// Clear the previous timer if the file changes again quickly
|
|
301
|
+
clearTimeout(reloadTimeout);
|
|
302
|
+
// Wait 300ms for the bundler to finish writing the file before reloading
|
|
303
|
+
reloadTimeout = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
color_logger_1.default.log(`\n🔄 Local bundle updated! Reloading the page...`);
|
|
305
|
+
try {
|
|
306
|
+
yield page.reload();
|
|
307
|
+
}
|
|
308
|
+
catch (err) {
|
|
309
|
+
color_logger_1.default.error('⚠️ Could not reload page (browser might be closed).', err);
|
|
310
|
+
}
|
|
311
|
+
}), 300);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}))();
|
|
315
|
+
});
|
|
269
316
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleResults = exports.setupExecutionContext = void 0;
|
|
4
|
+
const color_logger_1 = require("@tywalk/color-logger");
|
|
5
|
+
const performanceUtil_1 = require("./performanceUtil");
|
|
6
|
+
const setupExecutionContext = (options) => {
|
|
7
|
+
const logger = new color_logger_1.Logger('log');
|
|
8
|
+
if (options.verbose) {
|
|
9
|
+
logger.setDebug(true);
|
|
10
|
+
logger.setLevel('debug');
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
logger.setLevel('info');
|
|
14
|
+
}
|
|
15
|
+
return { logger, tick: performance.now() };
|
|
16
|
+
};
|
|
17
|
+
exports.setupExecutionContext = setupExecutionContext;
|
|
18
|
+
// Helper function to execute tasks and handle results
|
|
19
|
+
const handleResults = (taskName, logger, tick, result) => {
|
|
20
|
+
if (taskName !== 'session') {
|
|
21
|
+
if (result === 0) {
|
|
22
|
+
logger.log(`[PCF Helper] ${taskName} completed successfully!`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
logger.log(`[PCF Helper] ${taskName} completed with errors.`);
|
|
26
|
+
}
|
|
27
|
+
const tock = performance.now();
|
|
28
|
+
logger.log((0, performanceUtil_1.formatMsToSec)(`[PCF Helper] ${(0, performanceUtil_1.formatTime)(new Date())} ${taskName} finished in %is.`, tock - tick));
|
|
29
|
+
}
|
|
30
|
+
if (taskName !== 'session' || result === 1) {
|
|
31
|
+
process.exit(result);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.handleResults = handleResults;
|
package/package.json
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
export type SessionOptions = {
|
|
2
|
+
verbose?: boolean;
|
|
3
|
+
url?: string;
|
|
4
|
+
script?: string;
|
|
5
|
+
stylesheet?: string;
|
|
6
|
+
bundle?: string;
|
|
7
|
+
css?: string;
|
|
8
|
+
config?: string;
|
|
9
|
+
watch?: boolean;
|
|
10
|
+
};
|
|
1
11
|
/**
|
|
2
12
|
* Loads configuration for the session task, supporting a combination of config file, environment variables, and CLI arguments.
|
|
3
13
|
* The priority order is: CLI arguments > environment variables > config file > defaults.
|
|
@@ -11,12 +21,14 @@ declare function loadConfig(config?: string): {
|
|
|
11
21
|
remoteStylesheetToIntercept?: undefined;
|
|
12
22
|
localCssPath?: undefined;
|
|
13
23
|
localBundlePath?: undefined;
|
|
24
|
+
startWatch?: undefined;
|
|
14
25
|
} | {
|
|
15
26
|
remoteEnvironmentUrl: any;
|
|
16
27
|
remoteScriptToIntercept: any;
|
|
17
28
|
remoteStylesheetToIntercept: any;
|
|
18
29
|
localCssPath: any;
|
|
19
30
|
localBundlePath: any;
|
|
31
|
+
startWatch: any;
|
|
20
32
|
};
|
|
21
33
|
/**
|
|
22
34
|
* Runs an ephemeral browser session that intercepts requests to the specified remote script and stylesheet URLs, serving local files instead.
|
|
@@ -27,6 +39,8 @@ declare function loadConfig(config?: string): {
|
|
|
27
39
|
* @param remoteStylesheetToIntercept The full URL of the remote stylesheet to intercept (e.g., https://app.your-remote-environment.com/static/css/remote-control-styles.css).
|
|
28
40
|
* @param localBundlePath The local file path to the JavaScript bundle that should be served when the remote script URL is requested.
|
|
29
41
|
* @param localCssPath The local file path to the CSS file that should be served when the remote stylesheet URL is requested.
|
|
42
|
+
* @param startWatch Optional flag to start the session in watch mode. If true, the process will kick off "pcf-scripts start watch" in parallel to automatically rebuild the bundle on changes.
|
|
43
|
+
* @returns A promise that resolves when the session is set up and running. The session will continue to run until the process is exited, at which point it will clean up and save state.
|
|
30
44
|
*/
|
|
31
|
-
declare function runSession(remoteEnvironmentUrl: string, remoteScriptToIntercept: string, remoteStylesheetToIntercept: string, localBundlePath: string, localCssPath: string): void
|
|
45
|
+
declare function runSession(remoteEnvironmentUrl: string, remoteScriptToIntercept: string, remoteStylesheetToIntercept: string, localBundlePath: string, localCssPath: string, startWatch?: boolean): Promise<void>;
|
|
32
46
|
export { runSession, loadConfig };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Logger } from '@tywalk/color-logger';
|
|
2
|
+
export declare const setupExecutionContext: (options: CommonOptions) => {
|
|
3
|
+
logger: Logger;
|
|
4
|
+
tick: number;
|
|
5
|
+
};
|
|
6
|
+
export declare const handleResults: (taskName: string, logger: Logger, tick: number, result: number) => void;
|