codepet 1.0.1 → 1.0.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/bin/codepet.js +19 -5
- package/package.json +1 -1
package/bin/codepet.js
CHANGED
|
@@ -13,6 +13,18 @@ const fs = require('fs');
|
|
|
13
13
|
const path = require('path');
|
|
14
14
|
const os = require('os');
|
|
15
15
|
const { execSync } = require('child_process');
|
|
16
|
+
|
|
17
|
+
// 跨平台 Python 命令检测
|
|
18
|
+
function getPython() {
|
|
19
|
+
for (const cmd of ['python3', 'python']) {
|
|
20
|
+
try {
|
|
21
|
+
execSync(`${cmd} --version`, { stdio: 'ignore' });
|
|
22
|
+
return cmd;
|
|
23
|
+
} catch {}
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const PYTHON = getPython();
|
|
16
28
|
const core = require('../core');
|
|
17
29
|
|
|
18
30
|
const SPRITES_DIR = path.join(__dirname, '..', 'sprites');
|
|
@@ -181,12 +193,13 @@ function renderSprite(character, variant) {
|
|
|
181
193
|
const renderImgScript = path.join(__dirname, '..', 'scripts', 'render_to_image.py');
|
|
182
194
|
if (fs.existsSync(renderImgScript)) {
|
|
183
195
|
try {
|
|
184
|
-
const imgPath = execSync(
|
|
196
|
+
const imgPath = execSync(`${PYTHON} "${renderImgScript}" "${character}" "${variant}"`, { encoding: 'utf-8' }).trim();
|
|
185
197
|
console.log(`[图片:${imgPath}]`);
|
|
186
198
|
} catch (e) {}
|
|
187
199
|
}
|
|
188
200
|
} else {
|
|
189
201
|
// 终端模式:彩色像素画
|
|
202
|
+
if (!PYTHON) return; // Python 未安装,静默跳过
|
|
190
203
|
const renderScript = path.join(__dirname, '..', 'scripts', 'img2terminal.py');
|
|
191
204
|
let spritePath = path.join(SPRITES_DIR, character, `${variant}.png`);
|
|
192
205
|
if (!fs.existsSync(spritePath)) {
|
|
@@ -194,7 +207,7 @@ function renderSprite(character, variant) {
|
|
|
194
207
|
}
|
|
195
208
|
if (fs.existsSync(spritePath) && fs.existsSync(renderScript)) {
|
|
196
209
|
try {
|
|
197
|
-
const output = execSync(
|
|
210
|
+
const output = execSync(`${PYTHON} "${renderScript}" "${spritePath}" ${RENDER_WIDTH}`, { encoding: 'utf-8' });
|
|
198
211
|
console.log(output);
|
|
199
212
|
} catch (e) {}
|
|
200
213
|
}
|
|
@@ -336,10 +349,11 @@ function doCard() {
|
|
|
336
349
|
const cardScript = path.join(__dirname, '..', 'scripts', 'pet_card.py');
|
|
337
350
|
if (fs.existsSync(cardScript)) {
|
|
338
351
|
try {
|
|
339
|
-
execSync(
|
|
340
|
-
//
|
|
352
|
+
execSync(`${PYTHON} "${cardScript}"`, { encoding: 'utf-8', stdio: 'inherit' });
|
|
353
|
+
// 跨平台打开图片
|
|
341
354
|
const cardPath = path.join(os.homedir(), '.codepet', 'card.png');
|
|
342
|
-
|
|
355
|
+
const openCmd = process.platform === 'win32' ? 'start ""' : process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
356
|
+
execSync(`${openCmd} "${cardPath}"`, { stdio: 'ignore', shell: true });
|
|
343
357
|
} catch (e) {
|
|
344
358
|
console.log(' 卡片生成失败。');
|
|
345
359
|
}
|