Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
.dev/
.mint

# Node / pnpm
node_modules/
.pnpm-store/

# Swift / Xcode / SwiftPM
.build/
.swiftpm/
Expand Down
5 changes: 4 additions & 1 deletion dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: checkout-kit

up:
- packages:
- quicktype
- rover
- mint
- xcbeautify
Expand Down Expand Up @@ -56,6 +55,7 @@ up:
version: v22.14.0
package_manager: pnpm@10.33.1
packages:
- protocol
- platforms/react-native
- platforms/web
- custom:
Expand Down Expand Up @@ -105,6 +105,9 @@ commands:
update-upstream:
desc: "Update vendored UCP snapshot files from upstream"
run: ./protocol/scripts/update_ucp_snapshot.sh "$@"
check-tools:
desc: "Verify protocol codegen tools match protocol/package.json"
run: ./protocol/scripts/check_codegen_tools.sh "$@"
apollo:
subcommands:
download_schema:
Expand Down
17 changes: 17 additions & 0 deletions protocol/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@shopify/checkout-kit-protocol-tooling",
"version": "0.0.0",
"private": true,
"license": "MIT",
"description": "Tooling for generating Shopify Checkout Kit protocol models.",
"packageManager": "pnpm@10.33.1+sha512.05ba3c1d5d1c18f68df06470d74055e62d41fc110a0c660db1b2dfb2785327f04cf0f68345d4609bc52089e7fa0343c31593b2f9594e2c5d5da426230acc9820",
"scripts": {
"check-tools": "./scripts/check_codegen_tools.sh",
"codegen:kotlin": "./scripts/generate_models.sh --lang kotlin",
"codegen:swift": "./scripts/generate_models.sh --lang swift",
"codegen:typescript": "./scripts/generate_models.sh --lang typescript"
},
"devDependencies": {
"quicktype": "23.2.6"
}
}
838 changes: 838 additions & 0 deletions protocol/pnpm-lock.yaml

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions protocol/scripts/check_codegen_tools.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node
/*
* MIT License
*
* Copyright 2023-present, Shopify Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {requireQuicktype} from "./codegen_tools.mjs";

function usage() {
console.error(`Usage: check_codegen_tools.sh [--quiet]

Verifies that protocol codegen tools are installed from the repo-local pnpm
dependencies and match the exact versions in protocol/package.json.`);
}

function parseArgs(argv) {
let quiet = false;

for (let index = 0; index < argv.length;) {
const arg = argv[index];
switch (arg) {
case "--quiet":
quiet = true;
index += 1;
break;
case "-h":
case "--help":
usage();
process.exit(0);
break;
default:
console.error(`Unknown argument: ${arg}`);
usage();
process.exit(2);
}
}

return {quiet};
}

async function main() {
const {quiet} = parseArgs(process.argv.slice(2));

await requireQuicktype();

if (!quiet) {
console.log("Protocol codegen tools are installed and match protocol/package.json.");
}
}

main().catch((error) => {
console.error(error.message);
process.exit(1);
});
5 changes: 5 additions & 0 deletions protocol/scripts/check_codegen_tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
exec node "${SCRIPT_DIR}/check_codegen_tools.mjs" "$@"
108 changes: 108 additions & 0 deletions protocol/scripts/codegen_tools.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* MIT License
*
* Copyright 2023-present, Shopify Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {spawn} from "node:child_process";
import {constants as fsConstants} from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import {fileURLToPath} from "node:url";

export const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
export const PROTOCOL_DIR = path.resolve(SCRIPT_DIR, "..");
export const REPO_ROOT = path.resolve(PROTOCOL_DIR, "..");
export const PACKAGE_JSON = path.join(PROTOCOL_DIR, "package.json");
export const QUICKTYPE_BIN = path.join(PROTOCOL_DIR, "node_modules", ".bin", "quicktype");

export function run(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd ?? REPO_ROOT,
stdio: options.capture ? ["ignore", "pipe", "pipe"] : "inherit",
});

let stdout = "";
let stderr = "";
if (options.capture) {
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdout += chunk;
});
child.stderr.on("data", (chunk) => {
stderr += chunk;
});
}

child.on("error", reject);
child.on("close", (code) => {
if (code === 0) {
resolve({stdout, stderr});
} else {
const error = new Error(`${command} ${args.join(" ")} exited with ${code}`);
error.code = code;
error.stdout = stdout;
error.stderr = stderr;
reject(error);
}
});
});
}

export async function readJson(file) {
return JSON.parse(await fs.readFile(file, "utf8"));
}

async function fileIsExecutable(file) {
try {
await fs.access(file, fsConstants.X_OK);
return true;
} catch {
return false;
}
}

export async function requireQuicktype() {
if (!(await fileIsExecutable(QUICKTYPE_BIN))) {
throw new Error(`quicktype is required at ${QUICKTYPE_BIN}. Run dev up or (cd protocol && pnpm install).`);
}

const packageJson = await readJson(PACKAGE_JSON);
const expected = packageJson.devDependencies?.quicktype ?? "";
if (expected === "") {
throw new Error(`Missing quicktype version in ${PACKAGE_JSON}`);
}

if (!/^[0-9]+[.][0-9]+[.][0-9]+(-[0-9A-Za-z.-]+)?$/.test(expected)) {
throw new Error(`quicktype must use an exact version in ${PACKAGE_JSON}; found ${expected}.`);
}

const {stdout} = await run(QUICKTYPE_BIN, ["--version"], {capture: true});
const actual = stdout.match(/^quicktype version (\S+)/m)?.[1] ?? "";
if (actual === "") {
throw new Error(`Unable to determine quicktype version from ${QUICKTYPE_BIN}`);
}

if (actual !== expected) {
throw new Error(`Unsupported quicktype version: ${actual}. Expected ${expected} from ${PACKAGE_JSON}.`);
}
}
Loading
Loading