js-waku/packages/browser-tests/src/setup-example.js
Sasha 281d9b2c9d
feat: add playwright CI testing (#1542)
* add tests-browser package

* rename to browser

* add playwright and experiment with karma

* add lock

* remove karma

* remove readme

* replace default app, rename

* add and configure playwright

* up package-lock

* use @waku/create-app, add scripts to handle it

* remove tsconfig

* update playwright script

* move dependency to root

* set folder

* up

* try install step

* add playwright dep

* remove step

* add es module utils

* fix import issue

* run on master

* use image prop

* use dotenv-flow, set .env.local

* add log, use dotenv-flow

* add env var to ci

* add env vars to CI

* return install of deps

* return container & log build step

* upgrade @waku/create-app

* fix firefox in container problem
2023-09-22 14:34:16 +02:00

94 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
import "dotenv-flow/config";
import { execSync } from "child_process";
import path from "path";
import { __dirname, readJSON } from "./utils.js";
const ROOT_PATH = path.resolve(__dirname, "../../../");
const JS_WAKU_PACKAGES = readWorkspaces();
const EXAMPLE_NAME = process.env.EXAMPLE_NAME;
const EXAMPLE_TEMPLATE = process.env.EXAMPLE_TEMPLATE;
const EXAMPLE_PATH = path.resolve(__dirname, "..", EXAMPLE_NAME);
run();
function run() {
cleanExampleIfExists();
bootstrapExample();
linkPackages();
}
function cleanExampleIfExists() {
try {
console.log("Cleaning previous example if exists.");
execSync(`rm -rf ${EXAMPLE_PATH}`, { stdio: "ignore" });
} catch (error) {
console.error(`Failed to clean previous example: ${error.message}`);
throw error;
}
}
function bootstrapExample() {
try {
console.log("Bootstrapping example.");
execSync(
`npx @waku/create-app --template ${EXAMPLE_TEMPLATE} ${EXAMPLE_NAME}`,
{ stdio: "ignore" }
);
} catch (error) {
console.error(`Failed to bootstrap example: ${error.message}`);
throw error;
}
}
function linkPackages() {
const examplePackage = readJSON(`${EXAMPLE_PATH}/package.json`);
// remove duplicates if any
const dependencies = filterWakuDependencies({
...examplePackage.dependencies,
...examplePackage.devDependencies
});
Object.keys(dependencies).forEach(linkDependency);
}
function filterWakuDependencies(dependencies) {
return Object.entries(dependencies)
.filter((pair) => JS_WAKU_PACKAGES.includes(pair[0]))
.reduce((acc, pair) => {
acc[pair[0]] = pair[1];
return acc;
}, {});
}
function linkDependency(dependency) {
try {
console.log(`Linking dependency to example: ${dependency}`);
const pathToDependency = path.resolve(ROOT_PATH, toFolderName(dependency));
execSync(`npm link ${pathToDependency}`, { stdio: "ignore" });
} catch (error) {
console.error(
`Failed to npm link dependency ${dependency} in example: ${error.message}`
);
throw error;
}
}
function readWorkspaces() {
const rootPath = path.resolve(ROOT_PATH, "package.json");
const workspaces = readJSON(rootPath).workspaces;
return workspaces.map(toPackageName);
}
function toPackageName(str) {
// assumption is that package name published is always the same in `@waku/package` name
return str.replace("packages", "@waku");
}
function toFolderName(str) {
return str.replace("@waku", "packages");
}