create-playwright-performance-regression 0.0.0-stage → 0.1.0-next.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.
Files changed (3) hide show
  1. package/README.md +12 -2
  2. package/index.js +120 -0
  3. package/package.json +30 -4
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # create-playwright-performance-regression
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Sets up [playwright-performance-regression](https://github.com/Meser6/playwright-performance-regression)
4
+ in an existing Playwright project with one command:
5
+
6
+ ```sh
7
+ npm init playwright-performance-regression
8
+ # or: pnpm create playwright-performance-regression · yarn create playwright-performance-regression
9
+ ```
10
+
11
+ It installs `playwright-performance-regression` as a dev dependency with the package manager of the
12
+ project (found by its lockfile) and starts the guided setup, `npx pw-perf init`. Arguments are passed
13
+ to the setup, e.g. `npm init playwright-performance-regression -- --yes`.
package/index.js ADDED
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env node
2
+ // create-playwright-performance-regression (01-REQ-39; 04, chapter 4.10): `npm init
3
+ // playwright-performance-regression`, `pnpm create …` and `yarn create …`. Installs the package as
4
+ // a dev dependency with the package manager of the project (found by its lockfile) and hands over
5
+ // to `pw-perf init`. No dependencies, no shell, no network access of its own: only the package
6
+ // manager is started.
7
+ import { spawn } from 'node:child_process';
8
+ import { existsSync, readFileSync } from 'node:fs';
9
+ import { createRequire } from 'node:module';
10
+ import { basename, dirname, join, resolve } from 'node:path';
11
+
12
+ const PKG = 'playwright-performance-regression';
13
+ const PREFIX = `create-${PKG}: `;
14
+ const own = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
15
+
16
+ const LOCKFILES = [
17
+ ['pnpm-lock.yaml', 'pnpm'],
18
+ ['yarn.lock', 'yarn'],
19
+ ['package-lock.json', 'npm'],
20
+ ];
21
+
22
+ const INSTALL = {
23
+ npm: ['install', '--save-dev'],
24
+ pnpm: ['add', '--save-dev'],
25
+ yarn: ['add', '--dev'],
26
+ };
27
+
28
+ function say(text) {
29
+ process.stderr.write(`${PREFIX}${text}\n`);
30
+ }
31
+
32
+ /** Package manager of the project: lockfile, else the one running this command, else npm. */
33
+ function packageManager(cwd, env) {
34
+ for (const [file, manager] of LOCKFILES) {
35
+ if (existsSync(join(cwd, file))) return manager;
36
+ }
37
+ const agent = /^(npm|pnpm|yarn)\//u.exec(env.npm_config_user_agent ?? '');
38
+ return agent?.[1] ?? 'npm';
39
+ }
40
+
41
+ /** Manager name of the JavaScript entry in npm_execpath (npm-cli.js, pnpm.cjs, yarn.js …). */
42
+ function execPathManager(execPath) {
43
+ const name = basename(execPath ?? '');
44
+ if (/^npm-cli\.[cm]?js$/u.test(name)) return 'npm';
45
+ if (/^pnpm\.[cm]?js$/u.test(name)) return 'pnpm';
46
+ if (/^yarn(?:-[\d.]+)?\.[cm]?js$/u.test(name)) return 'yarn';
47
+ return null;
48
+ }
49
+
50
+ /** Runs a command without a shell; resolves with the exit code (128 + n for a signal). */
51
+ function run(command, args, cwd) {
52
+ return new Promise((done) => {
53
+ const child = spawn(command, args, { cwd, stdio: 'inherit', shell: false });
54
+ // The terminal sends Ctrl+C to the child too; this process waits for its exit code.
55
+ const ignore = () => undefined;
56
+ process.on('SIGINT', ignore);
57
+ process.on('SIGTERM', ignore);
58
+ const finish = (code) => {
59
+ process.off('SIGINT', ignore);
60
+ process.off('SIGTERM', ignore);
61
+ done(code);
62
+ };
63
+ child.on('error', (error) => {
64
+ say(`could not start ${command}: ${error.message}`);
65
+ finish(3);
66
+ });
67
+ child.on('exit', (code, signal) => {
68
+ finish(code ?? (signal === 'SIGINT' ? 130 : signal === 'SIGTERM' ? 143 : 1));
69
+ });
70
+ });
71
+ }
72
+
73
+ /** The pw-perf binary of the package installed in the project, or null. */
74
+ function installedCli(cwd) {
75
+ try {
76
+ const require = createRequire(join(cwd, 'package.json'));
77
+ const manifest = require.resolve(`${PKG}/package.json`);
78
+ const bin = JSON.parse(readFileSync(manifest, 'utf8')).bin?.['pw-perf'];
79
+ return typeof bin === 'string' ? resolve(dirname(manifest), bin) : null;
80
+ } catch {
81
+ return null;
82
+ }
83
+ }
84
+
85
+ async function main() {
86
+ const cwd = process.cwd();
87
+ const args = process.argv.slice(2);
88
+ if (!existsSync(join(cwd, 'package.json'))) {
89
+ say(`no package.json in ${cwd}. Run this command in the directory of your Playwright project.`);
90
+ return 3;
91
+ }
92
+ if (installedCli(cwd) === null) {
93
+ const manager = packageManager(cwd, process.env);
94
+ const installArgs = [...INSTALL[manager], `${PKG}@${own.version}`];
95
+ say(`installing ${PKG}@${own.version} with ${manager}: ${manager} ${installArgs.join(' ')}`);
96
+ let code;
97
+ if (execPathManager(process.env.npm_execpath) === manager) {
98
+ // The JavaScript entry of the running package manager: works on every system.
99
+ code = await run(process.execPath, [process.env.npm_execpath, ...installArgs], cwd);
100
+ } else if (process.platform === 'win32') {
101
+ // npm.cmd and friends cannot be started without a shell on Windows.
102
+ say(`run "${manager} ${installArgs.join(' ')}", then "npx pw-perf init".`);
103
+ return 3;
104
+ } else {
105
+ code = await run(manager, installArgs, cwd);
106
+ }
107
+ if (code !== 0) {
108
+ say(`the installation ended with exit code ${code}.`);
109
+ return code === 130 || code === 143 ? code : 3;
110
+ }
111
+ }
112
+ const cli = installedCli(cwd);
113
+ if (cli === null) {
114
+ say(`${PKG} is not resolvable from ${cwd} after the installation. Run "npx pw-perf init".`);
115
+ return 3;
116
+ }
117
+ return run(process.execPath, [cli, 'init', ...args], cwd);
118
+ }
119
+
120
+ process.exitCode = await main();
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "create-playwright-performance-regression",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-next.0",
4
+ "description": "Set up playwright-performance-regression in a Playwright project: npm init playwright-performance-regression",
5
+ "keywords": [
6
+ "playwright",
7
+ "performance",
8
+ "regression",
9
+ "create",
10
+ "init"
11
+ ],
12
+ "homepage": "https://github.com/Meser6/playwright-performance-regression#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/Meser6/playwright-performance-regression/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Meser6/playwright-performance-regression.git",
19
+ "directory": "create"
20
+ },
21
+ "license": "MIT",
22
+ "type": "module",
23
+ "bin": {
24
+ "create-playwright-performance-regression": "./index.js"
25
+ },
26
+ "files": [
27
+ "index.js"
28
+ ],
29
+ "engines": {
30
+ "node": ">=22.18.0"
31
+ }
32
+ }