Skip to content

Commit

Permalink
fix(templates): install browsers on postinstall for playwright (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed Oct 2, 2023
1 parent 85d2341 commit 323768b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
44 changes: 44 additions & 0 deletions packages/cli/src/commands/InstallPlaywrightBrowsersCommand.ts
@@ -0,0 +1,44 @@
/* eslint-disable no-console */

import { execSync } from 'node:child_process';

import ansiColors from 'ansi-colors';
import type { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';

const envVariable = 'CRAWLEE_SKIP_BROWSER_INSTALL';

interface InstallPlaywrightBrowsersArgs {
force?: boolean;
}

export class InstallPlaywrightBrowsersCommand<T> implements CommandModule<T, InstallPlaywrightBrowsersArgs> {
command = 'install-playwright-browsers';
describe = 'Installs browsers needed by Playwright for local testing';

builder = async (args: Argv<T>) => {
args.options('force', {
alias: 'f',
default: false,
type: 'boolean',
describe: 'Use `--force` to force installation of browsers even if the environment is marked as having them.',
});

return args as Argv<InstallPlaywrightBrowsersArgs>;
};

handler = (args: ArgumentsCamelCase<InstallPlaywrightBrowsersArgs>) => {
if (process.env[envVariable]) {
if (!args.force) {
console.log(ansiColors.green('Browsers are already installed!'));
return;
}

console.warn(ansiColors.yellow('Installing Playwright browsers in an environment where browsers have already been installed...'));
} else {
console.log(ansiColors.green('Installing Playwright browsers...'));
}

// TODO: detect package manager
execSync(`npx playwright install`, { stdio: 'inherit' });
};
}
3 changes: 3 additions & 0 deletions packages/cli/src/index.ts
Expand Up @@ -10,6 +10,8 @@ require('yargonaut')
// eslint-disable-next-line
import { CreateProjectCommand } from './commands/CreateProjectCommand';
// eslint-disable-next-line
import { InstallPlaywrightBrowsersCommand } from './commands/InstallPlaywrightBrowsersCommand';
// eslint-disable-next-line
import { RunProjectCommand } from './commands/RunProjectCommand';

// eslint-disable-next-line
Expand All @@ -35,6 +37,7 @@ const cli = yargs.scriptName('crawlee')
.alias('h', 'help')
.command(new CreateProjectCommand())
.command(new RunProjectCommand())
.command(new InstallPlaywrightBrowsersCommand())
.recommendCommands()
.strict();

Expand Down
3 changes: 2 additions & 1 deletion packages/templates/templates/playwright-js/package.json
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"start": "node src/main.js",
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1",
"postinstall": "npx crawlee install-playwright-browsers"
},
"author": "It's not you it's me",
"license": "ISC"
Expand Down
3 changes: 2 additions & 1 deletion packages/templates/templates/playwright-ts/package.json
Expand Up @@ -18,7 +18,8 @@
"start:prod": "node dist/main.js",
"start:dev": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only src/main.ts",
"build": "tsc",
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1",
"postinstall": "npx crawlee install-playwright-browsers"
},
"author": "It's not you it's me",
"license": "ISC"
Expand Down

0 comments on commit 323768b

Please sign in to comment.