kcode-pi 0.1.1 → 0.1.2

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/kcode.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
- import { dirname, join, resolve } from "node:path";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
3
  import { spawnSync } from "node:child_process";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { createRequire } from "node:module";
6
6
  const packageRoot = dirname(dirname(dirname(fileURLToPath(import.meta.url))));
7
7
  const require = createRequire(import.meta.url);
8
+ const packageName = readPackageName(packageRoot) ?? "kcode-pi";
8
9
  export function runKcodeCli(args, cwd = process.cwd()) {
9
10
  const command = args[0] ?? "help";
10
11
  switch (command) {
@@ -26,16 +27,14 @@ export function initProject(cwd) {
26
27
  const settingsPath = projectSettingsPath(cwd);
27
28
  const settings = readSettings(settingsPath);
28
29
  const kcodePackage = normalizePath(packageRoot);
29
- const packages = settings.packages ?? [];
30
- if (!packages.includes(kcodePackage)) {
31
- packages.unshift(kcodePackage);
32
- }
30
+ const packages = (settings.packages ?? []).filter((pkg) => !isSameKcodePackage(pkg, kcodePackage));
31
+ packages.unshift(kcodePackage);
33
32
  settings.packages = packages;
34
33
  mkdirSync(dirname(settingsPath), { recursive: true });
35
34
  writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}\n`, "utf8");
36
35
  return {
37
36
  exitCode: 0,
38
- output: [`已更新项目级 Pi 配置:${settingsPath}`, `已加入 KCode package:${kcodePackage}`].join("\n"),
37
+ output: [`已更新项目级 Pi 配置:${settingsPath}`, `已保留当前 KCode package:${kcodePackage}`].join("\n"),
39
38
  };
40
39
  }
41
40
  export function doctor(cwd) {
@@ -107,6 +106,27 @@ function readSettings(path) {
107
106
  function normalizePath(path) {
108
107
  return resolve(path);
109
108
  }
109
+ function isSameKcodePackage(candidate, currentPackage) {
110
+ const normalized = normalizePath(candidate);
111
+ if (normalized === currentPackage)
112
+ return true;
113
+ const candidateName = readPackageName(normalized) ?? packageNameFromPath(normalized);
114
+ return candidateName === packageName;
115
+ }
116
+ function readPackageName(packagePath) {
117
+ try {
118
+ const packageJson = JSON.parse(readFileSync(join(packagePath, "package.json"), "utf8"));
119
+ return packageJson.name;
120
+ }
121
+ catch {
122
+ return undefined;
123
+ }
124
+ }
125
+ function packageNameFromPath(path) {
126
+ const name = basename(path);
127
+ const scope = basename(dirname(path));
128
+ return scope.startsWith("@") ? `${scope}/${name}` : name;
129
+ }
110
130
  function bundledPiCliPath() {
111
131
  const directPath = join(packageRoot, "node_modules", "@earendil-works", "pi-coding-agent", "dist", "cli.js");
112
132
  if (existsSync(directPath))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kcode-pi",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Kingdee-specific package and harness for Pi Coding Agent",
5
5
  "type": "module",
6
6
  "private": false,
package/src/cli/kcode.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
- import { dirname, join, resolve } from "node:path";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
3
  import { spawnSync } from "node:child_process";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { createRequire } from "node:module";
@@ -16,6 +16,7 @@ interface PiSettings {
16
16
 
17
17
  const packageRoot = dirname(dirname(dirname(fileURLToPath(import.meta.url))));
18
18
  const require = createRequire(import.meta.url);
19
+ const packageName = readPackageName(packageRoot) ?? "kcode-pi";
19
20
 
20
21
  export interface PiCliCommand {
21
22
  command: string;
@@ -47,11 +48,9 @@ export function initProject(cwd: string): KcodeCliResult {
47
48
  const settingsPath = projectSettingsPath(cwd);
48
49
  const settings = readSettings(settingsPath);
49
50
  const kcodePackage = normalizePath(packageRoot);
50
- const packages = settings.packages ?? [];
51
+ const packages = (settings.packages ?? []).filter((pkg) => !isSameKcodePackage(pkg, kcodePackage));
51
52
 
52
- if (!packages.includes(kcodePackage)) {
53
- packages.unshift(kcodePackage);
54
- }
53
+ packages.unshift(kcodePackage);
55
54
 
56
55
  settings.packages = packages;
57
56
  mkdirSync(dirname(settingsPath), { recursive: true });
@@ -59,7 +58,7 @@ export function initProject(cwd: string): KcodeCliResult {
59
58
 
60
59
  return {
61
60
  exitCode: 0,
62
- output: [`已更新项目级 Pi 配置:${settingsPath}`, `已加入 KCode package:${kcodePackage}`].join("\n"),
61
+ output: [`已更新项目级 Pi 配置:${settingsPath}`, `已保留当前 KCode package:${kcodePackage}`].join("\n"),
63
62
  };
64
63
  }
65
64
 
@@ -146,6 +145,29 @@ function normalizePath(path: string): string {
146
145
  return resolve(path);
147
146
  }
148
147
 
148
+ function isSameKcodePackage(candidate: string, currentPackage: string): boolean {
149
+ const normalized = normalizePath(candidate);
150
+ if (normalized === currentPackage) return true;
151
+
152
+ const candidateName = readPackageName(normalized) ?? packageNameFromPath(normalized);
153
+ return candidateName === packageName;
154
+ }
155
+
156
+ function readPackageName(packagePath: string): string | undefined {
157
+ try {
158
+ const packageJson = JSON.parse(readFileSync(join(packagePath, "package.json"), "utf8")) as { name?: string };
159
+ return packageJson.name;
160
+ } catch {
161
+ return undefined;
162
+ }
163
+ }
164
+
165
+ function packageNameFromPath(path: string): string | undefined {
166
+ const name = basename(path);
167
+ const scope = basename(dirname(path));
168
+ return scope.startsWith("@") ? `${scope}/${name}` : name;
169
+ }
170
+
149
171
  function bundledPiCliPath(): string | undefined {
150
172
  const directPath = join(packageRoot, "node_modules", "@earendil-works", "pi-coding-agent", "dist", "cli.js");
151
173
  if (existsSync(directPath)) return directPath;