diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ba3384..6c032b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -106,12 +106,51 @@ jobs: - name: Test run: npm test + e2e: + name: Test + strategy: + matrix: + os: [ubuntu-latest] + node: [18.x] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout codes + uses: actions/checkout@v4 + + - name: Setup deno + uses: denoland/setup-deno@v1 + with: + deno-version: v1.x + + - name: Setup bun + uses: oven-sh/setup-bun@v1 + + - name: Setup node + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + + - name: Enable corepack + run: corepack enable + + - name: Install dependencies + run: bun install + + - name: Build codes + run: npm run build + + - name: Run test + run: ./scripts/e2e.sh + edge-release: name: Edge Release needs: - lint - build - test + - e2e runs-on: ${{ matrix.os }} strategy: matrix: diff --git a/.gitignore b/.gitignore index 97aed75..45f8f29 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ node_modules coverage .DS_Store .idea +playground/**/bun.lockb +playground/**/deno.lock *.log *.swp diff --git a/.npmignore b/.npmignore index 985bb72..d7627bf 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,5 @@ coverage -examples +playground deno.lock .* *.log diff --git a/bun.lockb b/bun.lockb index e177726..5c2dda0 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5b1ccf9..8b8cbfa 100644 --- a/package.json +++ b/package.json @@ -76,15 +76,20 @@ "test": "npm run test:typecheck && npm run test:unit", "test:unit": "NODE_OPTIONS=--experimental-vm-modules vitest run ./src", "test:typecheck": "vitest typecheck --config ./vitest.type.config.ts --run", + "test:coverage": "npm test -- --reporter verbose --coverage", "test:e2e": "run-s test:e2e:*", - "test:e2e:node": "npm run -w example-node test", + "test:e2e:node": "cd playground/node && node --test", "test:e2e:deno": "cd playground/deno && deno task test", - "test:e2e:bun": "npm run -w example-bun test", - "test:coverage": "npm test -- --reporter verbose --coverage", - "play:browser": "npm run -w example-browser dev", - "play:node": "npm run -w example-node dev", + "test:e2e:bun": "cd playground/bun && npm run test", + "setup": "run-s setup:*", + "setup:browser": "cd playground/browser && bun install", + "setup:node": "cd playground/node && bun install", + "setup:deno": "cd playground/deno && deno cache --reload ./main.ts", + "setup:bun": "cd playground/bun && bun install", + "play:browser": "cd playground/browser && npm run dev", + "play:node": "cd playground/node && npm run dev", "play:deno": "cd playground/deno && deno run --allow-net main.ts", - "play:bun": "npm run -w example-bun dev" + "play:bun": "cd playground/bun && npm run dev" }, "lint-staged": { "*.{js,ts,jsx,tsx,json,jsonc}": [ @@ -109,12 +114,10 @@ "npm-run-all": "^4.1.5", "miniflare": "^3.20231016.0", "supertest": "^6.3.3", + "pkg-types": "^1.0.2", "typescript": "^5.2.2", "unbuild": "^2.0.0", "vitest": "^0.34.6", "vitest-environment-miniflare": "^2.14.1" - }, - "workspaces": [ - "playground/*" - ] + } } diff --git a/playground/deno/deno.lock b/playground/deno/deno.lock deleted file mode 100644 index 81cd7c3..0000000 --- a/playground/deno/deno.lock +++ /dev/null @@ -1,39 +0,0 @@ -{ - "version": "3", - "remote": { - "https://deno.land/std@0.204.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", - "https://deno.land/std@0.204.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", - "https://deno.land/std@0.204.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", - "https://deno.land/std@0.204.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", - "https://deno.land/std@0.204.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", - "https://deno.land/std@0.204.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", - "https://deno.land/std@0.204.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", - "https://deno.land/std@0.204.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", - "https://deno.land/std@0.204.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", - "https://deno.land/std@0.204.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", - "https://deno.land/std@0.204.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", - "https://deno.land/std@0.204.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", - "https://deno.land/std@0.204.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", - "https://deno.land/std@0.204.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", - "https://deno.land/std@0.204.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", - "https://deno.land/std@0.204.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", - "https://deno.land/std@0.204.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", - "https://deno.land/std@0.204.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", - "https://deno.land/std@0.204.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", - "https://deno.land/std@0.204.0/assert/assert_not_strict_equals.ts": "ca6c6d645e95fbc873d25320efeb8c4c6089a9a5e09f92d7c1c4b6e935c2a6ad", - "https://deno.land/std@0.204.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", - "https://deno.land/std@0.204.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", - "https://deno.land/std@0.204.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", - "https://deno.land/std@0.204.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", - "https://deno.land/std@0.204.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", - "https://deno.land/std@0.204.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", - "https://deno.land/std@0.204.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", - "https://deno.land/std@0.204.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", - "https://deno.land/std@0.204.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", - "https://deno.land/std@0.204.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", - "https://deno.land/std@0.204.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", - "https://deno.land/std@0.204.0/fmt/colors.ts": "c51c4642678eb690dcf5ffee5918b675bf01a33fba82acf303701ae1a4f8c8d9", - "https://esm.sh/@intlify/utils@0.10.0": "0fe85fa01c25d7db7f11169ff8988961fcdd7e336b40456647495cfcc3923837", - "https://esm.sh/v133/@intlify/utils@0.10.0/denonext/utils.mjs": "302b975d6ff52aa5bc2932463f1bf65e54b18d26d3ca486f13813bcd13baae31" - } -} diff --git a/playground/deno/main.ts b/playground/deno/main.ts index c4d90eb..8b01aec 100644 --- a/playground/deno/main.ts +++ b/playground/deno/main.ts @@ -1,11 +1,11 @@ // @ts-ignore: this is example -import { getHeaderLanguages } from "@intlify/utils"; +import { getHeaderLanguages } from '@intlify/utils' -const port = 8125; +const port = 8125 Deno.serve({ port, }, (req: Request) => { - const languages = getHeaderLanguages(req); - return new Response(`detect accpect-language: ${languages}`); -}); -console.log(`server listening on ${port}`); + const languages = getHeaderLanguages(req) + return new Response(`detect accpect-language: ${languages}`) +}) +console.log(`server listening on ${port}`) diff --git a/playground/node/package.json b/playground/node/package.json index 9f5e43e..d7fc08b 100644 --- a/playground/node/package.json +++ b/playground/node/package.json @@ -9,9 +9,12 @@ "dependencies": { "@intlify/utils": "npm:@intlify/utils-edge@latest" }, + "peerDependencies": { + "typescript": "^5.2.2" + }, "devDependencies": { "ofetch": "^1.3.3", - "@types/node": "^20.6.0", - "typescript": "^5.2.2" + "bun-types": "latest", + "@types/node": "^20.6.0" } } diff --git a/scripts/e2e.sh b/scripts/e2e.sh new file mode 100755 index 0000000..483e73e --- /dev/null +++ b/scripts/e2e.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +# Pack packages +npm pack + +# Replace deps +bun run ./scripts/replaceDeps.ts + +# show the diff of deps +git diff + +# setup playground/* for e2e +npm run setup + +# just do e2e! +npm run test:e2e diff --git a/scripts/replaceDeps.ts b/scripts/replaceDeps.ts new file mode 100644 index 0000000..e4aa724 --- /dev/null +++ b/scripts/replaceDeps.ts @@ -0,0 +1,65 @@ +import { constants as FS_CONSTANTS, promises as fs } from 'node:fs' +import { resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import { readPackageJSON, writePackageJSON } from 'pkg-types' + +const __dirname = fileURLToPath(new URL('.', import.meta.url)) + +export async function isExists(path: string) { + try { + await fs.access(path, FS_CONSTANTS.F_OK) + return true + } catch { + return false + } +} + +type Platform = 'browser' | 'node' | 'deno' | 'bun' + +async function replaceNodePlatform(platform: 'node' | 'bun', playgroundPath: string) { + const utilsPath = resolve(__dirname, '..') + const utilsPkg = await readPackageJSON(resolve(utilsPath, 'package.json')) + const utilsTgzPath = resolve(utilsPath, `intlify-utils-${utilsPkg.version}.tgz`) + if (!await isExists(utilsTgzPath)) { + return false + } + const targetPath = resolve(playgroundPath, platform, 'package.json') + const platformPkg = await readPackageJSON(targetPath) + platformPkg.dependencies![`@intlify/utils`] = `file:${utilsTgzPath}` + await writePackageJSON(targetPath, platformPkg) + return true +} + +async function replaceDenoPlatform(playgroundPath: string) { + const denoConfigPath = resolve(playgroundPath, 'deno', 'deno.jsonc') + const denoConfig = JSON.parse(await fs.readFile(denoConfigPath, 'utf-8')) as { + imports: Record + } + denoConfig['imports']['@intlify/utils'] = '../../src/index.ts' + denoConfig['imports']['cookie-es'] = 'npm:cookie-es@1.0.0' + + await fs.writeFile(denoConfigPath, JSON.stringify(denoConfig), 'utf-8') + return true +} + +async function main() { + const playgroundPath = resolve(__dirname, '../playground') + for (const platform of (await fs.readdir(playgroundPath)) as Platform[]) { + if (platform === 'node' || platform === 'bun') { + if (!await replaceNodePlatform(platform, playgroundPath)) { + console.error(`cannot replace '@intlify/utils' dependency on ${platform}`) + } + } else if (platform === 'deno') { + if (!await replaceDenoPlatform(playgroundPath)) { + console.error(`cannot replace '@intlify/utils' dependency on ${platform}`) + } + } else { // for browser + // TODO: + } + } +} + +main().catch((err) => { + console.error(err) + process.exit(1) +})