-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathecosystem-check.ts
More file actions
165 lines (146 loc) · 4.2 KB
/
ecosystem-check.ts
File metadata and controls
165 lines (146 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// scripts/ecosystem-check.ts
import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const root = join(__dirname, '..');
const lib = join(root, 'lib');
const tmp = mkdtempSync(join(os.tmpdir(), 'workos-test-'));
// Map of "runtime label" → { cmd, args }
const tests: Record<string, { cmd: string; args: string[] }> = {
'node-cjs': {
cmd: 'node',
args: [
'-e',
`console.log('✅ Node CJS:', require("${lib}/index.cjs").WorkOS.name)`,
],
},
'node-esm': {
cmd: 'node',
args: [
'-e',
`import("${lib}/index.mjs").then(m => console.log('✅ Node ESM:', m.WorkOS.name))`,
],
},
deno: {
cmd: 'deno',
args: [
'eval',
`import("${lib}/index.mjs").then(m => console.log('✅ Deno:', m.WorkOS.name))`,
],
},
'bun-cjs': {
cmd: 'bun',
args: [
'-e',
`console.log('✅ Bun CJS:', require("${lib}/index.cjs").WorkOS.name)`,
],
},
'bun-esm': {
cmd: 'bun',
args: [
'-e',
`import("${lib}/index.mjs").then(m => console.log('✅ Bun ESM:', m.WorkOS.name))`,
],
},
};
let allOK = true;
let ranTests = 0;
console.log('🚀 Running WorkOS SDK ecosystem compatibility checks...\n');
// Run basic runtime tests
for (const [name, { cmd, args }] of Object.entries(tests)) {
process.stdout.write(`Testing ${name.padEnd(12)}... `);
const { status, stderr } = spawnSync(cmd, args, {
stdio: ['inherit', 'pipe', 'pipe'],
encoding: 'utf8',
});
if (status !== 0) {
allOK = false;
console.error(`❌ Failed`);
if (stderr) {
console.error(` Error: ${stderr.trim()}`);
}
} else {
ranTests++;
console.log(`✅ Passed`);
}
}
// Test Cloudflare Worker environment using miniflare
process.stdout.write(`Testing worker ... `);
// 1. Check if miniflare is available
const miniflareCheck = spawnSync('npx', ['miniflare', '--version'], {
stdio: 'ignore', // We don't need to see the version output
encoding: 'utf8',
});
if (miniflareCheck.status !== 0) {
console.log(`⚠️ Skipped (miniflare not found or failed to execute)`);
} else {
// 2. Create the temporary worker script
const workerScriptPath = join(tmp, 'worker-test.js');
const safeLibPath = lib.replace(/\\/g, '\\\\'); // For Windows compatibility
const workerScriptContent = `
import { WorkOS } from '${safeLibPath}/index.worker.mjs';
if (WorkOS && WorkOS.name === 'WorkOS') {
console.log('✅ Worker (miniflare): SDK imported successfully.');
} else {
console.error('❌ Worker (miniflare): SDK import failed or WorkOS class is incorrect.');
process.exit(1);
}
`;
writeFileSync(workerScriptPath, workerScriptContent);
// 3. Execute the worker script with miniflare
const { status, stderr } = spawnSync(
'npx',
['miniflare', '--modules', workerScriptPath],
{
stdio: ['inherit', 'pipe', 'pipe'],
encoding: 'utf8',
},
);
// 4. Process the result
if (status !== 0) {
allOK = false;
console.error(`❌ Failed`);
if (stderr) {
console.error(` Error: ${stderr.trim()}`);
}
} else {
ranTests++;
console.log(`✅ Passed`);
}
}
// Test that worker entry bundles cleanly without node builtins
process.stdout.write(`Testing bundler ... `);
const bundleCheck = spawnSync(
'npx',
[
'esbuild',
`${lib}/index.worker.mjs`,
'--bundle',
'--platform=browser',
`--outfile=${join(tmp, 'bundle-check.js')}`,
],
{ encoding: 'utf8' },
);
if (bundleCheck.status !== 0) {
allOK = false;
console.error(`❌ Failed`);
if (bundleCheck.stderr) {
console.error(` ${bundleCheck.stderr.trim()}`);
}
} else {
ranTests++;
console.log(`✅ Passed`);
}
// Cleanup
rmSync(tmp, { recursive: true, force: true });
console.log(`\n📊 Results: ${ranTests} runtime tests completed`);
if (allOK) {
console.log('🎉 All core runtime compatibility checks passed!');
} else {
console.log('💥 Some runtime tests failed. Check the output above.');
throw new Error('Ecosystem compatibility checks failed');
}