-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_npm.ts
224 lines (214 loc) · 6.32 KB
/
build_npm.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { build, emptyDir } from "@deno/dnt";
import { copy } from "https://deno.land/std/fs/mod.ts";
import fs from "node:fs";
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import { denoPlugins } from "jsr:@luca/[email protected]";
// Clear the npm directory to ensure a clean build
// Confirm that these directories exist so copying files works
await emptyDir("./npm");
await emptyDir("./npm/esm/src/setup/");
await emptyDir("./npm/script/src/setup/");
try {
// Copy necessary files for both ESM and CommonJS builds
// This ensures that test data and test files are available in the npm package
await copy("test_data", "npm/esm/test_data", { overwrite: true });
await copy("test_data", "npm/script/test_data", { overwrite: true });
await copy("src/tests", "npm/esm/src/tests", { overwrite: true });
await copy("src/tests", "npm/script/src/tests", { overwrite: true });
// Copy default schema files
// These files are necessary for the validator to function without network access
await Deno.copyFile(
"src/setup/defaultSchema.json",
"npm/esm/src/setup/defaultSchema.json",
);
await Deno.copyFile(
"src/setup/defaultSchemaOrg.json",
"npm/esm/src/setup/defaultSchemaOrg.json",
);
await Deno.copyFile(
"src/setup/defaultSchema.json",
"npm/script/src/setup/defaultSchema.json",
);
await Deno.copyFile(
"src/setup/defaultSchemaOrg.json",
"npm/script/src/setup/defaultSchemaOrg.json",
);
// Create CLI entry point
// This file allows the package to be used as a command-line tool
const cliContent = `#!/usr/bin/env node
const { run } = require('./script/src/index.js');
run(process.argv.slice(2)).catch((error) => {
console.error('An error occurred:', error);
process.exit(1);
});
`;
await Deno.writeTextFile("npm/cli.js", cliContent);
// Log the contents of the npm directory for debugging purposes
console.log("Contents of npm directory:");
for await (const dirEntry of Deno.readDir("npm")) {
console.log(dirEntry.name);
}
// Build configuration for dnt (Deno to Node Transform)
await build({
entryPoints: ["./src/index.ts"],
outDir: "./npm",
shims: {
// Provide Deno-specific APIs in Node.js environment
deno: true,
jsonld: "npm:jsonld",
// Custom shim for ReadableStream
custom: [
{
module: "./shims.ts",
globalNames: ["ReadableStream"],
},
],
},
compilerOptions: {
target: "ES2022",
lib: ["DOM", "ES2022"],
moduleResolution: "node",
},
rootTestDir: "./src",
typeCheck: "both",
test: true,
package: {
// npm package configuration
name: "psychds-validator",
version: Deno.args[0],
description: "psychds-validator",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/psych-ds/psychds-validator.git",
},
bugs: {
url: "https://github.com/psych-ds/psychds-validator/issues",
},
// External dependencies required by the package
dependencies: {
"jsonld": "8.3.2",
"node-fetch": "^3.3.0",
"undici": "5.28.4",
"winston": "^3.8.2",
"commander": "^9.4.0",
"chalk": "^4.1.2",
"cli-table3": "^0.6.3",
"eventemitter3": "^5.0.0",
},
// Entry points for different module systems
main: "./script/src/index.js",
module: "./esm/src/index.js",
exports: {
".": {
"import": "./esm/src/index.js",
"require": "./script/src/index.js",
},
},
// CLI configuration
bin: {
validate: "./cli.js",
},
},
plugins: [
...denoPlugins({
nodeModulesDir: true,
allowImports: {
"cdn.skypack.dev": true,
"unpkg.com": true,
},
}),
{
name: "node-modules-resolver",
setup(build) {
// Handle Node.js built-ins
build.onResolve({ filter: /^node:/ }, (args) => {
return { path: args.path, external: true };
});
},
},
{
name: "expose-validate-web",
setup(build) {
build.onEnd(() => {
const outfile = "./npm/web/psychds-validator.js";
let content = fs.readFileSync(outfile, "utf8");
if (!content.includes("window.psychDSValidator")) {
content +=
'\nif (typeof window !== "undefined") { window.psychDSValidator = { validateWeb }; }';
fs.writeFileSync(outfile, content);
}
});
},
},
],
packageManager: "npm",
importMap: "deno.json",
});
const _result = await esbuild.build({
entryPoints: ["./src/validate-web.ts"],
bundle: true,
outfile: "./npm/web/psychds-validator.js",
format: "esm",
target: "es2020",
sourcemap: true,
minify: false,
platform: "browser",
treeShaking: true,
define: {
"Deno.env.get": "undefined",
"import.meta.main": "undefined",
"global": "window",
},
external: [
"chalk",
"cli-table3",
"commander",
"winston",
"node-fetch",
"undici",
"jsonld",
"rdf-canonize-native",
],
plugins: [
...denoPlugins({
nodeModulesDir: true,
allowImports: {
"cdn.skypack.dev": true,
"unpkg.com": true,
},
}),
{
name: "node-modules-resolver",
setup(build) {
// Handle Node.js built-ins
build.onResolve({ filter: /^node:/ }, (args) => {
return { path: args.path, external: true };
});
},
},
{
name: "expose-validate-web",
setup(build) {
build.onEnd(() => {
const outfile = "./npm/web/psychds-validator.js";
let content = fs.readFileSync(outfile, "utf8");
if (!content.includes("window.psychDSValidator")) {
content +=
'\nif (typeof window !== "undefined") { window.psychDSValidator = { validateWeb }; }';
fs.writeFileSync(outfile, content);
}
});
},
},
],
});
} catch (error) {
console.error("Build failed with error:", error);
if (error.stack) {
console.error("Stack trace:", error.stack);
}
Deno.exit(1);
} finally {
esbuild.stop();
}