|
14 | 14 | # limitations under the License.
|
15 | 15 |
|
16 | 16 | # This script calls wasm-pack build and post-processes the generated TS types to fix them.
|
| 17 | +# It also produces three sets of outputs for different needs of different consumers |
17 | 18 | # Without this, the built wasm still works, but the Typescript definitions made by tsify don't.
|
18 | 19 | set -e
|
19 |
| -cargo build |
20 |
| -wasm-pack build --scope cedar-policy --target bundler |
21 |
| - |
22 |
| -sed -i "s/[{]\s*!: /{ \"!\": /g" pkg/cedar_wasm.d.ts |
23 |
| -sed -i "s/[{]\s*==: /{ \"==\": /g" pkg/cedar_wasm.d.ts |
24 |
| -sed -i "s/[{]\s*!=: /{ \"!=\": /g" pkg/cedar_wasm.d.ts |
25 |
| -sed -i "s/[{]\s*<: /{ \"<\": /g" pkg/cedar_wasm.d.ts |
26 |
| -sed -i "s/[{]\s*<=: /{ \"<=\": /g" pkg/cedar_wasm.d.ts |
27 |
| -sed -i "s/[{]\s*>: /{ \">\": /g" pkg/cedar_wasm.d.ts |
28 |
| -sed -i "s/[{]\s*>=: /{ \">=\": /g" pkg/cedar_wasm.d.ts |
29 |
| -sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" pkg/cedar_wasm.d.ts |
30 |
| -sed -i "s/[{]\s*||: /{ \"||\": /g" pkg/cedar_wasm.d.ts |
31 |
| -sed -i "s/[{]\s*[+]: /{ \"+\": /g" pkg/cedar_wasm.d.ts |
32 |
| -sed -i "s/[{]\s*-: /{ \"-\": /g" pkg/cedar_wasm.d.ts |
33 |
| -sed -i "s/[{]\s*[*]: /{ \"*\": /g" pkg/cedar_wasm.d.ts |
34 |
| -sed -i "s/[{]\s*\.: /{ \".\": /g" pkg/cedar_wasm.d.ts |
35 |
| -sed -i "s/ | __skip//g" pkg/cedar_wasm.d.ts |
36 |
| -sed -i "s/SchemaFragment/Schema/g" pkg/cedar_wasm.d.ts |
37 |
| - |
38 |
| -echo "type SmolStr = string;" >> pkg/cedar_wasm.d.ts |
39 |
| -echo "type Name = string;" >> pkg/cedar_wasm.d.ts |
40 |
| -echo "type Id = string;" >> pkg/cedar_wasm.d.ts |
41 |
| -echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> pkg/cedar_wasm.d.ts |
42 |
| -echo "export type Context = Record<string, CedarValueJson>;" >> pkg/cedar_wasm.d.ts |
43 |
| -echo "Finished post-processing types file" |
| 20 | +main () { |
| 21 | + rm -rf pkg || true |
| 22 | + mkdir pkg |
| 23 | + cargo build |
| 24 | + wasm-pack build --scope cedar-policy --target bundler --out-dir pkg/esm |
| 25 | + wasm-pack build --scope cedar-policy --target nodejs --out-dir pkg/nodejs |
| 26 | + wasm-pack build --scope cedar-policy --target web --out-dir pkg/web |
| 27 | + cp pkg/esm/README.md pkg/README.md |
| 28 | + |
| 29 | + fix_package_json_files |
| 30 | + |
| 31 | + process_types_file "pkg/esm/cedar_wasm.d.ts" |
| 32 | + process_types_file "pkg/nodejs/cedar_wasm.d.ts" |
| 33 | + process_types_file "pkg/web/cedar_wasm.d.ts" |
| 34 | +} |
| 35 | + |
| 36 | +fix_package_json_files() { |
| 37 | + jq -s '.[0] * .[1]' pkg/esm/package.json package.json.patch > pkg/package.json |
| 38 | + echo "Created root package.json" |
| 39 | + mv esm/package.json esm/package.json.bak |
| 40 | + mv web/package.json web/package.json.bak |
| 41 | + mv nodejs/package.json nodejs/package.json.bak |
| 42 | + jq -s '. + {"type": "module"}' esm/package.json.bak > esm/package.json |
| 43 | + jq -s '. + {"type": "module"}' web/package.json.bak > web/package.json |
| 44 | + jq -s '. + {"type": "commonjs"}' nodejs/package.json.bak > nodejs/package.json |
| 45 | + rm esm/package.json.bak |
| 46 | + rm web/package.json.bak |
| 47 | + rm nodejs/package.json.bak |
| 48 | + echo "Patched sub-package json files" |
| 49 | +} |
| 50 | + |
| 51 | +process_types_file() { |
| 52 | + local types_file="$1" |
| 53 | + echo "processing types file: $1" |
| 54 | + sed -i "s/[{]\s*!: /{ \"!\": /g" "$types_file" |
| 55 | + sed -i "s/[{]\s*==: /{ \"==\": /g" "$types_file" |
| 56 | + sed -i "s/[{]\s*!=: /{ \"!=\": /g" "$types_file" |
| 57 | + sed -i "s/[{]\s*<: /{ \"<\": /g" "$types_file" |
| 58 | + sed -i "s/[{]\s*<=: /{ \"<=\": /g" "$types_file" |
| 59 | + sed -i "s/[{]\s*>: /{ \">\": /g" "$types_file" |
| 60 | + sed -i "s/[{]\s*>=: /{ \">=\": /g" "$types_file" |
| 61 | + sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" "$types_file" |
| 62 | + sed -i "s/[{]\s*||: /{ \"||\": /g" "$types_file" |
| 63 | + sed -i "s/[{]\s*[+]: /{ \"+\": /g" "$types_file" |
| 64 | + sed -i "s/[{]\s*-: /{ \"-\": /g" "$types_file" |
| 65 | + sed -i "s/[{]\s*[*]: /{ \"*\": /g" "$types_file" |
| 66 | + sed -i "s/[{]\s*\.: /{ \".\": /g" "$types_file" |
| 67 | + sed -i "s/ | __skip//g" "$types_file" |
| 68 | + sed -i "s/SchemaFragment/SchemaJson/g" "$types_file" |
| 69 | + sed -i "s/[{] json: JsonValueWithNoDuplicateKeys /{ json: SchemaJson /g" "$types_file" |
| 70 | + |
| 71 | + echo "type SmolStr = string;" >> "$types_file" |
| 72 | + echo "type Name = string;" >> "$types_file" |
| 73 | + echo "type Id = string;" >> "$types_file" |
| 74 | + echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> "$types_file" |
| 75 | + echo "export type Context = Record<string, CedarValueJson>;" >> "$types_file" |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +main |
| 80 | +echo "Finished custom build script" |
0 commit comments