@mcp-tool-kit/shared 0.0.4 → 0.0.5

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.
@@ -0,0 +1,17 @@
1
+ import Handlebars from "handlebars";
2
+
3
+ //#region src/handlebars/register.ts
4
+ function registerHandlebarsHelpers() {
5
+ Handlebars.registerHelper("includes", function(array, value) {
6
+ return array && Array.isArray(array) && array.includes(value);
7
+ });
8
+ Handlebars.registerHelper("and", function(a, b) {
9
+ return a && b;
10
+ });
11
+ Handlebars.registerHelper("or", function(a, b) {
12
+ return a || b;
13
+ });
14
+ }
15
+
16
+ //#endregion
17
+ export { registerHandlebarsHelpers };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { createProject, installDependencies, sleep } from "./projectSetup.js";
1
+ import { createProject, fileExists, installDependencies, sleep } from "./projectSetup.js";
2
2
 
3
- export { createProject, installDependencies, sleep };
3
+ export { createProject, fileExists, installDependencies, sleep };
@@ -1,41 +1,61 @@
1
+ import { registerHandlebarsHelpers } from "./handlebars/register.js";
1
2
  import { setTimeout as sleep } from "timers/promises";
2
- import { cp, mkdir, readFile, readdir, rename, stat, writeFile } from "fs/promises";
3
- import path from "path";
3
+ import { access, constants, cp, mkdir, readFile, readdir, rename, rmdir, stat, unlink, writeFile } from "fs/promises";
4
+ import { join } from "path";
4
5
  import { spawn } from "child_process";
6
+ import Handlebars from "handlebars";
5
7
 
6
8
  //#region src/projectSetup.ts
9
+ registerHandlebarsHelpers();
7
10
  async function renameFiles(currentDir) {
8
11
  const renameMap = {
9
- _env: ".env",
10
- _gitignore: ".gitignore",
11
12
  _git: ".git",
12
- _nvmrc: ".nvmrc",
13
- _prettierrc: ".prettierrc",
14
13
  _husky: ".husky",
15
14
  _github: ".github"
16
15
  };
17
16
  const items = await readdir(currentDir, { recursive: true });
18
17
  for (const item of items) {
19
18
  if (!(item in renameMap)) continue;
20
- await rename(path.join(currentDir, item), path.join(currentDir, renameMap[item]));
19
+ await rename(join(currentDir, item), join(currentDir, renameMap[item]));
21
20
  }
22
21
  }
23
- async function replaceVariables(currentDir, replacements) {
24
- const variables = {
25
- "{{PROJECT_NAME}}": replacements.projectName,
26
- "{{YEAR}}": (/* @__PURE__ */ new Date()).getFullYear().toString()
27
- };
22
+ async function compileTemplateFiles(currentDir, templateData) {
28
23
  const items = await readdir(currentDir, { recursive: true });
29
24
  for (const item of items) {
30
- const itemPath = path.join(currentDir, item);
25
+ const itemPath = join(currentDir, item);
31
26
  const itemStat = await stat(itemPath);
32
27
  if (itemStat.isDirectory()) continue;
33
- for (const [key, value] of Object.entries(variables)) {
34
- const content = await readFile(itemPath, "utf-8");
35
- const regex = new RegExp(key, "g");
36
- const newContent = content.replace(regex, value);
37
- await writeFile(itemPath, newContent, "utf-8");
28
+ const content = await readFile(itemPath, "utf-8");
29
+ const template = Handlebars.compile(content);
30
+ const newContent = template(templateData);
31
+ if (newContent.trim() === "") {
32
+ await unlink(itemPath);
33
+ continue;
38
34
  }
35
+ if (itemPath.endsWith(".hbs")) {
36
+ const newItemPath = itemPath.slice(0, -4);
37
+ await writeFile(newItemPath, newContent, "utf-8");
38
+ await unlink(itemPath);
39
+ } else await writeFile(itemPath, newContent, "utf-8");
40
+ }
41
+ }
42
+ async function removeEmptyDirectories(currentDir) {
43
+ const items = await readdir(currentDir, {
44
+ recursive: true,
45
+ withFileTypes: true
46
+ });
47
+ const directories = items.filter((item) => item.isDirectory()).map((item) => join(item.parentPath || currentDir, item.name)).sort((a, b) => b.split("/").length - a.split("/").length);
48
+ for (const dirPath of directories) try {
49
+ const dirItems = await readdir(dirPath);
50
+ if (dirItems.length === 0) await rmdir(dirPath);
51
+ } catch {}
52
+ }
53
+ async function fileExists(path) {
54
+ try {
55
+ await access(path, constants.F_OK);
56
+ return true;
57
+ } catch {
58
+ return false;
39
59
  }
40
60
  }
41
61
  function installDependencies(currentDir) {
@@ -51,12 +71,13 @@ function installDependencies(currentDir) {
51
71
  npm.on("error", reject);
52
72
  });
53
73
  }
54
- async function createProject(targetPath, templatePath, replacements) {
74
+ async function createProject(targetPath, templatePath, templateData) {
55
75
  await mkdir(targetPath, { recursive: true });
56
76
  await cp(templatePath, targetPath, { recursive: true });
57
77
  await renameFiles(targetPath);
58
- await replaceVariables(targetPath, replacements);
78
+ await compileTemplateFiles(targetPath, templateData);
79
+ await removeEmptyDirectories(targetPath);
59
80
  }
60
81
 
61
82
  //#endregion
62
- export { createProject, installDependencies, sleep };
83
+ export { createProject, fileExists, installDependencies, sleep };
@@ -0,0 +1 @@
1
+ export declare function registerHandlebarsHelpers(): void;
@@ -1,6 +1,11 @@
1
1
  import { setTimeout as sleep } from 'timers/promises';
2
+ export declare function fileExists(path: string): Promise<boolean>;
2
3
  export declare function installDependencies(currentDir: string): Promise<void>;
3
- export declare function createProject(targetPath: string, templatePath: string, replacements: {
4
+ export declare function createProject(targetPath: string, templatePath: string, templateData: {
4
5
  projectName: string;
6
+ year: string;
7
+ transports: string[];
8
+ plugins: string[];
9
+ components: string[];
5
10
  }): Promise<void>;
6
11
  export { sleep };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcp-tool-kit/shared",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "mcp tool kit shared",
5
5
  "type": "module",
6
6
  "author": "zhensherlock",
@@ -36,7 +36,9 @@
36
36
  "bugs": {
37
37
  "url": "https://github.com/my-mcp-hub/mcp-kit/issues"
38
38
  },
39
- "dependencies": {},
39
+ "dependencies": {
40
+ "handlebars": "^4.7.8"
41
+ },
40
42
  "scripts": {
41
43
  "clean:dist": "rimraf dist",
42
44
  "build:types": "tsc",