mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-09 20:51:22 +00:00
refactor: publish skills directly from synced vendor
This commit is contained in:
@@ -1,47 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const TEXT_EXTENSIONS = new Set([
|
||||
"md",
|
||||
"mdx",
|
||||
"txt",
|
||||
"json",
|
||||
"json5",
|
||||
"yaml",
|
||||
"yml",
|
||||
"toml",
|
||||
"js",
|
||||
"cjs",
|
||||
"mjs",
|
||||
"ts",
|
||||
"tsx",
|
||||
"jsx",
|
||||
"py",
|
||||
"sh",
|
||||
"rb",
|
||||
"go",
|
||||
"rs",
|
||||
"swift",
|
||||
"kt",
|
||||
"java",
|
||||
"cs",
|
||||
"cpp",
|
||||
"c",
|
||||
"h",
|
||||
"hpp",
|
||||
"sql",
|
||||
"csv",
|
||||
"ini",
|
||||
"cfg",
|
||||
"env",
|
||||
"xml",
|
||||
"html",
|
||||
"css",
|
||||
"scss",
|
||||
"sass",
|
||||
"svg",
|
||||
]);
|
||||
|
||||
const PACKAGE_DEPENDENCY_SECTIONS = [
|
||||
"dependencies",
|
||||
"optionalDependencies",
|
||||
@@ -49,15 +8,18 @@ const PACKAGE_DEPENDENCY_SECTIONS = [
|
||||
"devDependencies",
|
||||
];
|
||||
|
||||
export async function listTextFiles(root) {
|
||||
const SKIPPED_DIRS = new Set([".git", ".clawhub", ".clawdhub", "node_modules"]);
|
||||
const SKIPPED_FILES = new Set([".DS_Store"]);
|
||||
|
||||
export async function listReleaseFiles(root) {
|
||||
const resolvedRoot = path.resolve(root);
|
||||
const files = [];
|
||||
|
||||
async function walk(folder) {
|
||||
const entries = await fs.readdir(folder, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
if (entry.name.startsWith(".")) continue;
|
||||
if (entry.name === "node_modules") continue;
|
||||
if (entry.name === ".clawhub" || entry.name === ".clawdhub") continue;
|
||||
if (entry.isDirectory() && SKIPPED_DIRS.has(entry.name)) continue;
|
||||
if (entry.isFile() && SKIPPED_FILES.has(entry.name)) continue;
|
||||
|
||||
const fullPath = path.join(folder, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
@@ -66,36 +28,19 @@ export async function listTextFiles(root) {
|
||||
}
|
||||
if (!entry.isFile()) continue;
|
||||
|
||||
const relPath = path.relative(root, fullPath).split(path.sep).join("/");
|
||||
const ext = relPath.split(".").pop()?.toLowerCase() ?? "";
|
||||
if (!TEXT_EXTENSIONS.has(ext)) continue;
|
||||
|
||||
const relPath = path.relative(resolvedRoot, fullPath).split(path.sep).join("/");
|
||||
const bytes = await fs.readFile(fullPath);
|
||||
files.push({ relPath, bytes });
|
||||
}
|
||||
}
|
||||
|
||||
await walk(root);
|
||||
await walk(resolvedRoot);
|
||||
files.sort((left, right) => left.relPath.localeCompare(right.relPath));
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function collectReleaseFiles(root) {
|
||||
await validateSelfContainedRelease(root);
|
||||
return listTextFiles(root);
|
||||
}
|
||||
|
||||
export async function materializeReleaseFiles(files, outDir) {
|
||||
await fs.mkdir(outDir, { recursive: true });
|
||||
for (const file of files) {
|
||||
const outputPath = path.join(outDir, fromPosixRel(file.relPath));
|
||||
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
||||
await fs.writeFile(outputPath, file.bytes);
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateSelfContainedRelease(root) {
|
||||
const files = await listTextFiles(root);
|
||||
const files = await listReleaseFiles(root);
|
||||
for (const file of files.filter((entry) => path.posix.basename(entry.relPath) === "package.json")) {
|
||||
const packageDir = path.resolve(root, fromPosixRel(path.posix.dirname(file.relPath)));
|
||||
const packageJson = JSON.parse(file.bytes.toString("utf8"));
|
||||
@@ -108,7 +53,7 @@ export async function validateSelfContainedRelease(root) {
|
||||
const targetDir = path.resolve(packageDir, spec.slice(5));
|
||||
if (!isWithinRoot(root, targetDir)) {
|
||||
throw new Error(
|
||||
`Release artifact is not self-contained: ${file.relPath} depends on ${name} via ${spec}`,
|
||||
`Release target is not self-contained: ${file.relPath} depends on ${name} via ${spec}`,
|
||||
);
|
||||
}
|
||||
await fs.access(targetDir).catch(() => {
|
||||
@@ -16,7 +16,8 @@ const SKIPPED_FILES = new Set([".DS_Store"]);
|
||||
export async function syncSharedSkillPackages(repoRoot, options = {}) {
|
||||
const root = path.resolve(repoRoot);
|
||||
const workspacePackages = await discoverWorkspacePackages(root);
|
||||
const consumers = await discoverSkillScriptPackages(root);
|
||||
const targetConsumerDirs = normalizeTargetConsumerDirs(root, options.targets ?? []);
|
||||
const consumers = await discoverSkillScriptPackages(root, targetConsumerDirs);
|
||||
const runtime = options.install === false ? null : resolveBunRuntime();
|
||||
const managedPaths = new Set();
|
||||
const packageDirs = [];
|
||||
@@ -42,6 +43,25 @@ export async function syncSharedSkillPackages(repoRoot, options = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeTargetConsumerDirs(repoRoot, targets) {
|
||||
if (!targets || targets.length === 0) return null;
|
||||
|
||||
const consumerDirs = new Set();
|
||||
for (const target of targets) {
|
||||
if (!target) continue;
|
||||
|
||||
const resolvedTarget = path.resolve(repoRoot, target);
|
||||
if (path.basename(resolvedTarget) === "scripts") {
|
||||
consumerDirs.add(resolvedTarget);
|
||||
continue;
|
||||
}
|
||||
|
||||
consumerDirs.add(path.join(resolvedTarget, "scripts"));
|
||||
}
|
||||
|
||||
return consumerDirs;
|
||||
}
|
||||
|
||||
export function ensureManagedPathsClean(repoRoot, managedPaths) {
|
||||
if (managedPaths.length === 0) return;
|
||||
|
||||
@@ -182,13 +202,14 @@ async function discoverWorkspacePackages(repoRoot) {
|
||||
return map;
|
||||
}
|
||||
|
||||
async function discoverSkillScriptPackages(repoRoot) {
|
||||
async function discoverSkillScriptPackages(repoRoot, targetConsumerDirs = null) {
|
||||
const skillsRoot = path.join(repoRoot, "skills");
|
||||
const consumers = [];
|
||||
const skillEntries = await fs.readdir(skillsRoot, { withFileTypes: true });
|
||||
for (const entry of skillEntries) {
|
||||
if (!entry.isDirectory()) continue;
|
||||
const scriptsDir = path.join(skillsRoot, entry.name, "scripts");
|
||||
if (targetConsumerDirs && !targetConsumerDirs.has(path.resolve(scriptsDir))) continue;
|
||||
const packageJsonPath = path.join(scriptsDir, "package.json");
|
||||
if (!existsSync(packageJsonPath)) continue;
|
||||
consumers.push({ dir: scriptsDir, packageJsonPath });
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import path from "node:path";
|
||||
|
||||
import { collectReleaseFiles, materializeReleaseFiles } from "./lib/skill-artifact.mjs";
|
||||
|
||||
async function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
if (!options.skillDir || !options.outDir) {
|
||||
throw new Error("--skill-dir and --out-dir are required");
|
||||
}
|
||||
|
||||
const skillDir = path.resolve(options.skillDir);
|
||||
const outDir = path.resolve(options.outDir);
|
||||
const files = await collectReleaseFiles(skillDir);
|
||||
await materializeReleaseFiles(files, outDir);
|
||||
|
||||
console.log(`Prepared artifact for ${path.basename(skillDir)}`);
|
||||
console.log(`Source: ${skillDir}`);
|
||||
console.log(`Output: ${outDir}`);
|
||||
console.log(`Files: ${files.length}`);
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const options = {
|
||||
skillDir: "",
|
||||
outDir: "",
|
||||
};
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const arg = argv[index];
|
||||
if (arg === "--skill-dir") {
|
||||
options.skillDir = argv[index + 1] ?? "";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--out-dir") {
|
||||
options.outDir = argv[index + 1] ?? "";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "-h" || arg === "--help") {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
throw new Error(`Unknown argument: ${arg}`);
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
function printUsage() {
|
||||
console.log(`Usage: prepare-skill-artifact.mjs --skill-dir <dir> --out-dir <dir>
|
||||
|
||||
Options:
|
||||
--skill-dir <dir> Source skill directory
|
||||
--out-dir <dir> Artifact output directory
|
||||
-h, --help Show help`);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -5,31 +5,31 @@ import { existsSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { listTextFiles } from "./lib/skill-artifact.mjs";
|
||||
import { listReleaseFiles, validateSelfContainedRelease } from "./lib/release-files.mjs";
|
||||
|
||||
const DEFAULT_REGISTRY = "https://clawhub.ai";
|
||||
|
||||
async function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
if (!options.skillDir || !options.artifactDir || !options.version) {
|
||||
throw new Error("--skill-dir, --artifact-dir, and --version are required");
|
||||
if (!options.skillDir || !options.version) {
|
||||
throw new Error("--skill-dir and --version are required");
|
||||
}
|
||||
|
||||
const skillDir = path.resolve(options.skillDir);
|
||||
const artifactDir = path.resolve(options.artifactDir);
|
||||
const skill = buildSkillEntry(skillDir, options.slug, options.displayName);
|
||||
const changelog = options.changelogFile
|
||||
? await fs.readFile(path.resolve(options.changelogFile), "utf8")
|
||||
: "";
|
||||
|
||||
const files = await listTextFiles(artifactDir);
|
||||
await validateSelfContainedRelease(skillDir);
|
||||
const files = await listReleaseFiles(skillDir);
|
||||
if (files.length === 0) {
|
||||
throw new Error(`Artifact directory is empty: ${artifactDir}`);
|
||||
throw new Error(`Skill directory is empty: ${skillDir}`);
|
||||
}
|
||||
|
||||
if (options.dryRun) {
|
||||
console.log(`Dry run: would publish ${skill.slug}@${options.version}`);
|
||||
console.log(`Artifact: ${artifactDir}`);
|
||||
console.log(`Skill: ${skillDir}`);
|
||||
console.log(`Files: ${files.length}`);
|
||||
return;
|
||||
}
|
||||
@@ -68,7 +68,6 @@ async function main() {
|
||||
function parseArgs(argv) {
|
||||
const options = {
|
||||
skillDir: "",
|
||||
artifactDir: "",
|
||||
version: "",
|
||||
changelogFile: "",
|
||||
registry: "",
|
||||
@@ -85,11 +84,6 @@ function parseArgs(argv) {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--artifact-dir") {
|
||||
options.artifactDir = argv[index + 1] ?? "";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--version") {
|
||||
options.version = argv[index + 1] ?? "";
|
||||
index += 1;
|
||||
@@ -141,11 +135,10 @@ function parseArgs(argv) {
|
||||
}
|
||||
|
||||
function printUsage() {
|
||||
console.log(`Usage: publish-skill-artifact.mjs --skill-dir <dir> --artifact-dir <dir> --version <semver> [options]
|
||||
console.log(`Usage: publish-skill.mjs --skill-dir <dir> --version <semver> [options]
|
||||
|
||||
Options:
|
||||
--skill-dir <dir> Source skill directory (used for slug/display name)
|
||||
--artifact-dir <dir> Prepared artifact directory
|
||||
--skill-dir <dir> Skill directory to publish
|
||||
--version <semver> Version to publish
|
||||
--changelog-file <file> Release notes file
|
||||
--registry <url> Override registry base URL
|
||||
@@ -227,7 +220,7 @@ async function publishSkill({ registry, token, skill, files, version, changelog,
|
||||
);
|
||||
|
||||
for (const file of files) {
|
||||
form.append("files", new Blob([file.bytes], { type: "text/plain" }), file.relPath);
|
||||
form.append("files", new Blob([file.bytes], { type: "application/octet-stream" }), file.relPath);
|
||||
}
|
||||
|
||||
const response = await fetch(`${registry}/api/v1/skills`, {
|
||||
@@ -10,7 +10,9 @@ import {
|
||||
async function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const repoRoot = path.resolve(options.repoRoot);
|
||||
const result = await syncSharedSkillPackages(repoRoot);
|
||||
const result = await syncSharedSkillPackages(repoRoot, {
|
||||
targets: options.targets,
|
||||
});
|
||||
|
||||
if (options.enforceClean) {
|
||||
ensureManagedPathsClean(repoRoot, result.managedPaths);
|
||||
@@ -23,6 +25,7 @@ function parseArgs(argv) {
|
||||
const options = {
|
||||
repoRoot: process.cwd(),
|
||||
enforceClean: false,
|
||||
targets: [],
|
||||
};
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
@@ -36,6 +39,11 @@ function parseArgs(argv) {
|
||||
options.enforceClean = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--target") {
|
||||
options.targets.push(argv[index + 1] ?? "");
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "-h" || arg === "--help") {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
@@ -51,6 +59,7 @@ function printUsage() {
|
||||
|
||||
Options:
|
||||
--repo-root <dir> Repository root (default: current directory)
|
||||
--target <dir> Sync only one skill directory (can be repeated)
|
||||
--enforce-clean Fail if managed files change after sync
|
||||
-h, --help Show help`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user