opentool 0.8.1 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import { program } from 'commander';
3
3
  import * as fs4 from 'fs';
4
+ import { promises } from 'fs';
4
5
  import * as path5 from 'path';
6
+ import path5__default from 'path';
5
7
  import { tmpdir } from 'os';
6
8
  import { build } from 'esbuild';
7
9
  import { z } from 'zod';
@@ -2263,6 +2265,74 @@ async function generateMetadata(options) {
2263
2265
  function timestamp2() {
2264
2266
  return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, 19);
2265
2267
  }
2268
+ function resolveTemplateDir() {
2269
+ const here = path5__default.dirname(fileURLToPath(import.meta.url));
2270
+ return path5__default.resolve(here, "../../templates/base");
2271
+ }
2272
+ async function directoryIsEmpty(targetDir) {
2273
+ try {
2274
+ const entries = await promises.readdir(targetDir);
2275
+ return entries.length === 0;
2276
+ } catch (error) {
2277
+ if (error.code === "ENOENT") {
2278
+ return true;
2279
+ }
2280
+ throw error;
2281
+ }
2282
+ }
2283
+ async function copyDir(src, dest) {
2284
+ await promises.mkdir(dest, { recursive: true });
2285
+ const entries = await promises.readdir(src, { withFileTypes: true });
2286
+ for (const entry of entries) {
2287
+ const srcPath = path5__default.join(src, entry.name);
2288
+ const destPath = path5__default.join(dest, entry.name);
2289
+ if (entry.isDirectory()) {
2290
+ await copyDir(srcPath, destPath);
2291
+ } else if (entry.isFile()) {
2292
+ await promises.copyFile(srcPath, destPath);
2293
+ }
2294
+ }
2295
+ }
2296
+ function toPackageName(value) {
2297
+ return value.trim().toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "") || "opentool-project";
2298
+ }
2299
+ function toDisplayName(value) {
2300
+ return value.trim().replace(/[-_]+/g, " ").replace(/\b\w/g, (ch) => ch.toUpperCase()) || "OpenTool Project";
2301
+ }
2302
+ async function updatePackageJson(targetDir, name, description) {
2303
+ const filePath = path5__default.join(targetDir, "package.json");
2304
+ const raw = await promises.readFile(filePath, "utf-8");
2305
+ const pkg = JSON.parse(raw);
2306
+ pkg.name = toPackageName(name);
2307
+ if (description) {
2308
+ pkg.description = description;
2309
+ }
2310
+ await promises.writeFile(filePath, `${JSON.stringify(pkg, null, 2)}
2311
+ `, "utf-8");
2312
+ }
2313
+ async function updateMetadata(targetDir, name, description) {
2314
+ const filePath = path5__default.join(targetDir, "metadata.ts");
2315
+ const raw = await promises.readFile(filePath, "utf-8");
2316
+ const displayName = toDisplayName(name);
2317
+ const resolvedDescription = description || "OpenTool project";
2318
+ const updated = raw.replace(/name:\s*\".*?\"/, `name: "${toPackageName(name)}"`).replace(/displayName:\s*\".*?\"/, `displayName: "${displayName}"`).replace(/description:\s*\".*?\"/, `description: "${resolvedDescription}"`);
2319
+ await promises.writeFile(filePath, updated, "utf-8");
2320
+ }
2321
+ async function initCommand(options) {
2322
+ const targetDir = path5__default.resolve(process.cwd(), options.dir || ".");
2323
+ const templateDir = resolveTemplateDir();
2324
+ const empty = await directoryIsEmpty(targetDir);
2325
+ if (!empty && !options.force) {
2326
+ throw new Error(
2327
+ `Directory not empty: ${targetDir}. Use --force to overwrite.`
2328
+ );
2329
+ }
2330
+ await copyDir(templateDir, targetDir);
2331
+ const projectName = options.name || path5__default.basename(targetDir);
2332
+ const description = options.description;
2333
+ await updatePackageJson(targetDir, projectName, description);
2334
+ await updateMetadata(targetDir, projectName, description);
2335
+ }
2266
2336
 
2267
2337
  // src/cli/index.ts
2268
2338
  program.name("opentool").description("OpenTool CLI for building and developing serverless MCP tools").version("1.0.0");
@@ -2282,6 +2352,15 @@ program.command("metadata").description("Generate OpenTool metadata JSON without
2282
2352
  "Output file path for metadata.json",
2283
2353
  "metadata.json"
2284
2354
  ).option("--name <name>", "Server name", "opentool-server").option("--version <version>", "Server version", "1.0.0").action(generateMetadataCommand);
2355
+ program.command("init").description("Create a new OpenTool project in the target directory").option("-d, --dir <dir>", "Target directory", ".").option("-n, --name <name>", "Project name").option("--description <description>", "Project description").option("--force", "Overwrite existing files", false).action(async (cmdOptions) => {
2356
+ await initCommand({
2357
+ dir: cmdOptions.dir,
2358
+ name: cmdOptions.name,
2359
+ description: cmdOptions.description,
2360
+ force: cmdOptions.force
2361
+ });
2362
+ console.log(`Initialized OpenTool project in ${cmdOptions.dir || "."}`);
2363
+ });
2285
2364
  program.parse();
2286
2365
 
2287
2366
  export { buildCommand, buildProject, devCommand, generateMetadata, generateMetadataCommand, loadAndValidateTools, validateCommand, validateFullCommand };