dsh-ppt 0.31.0 → 0.32.1

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.
@@ -8,9 +8,10 @@
8
8
  * 把“自由设计 PPT”降维为“按标杆槽位精准填空”。
9
9
  */
10
10
 
11
- import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
11
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
12
12
  import { basename, extname, join, resolve } from 'node:path'
13
13
  import { GITHUB_DISTILLED_EXEMPLARS, getExemplarFewShotSlides } from './deck-exemplars-corpus.mjs'
14
+ import { writeWorkspaceJson } from './deck-store.mjs'
14
15
 
15
16
  export { GITHUB_DISTILLED_EXEMPLARS, getExemplarFewShotSlides }
16
17
 
@@ -252,11 +253,13 @@ export const CURATED_EXEMPLAR_IDS = Object.freeze(Object.keys(CURATED_EXEMPLARS)
252
253
  /**
253
254
  * 读取工作区 .dsh-ppt/exemplars.json 中通过真实优质 .pptx 吸收持久化的标杆范本。
254
255
  */
255
- export function loadWorkspaceExemplars(cwd = '.') {
256
+ function readWorkspaceExemplars(cwd, strict = false) {
256
257
  const filePath = resolve(cwd, EXEMPLAR_STORE_REL)
257
258
  if (!existsSync(filePath)) return {}
258
259
  try {
259
260
  const raw = JSON.parse(readFileSync(filePath, 'utf8'))
261
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('Invalid exemplar store')
262
+ if (Object.hasOwn(raw, 'exemplars') && !Array.isArray(raw.exemplars)) throw new Error('Invalid exemplar entries')
260
263
  const list = Array.isArray(raw?.exemplars) ? raw.exemplars : (raw && typeof raw === 'object' ? Object.values(raw) : [])
261
264
  const map = {}
262
265
  for (const item of list) {
@@ -264,11 +267,14 @@ export function loadWorkspaceExemplars(cwd = '.') {
264
267
  map[String(item.id)] = item
265
268
  }
266
269
  return map
267
- } catch {
270
+ } catch (error) {
271
+ if (strict) throw new Error('dsh-ppt:范本库格式无效或无法读取,已保留原文件,请备份并修复后重试', { cause: error })
268
272
  return {}
269
273
  }
270
274
  }
271
275
 
276
+ export function loadWorkspaceExemplars(cwd = '.') { return readWorkspaceExemplars(cwd) }
277
+
272
278
  /**
273
279
  * 持久化单份吸收自外部优质 .pptx 的标杆范本至工作区 .dsh-ppt/exemplars.json。
274
280
  */
@@ -309,12 +315,11 @@ export function saveWorkspaceExemplar(cwd = '.', exemplar) {
309
315
  savedAt: new Date().toISOString(),
310
316
  }
311
317
 
312
- const existing = loadWorkspaceExemplars(cwd)
318
+ const existing = readWorkspaceExemplars(cwd, true)
313
319
  existing[id] = normalized
314
320
  const trimmed = Object.values(existing).slice(-MAX_WORKSPACE_EXEMPLARS)
315
321
  const outPath = resolve(cwd, EXEMPLAR_STORE_REL)
316
- mkdirSync(resolve(cwd, '.dsh-ppt'), { recursive: true })
317
- writeFileSync(outPath, JSON.stringify({ schemaVersion: 1, exemplars: trimmed }, null, 2) + '\n', 'utf8')
322
+ writeWorkspaceJson(outPath, { schemaVersion: 1, exemplars: trimmed })
318
323
  return { exemplar: normalized, path: outPath }
319
324
  }
320
325
 
@@ -435,6 +440,7 @@ export function extractExemplarFromAbsorbed(absorbed, options = {}) {
435
440
  * 批量扫描并吸收指定目录下的优质 .pptx 语料,统一写入 .dsh-ppt/exemplars.json 与 .dsh-ppt/taste-recipes.json。
436
441
  */
437
442
  export async function ingestExemplarDirectory({ dirPath, cwd = '.', limit = 20, absorbFn, saveTasteFn, signal }) {
443
+ signal?.throwIfAborted()
438
444
  if (!dirPath || typeof dirPath !== 'string') {
439
445
  throw new Error('dsh-ppt:请提供包含 .pptx 文件的目录路径 dirPath')
440
446
  }
@@ -451,9 +457,11 @@ export async function ingestExemplarDirectory({ dirPath, cwd = '.', limit = 20,
451
457
  const absorbedList = []
452
458
  const failed = []
453
459
  for (const file of files) {
460
+ signal?.throwIfAborted()
454
461
  const fullPath = join(absDir, file)
455
462
  try {
456
463
  const absorbed = await absorbFn({ pptxPath: fullPath, cwd: absDir, extractLogo: true, signal })
464
+ signal?.throwIfAborted()
457
465
  const exemplar = extractExemplarFromAbsorbed(absorbed)
458
466
  const savedEx = saveWorkspaceExemplar(cwd, exemplar)
459
467
  let tastePath = ''
@@ -473,6 +481,7 @@ export async function ingestExemplarDirectory({ dirPath, cwd = '.', limit = 20,
473
481
  tastePath,
474
482
  })
475
483
  } catch (err) {
484
+ signal?.throwIfAborted()
476
485
  failed.push({ file, error: err instanceof Error ? err.message : String(err) })
477
486
  }
478
487
  }
@@ -0,0 +1,18 @@
1
+ /** Replace workspace metadata only after its complete new JSON is written. */
2
+ import { lstatSync, mkdirSync, writeFileSync, renameSync, unlinkSync } from 'node:fs'
3
+ import { dirname } from 'node:path'
4
+ import { randomUUID } from 'node:crypto'
5
+
6
+ export function writeWorkspaceJson(file, value) {
7
+ const existing = lstatSync(file, { throwIfNoEntry: false })
8
+ if (existing && (!existing.isFile() || existing.isSymbolicLink())) throw new Error('dsh-ppt:存储文件不是普通文件,已保留原文件')
9
+ const bytes = JSON.stringify(value, null, 2) + '\n'
10
+ const temporary = file + '.tmp-' + randomUUID()
11
+ mkdirSync(dirname(file), { recursive: true })
12
+ try {
13
+ writeFileSync(temporary, bytes, { flag: 'wx', mode: 0o600 })
14
+ renameSync(temporary, file)
15
+ } finally {
16
+ try { unlinkSync(temporary) } catch (error) { if (error.code !== 'ENOENT') throw error }
17
+ }
18
+ }
@@ -8,8 +8,9 @@
8
8
  * 零外部运行时依赖,仅使用 Node 内置模块。
9
9
  */
10
10
 
11
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
12
- import { dirname, join, resolve } from 'node:path'
11
+ import { existsSync, readFileSync } from 'node:fs'
12
+ import { join, resolve } from 'node:path'
13
+ import { writeWorkspaceJson } from './deck-store.mjs'
13
14
 
14
15
  export const CARD_MATERIALS = Object.freeze(['solid', 'frameless', 'glass', 'editorial-rule'])
15
16
  export const COVER_VARIANTS = Object.freeze([
@@ -282,11 +283,13 @@ export function sanitizeCustomTasteRecipe(raw) {
282
283
  /**
283
284
  * 读取工作区持久化的用户品味配方库(.dsh-ppt/taste-recipes.json)。
284
285
  */
285
- export function loadWorkspaceTasteRecipes(cwd = process.cwd()) {
286
+ function readWorkspaceTasteRecipes(cwd, strict = false) {
286
287
  const storePath = resolve(cwd, '.dsh-ppt', 'taste-recipes.json')
287
288
  if (!existsSync(storePath)) return {}
288
289
  try {
289
290
  const parsed = JSON.parse(readFileSync(storePath, 'utf8'))
291
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('Invalid taste store')
292
+ if (Object.hasOwn(parsed, 'recipes') && (!parsed.recipes || typeof parsed.recipes !== 'object' || Array.isArray(parsed.recipes))) throw new Error('Invalid taste recipes')
290
293
  const rawMap = parsed && typeof parsed === 'object' && !Array.isArray(parsed)
291
294
  ? (parsed.recipes && typeof parsed.recipes === 'object' ? parsed.recipes : parsed)
292
295
  : {}
@@ -296,11 +299,14 @@ export function loadWorkspaceTasteRecipes(cwd = process.cwd()) {
296
299
  if (clean) out[clean.id.toLowerCase()] = clean
297
300
  }
298
301
  return out
299
- } catch {
302
+ } catch (error) {
303
+ if (strict) throw new Error('dsh-ppt:品味配方库格式无效或无法读取,已保留原文件,请备份并修复后重试', { cause: error })
300
304
  return {}
301
305
  }
302
306
  }
303
307
 
308
+ export function loadWorkspaceTasteRecipes(cwd = process.cwd()) { return readWorkspaceTasteRecipes(cwd) }
309
+
304
310
  /**
305
311
  * 将从优质 .pptx 吸收提炼的品味配方持久化到工作区(.dsh-ppt/taste-recipes.json),供后续所有新稿一键复用。
306
312
  */
@@ -308,13 +314,12 @@ export function saveWorkspaceTasteRecipe(recipe, cwd = process.cwd()) {
308
314
  const clean = sanitizeCustomTasteRecipe(recipe)
309
315
  if (!clean) throw new Error('dsh-ppt:无效的品味配方对象')
310
316
  const storePath = resolve(cwd, '.dsh-ppt', 'taste-recipes.json')
311
- const existing = loadWorkspaceTasteRecipes(cwd)
317
+ const existing = readWorkspaceTasteRecipes(cwd, true)
312
318
  existing[clean.id.toLowerCase()] = {
313
319
  ...clean,
314
320
  absorbedAt: clean.absorbedAt || new Date().toISOString(),
315
321
  }
316
- mkdirSync(dirname(storePath), { recursive: true })
317
- writeFileSync(storePath, JSON.stringify({ version: 1, recipes: existing }, null, 2) + '\n', 'utf8')
322
+ writeWorkspaceJson(storePath, { version: 1, recipes: existing })
318
323
  return { path: storePath, recipe: existing[clean.id.toLowerCase()], totalSaved: Object.keys(existing).length }
319
324
  }
320
325