|
1 |
| -import path from "node:path"; |
2 |
| -import { glob } from "glob"; |
3 |
| -import { build } from "tsup"; |
4 |
| -import { ensureCacheDirExists } from "../ensureCacheDirExists"; |
5 |
| -import { FIXES_BANNER } from "./compilationFixes"; |
6 |
| -import buildWalletSetupFunction from "../utils/buildWalletSetupFunction"; |
| 1 | +import path from 'node:path' |
| 2 | +import fs from 'fs-extra' |
| 3 | +import { glob } from 'glob' |
| 4 | +import { build } from 'tsup' |
7 | 5 |
|
8 |
| -const OUT_DIR_NAME = "wallet-setup-dist"; |
| 6 | +import { ensureCacheDirExists } from '../ensureCacheDirExists' |
| 7 | +import buildWalletSetupFunction from '../utils/buildWalletSetupFunction' |
| 8 | +import { extractWalletSetupFunction } from '../utils/extractWalletSetupFunction' |
| 9 | +import { getWalletSetupFuncHash } from '../utils/getWalletSetupFuncHash' |
| 10 | +import { FIXES_BANNER } from './compilationFixes' |
9 | 11 |
|
10 |
| -const createGlobPattern = (walletSetupDir: string) => |
11 |
| - path.join(walletSetupDir, "**", "*.setup.{ts,js,mjs}"); |
| 12 | +const OUT_DIR_NAME = '.wallet-setup-dist' |
12 | 13 |
|
13 |
| -export async function compileWalletSetupFunctions( |
14 |
| - walletSetupDir: string, |
15 |
| - debug: boolean |
16 |
| -) { |
17 |
| - const outDir = path.join(ensureCacheDirExists(), OUT_DIR_NAME); |
| 14 | +const createGlobPattern = (walletSetupDir: string) => path.join(walletSetupDir, '**', '*.setup.{ts,js,mjs}') |
18 | 15 |
|
19 |
| - const globPattern = createGlobPattern(walletSetupDir); |
20 |
| - const fileList = await glob(globPattern); |
| 16 | +export async function compileWalletSetupFunctions(walletSetupDir: string, debug: boolean) { |
| 17 | + const outDir = path.join(ensureCacheDirExists(), OUT_DIR_NAME) |
| 18 | + |
| 19 | + fs.ensureDirSync(outDir) |
| 20 | + |
| 21 | + const globPattern = createGlobPattern(walletSetupDir) |
| 22 | + const fileList = await glob(globPattern) |
21 | 23 |
|
22 | 24 | if (debug) {
|
23 |
| - console.log("[DEBUG] Found the following wallet setup files:"); |
24 |
| - console.log(fileList, "\n"); |
| 25 | + console.log('[DEBUG] Found the following wallet setup files:') |
| 26 | + console.log(fileList, '\n') |
25 | 27 | }
|
26 | 28 |
|
27 | 29 | // TODO: This error message is copied over from another function. Refactor this.
|
28 | 30 | if (!fileList.length) {
|
29 | 31 | throw new Error(
|
30 | 32 | [
|
31 | 33 | `No wallet setup files found at ${walletSetupDir}`,
|
32 |
| - "Remember that all wallet setup files must end with `.setup.{ts,js,mjs}` extension!", |
33 |
| - ].join("\n") |
34 |
| - ); |
| 34 | + 'Remember that all wallet setup files must end with `.setup.{ts,js,mjs}` extension!' |
| 35 | + ].join('\n') |
| 36 | + ) |
35 | 37 | }
|
36 | 38 |
|
37 | 39 | await build({
|
38 |
| - name: "cli-build", |
| 40 | + name: 'cli-build', |
39 | 41 | silent: true,
|
40 | 42 | entry: fileList,
|
41 | 43 | clean: true,
|
42 | 44 | outDir,
|
43 |
| - format: "esm", |
| 45 | + format: 'esm', |
44 | 46 | splitting: true,
|
45 | 47 | sourcemap: false,
|
46 | 48 | config: false,
|
47 | 49 | // TODO: Make this list configurable.
|
48 |
| - external: [ |
49 |
| - "@synthetixio/synpress", |
50 |
| - "@playwright/test", |
51 |
| - "playwright-core", |
52 |
| - "esbuild", |
53 |
| - "tsup", |
54 |
| - ], |
| 50 | + external: ['@synthetixio/synpress', '@playwright/test', 'playwright-core', 'esbuild', 'tsup'], |
55 | 51 | banner: {
|
56 |
| - js: FIXES_BANNER, |
| 52 | + js: FIXES_BANNER |
57 | 53 | },
|
58 | 54 | esbuildOptions(options) {
|
59 | 55 | // TODO: In this step, if the debug file is present, we should modify `console.log` so it prints from which file the log is coming from.
|
60 | 56 | // We're dropping `console.log` and `debugger` statements because they do not play nicely with the Playwright Test Runner.
|
61 |
| - options.drop = debug ? [] : ["console", "debugger"]; |
62 |
| - }, |
63 |
| - }); |
| 57 | + options.drop = debug ? [] : ['console', 'debugger'] |
| 58 | + } |
| 59 | + }) |
64 | 60 |
|
65 |
| - const functionStrings = await Promise.all( |
66 |
| - fileList.map(async (fileName) => { |
67 |
| - const walletSetupFunction = await import(fileName); |
| 61 | + const setupFunctionHashes = await Promise.all( |
| 62 | + fileList.map(async (filePath) => { |
| 63 | + const sourceCode = fs.readFileSync(filePath, 'utf8') |
| 64 | + const functionString = extractWalletSetupFunction(sourceCode) |
68 | 65 |
|
69 |
| - return buildWalletSetupFunction(walletSetupFunction.toString()); |
70 |
| - }) |
71 |
| - ); |
| 66 | + const rawFunctionBuild = buildWalletSetupFunction(functionString) |
72 | 67 |
|
73 |
| - console.log({functionStrings}) |
| 68 | + return getWalletSetupFuncHash(rawFunctionBuild) |
| 69 | + }) |
| 70 | + ) |
74 | 71 |
|
75 |
| - return { outDir, functionStrings: functionStrings }; |
| 72 | + return { outDir, setupFunctionHashes } |
76 | 73 | }
|
0 commit comments