Skip to content

Commit d72fe06

Browse files
Add support for native connector packaging (#30)
1 parent e2e59f5 commit d72fe06

File tree

10 files changed

+157
-2
lines changed

10 files changed

+157
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Changes to be included in the next upcoming release
1313
- Built-in scalar types that support equality now define it in the NDC schema.
1414
- Built-in scalar types now have an explicit type representation defined in the NDC schema.
1515
- Fixed functions that return null causing crashes ([#31](https://github.com/hasura/ndc-nodejs-lambda/pull/31))
16+
- Added support for native connector packaging ([#30](https://github.com/hasura/ndc-nodejs-lambda/pull/30))
1617

1718
## [1.2.0] - 2024-03-18
1819
- Improved error messages when unsupported enum types or unions of literal types are found, and allow these types to be used in relaxed types mode ([#17](https://github.com/hasura/ndc-nodejs-lambda/pull/17))

connector-definition/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ build: dist/connector-definition.tgz
1010
.PHONY: clean
1111
clean:
1212
rm -rf dist
13-
rm -f connector-definition.tgz
1413

1514
dist dist/.hasura-connector:
1615
mkdir dist
@@ -32,6 +31,12 @@ dist_template_files := $(patsubst template/%,dist/%,$(template_files))
3231
$(dist_template_files): $(template_files)
3332
cp -f $(template_files) dist/
3433

34+
script_files := $(wildcard scripts/*)
35+
dist_script_files := $(patsubst scripts/%,dist/.hasura-connector/%,$(script_files))
36+
37+
$(dist_script_files): $(script_files)
38+
cp -f $(script_files) dist/.hasura-connector/
39+
3540
dist/package.json: template/package.json $(RELEASE_VERSION_DEP)
3641
cp -f template/package.json dist/
3742
jq '.dependencies["@hasura/ndc-lambda-sdk"] = "$(RELEASE_VERSION)"' dist/package.json > dist/package.json.tmp
@@ -41,5 +46,5 @@ dist/package-lock.json: dist/package.json
4146
cd dist && npm install
4247
rm -rf dist/node_modules
4348

44-
dist/connector-definition.tgz: dist/.hasura-connector/connector-metadata.yaml dist/.hasura-connector/Dockerfile dist/.hasura-connector/.dockerignore $(dist_template_files) dist/package-lock.json
49+
dist/connector-definition.tgz: dist/.hasura-connector/connector-metadata.yaml dist/.hasura-connector/Dockerfile dist/.hasura-connector/.dockerignore $(dist_template_files) dist/package-lock.json $(dist_script_files)
4550
shopt -s dotglob && cd dist && tar -czvf connector-definition.tgz *

connector-definition/connector-metadata.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
packagingDefinition:
22
type: ManagedDockerBuild
3+
nativeToolchainDefinition:
4+
commands:
5+
start:
6+
type: ShellScript
7+
bash: ./start.sh
8+
powershell: ./start.ps1
9+
watch:
10+
type: ShellScript
11+
bash: ./watch.sh
12+
powershell: ./watch.ps1
313
supportedEnvironmentVariables: []
414
commands: {}
515
dockerComposeWatch:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$minimumNodeVersion = 20
4+
5+
if (-not (Get-Command "node" -ErrorAction SilentlyContinue)) {
6+
Write-Host "node could not be found. Please install NodeJS v$minimumNodeVersion+."
7+
exit 1
8+
}
9+
10+
$nodeVersion = & node --version
11+
if ($nodeVersion -match "^v(\d+)\.") {
12+
$majorVersion = $Matches[1]
13+
if ($majorVersion -lt $minimumNodeVersion) {
14+
Write-Host "Detected Node.js version $nodeVersion on the PATH. The minimum required version is v$minimumNodeVersion."
15+
exit 1
16+
}
17+
}
18+
19+
Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
20+
try {
21+
if ((Test-Path "./node_modules") -eq $false) {
22+
Write-Host "node_modules not found, please ensure you have run 'npm install'."
23+
exit 1
24+
}
25+
} finally {
26+
Pop-Location
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
set -eu -o pipefail
3+
4+
minimum_node_version="20"
5+
6+
if ! command -v node &> /dev/null
7+
then
8+
echo "node could not be found on the PATH. Please install Node.js v$minimum_node_version+."
9+
exit 1
10+
fi
11+
12+
node_version=$(node --version)
13+
if [[ "$node_version" =~ ^v([[:digit:]]+)\. ]]; then
14+
major_version="${BASH_REMATCH[1]}"
15+
if [[ $major_version -lt $minimum_node_version ]]; then
16+
echo "Detected Node.js version $node_version on the PATH. The minimum required version is v$minimum_node_version."
17+
exit 1
18+
fi
19+
else
20+
echo "no match"
21+
fi
22+
23+
cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
24+
25+
if [ ! -d "node_modules" ]
26+
then
27+
echo "node_modules directory not found, please ensure you have run 'npm install'."
28+
exit 1
29+
fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This script reads from the package.json file in the current working directory
2+
// and prints out the text in the "scripts.<name>" entry. This is used to bypass
3+
// npm, which does not handle signals correctly, and execute the command directly
4+
5+
const fs = require("node:fs");
6+
const process = require("node:process");
7+
const path = require("node:path");
8+
9+
const cwd = process.cwd();
10+
const packageJsonPath = path.join(cwd, "./package.json");
11+
12+
if (process.argv.length < 3) {
13+
console.error("Error: Pass the name of script command you want to read as the first command line arg.");
14+
console.error("Usage: node read-package-script.js <name>");
15+
process.exit(1);
16+
}
17+
const desiredScript = process.argv[2];
18+
19+
try {
20+
const packageJsonText = fs.readFileSync(packageJsonPath);
21+
const packageJson = JSON.parse(packageJsonText);
22+
const script = packageJson.scripts[desiredScript];
23+
if (script === undefined) {
24+
console.error(`Error: script ${desiredScript} not found in ${packageJsonPath}`)
25+
}
26+
console.log(script);
27+
} catch (e) {
28+
console.error(`Error reading ${packageJsonPath}: ${e}`);
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$scriptDir = Get-Location
4+
5+
& ./check-reqs.ps1
6+
7+
Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
8+
try {
9+
$startScript = & node "$PSScriptRoot\read-package-script.js" "start"
10+
if ($LASTEXITCODE -ne 0) {
11+
exit 1
12+
}
13+
$env:PATH = "$($env:PATH);$($env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH)\node_modules\.bin"
14+
Invoke-Expression "& $startScript"
15+
} finally {
16+
Pop-Location
17+
}

connector-definition/scripts/start.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -eu -o pipefail
3+
4+
script_dir=$(pwd)
5+
6+
./check-reqs.sh
7+
8+
cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
9+
start_script=$(node "$script_dir/read-package-script.js" "start")
10+
PATH="$PATH:$(pwd)/node_modules/.bin" exec $start_script
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$scriptDir = Get-Location
4+
5+
& ./check-reqs.ps1
6+
7+
Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
8+
try {
9+
$watchScript = & node "$PSScriptRoot\read-package-script.js" "watch"
10+
if ($LASTEXITCODE -ne 0) {
11+
exit 1
12+
}
13+
$env:PATH = "$($env:PATH);$($env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH)\node_modules\.bin"
14+
Invoke-Expression "& $watchScript"
15+
} finally {
16+
Pop-Location
17+
}

connector-definition/scripts/watch.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -eu -o pipefail
3+
4+
script_dir=$(pwd)
5+
6+
./check-reqs.sh
7+
8+
cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH
9+
watch_script=$(node "$script_dir/read-package-script.js" "watch")
10+
PATH="$PATH:$(pwd)/node_modules/.bin" exec $watch_script

0 commit comments

Comments
 (0)