|
| 1 | +name: Build, Test, and Publish Superscript NPM Package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + packages: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + build-test-publish: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + # Setup environment |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v3 |
| 22 | + |
| 23 | + - name: Setup Node.js |
| 24 | + uses: actions/setup-node@v3 |
| 25 | + with: |
| 26 | + node-version: '18' |
| 27 | + registry-url: 'https://registry.npmjs.org' |
| 28 | + |
| 29 | + - name: Setup Rust |
| 30 | + uses: actions-rs/toolchain@v1 |
| 31 | + with: |
| 32 | + toolchain: stable |
| 33 | + target: wasm32-unknown-unknown |
| 34 | + override: true |
| 35 | + |
| 36 | + - name: Install wasm-pack |
| 37 | + run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh |
| 38 | + |
| 39 | + # Build WASM module |
| 40 | + - name: Make build_wasm.sh executable |
| 41 | + run: chmod +x build_wasm.sh |
| 42 | + |
| 43 | + - name: Build WASM module |
| 44 | + run: | |
| 45 | + set -e |
| 46 | + ./build_wasm.sh |
| 47 | + if [ $? -ne 0 ]; then |
| 48 | + echo "WASM build failed" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + # Node.js example tests |
| 53 | + - name: Install dependencies for Node example |
| 54 | + run: | |
| 55 | + set -e |
| 56 | + cd wasm/example/node |
| 57 | + npm install |
| 58 | + npm install ../../target/node |
| 59 | + |
| 60 | + - name: Build TypeScript example |
| 61 | + run: | |
| 62 | + set -e |
| 63 | + cd wasm/example/node |
| 64 | + npm run build |
| 65 | + if [ ! -f dist/test_node.js ]; then |
| 66 | + echo "TypeScript build failed - dist/test_node.js not found" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + - name: Run JavaScript example |
| 71 | + run: | |
| 72 | + set -e |
| 73 | + cd wasm/example/node |
| 74 | + # Run the original JavaScript example |
| 75 | + echo "Running JavaScript example (test_node_javascript.js)..." |
| 76 | + node test_node_javascript.js |
| 77 | + if [ $? -ne 0 ]; then |
| 78 | + echo "JavaScript example execution failed" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + echo "JavaScript example executed successfully" |
| 82 | + |
| 83 | + - name: Run TypeScript compiled example |
| 84 | + run: | |
| 85 | + set -e |
| 86 | + cd wasm/example/node |
| 87 | + # Run the compiled TypeScript example |
| 88 | + echo "Running compiled TypeScript example (dist/test_node.js)..." |
| 89 | + node dist/test_node.js |
| 90 | + if [ $? -ne 0 ]; then |
| 91 | + echo "TypeScript compiled example execution failed" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + echo "TypeScript compiled example executed successfully" |
| 95 | + |
| 96 | + # Run Superscript tests |
| 97 | + - name: Run Superscript tests |
| 98 | + run: | |
| 99 | + set -e |
| 100 | + cd wasm/example/node |
| 101 | + echo "Running Superscript tests..." |
| 102 | + npm run test |
| 103 | + if [ $? -ne 0 ]; then |
| 104 | + echo "Superscript tests failed" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + echo "Superscript tests completed successfully" |
| 108 | + |
| 109 | + # Browser example test |
| 110 | + - name: Install dependencies for Browser example |
| 111 | + run: | |
| 112 | + set -e |
| 113 | + cd wasm/example/browser |
| 114 | + npm install |
| 115 | + npm install ../../target/browser |
| 116 | + |
| 117 | + - name: Build Browser example |
| 118 | + run: | |
| 119 | + set -e |
| 120 | + cd wasm/example/browser |
| 121 | + CI=false npm run build |
| 122 | + if [ ! -d build ]; then |
| 123 | + echo "Browser example build failed - build directory not found" |
| 124 | + exit 1 |
| 125 | + fi |
| 126 | + echo "Browser example built successfully" |
| 127 | + |
| 128 | + # Summarize test results |
| 129 | + - name: Summarize test results |
| 130 | + run: | |
| 131 | + echo "✅ All tests passed successfully!" |
| 132 | + echo "✓ WASM module built successfully" |
| 133 | + echo "✓ Node.js JavaScript example ran successfully" |
| 134 | + echo "✓ Node.js TypeScript example ran successfully" |
| 135 | + echo "✓ Superscript tests completed successfully" |
| 136 | + echo "✓ Browser example built successfully" |
| 137 | + |
| 138 | + # Version management and publishing |
| 139 | + - name: Check for version changes |
| 140 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 141 | + id: version_check |
| 142 | + run: | |
| 143 | + cd wasm |
| 144 | + # Get the current version from package.json |
| 145 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 146 | + echo "Current version: $CURRENT_VERSION" |
| 147 | + |
| 148 | + # Check if this version already exists on npm |
| 149 | + if npm view @superwall/superscript@$CURRENT_VERSION version &>/dev/null; then |
| 150 | + echo "Version $CURRENT_VERSION already exists on npm. Incrementing patch version." |
| 151 | + # Increment patch version |
| 152 | + NEW_VERSION=$(node -p "const [major, minor, patch] = '$CURRENT_VERSION'.split('.'); \`\${major}.\${minor}.\${parseInt(patch) + 1}\`") |
| 153 | + echo "New version: $NEW_VERSION" |
| 154 | + |
| 155 | + # Update package.json with new version |
| 156 | + npm version $NEW_VERSION --no-git-tag-version |
| 157 | + echo "version_changed=true" >> $GITHUB_OUTPUT |
| 158 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 159 | + else |
| 160 | + echo "Version $CURRENT_VERSION does not exist on npm. No need to bump version." |
| 161 | + echo "version_changed=false" >> $GITHUB_OUTPUT |
| 162 | + echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 163 | + fi |
| 164 | + |
| 165 | + - name: Prepare for NPM publish |
| 166 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 167 | + run: | |
| 168 | + cd wasm |
| 169 | + # Check if all required files exist |
| 170 | + if [ ! -d "./target/node" ] || [ ! -d "./target/browser" ] || [ ! -d "./types" ]; then |
| 171 | + echo "Missing required directories for npm publish" |
| 172 | + exit 1 |
| 173 | + fi |
| 174 | + |
| 175 | + - name: Publish to NPM |
| 176 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 177 | + run: | |
| 178 | + cd wasm |
| 179 | + npm publish --access public |
| 180 | + env: |
| 181 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 182 | + |
| 183 | + - name: Commit version change |
| 184 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.version_check.outputs.version_changed == 'true' |
| 185 | + run: | |
| 186 | + git config --global user.name 'GitHub Actions' |
| 187 | + git config --global user.email '[email protected]' |
| 188 | + git add wasm/package.json |
| 189 | + git commit -m "Bump version to ${{ steps.version_check.outputs.new_version }} [skip ci]" |
| 190 | + git push |
0 commit comments