diff --git a/packages/core/src/cli/cliEntrypoint.ts b/packages/core/src/cli/cliEntrypoint.ts new file mode 100644 index 000000000..0138343f8 --- /dev/null +++ b/packages/core/src/cli/cliEntrypoint.ts @@ -0,0 +1,58 @@ +import path from 'node:path' +import chalk from 'chalk' +import { Command } from 'commander' +import { rimraf } from 'rimraf' +import { WALLET_SETUP_DIR_NAME } from '../constants' +import { createCache } from '../createCache' +import { prepareExtension } from '../prepareExtension' +import { compileWalletSetupFunctions } from './compileWalletSetupFunctions' +import { footer } from './footer' + +interface CliFlags { + headless: boolean + force: boolean + debug: boolean +} + +// TODO: Add unit tests for the CLI! +export const cliEntrypoint = async () => { + console.log(`⚠️ ${chalk.yellowBright`The CLI is in alpha so expect breaking changes!`} ⚠️\n`) + + const program = new Command() + .name(chalk.magenta('core')) + .description('A CLI for building the cache of wallet setup functions') + .argument('[dir]', 'Directory containing the wallet setup functions', path.join('test', WALLET_SETUP_DIR_NAME)) + .option( + '--headless', + 'Build cache in the headless browser mode. Alternatively, set the `HEADLESS` env variable to `true`', + false + ) + .option('-f, --force', 'Force the creation of cache even if it already exists', false) + .option('-d, --debug', 'If this flag is present, the compilation files are not going to be deleted', false) + .helpOption(undefined, 'Display help for command') + .addHelpText('afterAll', `\n${footer}\n`) + .parse(process.argv) + + let walletSetupDir = program.args[0] + if (!walletSetupDir) { + walletSetupDir = path.join(process.cwd(), 'test', WALLET_SETUP_DIR_NAME) + } + + const flags: CliFlags = program.opts() + + if (flags.headless) { + process.env.HEADLESS = true + } + + console.log('[DEBUG] Running with the following options:') + console.log({ cacheDir: walletSetupDir, ...flags, headless: Boolean(process.env.HEADLESS) ?? false }, '\n') + + const compiledWalletSetupDirPath = await compileWalletSetupFunctions(walletSetupDir, flags.debug) + + // TODO: We should be using `prepareExtension` function from the wallet itself! + await createCache(compiledWalletSetupDirPath, prepareExtension, flags.force) + + if (!flags.debug) { + await rimraf(compiledWalletSetupDirPath) + } +} diff --git a/packages/core/src/cli/index.ts b/packages/core/src/cli/index.ts index 663a71ab7..0f50aa542 100644 --- a/packages/core/src/cli/index.ts +++ b/packages/core/src/cli/index.ts @@ -1,65 +1,8 @@ #!/usr/bin/env node -import path from 'node:path' -import chalk from 'chalk' -import { Command } from 'commander' -import { rimraf } from 'rimraf' -import { WALLET_SETUP_DIR_NAME } from '../constants' -import { createCache } from '../createCache' -import { prepareExtension } from '../prepareExtension' -import { compileWalletSetupFunctions } from './compileWalletSetupFunctions' -import { footer } from './footer' +import { cliEntrypoint } from './cliEntrypoint' -interface CliFlags { - headless: boolean - force: boolean - debug: boolean -} - -// TODO: Add unit tests for the CLI! -export const main = async () => { - console.log(`⚠️ ${chalk.yellowBright`The CLI is in alpha so expect breaking changes!`} ⚠️\n`) - - const program = new Command() - .name(chalk.magenta('core')) - .description('A CLI for building the cache of wallet setup functions') - .argument('[dir]', 'Directory containing the wallet setup functions', path.join('test', WALLET_SETUP_DIR_NAME)) - .option( - '--headless', - 'Build cache in the headless browser mode. Alternatively, set the `HEADLESS` env variable to `true`', - false - ) - .option('-f, --force', 'Force the creation of cache even if it already exists', false) - .option('-d, --debug', 'If this flag is present, the compilation files are not going to be deleted', false) - .helpOption(undefined, 'Display help for command') - .addHelpText('afterAll', `\n${footer}\n`) - .parse(process.argv) - - let walletSetupDir = program.args[0] - if (!walletSetupDir) { - walletSetupDir = path.join(process.cwd(), 'test', WALLET_SETUP_DIR_NAME) - } - - const flags: CliFlags = program.opts() - - if (flags.headless) { - process.env.HEADLESS = true - } - - console.log('[DEBUG] Running with the following options:') - console.log({ cacheDir: walletSetupDir, ...flags, headless: Boolean(process.env.HEADLESS) ?? false }, '\n') - - const compiledWalletSetupDirPath = await compileWalletSetupFunctions(walletSetupDir, flags.debug) - - // TODO: We should be using `prepareExtension` function from the wallet itself! - await createCache(compiledWalletSetupDirPath, prepareExtension, flags.force) - - if (!flags.debug) { - await rimraf(compiledWalletSetupDirPath) - } -} - -main().catch((err) => { +cliEntrypoint().catch((err) => { console.log('Aborting...') if (err instanceof Error) { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6800c5104..027dbc751 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -7,3 +7,4 @@ export * from './defineWalletSetup' export * from './utils/createTempContextDir' export * from './utils/removeTempContextDir' export * from './prepareExtension' +export * from './cli/cliEntrypoint' diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b85eaadf..c4540230e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -190,6 +190,37 @@ importers: packages/tsconfig: {} + release: + dependencies: + '@playwright/test': + specifier: 1.40.0 + version: 1.40.0 + '@synthetixio/synpress-core': + specifier: workspace:* + version: link:../packages/core + '@synthetixio/synpress-fixtures': + specifier: workspace:* + version: link:../packages/fixtures + '@synthetixio/synpress-metamask': + specifier: workspace:* + version: link:../wallets/metamask + devDependencies: + '@types/node': + specifier: ^20.8.0 + version: 20.8.0 + rimraf: + specifier: ^5.0.1 + version: 5.0.1 + tsconfig: + specifier: workspace:* + version: link:../packages/tsconfig + tsup: + specifier: ^7.2.0 + version: 7.2.0(typescript@5.2.2) + typescript: + specifier: ^5.2.2 + version: 5.2.2 + wallets/metamask: dependencies: '@playwright/test': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 835d172f2..f2598b6eb 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -2,3 +2,4 @@ packages: - "packages/*" - "wallets/*" - "examples/*" + - "release" diff --git a/release/package.json b/release/package.json new file mode 100644 index 000000000..4b1ec34ae --- /dev/null +++ b/release/package.json @@ -0,0 +1,46 @@ +{ + "name": "@synthetixio/synpress", + "version": "4.0.0", + "type": "module", + "exports": { + ".": { + "import": { + "types": "./types/index.d.ts", + "default": "./dist/index.js" + } + } + }, + "main": "./dist/index.js", + "bin": "./dist/cli.js", + "files": [ + "dist", + "src", + "types" + ], + "scripts": { + "build": "pnpm run clean && pnpm run build:dist && pnpm run build:types", + "build:dist": "tsup", + "build:types": "tsc --emitDeclarationOnly", + "clean": "rimraf dist types", + "lint": "biome check . --apply", + "lint:check": "biome check . --verbose", + "lint:unsafe": "biome check . --apply-unsafe", + "prepublishOnly": "pnpm run build", + "types:check": "tsc --noEmit" + }, + "dependencies": { + "@synthetixio/synpress-core": "workspace:*", + "@synthetixio/synpress-fixtures": "workspace:*", + "@synthetixio/synpress-metamask": "workspace:*" + }, + "devDependencies": { + "@types/node": "^20.8.0", + "rimraf": "^5.0.1", + "tsconfig": "workspace:*", + "tsup": "^7.2.0", + "typescript": "^5.2.2" + }, + "peerDependencies": { + "@playwright/test": "1.40.0" + } +} diff --git a/release/src/cli.ts b/release/src/cli.ts new file mode 100644 index 000000000..bf94b8b8e --- /dev/null +++ b/release/src/cli.ts @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +import { cliEntrypoint } from '@synthetixio/synpress-core' + +cliEntrypoint().catch((err) => { + console.log('Aborting...') + + if (err instanceof Error) { + console.error(err) + } else { + console.error('Unknown error occurred!', err) + } + + process.exit(1) +}) diff --git a/release/src/index.ts b/release/src/index.ts new file mode 100644 index 000000000..880aaa3f2 --- /dev/null +++ b/release/src/index.ts @@ -0,0 +1,5 @@ +import { defineWalletSetup } from '@synthetixio/synpress-core' +import { getExtensionId, testWithSynpress } from '@synthetixio/synpress-fixtures' +import { MetaMask, unlockForFixture } from '@synthetixio/synpress-metamask' + +export { defineWalletSetup, testWithSynpress, getExtensionId, MetaMask, unlockForFixture } diff --git a/release/tsconfig.json b/release/tsconfig.json new file mode 100644 index 000000000..228a21766 --- /dev/null +++ b/release/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "tsconfig/base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "types", + "declaration": true, + "sourceMap": false, + "declarationMap": true + }, + "include": ["src/index.ts"] +} diff --git a/release/tsup.config.ts b/release/tsup.config.ts new file mode 100644 index 000000000..cae710399 --- /dev/null +++ b/release/tsup.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'tsup' + +export default defineConfig({ + name: 'synpress', + entry: ['src/index.ts', 'src/cli.ts'], + outDir: 'dist', + format: 'esm', + splitting: false, + sourcemap: false +})