Skip to content
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
67 changes: 67 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Build and Publish

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build-and-publish:
name: "Build and Publish"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Install Dependencies
run: |
corepack enable
pnpm i --frozen-lockfile
- name: Build
run: |
pnpm compile
for PACKAGE in packages/client-component-transforms packages/server-function-transforms packages/framework; do
pushd $PACKAGE
pnpm lint
popd
done
- name: Push to GitHub
if: github.event_name != 'pull_request'
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
export PACKAGE_NAMESPACE=$GITHUB_REPOSITORY_OWNER

# set repository
for PACKAGE_JSON in packages/*/package.json; do jq ".repository |= \"https://github.com/$GITHUB_REPOSITORY\"" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done

# rename package versions
export PACKAGE_VERSION_SUFFIX="dev.$(date +%Y.%m.%d.%H.%M.%S)+$(git rev-parse --short HEAD)"
for PACKAGE_JSON in packages/*/package.json; do jq ".version |= (match(\"^([0-9.]+)\").captures[0].string + \"-$PACKAGE_VERSION_SUFFIX\")" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done

# rename package names
for PACKAGE_JSON in packages/*/package.json; do jq ".name |= sub(\"@twofold\"; \"@$PACKAGE_NAMESPACE\")" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done

# rename package references
for PACKAGE_JSON in packages/*/package.json; do jq ".dependencies |= (to_entries | map({key: (.key | sub(\"@twofold/\"; \"@$PACKAGE_NAMESPACE/\")), value: .value}) | from_entries)" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done

# compile and package again
# PACKAGE_NAMESPACE is read by compile.js and rewrites imports
# in built code once compiled
pnpm compile
pnpm -r exec pnpm pack

# set npmrc to auth with GitHub
cat >.npmrc <<EOF
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
@{GITHUB_REPOSITORY_OWNER}:registry=https://npm.pkg.github.com/
always-auth=true
EOF

# push packages
for TARBALL in $(find packages/ -name *.tgz); do
pnpm publish $TARBALL
done
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.env
.DS_Store

*.tgz
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
44 changes: 44 additions & 0 deletions packages/framework/compile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import { build } from "esbuild";
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { rm } from "fs/promises";
import path from "path";

const rewriteImports = {
name: "rewrite-imports",
async setup(build) {
let outDir;
build.onStart(() => {
const options = build.initialOptions;
const absWorkingDir = options.absWorkingDir ?? process.cwd();
outDir = options.outdir
? path.resolve(absWorkingDir, options.outdir)
: options.outfile
? path.dirname(path.resolve(absWorkingDir, options.outfile))
: null;
});
build.onEnd(async (args) => {
if (outDir) {
for (const file of readdirSync(outDir, {
recursive: true,
withFileTypes: true,
})) {
if (file.isFile() && file.name.endsWith(".js")) {
const originalContents = readFileSync(
path.join(file.parentPath, file.name),
"utf-8",
);
const newContents = originalContents.replaceAll(
/"@twofold\/([a-z-]+)"/g,
`"@${process.env.PACKAGE_NAMESPACE}/$1"`,
);
if (originalContents != newContents) {
writeFileSync(path.join(file.parentPath, file.name), newContents);
}
}
}
}
});
},
};

async function main() {
let dir = new URL("./dist/", import.meta.url);
Expand All @@ -13,6 +53,10 @@ async function main() {
outdir: "./dist/backend",
packages: "external",
platform: "node",
plugins:
process.env.PACKAGE_NAMESPACE !== undefined
? [rewriteImports]
: undefined,
});
}

Expand Down