Skip to content

Commit

Permalink
test: test node packages
Browse files Browse the repository at this point in the history
  • Loading branch information
charislam committed Dec 6, 2024
1 parent dda2a52 commit cea4dae
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 21 deletions.
167 changes: 167 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,170 @@ jobs:
name: artifact-npm-binary-distributions
path: npm-binary-distributions/*/*.tgz
if-no-files-found: "error"

test-linux:
runs-on: ubuntu-latest
needs: [node, npm-distributions]
strategy:
fail-fast: false
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-npm-binary-distributions
path: ./npm-binary-distributions
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-pkg-node
path: ./node-pkg
- name: Create package.json
run: |
mkdir test
cd test
cat <<EOF > package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "supa-mdx-lint --version"
},
"keywords": [],
"author": "",
"license": "ISC"
}
EOF
- name: Install test packages
working-directory: test
run: |
npm install ../npm-binary-distributions/linux-x64/*.tgz
npm install ../node-pkg/*.tgz
- name: Test lint script
working-directory: test
run: npm run lint

test-macos:
runs-on: macos-latest
needs: [node, npm-distributions]
strategy:
fail-fast: false
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-npm-binary-distributions
path: ./npm-binary-distributions
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-pkg-node
path: ./node-pkg
- name: Create package.json
run: |
mkdir test
cd test
cat <<EOF > package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "supa-mdx-lint --version"
},
"keywords": [],
"author": "",
"license": "ISC"
}
EOF
- name: Install test packages
working-directory: test
run: |
npm install ../npm-binary-distributions/darwin/*.tgz
npm install ../node-pkg/*.tgz
- name: Test lint script
working-directory: test
run: npm run lint

test-windows:
runs-on: windows-latest
needs: [node, npm-distributions]
strategy:
fail-fast: false
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-npm-binary-distributions
path: ./npm-binary-distributions
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact-pkg-node
path: ./node-pkg
- name: Create package.json
shell: pwsh
run: |
mkdir test
cd test
$json = @"
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "supa-mdx-lint --version"
},
"keywords": [],
"author": "",
"license": "ISC"
}
"@
$json | Out-File -FilePath package.json -Encoding utf8
- name: Unpack binary tarball
shell: pwsh
run: |
$tarballPath = Get-ChildItem -Path npm-binary-distributions\win32-x64\*.tgz | Select-Object -First 1
tar -xzf $tarballPath.FullName -C npm-binary-distributions\win32-x64
Get-ChildItem -Path npm-binary-distributions\win32-x64\package
- name: Install binary
run: |
cd test
npm install ..\npm-binary-distributions\win32-x64\package
- name: Unpack node-pkg tarball
shell: pwsh
run: |
$tarballPath = Get-ChildItem -Path node-pkg\*.tgz | Select-Object -First 1
tar -xzf $tarballPath.FullName -C node-pkg
Get-ChildItem -Path node-pkg\package
- name: Install main package
run: |
cd test
npm install ..\node-pkg\package
- name: Verify lint executable (debug)
shell: pwsh
run: |
cd test\node_modules\@supabase\supa-mdx-lint-win32-x64\bin
Get-Item supa-mdx-lint.exe | Format-List -Property *
cd ../../../..
.\node_modules\@supabase\supa-mdx-lint-win32-x64\bin\supa-mdx-lint.exe --version
- name: Test lint script
working-directory: test
run: |
cat package.json
cat package-lock.json
ls -r node_modules
ls -r node_modules\@supabase\supa-mdx-lint-win32-x64
npm run lint
3 changes: 3 additions & 0 deletions packages/supa-mdx-lint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@supabase/supa-mdx-lint",
"version": "0.1.6-pre",
"main": "src/index.js",
"bin": {
"supa-mdx-lint": "src/index.js"
},
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
Expand Down
85 changes: 64 additions & 21 deletions packages/supa-mdx-lint/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"use strict";

const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const util = require("util");

const BINARY_DISTRIBUTIONS = [
{
Expand Down Expand Up @@ -110,6 +113,24 @@ supa-mdx-lint supports:
);
}

/**
* Tries to find the node_modules directory by walking up the directory tree.
*
* @param {string} startDir The directory to start the search from.
*/
function findNodeModulesDir(startDir) {
let currentDir = startDir;
while (currentDir !== path.parse(currentDir).root) {
const nodeModulesPath = path.join(currentDir, "node_modules");
console.log("Testing node_modules path: %s", nodeModulesPath);
if (fs.existsSync(nodeModulesPath)) {
return nodeModulesPath;
}
currentDir = path.dirname(currentDir);
}
throw new Error("node_modules directory not found");
}

/**
* Tries to find the installed supa-mdx-lint binary - either by looking into the relevant
* optional dependencies or by trying to resolve the fallback binary.
Expand All @@ -127,36 +148,58 @@ function getBinaryPath() {
throwUnsupportedPlatformError();
}

let compatibleBinaryPath;
try {
compatibleBinaryPath = require.resolve(`${packageName}/${subpath}`);
} catch {
const otherInstalledDistribution = BINARY_DISTRIBUTIONS.find(
({ packageName, subpath }) => {
try {
require.resolve(`${packageName}/${subpath}`);
return true;
} catch {
return false;
}
},
);

// These error messages are heavily inspired by esbuild's error messages: https://github.com/evanw/esbuild/blob/f3d535262e3998d845d0f102b944ecd5a9efda57/lib/npm/node-platform.ts#L150
if (otherInstalledDistribution) {
throw new Error(`supa-mdx-lint binary for this platform/architecture not found!
return require.resolve(`${packageName}/${subpath}`);
} catch (error) {
try {
console.error(
"Error resolving binary path with require.resolve: %o",
error,
);
console.log("Trying to manually find the required binary...");

// Windows has a problem resolving the package with require.resolve, so
// try to manually construct the path from node_modules.
const nodeModulesPath = findNodeModulesDir(__dirname);
const binaryPath = path.join(nodeModulesPath, packageName, subpath);
if (fs.existsSync(binaryPath)) {
return binaryPath;
} else {
throw new Error(
util.format(
"Binary not found from manually constructed path: %s",
binaryPath,
),
);
}
} catch (error) {
console.error("Error resolving binary path manually: %o", error);

const otherInstalledDistribution = BINARY_DISTRIBUTIONS.find(
({ packageName, subpath }) => {
try {
require.resolve(`${packageName}/${subpath}`);
return true;
} catch {
return false;
}
},
);

// These error messages are heavily inspired by esbuild's error messages: https://github.com/evanw/esbuild/blob/f3d535262e3998d845d0f102b944ecd5a9efda57/lib/npm/node-platform.ts#L150
if (otherInstalledDistribution) {
throw new Error(`supa-mdx-lint binary for this platform/architecture not found!
The "${otherInstalledDistribution.packageName}" package is installed, but for the current platform, you should have the "${packageName}" package installed instead. This usually happens if the "supa-mdx-lint" package is installed on one platform (for example Windows or MacOS) and then the "node_modules" folder is reused on another operating system (for example Linux in Docker).
To fix this, avoid copying the "node_modules" folder, and instead freshly install your dependencies on the target system. You can also configure your package manager to install the right package. For example, yarn has the "supportedArchitectures" feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitecture.`);
} else {
throw new Error(`supa-mdx-lint binary for this platform/architecture not found!
} else {
throw new Error(`supa-mdx-lint binary for this platform/architecture not found!
It seems like none of the "@supabase/supa-mdx-lint" package's optional dependencies got installed. Please make sure your package manager is configured to install optional dependencies. If you are using npm to install your dependencies, please don't set the "--no-optional", "--ignore-optional", or "--omit=optional" flags. supa-mdx-lint needs the "optionalDependencies" feature in order to install its binary.`);
}
}
}

return compatibleBinaryPath;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/supa-mdx-lint/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

//@ ts-check

"use strict";
Expand Down

0 comments on commit cea4dae

Please sign in to comment.