Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno/Isolated Modules support, and GitHub Actions to test and fix #269

Merged
merged 17 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/fix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Fix, Format, Regenerate

on:
push:
create:
schedule:
- cron: '33 3 * * SAT'
workflow_dispatch:

jobs:
fix:
runs-on: Ubuntu-20.04
name: Fix
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- run: yarn install
- run: yarn fix:lint
- run: yarn build:deno
- run: yarn fix:format
- name: Check for Changes
id: check
run: |
git add .
git status
declare had_changes="$(
[[ -n "$(git status --porcelain)" ]] && echo true || echo false
)"
echo "::set-output name=had-changes::${had_changes}"
- name: Push All Changes
if: steps.check.outputs.had-changes == 'true'
run: |
declare br="
"
declare branch="$(echo ${GITHUB_REF#refs/heads/})"
declare date="$(git log -1 --pretty=format:'%ad')"
declare short_hash="$(git rev-parse --short=8 HEAD)"
declare short_subject="$(git log -1 --pretty=format:'%s')"
declare short_subject="$(git log -1 --pretty=format:'%s')"
if [[ ${#short_subject} -gt 17 ]]; then
short_subject="${short_subject:0:16}…"
fi
declare author_name="$(git log -1 --pretty=format:'%an')"
declare author_email="$(git log -1 --pretty=format:'%ae')"
declare current_action_url="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${{ github.run_id }}"
export GIT_AUTHOR_DATE="${date}"
export GIT_AUTHOR_NAME="${author_name}"
export GIT_AUTHOR_EMAIL="${author_email}"
export GIT_COMMITTER_DATE="${date}"
export GIT_COMMITTER_NAME="github-actions"
export GIT_COMMITTER_EMAIL="41898282+github-actions[bot]@users.noreply.github.com"
git remote rm origin
git remote add origin "https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git"
git checkout "${branch}"
git add .
git commit -m "chore: automatic fixes and code generation for ${short_hash} (${short_subject})${br}${br}${current_action_url}"
git push --set-upstream origin "${branch}"
60 changes: 60 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Test and Lint

on:
push:
create:
pull_request:
schedule:
- cron: '44 4 * * SAT'
workflow_dispatch:

jobs:
test-node:
runs-on: Ubuntu-20.04
strategy:
matrix:
node: [ '14' ]
typescript: [ '3.8', '4.1' ]
name: Test with TypeScript ${{ matrix.typescript }} on Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: yarn install
- run: yarn add typescript@${{ matrix.typescript }}
- run: yarn build
- run: yarn test

test-deno:
runs-on: Ubuntu-20.04
strategy:
matrix:
deno: [ '1.6' ]
name: Test with Deno ${{ matrix.deno }}
steps:
- uses: actions/checkout@v2
- uses: denolib/setup-deno@v2
with:
deno-version: ${{ matrix.deno }}
- run: deno test
working-directory: ./deno/lib
- run: deno run ./index.ts
working-directory: ./deno/lib
- run: deno run ./mod.ts
working-directory: ./deno/lib

lint:
runs-on: Ubuntu-20.04
strategy:
matrix:
node: [ '14' ]
name: Lint on Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: yarn install
- run: yarn check:format
- run: yarn check:lint
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**/.DS_Store
node_modules
lib
/lib
coverage
.vscode
*.log
*.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ npm install zod

#### TypeScript requirements

- Zod 3.x requires TypeScript 3.8+
- Zod 2.x requires TypeScript 3.7+
- Zod 1.x requires TypeScript 3.3+

Expand Down
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions deno/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// This script expects to be run via `yarn build:deno`.
//
// Although this script generates code for use in Deno, this script itself is
// written for Node so that contributors do not need to install Deno to build.
//
// @ts-check

import {
mkdirSync,
readdirSync,
readFileSync,
statSync,
writeFileSync,
} from "fs";
import { dirname } from "path";

// Node's path.join() normalize explicitly-relative paths like "./index.ts" to
// paths like "index.ts" which don't work as relative ES imports, so we do this.
const join = (/** @type string[] */ ...parts) => parts.join("/");

const projectRoot = process.cwd();
const nodeSrcRoot = join(projectRoot, "src");
const denoLibRoot = join(projectRoot, "deno", "lib");

const walkAndBuild = (/** @type string */ dir) => {
for (const entry of readdirSync(join(nodeSrcRoot, dir), {
withFileTypes: true,
encoding: "utf-8",
})) {
if (entry.isDirectory()) {
walkAndBuild(join(dir, entry.name));
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
const nodePath = join(nodeSrcRoot, dir, entry.name);
const denoPath = join(denoLibRoot, dir, entry.name);

const nodeSource = readFileSync(nodePath, { encoding: "utf-8" });

const denoSource = nodeSource.replace(
/^(?:import|export)[\s\S]*?from\s*['"]([^'"]*)['"];$/gm,
(line, target) => {
if (target === "@jest/globals") {
return `import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";\nconst test = Deno.test;`;
}

const targetNodePath = join(dirname(nodePath), target);
const targetNodePathIfFile = targetNodePath + ".ts";
const targetNodePathIfDir = join(targetNodePath, "index.ts");

try {
if (statSync(targetNodePathIfFile)?.isFile()) {
return line.replace(target, target + ".ts");
}
} catch (error) {
if (error?.code !== "ENOENT") {
throw error;
}
}

try {
if (statSync(targetNodePathIfDir)?.isFile()) {
return line.replace(target, join(target, "index.ts"));
}
} catch (error) {
if (error?.code !== "ENOENT") {
throw error;
}
}

console.warn(`Skipping non-resolvable import:\n ${line}`);
return line;
}
);

mkdirSync(dirname(denoPath), { recursive: true });
writeFileSync(denoPath, denoSource, { encoding: "utf-8" });
}
}
};

walkAndBuild("");

writeFileSync(join(denoLibRoot, "mod.ts"), `export * from "./index.ts";\n`, {
encoding: "utf-8",
});
Loading