From a090c23efcc02e91f5469fd9421b59b5c90b05d9 Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Tue, 7 Jan 2025 13:45:15 -0500 Subject: [PATCH] perf: modify cli package entrypoint, add custom entrypoint option --- apps/cli/CHANGELOG.md | 6 ++++ apps/cli/cli.ts | 5 ++-- apps/cli/package.json | 2 +- apps/cli/src/commands/build.ts | 16 +++++++--- apps/cli/src/docker/entrypoint.sh | 43 ++++++++++++++++++++++---- apps/cli/src/utils.ts | 22 +++++++++----- pnpm-lock.yaml | 50 +++++++++++++++---------------- 7 files changed, 99 insertions(+), 45 deletions(-) diff --git a/apps/cli/CHANGELOG.md b/apps/cli/CHANGELOG.md index 5f658f3..188544b 100644 --- a/apps/cli/CHANGELOG.md +++ b/apps/cli/CHANGELOG.md @@ -1,5 +1,11 @@ # kksh +## 0.0.28 + +### Patch Changes + +- Improve entrypoint, add custom entrypoint for build cmd + ## 0.0.27 ### Patch Changes diff --git a/apps/cli/cli.ts b/apps/cli/cli.ts index 8a9f196..ff15b09 100644 --- a/apps/cli/cli.ts +++ b/apps/cli/cli.ts @@ -41,10 +41,11 @@ program program .command("build [project_path]") + .option("--entrypoint [path]", "Use custom entrypoint.sh (for debug purpose)") .description("Build extension with docker and validate (You must have docker installed)") - .action((projectPath: string | undefined) => { + .action((projectPath: string | undefined, opts: { entrypoint?: string }) => { logger.info("cwd:", cwd) - buildCmd(computeProjectDir(projectPath)) + buildCmd(computeProjectDir(projectPath), opts.entrypoint) }) program.parse() diff --git a/apps/cli/package.json b/apps/cli/package.json index d223f25..0c67850 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -1,7 +1,7 @@ { "name": "kksh", "module": "dist/cli.js", - "version": "0.0.27", + "version": "0.0.28", "type": "module", "bin": { "kksh": "./dist/cli.js", diff --git a/apps/cli/src/commands/build.ts b/apps/cli/src/commands/build.ts index 22436a9..9da291d 100644 --- a/apps/cli/src/commands/build.ts +++ b/apps/cli/src/commands/build.ts @@ -1,11 +1,19 @@ +import fs from "fs" +import path from "path" import { getRootDir } from "@/constants" import { buildWithDockerAndValidate } from "@/utils" -export async function buildCmd(projectPath: string) { +export async function buildCmd(projectPath: string, entrypoint?: string) { const rootDir = getRootDir() - console.log("rootDir: ", rootDir) - - const buildResult = await buildWithDockerAndValidate(projectPath) + const entrypointPath = entrypoint + ? fs.existsSync(entrypoint) + ? entrypoint + : path.join(rootDir, entrypoint) + : undefined + const buildResult = await buildWithDockerAndValidate( + projectPath, + entrypointPath && fs.existsSync(entrypointPath) ? entrypointPath : undefined + ) console.log(buildResult) } diff --git a/apps/cli/src/docker/entrypoint.sh b/apps/cli/src/docker/entrypoint.sh index c469e86..2d5a796 100755 --- a/apps/cli/src/docker/entrypoint.sh +++ b/apps/cli/src/docker/entrypoint.sh @@ -1,12 +1,45 @@ -. ~/.bashrc - -cd /workspace +#!/bin/bash +cd /workspace # source code will be mounted to /workspace +echo $PWD +source ~/.bashrc +bun --version rm *.tgz rm -rf node_modules cp -r /workspace /workspace-copy cd /workspace-copy -pnpm i -pnpm run build + +# Detect package manager based on lock files +if [ -f "bun.lockb" ]; then + echo "Using bun package manager" + bun install + echo $? + bun run build +elif [ -f "pnpm-lock.yaml" ]; then + echo "Using pnpm package manager" + corepack enable pnpm + pnpm install + echo $? + ls node_modules + pnpm build +elif [ -f "package-lock.json" ]; then + echo "Using npm package manager" + npm install + echo $? + npm run build +else + corepack enable pnpm + echo "No lock file found, defaulting to pnpm" + pnpm install + echo $? + pnpm build +fi +echo $? +npx kksh@latest verify --publish +# if previous exit code is not 0, then exit with error +if [ $? -ne 0 ]; then + echo "Build failed" + exit 1 +fi npm pack # check number of *.tgz file in current directory # if more than 1, then exit with error diff --git a/apps/cli/src/utils.ts b/apps/cli/src/utils.ts index 863ca5b..328cf3b 100644 --- a/apps/cli/src/utils.ts +++ b/apps/cli/src/utils.ts @@ -5,6 +5,7 @@ import { ExtPackageJson } from "@kksh/api/models" import fs from "fs-extra" import * as v from "valibot" import { getDockerEntrypoint } from "./constants" +import logger from "./logger" import type { BuildResult } from "./types" /** @@ -71,20 +72,22 @@ export function computeHash(buffer: Buffer, algorithm: "sha1" | "sha256" | "sha5 * @param extPath * @returns shasum of the tarball parsed from stderr output */ -export function buildWithDocker(extPath: string): Promise<{ +export function buildWithDocker( + extPath: string, + entrypoint?: string +): Promise<{ stderrShasum: string stderrTarballFilename: string pkg: ExtPackageJson }> { - console.log(`Building ${extPath}`) + logger.info(`Building ${extPath}`) return new Promise((resolve, reject) => { const pkg = v.parse(ExtPackageJson, fs.readJsonSync(path.join(extPath, "package.json"))) - const dockerEntrypoint = getDockerEntrypoint() - console.log("Docker Entrypoint", dockerEntrypoint) - + const dockerEntrypoint = entrypoint ? entrypoint : getDockerEntrypoint() + logger.info("Docker Entrypoint", dockerEntrypoint) const dockerCmd = ` run -v ${dockerEntrypoint}:/entrypoint.sh -v ${extPath}:/workspace -w /workspace --rm huakunshen/kunkun-ext-builder:latest /entrypoint.sh` - console.log("dockerCmd", dockerCmd) + logger.info("dockerCmd", dockerCmd) const args = dockerCmd .split(" ") .filter((arg) => arg.length > 0) @@ -146,8 +149,11 @@ export function buildWithDocker(extPath: string): Promise<{ * @param extPath Extension Path * @returns */ -export function buildWithDockerAndValidate(extPath: string): Promise { - return buildWithDocker(extPath) +export function buildWithDockerAndValidate( + extPath: string, + entrypoint?: string +): Promise { + return buildWithDocker(extPath, entrypoint) .then((res) => { const parsedTarballPath = path.join(extPath, res.stderrTarballFilename) if (!fs.existsSync(parsedTarballPath)) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d7b65ea..d639878 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,7 +62,7 @@ catalogs: version: 2.2.1 '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 bits-ui: specifier: 1.0.0-next.77 version: 1.0.0-next.77 @@ -188,7 +188,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 '@types/debug': specifier: ^4.1.12 version: 4.1.12 @@ -228,7 +228,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 @@ -388,7 +388,7 @@ importers: version: 0.5.15(tailwindcss@3.4.17) '@types/bun': specifier: 'catalog:' - version: 1.1.14 + version: 1.1.15 '@types/semver': specifier: ^7.5.8 version: 7.5.8 @@ -518,7 +518,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 '@types/lodash': specifier: ^4.17.14 version: 4.17.14 @@ -555,7 +555,7 @@ importers: version: link:../typescript-config '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/config-eslint: dependencies: @@ -613,7 +613,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/extensions/demo-worker-template-ext: dependencies: @@ -638,7 +638,7 @@ importers: version: 11.1.6(rollup@4.28.1)(tslib@2.8.1)(typescript@5.6.3) '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 rollup-plugin-visualizer: specifier: ^5.12.0 version: 5.12.0(rollup@4.28.1) @@ -745,7 +745,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/grpc: dependencies: @@ -764,7 +764,7 @@ importers: version: 0.7.13 '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 '@types/google-protobuf': specifier: ^3.15.12 version: 3.15.12 @@ -795,7 +795,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 '@valibot/to-json-schema': specifier: 1.0.0-beta.3 version: 1.0.0-beta.3(valibot@1.0.0-beta.10(typescript@5.6.3)) @@ -814,7 +814,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/tauri-plugins/jarvis: dependencies: @@ -833,7 +833,7 @@ importers: version: link:../../supabase '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/templates/template-ext-headless: dependencies: @@ -852,7 +852,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/templates/template-ext-next: dependencies: @@ -1198,7 +1198,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 packages/types: dependencies: @@ -1211,7 +1211,7 @@ importers: version: link:../api '@types/bun': specifier: 'catalog:' - version: 1.1.14 + version: 1.1.15 packages/typescript-config: {} @@ -1250,7 +1250,7 @@ importers: version: 2.2.0 '@types/bun': specifier: 'catalog:' - version: 1.1.14 + version: 1.1.15 bits-ui: specifier: 'catalog:' version: 1.0.0-next.77(svelte@5.16.3) @@ -1311,7 +1311,7 @@ importers: devDependencies: '@types/bun': specifier: latest - version: 1.1.14 + version: 1.1.15 vendors/tauri-plugin-keyring: dependencies: @@ -4494,8 +4494,8 @@ packages: '@types/babel__traverse@7.20.6': resolution: {integrity: sha512-r1bzfrm0tomOI8g1SzvCaQHo6Lcv6zu0EA+W2kHrt8dyrHQxGzBBL4kdkzIS+jBMV+EYcMAEAqXqYaLJq5rOZg==} - '@types/bun@1.1.14': - resolution: {integrity: sha512-opVYiFGtO2af0dnWBdZWlioLBoxSdDO5qokaazLhq8XQtGZbY4pY3/JxY8Zdf/hEwGubbp7ErZXoN1+h2yesxA==} + '@types/bun@1.1.15': + resolution: {integrity: sha512-Fi7ND1jCq8O5iU3s9z3TKHggD0hidgpe7wSxyisviXpbMmY4B1KiokF3f/mmjOoDrEcf873tSpixgen7Wm9X0g==} '@types/cookie@0.6.0': resolution: {integrity: sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==} @@ -5503,8 +5503,8 @@ packages: buffer@6.0.3: resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==} - bun-types@1.1.37: - resolution: {integrity: sha512-C65lv6eBr3LPJWFZ2gswyrGZ82ljnH8flVE03xeXxKhi2ZGtFiO4isRKTKnitbSqtRAcaqYSR6djt1whI66AbA==} + bun-types@1.1.42: + resolution: {integrity: sha512-beMbnFqWbbBQHll/bn3phSwmoOQmnX2nt8NI9iOQKFbgR5Z6rlH3YuaMdlid8vp5XGct3/W4QVQBmhoOEoe4nw==} bundle-name@4.1.0: resolution: {integrity: sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==} @@ -14962,9 +14962,9 @@ snapshots: dependencies: '@babel/types': 7.26.0 - '@types/bun@1.1.14': + '@types/bun@1.1.15': dependencies: - bun-types: 1.1.37 + bun-types: 1.1.42 '@types/cookie@0.6.0': {} @@ -16386,7 +16386,7 @@ snapshots: base64-js: 1.5.1 ieee754: 1.2.1 - bun-types@1.1.37: + bun-types@1.1.42: dependencies: '@types/node': 20.12.14 '@types/ws': 8.5.13