diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 29fcfa0a0..6bb38a575 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -139,7 +139,7 @@ jobs: path: ~/.cache/deno key: cache-${{ env.CAPI_SHA }} - run: deno run -A _tasks/use_remote.ts - - run: deno task test:eg + - run: deno task test:eg:deno - name: Archive target uses: actions/upload-artifact@v3 if: failure() || cancelled() @@ -149,3 +149,32 @@ jobs: target !target/**/db/full/* !target/**/paritydb/full/* + examples-node: + name: Examples (Node) + runs-on: ubuntu-latest + timeout-minutes: 15 + needs: cache + steps: + - uses: actions/checkout@v3 + - uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.0 + with: + deno-version: v1.33.3 + - uses: actions/cache@v3 + with: + path: ~/.cache/deno + key: cache-${{ env.CAPI_SHA }} + - run: deno run -A _tasks/use_remote.ts + - run: deno task dnt --server ${{ env.CAPI_SHA }} --examples + - uses: actions/setup-node@v3 + with: + node-version: "20" + - run: deno task test:eg:node + - name: Archive target + uses: actions/upload-artifact@v3 + if: failure() || cancelled() + with: + name: examples-node-target + path: | + target + !target/**/db/full/* + !target/**/paritydb/full/* diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d36af01d1..aca45c66b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,12 +22,10 @@ jobs: registry-url: "https://registry.npmjs.org" - name: Build NPM Package run: deno task dnt --version ${{steps.get_tag_version.outputs.tag_version}} --server ${{steps.get_tag_version.outputs.tag_version}} - - run: npm pack - working-directory: "./target/npm" - uses: actions/upload-artifact@v3 with: name: package - path: "./target/npm/*.tgz" + path: "./target/npm/artifacts/capi*.tgz" - uses: octokit/request-action@v2.x if: startsWith(github.ref, 'refs/tags/') with: diff --git a/_tasks/dnt.ts b/_tasks/dnt.ts index 7615ef727..13ad50568 100755 --- a/_tasks/dnt.ts +++ b/_tasks/dnt.ts @@ -6,19 +6,30 @@ import importMap from "../import_map.json" assert { type: "json" } import * as nets from "../nets.ts" import { normalizePackageName } from "../util/normalize.ts" -const { version: packageVersion, server: serverVersion } = flags.parse(Deno.args, { - string: ["version", "server"], - default: { - version: "v0.0.0-local", +const { + version: packageVersion, + server: serverVersion, + examples: buildExamples, +} = flags.parse( + Deno.args, + { + string: ["version", "server"], + boolean: ["examples"], + default: { + version: "v0.0.0-local", + }, }, -}) +) const server = serverVersion ? `https://capi.dev/@${serverVersion}/` : "http://localhost:4646/" -const hash = new URL(importMap.imports["@capi/"]).pathname.slice(1, -1) +const hash = new URL(importMap.imports["@capi/"]).pathname.split("/").at(-2)! -const outDir = path.join("target", "npm") +const outDir = path.join(Deno.cwd(), "target", "npm") +const capiOutDir = path.join(outDir, "capi") +const examplesOutDir = path.join(outDir, "capi-examples") await fs.emptyDir(outDir) +await Deno.mkdir(capiOutDir) const entryPoints: EntryPoint[] = [] const mappings: Record = {} @@ -47,6 +58,12 @@ for (const pathname of allFiles) { } } +const capiCodegenDeps = Object.fromEntries( + Object.keys(nets).map((key) => normalizePackageName(key)).map(( + packageName, + ) => [`@capi/${packageName}`, `${server}${hash}/${packageName}.tar`]), +) + await Promise.all([ build({ package: { @@ -57,12 +74,7 @@ await Promise.all([ license: "Apache-2.0", repository: "github:paritytech/capi", dependencies: { - ...Object.fromEntries( - Object.keys(nets).map((key) => { - const name = normalizePackageName(key) - return [`@capi/${name}`, `${server}${hash}/${name}.tar`] - }), - ), + ...capiCodegenDeps, "ts-node": "^10.9.1", }, }, @@ -110,11 +122,12 @@ await Promise.all([ "node:net": "node:net", "node:http": "node:http", "node:stream": "node:stream", + "node:fs": "node:fs", "node:fs/promises": "node:fs/promises", "https://raw.githubusercontent.com/paritytech/capi-binary-builds/f5baeca/streamToFile.ts": "https://raw.githubusercontent.com/paritytech/capi-binary-builds/f5baeca/streamToFile.node.ts", }, - outDir, + outDir: capiOutDir, shims: { deno: true, custom: [{ @@ -131,25 +144,25 @@ await Promise.all([ test: false, typeCheck: false, }), - fs.copy("LICENSE", path.join(outDir, "LICENSE")), - fs.copy("Readme.md", path.join(outDir, "Readme.md")), - fs.copy("server/static/", path.join(outDir, "esm/server/static/")), + fs.copy("LICENSE", path.join(capiOutDir, "LICENSE")), + fs.copy("Readme.md", path.join(capiOutDir, "Readme.md")), + fs.copy("server/static/", path.join(capiOutDir, "esm/server/static/")), ]) await Promise.all([ fs.copy( - "target/npm/src/rune/_empty.d.ts", - "target/npm/types/rune/_empty.d.ts", + path.join(capiOutDir, "src/rune/_empty.d.ts"), + path.join(capiOutDir, "types/rune/_empty.d.ts"), { overwrite: true }, ), editFile( - "target/npm/esm/main.js", + path.join(capiOutDir, "esm/main.js"), (content) => content .replace(/^#!.+/, "#!/usr/bin/env -S node --loader ts-node/esm"), ), editFile( - "target/npm/esm/_dnt.shims.js", + path.join(capiOutDir, "esm/_dnt.shims.js"), (content) => content .replace(/"@deno\/shim-deno"/g, `"./deps/shims/Deno.node.js"`), @@ -159,3 +172,77 @@ await Promise.all([ async function editFile(path: string, modify: (content: string) => string) { await Deno.writeTextFile(path, modify(await Deno.readTextFile(path))) } + +await new Deno.Command("npm", { + args: ["pack", "--pack-destination", outDir], + cwd: capiOutDir, +}).output() + +if (buildExamples) { + const exampleEntryPoints: EntryPoint[] = [] + for await ( + const { path } of fs.walkSync(".", { + exts: [".eg.ts"], + includeDirs: false, + }) + ) { + exampleEntryPoints.push({ + name: path, + path: `./${path}`, + }) + } + + await build({ + package: { + name: "capi-examples", + version: packageVersion, + type: "module", + devDependencies: { + "ts-node": "^10.9.1", + }, + dependencies: { + ...capiCodegenDeps, + capi: `file:../capi-${packageVersion}.tgz`, + }, + }, + compilerOptions: { + importHelpers: true, + sourceMap: true, + target: "ES2021", + lib: ["es2022.error", "dom.iterable"], + }, + entryPoints: exampleEntryPoints, + mappings: { + "https://deno.land/x/polkadot@0.2.38/keyring/mod.ts": { + name: "@polkadot/keyring", + version: "12.2.1", + }, + "https://deno.land/x/polkadot@0.2.38/types/mod.ts": { + name: "@polkadot/types", + version: "10.7.2", + }, + }, + importMap: "_tasks/dnt_examples_import_map.json", + outDir: examplesOutDir, + scriptModule: false, + declaration: false, + shims: { deno: true }, + test: false, + typeCheck: false, + }) + + await Promise.all( + [ + fs.copy( + "examples/ink/erc20.json", + path.join(examplesOutDir, "esm/examples/ink/erc20.json"), + { overwrite: true }, + ), + fs.copy( + "examples/ink/erc20.wasm", + path.join(examplesOutDir, "esm/examples/ink/erc20.wasm"), + { overwrite: true }, + ), + ], + ) +} diff --git a/_tasks/dnt_examples_import_map.json b/_tasks/dnt_examples_import_map.json new file mode 100644 index 000000000..bbe34b49f --- /dev/null +++ b/_tasks/dnt_examples_import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "asserts": "../deps/std/testing/asserts.ts" + } +} diff --git a/deno.jsonc b/deno.jsonc index 7e613aca9..68b15a7b0 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -34,7 +34,9 @@ "dnt": "deno task run _tasks/dnt.ts", "test": "deno task capi serve -- deno test -A -L=info --ignore=target --parallel -r=http://localhost:4646/", "test:update": "deno task test -- --update", - "test:eg": "deno task run https://deno.land/x/egts@v0.1.0-beta.3/test.ts 'examples/**/*.eg.ts'", + "test:eg": "deno task run https://deno.land/x/egts@v0.1.0-beta.6/main.ts", + "test:eg:deno": "deno task test:eg test deno 'examples/**/*.eg.ts'", + "test:eg:node": "deno task test:eg test node 'target/npm/capi-examples/esm/examples/**/*.eg.js'", "moderate": "deno run -A https://deno.land/x/moderate@0.0.5/mod.ts --exclude '*.test.ts' '*.node.ts' && dprint fmt", "capi": "deno run -A main.ts", "cache": "deno task capi serve -- deno cache -r=http://localhost:4646/", diff --git a/deps/shims/Deno.node.ts b/deps/shims/Deno.node.ts index 45b1f4645..0ffd80d18 100644 --- a/deps/shims/Deno.node.ts +++ b/deps/shims/Deno.node.ts @@ -1,3 +1,4 @@ +import * as fs from "node:fs" import { Command } from "./command.node.ts" declare const process: { env: Record } @@ -19,6 +20,8 @@ let _deno = { }, toObject: () => ({ ...process.env }), }, + readFileSync: (path: string | URL): Uint8Array => fs.readFileSync(path), + readTextFileSync: (path: string | URL): string => fs.readFileSync(path, "utf8"), } as any as typeof globalThis.Deno export const Deno = new Proxy( diff --git a/examples/blocks.eg.ts b/examples/blocks.eg.ts index f6e6481f8..48e7de69e 100644 --- a/examples/blocks.eg.ts +++ b/examples/blocks.eg.ts @@ -8,7 +8,7 @@ import { $eventRecord, metadata, polkadot } from "@capi/polkadot" import { $, $extrinsic, known, Rune } from "capi" -import { babeBlockAuthor } from "capi/patterns/consensus/mod.ts" +import { babeBlockAuthor } from "capi/patterns/consensus" /// Reference the latest block hash. const blockHash = polkadot.blockHash() diff --git a/examples/dev/storage_sizes.eg.ts b/examples/dev/storage_sizes.eg.ts index 5afff8c0c..6002b0e13 100644 --- a/examples/dev/storage_sizes.eg.ts +++ b/examples/dev/storage_sizes.eg.ts @@ -9,7 +9,7 @@ import { polkadotDev } from "@capi/polkadot-dev" import { $ } from "capi" -import { storageSizes } from "capi/patterns/storage_sizes.ts" +import { storageSizes } from "capi/patterns/storage_sizes" /// Use the storageSizes factory to produce a Rune. Then execute it. const sizes = await storageSizes(polkadotDev).run() diff --git a/examples/ink/deploy.eg.ts b/examples/ink/deploy.eg.ts index a8fefefc6..008453606 100644 --- a/examples/ink/deploy.eg.ts +++ b/examples/ink/deploy.eg.ts @@ -8,12 +8,12 @@ import { contractsDev } from "@capi/contracts-dev" import { $, createDevUsers, hex, Sr25519, ss58 } from "capi" -import { InkMetadataRune } from "capi/patterns/ink/mod.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { InkMetadataRune } from "capi/patterns/ink" +import { signature } from "capi/patterns/signature/polkadot" /// Initialize an `InkMetadataRune` with the raw Ink metadata text. const metadata = InkMetadataRune.fromMetadataText( - Deno.readTextFileSync(new URL(import.meta.resolve("./erc20.json"))), + Deno.readTextFileSync(new URL("./erc20.json", import.meta.url)), ) /// Given that other examples may utilize this script, we'll allow the diff --git a/examples/ink/interact.eg.ts b/examples/ink/interact.eg.ts index 767f3398c..bcd964dd3 100644 --- a/examples/ink/interact.eg.ts +++ b/examples/ink/interact.eg.ts @@ -9,8 +9,8 @@ import { contractsDev } from "@capi/contracts-dev" import { assert } from "asserts" import { $, createDevUsers, hex } from "capi" -import { InkMetadataRune } from "capi/patterns/ink/mod.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { InkMetadataRune } from "capi/patterns/ink" +import { signature } from "capi/patterns/signature/polkadot" /// Get two test users. Alexa will deploy, Billy will be the recipient of an erc20 /// token transfer. @@ -23,7 +23,7 @@ await import("./deploy.eg.ts") const address = Deno.env.get("CONTRACT_SS58_ADDRESS")! /// Initialize an `InkMetadataRune` with the raw Ink metadata text. -export const metadata = InkMetadataRune.fromMetadataText( +const metadata = InkMetadataRune.fromMetadataText( Deno.readTextFileSync(new URL("./erc20.json", import.meta.url)), ) diff --git a/examples/misc/identity.eg.ts b/examples/misc/identity.eg.ts index 201af58c0..015876ecf 100644 --- a/examples/misc/identity.eg.ts +++ b/examples/misc/identity.eg.ts @@ -8,8 +8,8 @@ import { polkadotDev } from "@capi/polkadot-dev" import { $, createDevUsers, is } from "capi" -import { IdentityInfoTranscoders } from "capi/patterns/identity.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { IdentityInfoTranscoders } from "capi/patterns/identity" +import { signature } from "capi/patterns/signature/polkadot" const { alexa } = await createDevUsers() diff --git a/examples/misc/indices.eg.ts b/examples/misc/indices.eg.ts index 1bd7af9af..c05ee0261 100644 --- a/examples/misc/indices.eg.ts +++ b/examples/misc/indices.eg.ts @@ -8,7 +8,7 @@ import { polkadotDev } from "@capi/polkadot-dev" import { assertEquals } from "asserts" import { createDevUsers, is } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" const { alexa } = await createDevUsers() diff --git a/examples/multisig/basic.eg.ts b/examples/multisig/basic.eg.ts index d3d317c47..87879cb75 100644 --- a/examples/multisig/basic.eg.ts +++ b/examples/multisig/basic.eg.ts @@ -8,8 +8,8 @@ import { polkadotDev } from "@capi/polkadot-dev" import { assert } from "asserts" import { $, createDevUsers, is } from "capi" -import { MultisigRune } from "capi/patterns/multisig/mod.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { MultisigRune } from "capi/patterns/multisig" +import { signature } from "capi/patterns/signature/polkadot" const { alexa, billy, carol, david } = await createDevUsers() diff --git a/examples/multisig/stash.eg.ts b/examples/multisig/stash.eg.ts index 9d3b7bc7a..d17260cd3 100644 --- a/examples/multisig/stash.eg.ts +++ b/examples/multisig/stash.eg.ts @@ -8,9 +8,9 @@ import { MultiAddress, polkadotDev } from "@capi/polkadot-dev" import { assert } from "asserts" import { createDevUsers, is } from "capi" -import { MultisigRune } from "capi/patterns/multisig/mod.ts" -import { filterPureCreatedEvents } from "capi/patterns/proxy/mod.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { MultisigRune } from "capi/patterns/multisig" +import { filterPureCreatedEvents } from "capi/patterns/proxy" +import { signature } from "capi/patterns/signature/polkadot" const { alexa, billy, carol } = await createDevUsers() diff --git a/examples/multisig/virtual.eg.ts b/examples/multisig/virtual.eg.ts index 905077611..6b14ebc47 100644 --- a/examples/multisig/virtual.eg.ts +++ b/examples/multisig/virtual.eg.ts @@ -20,8 +20,8 @@ import { MultiAddress, polkadotDev } from "@capi/polkadot-dev" import { assert } from "asserts" import { $, createDevUsers, is, Rune, Sr25519 } from "capi" -import { VirtualMultisigRune } from "capi/patterns/multisig/mod.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { VirtualMultisigRune } from "capi/patterns/multisig" +import { signature } from "capi/patterns/signature/polkadot" import { parse } from "../../deps/std/flags.ts" const { alexa, billy, carol, david } = await createDevUsers() diff --git a/examples/nfts.eg.ts b/examples/nfts.eg.ts index 8cd95fbed..ebe5ea01f 100644 --- a/examples/nfts.eg.ts +++ b/examples/nfts.eg.ts @@ -16,8 +16,8 @@ import { } from "@capi/rococo-dev-westmint" import { assertEquals } from "asserts" import { $, createDevUsers, is, Rune } from "capi" -import { DefaultCollectionSetting, DefaultItemSetting } from "capi/patterns/nfts.ts" -import { signature } from "capi/patterns/signature/statemint.ts" +import { DefaultCollectionSetting, DefaultItemSetting } from "capi/patterns/nfts" +import { signature } from "capi/patterns/signature/statemint" /// Create two dev users. Alexa will mint and list the NFT. Billy will purchase it. const { alexa, billy } = await createDevUsers() diff --git a/examples/sign/ed25519.eg.ts b/examples/sign/ed25519.eg.ts index 01c6dceb5..9d3f62cbf 100644 --- a/examples/sign/ed25519.eg.ts +++ b/examples/sign/ed25519.eg.ts @@ -7,7 +7,7 @@ import { MultiAddress, westendDev } from "@capi/westend-dev" import { assert } from "asserts" import { createDevUsers, ExtrinsicSender, is } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" import * as ed from "../../deps/ed25519.ts" const { alexa, billy } = await createDevUsers() diff --git a/examples/sign/offline.eg.ts b/examples/sign/offline.eg.ts index fc470e437..d791ada85 100644 --- a/examples/sign/offline.eg.ts +++ b/examples/sign/offline.eg.ts @@ -7,7 +7,7 @@ import { $runtimeCall, westendDev } from "@capi/westend-dev" import { $, createDevUsers, SignedExtrinsicRune } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" const { alexa, billy } = await createDevUsers() diff --git a/examples/sign/pjs.eg.ts b/examples/sign/pjs.eg.ts index c06a9820b..c523daf3d 100644 --- a/examples/sign/pjs.eg.ts +++ b/examples/sign/pjs.eg.ts @@ -7,8 +7,8 @@ import { polkadotDev } from "@capi/polkadot-dev" import { createDevUsers, ss58 } from "capi" -import { pjsSender, PjsSigner } from "capi/patterns/compat/pjs_sender.ts" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { pjsSender, PjsSigner } from "capi/patterns/compat/pjs_sender" +import { signature } from "capi/patterns/signature/polkadot" import { createPair } from "https://deno.land/x/polkadot@0.2.38/keyring/mod.ts" import { TypeRegistry } from "https://deno.land/x/polkadot@0.2.38/types/mod.ts" diff --git a/examples/tx/balances_transfer.eg.ts b/examples/tx/balances_transfer.eg.ts index a8717557c..2bf6bf11a 100644 --- a/examples/tx/balances_transfer.eg.ts +++ b/examples/tx/balances_transfer.eg.ts @@ -7,7 +7,7 @@ import { westendDev } from "@capi/westend-dev" import { assert } from "asserts" import { createDevUsers, is } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" /// Create two dev users. Alexa will send the funds to Billy. const { alexa, billy } = await createDevUsers() diff --git a/examples/tx/handle_errors.eg.ts b/examples/tx/handle_errors.eg.ts index a13bdec83..a4594a330 100644 --- a/examples/tx/handle_errors.eg.ts +++ b/examples/tx/handle_errors.eg.ts @@ -8,7 +8,7 @@ import { contractsDev } from "@capi/contracts-dev" import { assertInstanceOf } from "asserts" import { createDevUsers, ExtrinsicError, is } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" const { alexa, billy } = await createDevUsers() diff --git a/examples/tx/utility_batch.eg.ts b/examples/tx/utility_batch.eg.ts index 32204ddaf..4b2d7631e 100644 --- a/examples/tx/utility_batch.eg.ts +++ b/examples/tx/utility_batch.eg.ts @@ -7,7 +7,7 @@ import { westendDev } from "@capi/westend-dev" import { assert } from "asserts" import { createDevUsers, is, Rune } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" /// Create four dev users, one of whom will be the batch sender. The other /// three will be recipients of balance transfers described in the batch. diff --git a/examples/xcm/asset_teleportation.eg.ts b/examples/xcm/asset_teleportation.eg.ts index 654960850..180ac6641 100644 --- a/examples/xcm/asset_teleportation.eg.ts +++ b/examples/xcm/asset_teleportation.eg.ts @@ -27,7 +27,7 @@ import { } from "@capi/rococo-dev-westmint" import { assert } from "asserts" import { createDevUsers, is, Rune } from "capi" -import { signature } from "capi/patterns/signature/polkadot.ts" +import { signature } from "capi/patterns/signature/polkadot" const { alexa } = await createDevUsers() diff --git a/examples/xcm/reserve_transfer.eg.ts b/examples/xcm/reserve_transfer.eg.ts index 76c807921..1885976e9 100644 --- a/examples/xcm/reserve_transfer.eg.ts +++ b/examples/xcm/reserve_transfer.eg.ts @@ -28,8 +28,8 @@ import { import { rococoDevXcmTrappist } from "@capi/rococo-dev-xcm-trappist" import { assert, assertNotEquals } from "asserts" import { $, alice as root, createDevUsers, is, Rune, ValueRune } from "capi" -import { $siblId } from "capi/patterns/para_id.ts" -import { signature } from "capi/patterns/signature/statemint.ts" +import { $siblId } from "capi/patterns/para_id" +import { signature } from "capi/patterns/signature/statemint" import { retry } from "../../deps/std/async.ts" const { alexa, billy } = await createDevUsers() diff --git a/import_map.json b/import_map.json index a9d1cf965..6051e73cf 100644 --- a/import_map.json +++ b/import_map.json @@ -6,7 +6,17 @@ "examples/": { "asserts": "./deps/std/testing/asserts.ts", "capi": "./mod.ts", - "capi/patterns/": "./patterns/" + "capi/patterns/compat/pjs_sender": "./patterns/compat/pjs_sender.ts", + "capi/patterns/consensus": "./patterns/consensus/mod.ts", + "capi/patterns/identity": "./patterns/identity.ts", + "capi/patterns/ink": "./patterns/ink/mod.ts", + "capi/patterns/multisig": "./patterns/multisig/mod.ts", + "capi/patterns/nfts": "./patterns/nfts.ts", + "capi/patterns/para_id": "./patterns/para_id.ts", + "capi/patterns/proxy": "./patterns/proxy/mod.ts", + "capi/patterns/signature/polkadot": "./patterns/signature/polkadot.ts", + "capi/patterns/signature/statemint": "./patterns/signature/statemint.ts", + "capi/patterns/storage_sizes": "./patterns/storage_sizes.ts" }, "http://localhost:4646/": { "http://localhost:4646/capi/": "./" diff --git a/words.txt b/words.txt index d631202fa..1e1af2128 100644 --- a/words.txt +++ b/words.txt @@ -14,6 +14,7 @@ chelios chev childstate codegen +compat concat deno denoland