@lincyaw/cli 1.0.0-rc.118 → 1.0.0-rc.119
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/README.md +3 -3
- package/package.json +6 -6
- package/src/cli.ts +16 -8
- package/src/document-file.ts +4 -4
- package/src/import.ts +1 -1
- package/src/messages.ts +58 -9
- package/src/migrate.ts +49 -0
- package/src/script.ts +5 -5
- package/src/voiceover.ts +6 -5
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ pnpm add -D @lincyaw/cli @lincyaw/web @lincyaw/render-slides @lincyaw/render-not
|
|
|
30
30
|
| `presmium serve [dir]` | The workspace service, on this machine: the documents and their sites, compiled on demand (`--port`, `--host`, `--operator-dir`, `--preview-origin`) |
|
|
31
31
|
| `presmium studio [dir]` | The workbench (experimental), included with Presmium (`--port`, `--host`); uses the running command's bundled version |
|
|
32
32
|
|
|
33
|
-
`[deck]` is a directory with `
|
|
33
|
+
`[deck]` is a directory with `index.pmd` or an entry file, by default the current directory. Exit codes: 0 no errors, 1 error findings, 2 usage or internal error. `presmium --help` lists every option.
|
|
34
34
|
|
|
35
35
|
## Testing plugins
|
|
36
36
|
|
|
@@ -44,8 +44,8 @@ const { code, stdout } = await presmium(dir, ['build', '--to', 'web'])
|
|
|
44
44
|
|
|
45
45
|
## Documentation
|
|
46
46
|
|
|
47
|
-
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/
|
|
48
|
-
- [Plugin guide](https://github.com/Lincyaw/presmium/blob/main/site/plugins/locales/en/
|
|
47
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/index.pmd)
|
|
48
|
+
- [Plugin guide](https://github.com/Lincyaw/presmium/blob/main/site/plugins/locales/en/index.pmd)
|
|
49
49
|
|
|
50
50
|
## License
|
|
51
51
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/cli",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.119",
|
|
4
4
|
"description": "The presmium command: build, dev, ir, check and serve for Presmium documents",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -37,10 +37,10 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"jiti": "^2.7.0",
|
|
39
39
|
"yaml": "^2.9.1",
|
|
40
|
-
"@lincyaw/compiler": "1.0.0-rc.
|
|
41
|
-
"@lincyaw/
|
|
42
|
-
"@lincyaw/
|
|
43
|
-
"@lincyaw/
|
|
44
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
40
|
+
"@lincyaw/compiler": "1.0.0-rc.119",
|
|
41
|
+
"@lincyaw/server": "1.0.0-rc.119",
|
|
42
|
+
"@lincyaw/skills": "1.0.0-rc.119",
|
|
43
|
+
"@lincyaw/workspace": "1.0.0-rc.119",
|
|
44
|
+
"@lincyaw/kernel": "1.0.0-rc.119"
|
|
45
45
|
}
|
|
46
46
|
}
|
package/src/cli.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type { ApprovalPolicy } from '@lincyaw/server'
|
|
|
15
15
|
import { buildWorkspace, isWorkspaceRoot, serveWorkspace } from '@lincyaw/workspace'
|
|
16
16
|
import type { WorkspaceBuild } from '@lincyaw/workspace'
|
|
17
17
|
import { runCheck } from './check.ts'
|
|
18
|
-
import {
|
|
18
|
+
import { effectiveDocument, findWorkspaceRoot, importDocuments, WORKSPACE_CONFIG } from '@lincyaw/compiler'
|
|
19
19
|
import type { CheckResult } from './check.ts'
|
|
20
20
|
import { devHost } from './dev-host.ts'
|
|
21
21
|
import { DocumentFileError, readDocumentFile, saveDocumentFile } from './document-file.ts'
|
|
@@ -74,6 +74,7 @@ export async function main(argv: string[], options: MainOptions = {}): Promise<n
|
|
|
74
74
|
if (argv[0] === 'studio') return runStudioCommand(argv.slice(1), bundled, options.bin ?? PRESMIUM_BIN)
|
|
75
75
|
if (argv[0] === 'mcp') return runMcpCommand(argv.slice(1), bundled)
|
|
76
76
|
if (argv[0] === 'import') return runImportCommand(argv.slice(1))
|
|
77
|
+
if (argv[0] === 'migrate') return (await import('./migrate.ts')).runMigrateCommand(argv.slice(1), t)
|
|
77
78
|
if (argv[0] === 'voiceover') return runVoiceoverCommand(argv.slice(1), bundled)
|
|
78
79
|
if (argv[0] === 'speech') return runSpeechCommand(argv.slice(1))
|
|
79
80
|
if (argv[0] === 'script') return runScriptCommand(argv.slice(1))
|
|
@@ -140,7 +141,7 @@ function parseCommand(argv: string[]): Command | 'help' {
|
|
|
140
141
|
if (values.audience !== undefined && !/^[\w.-]+$/.test(audience ?? '')) throw new Error(t.badAudience(values.audience))
|
|
141
142
|
const directory = name === 'serve'
|
|
142
143
|
const entry = directory || workspace ? resolve(deck) : resolveEntry(deck)
|
|
143
|
-
if (!existsSync(entry) || (directory && !statSync(entry).isDirectory())) throw new Error(
|
|
144
|
+
if (!existsSync(entry) || (directory && !statSync(entry).isDirectory())) throw new Error(missing(entry))
|
|
144
145
|
const to = values.to?.split(',').map(id => id.trim()).filter(Boolean)
|
|
145
146
|
if (to?.length === 0) throw new Error(t.emptyTo)
|
|
146
147
|
const allow = values.allow?.split(',').map(kind => kind.trim()).filter(Boolean) ?? []
|
|
@@ -347,7 +348,7 @@ async function runNewCommand(argv: string[], bundled: string): Promise<number> {
|
|
|
347
348
|
return createMain(argv)
|
|
348
349
|
}
|
|
349
350
|
|
|
350
|
-
/** Markdown notes to documents; prints what it wrote
|
|
351
|
+
/** Markdown notes to documents; prints what it wrote and the links it could not resolve. */
|
|
351
352
|
async function runImportCommand(argv: string[]): Promise<number> {
|
|
352
353
|
let parsed
|
|
353
354
|
try {
|
|
@@ -382,11 +383,12 @@ async function runImportCommand(argv: string[]): Promise<number> {
|
|
|
382
383
|
return 2
|
|
383
384
|
}
|
|
384
385
|
const out = resolve(values.out ?? '.')
|
|
385
|
-
const
|
|
386
|
+
const found = await findWorkspaceRoot(out).catch(() => out)
|
|
387
|
+
const workspaceRoot = existsSync(join(found, WORKSPACE_CONFIG)) ? found : out
|
|
388
|
+
const report = await importDocuments(input, { out, exclude: values.exclude, lang: values.lang, group, preserveOriginal: true, workspaceRoot }).catch(() => undefined)
|
|
386
389
|
if (!report) return fail(t.importFailed, 1)
|
|
387
390
|
for (const { note, link } of report.unresolved) process.stderr.write(`${t.importUnresolved(note, link)}\n`)
|
|
388
391
|
process.stdout.write(`${t.imported(report.documents.length, report.files, relative(process.cwd(), out) || '.')}\n`)
|
|
389
|
-
if (report.documents.length > 0) process.stdout.write(`${t.importPatterns(documentPatterns(report.documents))}\n`)
|
|
390
392
|
return 0
|
|
391
393
|
}
|
|
392
394
|
|
|
@@ -413,11 +415,16 @@ function documentFileMessage(error: DocumentFileError): string {
|
|
|
413
415
|
}
|
|
414
416
|
|
|
415
417
|
/** The deck's entry, or a usage error already printed. */
|
|
418
|
+
/** Why a document's entry is missing: the older format's file is there instead, or nothing is. */
|
|
419
|
+
function missing(entry: string): string {
|
|
420
|
+
return existsSync(join(dirname(entry), 'lecture.json')) ? t.olderFormat(relative(process.cwd(), dirname(entry)) || '.') : t.notFound(entry)
|
|
421
|
+
}
|
|
422
|
+
|
|
416
423
|
function deckEntry(deck: string): string | undefined {
|
|
417
424
|
try {
|
|
418
425
|
const entry = resolveEntry(deck)
|
|
419
426
|
if (existsSync(entry)) return entry
|
|
420
|
-
fail(
|
|
427
|
+
fail(missing(entry), 2)
|
|
421
428
|
} catch (error) {
|
|
422
429
|
fail(errorMessage(error), 2)
|
|
423
430
|
}
|
|
@@ -478,7 +485,7 @@ async function runSpeechCommand(argv: string[]): Promise<number> {
|
|
|
478
485
|
}
|
|
479
486
|
}
|
|
480
487
|
|
|
481
|
-
/** The narration as one Markdown file to polish (`export`), and the polished file back into
|
|
488
|
+
/** The narration as one Markdown file to polish (`export`), and the polished file back into index.pmd (`import`). */
|
|
482
489
|
async function runScriptCommand(argv: string[]): Promise<number> {
|
|
483
490
|
let parsed
|
|
484
491
|
try {
|
|
@@ -504,7 +511,8 @@ async function runScriptCommand(argv: string[]): Promise<number> {
|
|
|
504
511
|
const source = readDocumentFile(entry)
|
|
505
512
|
if (action === 'export') {
|
|
506
513
|
const out = resolve(values.out ?? fallback)
|
|
507
|
-
const
|
|
514
|
+
const { lang } = (await effectiveDocument(entry, source.document)).metadata
|
|
515
|
+
const written = exportScript(source.document, CLI_MESSAGES[deckLocale(lang)].scriptInstructions)
|
|
508
516
|
await mkdir(dirname(out), { recursive: true })
|
|
509
517
|
await writeFile(out, written.text)
|
|
510
518
|
process.stdout.write(`${t.scriptExported(shownPath(out), written.narrations, written.steps)}\n`)
|
package/src/document-file.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto'
|
|
2
2
|
import { readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
3
3
|
import { validateDocument } from '@lincyaw/kernel'
|
|
4
|
-
import type {
|
|
4
|
+
import type { PresmiumDocument } from '@lincyaw/kernel'
|
|
5
5
|
|
|
6
|
-
/** `
|
|
6
|
+
/** `index.pmd` as it was read: its exact text, to save a change in the same formatting and only if it is unchanged. */
|
|
7
7
|
export interface DocumentFile {
|
|
8
8
|
entry: string
|
|
9
9
|
text: string
|
|
10
|
-
document:
|
|
10
|
+
document: PresmiumDocument
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export class DocumentFileError extends Error {
|
|
@@ -38,7 +38,7 @@ export function readDocumentFile(entry: string): DocumentFile {
|
|
|
38
38
|
* indentation, line endings and byte order mark, so that the change is the only difference. Refuses when the file
|
|
39
39
|
* changed since it was read (another editor saved it).
|
|
40
40
|
*/
|
|
41
|
-
export function saveDocumentFile(file: DocumentFile, document:
|
|
41
|
+
export function saveDocumentFile(file: DocumentFile, document: PresmiumDocument): void {
|
|
42
42
|
const checked = validateDocument(document)
|
|
43
43
|
if (!checked.document) throw new DocumentFileError('document-invalid', issuesOf(checked))
|
|
44
44
|
const body = file.text.startsWith(BOM) ? file.text.slice(1) : file.text
|
package/src/import.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { importMarkdown } from '@lincyaw/compiler'
|
|
2
2
|
export type { ImportOptions, ImportReport } from '@lincyaw/compiler'
|
package/src/messages.ts
CHANGED
|
@@ -16,6 +16,7 @@ export interface CliMessages {
|
|
|
16
16
|
audienceNotForWorkspace: string
|
|
17
17
|
badAudience(value: string): string
|
|
18
18
|
notFound(entry: string): string
|
|
19
|
+
olderFormat(dir: string): string
|
|
19
20
|
emptyTo: string
|
|
20
21
|
badAllow(kinds: readonly string[], value: string): string
|
|
21
22
|
badPreviewOrigin(value: string): string
|
|
@@ -44,7 +45,14 @@ export interface CliMessages {
|
|
|
44
45
|
badGroup(value: string): string
|
|
45
46
|
imported(documents: number, files: number, out: string): string
|
|
46
47
|
importUnresolved(note: string, link: string): string
|
|
47
|
-
|
|
48
|
+
migrateHelp: string
|
|
49
|
+
migrateRename(from: string, to: string): string
|
|
50
|
+
migrateDocument(path: string): string
|
|
51
|
+
migrateWorkspaceFile: string
|
|
52
|
+
migrateProblem(path: string, reason: string): string
|
|
53
|
+
migrateNothing(dir: string): string
|
|
54
|
+
migrateNeeded(documents: number, workspaceFile: boolean): string
|
|
55
|
+
migrateDone(documents: number): string
|
|
48
56
|
badVoice(value: string): string
|
|
49
57
|
documentChanged: string
|
|
50
58
|
documentInvalid(detail: string): string
|
|
@@ -114,9 +122,9 @@ export const CLI_MESSAGES: Record<Locale, CliMessages> = {
|
|
|
114
122
|
|
|
115
123
|
命令:
|
|
116
124
|
new 在目录中新建项目(没有给出时逐项询问),由 create-presmium 完成
|
|
117
|
-
--type <种类>
|
|
125
|
+
--type <种类> document、workspace、site 或 plugin(默认询问)
|
|
118
126
|
--theme <主题> site 的主题:course、docs 或 showcase(默认 course)
|
|
119
|
-
--title <标题>
|
|
127
|
+
--title <标题> 文稿、工作区或网站的标题
|
|
120
128
|
--name <包名> 插件的包名
|
|
121
129
|
--no-git 不运行 git init
|
|
122
130
|
skills 安装内置技能(与 MCP 和可视化编辑器共用)
|
|
@@ -146,6 +154,9 @@ export const CLI_MESSAGES: Record<Locale, CliMessages> = {
|
|
|
146
154
|
--lang <语言> 没有在 frontmatter 中写 lang 的笔记的语言(默认按文字判断)
|
|
147
155
|
--group <层数> 把笔记所在的第几层文件夹作为文稿的第一个标签;网站的侧栏按第一个
|
|
148
156
|
标签分组,例如 --group 2 让 projects/<项目>/ 中的笔记成为一组
|
|
157
|
+
migrate 把文稿升级为当前格式(参数是工作区的根目录或一份文稿,默认为 .):lecture.json 重命名为
|
|
158
|
+
${DOCUMENT_ENTRY},每份文稿记下它在文稿树中的位置,presmium.workspace.yaml 不再列出 documents
|
|
159
|
+
--check 只报告要升级什么,不修改文件;需要升级时退出码为 1
|
|
149
160
|
check 检查文稿并运行插件的检查
|
|
150
161
|
--json 以 JSON 打印结果
|
|
151
162
|
--update-lock 重新定位代码锚点,并重写 .presmium/anchors.lock.json
|
|
@@ -166,7 +177,7 @@ export const CLI_MESSAGES: Record<Locale, CliMessages> = {
|
|
|
166
177
|
export <文稿> 写出每一步的讲稿:每段在它的标记行 <!-- n: … --> 之下,换行与空行照原样,
|
|
167
178
|
{显示|读法} 是显示与读法不同的词,⟦cue:…⟧ 是提示点,\\ 使后面的 \\、{、}、⟦、⟧
|
|
168
179
|
(读音中还有 |)作为文字
|
|
169
|
-
import <文稿> [<文件>] 把改过的讲稿写回
|
|
180
|
+
import <文稿> [<文件>] 把改过的讲稿写回 index.pmd,只改动有变化的讲稿;标记缺失或未知、
|
|
170
181
|
提示点被删除或移到别的段落时不写入
|
|
171
182
|
--out <文件> export 写到这个文件(默认为 <文稿>/dist/script.md,也是 import 默认读的文件)
|
|
172
183
|
speech 语音服务的设置
|
|
@@ -210,6 +221,7 @@ export const CLI_MESSAGES: Record<Locale, CliMessages> = {
|
|
|
210
221
|
audienceNotForWorkspace: '--audience 暂不能用于工作区。请分别为每份文稿加 --audience 导出。',
|
|
211
222
|
badAudience: value => `“${value}”不是读者类别的名称。请给出字母、数字、.、_ 或 - 组成的名称,例如 student。`,
|
|
212
223
|
notFound: entry => `找不到 ${entry}。请检查路径,或用 presmium new 新建文稿。`,
|
|
224
|
+
olderFormat: dir => `${dir} 中的文稿使用旧格式(lecture.json)。请运行 presmium migrate ${dir} 升级后再试。`,
|
|
213
225
|
emptyTo: '--to 至少需要一个形态,例如 --to web。',
|
|
214
226
|
badAllow: (kinds, value) => `--allow 不接受“${value}”。可用的种类:${kinds.join('、')}。`,
|
|
215
227
|
badPreviewOrigin: value => `“${value}”不是来源。请给出协议、主机与端口,例如 http://127.0.0.1:4320。`,
|
|
@@ -238,7 +250,23 @@ export const CLI_MESSAGES: Record<Locale, CliMessages> = {
|
|
|
238
250
|
importFailed: '文件未能导入。请检查文件编码、Word 文件是否完整,以及输出目录是否可写。',
|
|
239
251
|
imported: (documents, files, out) => `已导入 ${documents} 份文稿,复制了 ${files} 个文件,写在 ${out}`,
|
|
240
252
|
importUnresolved: (note, link) => `${note}:链接 ${link} 没有对应的笔记或文件,已改为普通文字`,
|
|
241
|
-
|
|
253
|
+
migrateHelp: `用法:presmium migrate [<目录>] [--check]
|
|
254
|
+
|
|
255
|
+
把工作区或一份文稿中旧格式的文稿升级为当前格式,与 Presmium Studio 中的“升级”相同:
|
|
256
|
+
lecture.json 重命名为 ${DOCUMENT_ENTRY}(保留文件的版本历史),翻译一同重命名;
|
|
257
|
+
每份文稿记下它在文稿树中的位置,按 presmium.workspace.yaml 中 documents 的顺序排在顶层,
|
|
258
|
+
其余按路径;documents 从 presmium.workspace.yaml 中删除;指向 lecture.json 的路径一并改写。
|
|
259
|
+
文稿自己的设置原样保留。重复运行不会再改动已升级的文稿。
|
|
260
|
+
|
|
261
|
+
--check 只报告要升级什么,不修改文件;需要升级时退出码为 1
|
|
262
|
+
-h, --help 显示这段说明`,
|
|
263
|
+
migrateRename: (from, to) => `重命名 ${from} → ${to}`,
|
|
264
|
+
migrateDocument: path => `升级 ${path}`,
|
|
265
|
+
migrateWorkspaceFile: '从 presmium.workspace.yaml 删除 documents,文稿的位置记在各自的文稿中',
|
|
266
|
+
migrateProblem: (path, reason) => `${path}:无法升级:${reason}`,
|
|
267
|
+
migrateNothing: dir => `${dir} 中的文稿已是当前格式,无需升级。`,
|
|
268
|
+
migrateNeeded: (documents, workspaceFile) => `需要升级 ${documents} 份文稿${workspaceFile ? '与 presmium.workspace.yaml' : ''}。运行 presmium migrate 完成升级。`,
|
|
269
|
+
migrateDone: documents => `已升级 ${documents} 份文稿。`,
|
|
242
270
|
badVoice: value => `--voice 需要“<服务>:<音色>”,例如 volcengine:zh_female_tianmeitaozi_uranus_bigtts,而不是“${value}”。`,
|
|
243
271
|
documentChanged: '文稿在读取之后被修改过,没有写入。请重新运行。',
|
|
244
272
|
documentInvalid: detail => `修改后的文稿无效,没有写入:${detail}`,
|
|
@@ -338,9 +366,9 @@ The deck is a directory holding ${DOCUMENT_ENTRY}, or that entry file (default:
|
|
|
338
366
|
|
|
339
367
|
Commands:
|
|
340
368
|
new a new project in the directory (default: asks), from create-presmium
|
|
341
|
-
--type <kind>
|
|
369
|
+
--type <kind> document, workspace, site, or plugin (default: asks)
|
|
342
370
|
--theme <name> site theme: course, docs, or showcase (default: course)
|
|
343
|
-
--title <text> the
|
|
371
|
+
--title <text> the document, workspace, or site title
|
|
344
372
|
--name <name> the plugin’s package name
|
|
345
373
|
--no-git do not run git init
|
|
346
374
|
skills install bundled skills, shared with MCP and the visual editor
|
|
@@ -372,6 +400,10 @@ Commands:
|
|
|
372
400
|
--lang <tag> the language of notes whose frontmatter has no lang (default: from the text)
|
|
373
401
|
--group <n> make the note's folder at depth n the document's first tag; the site's
|
|
374
402
|
sidebar groups documents by it, e.g. --group 2 groups projects/<project>/
|
|
403
|
+
migrate convert documents to the current format (a workspace root or one document, default .):
|
|
404
|
+
lecture.json becomes ${DOCUMENT_ENTRY}, each document records its place in the document tree,
|
|
405
|
+
and presmium.workspace.yaml no longer lists documents
|
|
406
|
+
--check only report what would change, without writing; exits with 1 when needed
|
|
375
407
|
check check the deck and run the plugins’ checks
|
|
376
408
|
--json print the result as JSON
|
|
377
409
|
--update-lock re-resolve anchors and rewrite .presmium/anchors.lock.json
|
|
@@ -396,7 +428,7 @@ Commands:
|
|
|
396
428
|
its line breaks and blank lines, {shown|spoken} for a word shown one way and said
|
|
397
429
|
another, ⟦cue:…⟧ for a cue, and \\ before a \\, {, }, ⟦ or ⟧ meant as text
|
|
398
430
|
(and before a | inside {…|…})
|
|
399
|
-
import <deck> [<file>] write the edited paragraphs back into
|
|
431
|
+
import <deck> [<file>] write the edited paragraphs back into index.pmd, changing only those;
|
|
400
432
|
nothing is written when a marker is missing or unknown, or a cue was removed
|
|
401
433
|
or moved to another paragraph
|
|
402
434
|
--out <file> where export writes (default: <deck>/dist/script.md, which import reads by default)
|
|
@@ -445,6 +477,7 @@ Exit Codes: 0 no errors, 1 error findings, 2 usage or internal error.`,
|
|
|
445
477
|
audienceNotForWorkspace: '--audience does not apply to a workspace yet. Export each document with --audience instead.',
|
|
446
478
|
badAudience: value => `“${value}” is not the name of an audience. Give a name of letters, digits, ., _, or -, such as student.`,
|
|
447
479
|
notFound: entry => `Couldn’t find ${entry}. Check the path, or make a document with presmium new.`,
|
|
480
|
+
olderFormat: dir => `The document in ${dir} uses the older format (lecture.json). Run presmium migrate ${dir} to upgrade it, then try again.`,
|
|
448
481
|
emptyTo: '--to needs at least one renderer, such as --to web.',
|
|
449
482
|
badAllow: (kinds, value) => `--allow does not take “${value}”. The kinds are ${kinds.join(', ')}.`,
|
|
450
483
|
badPreviewOrigin: value => `“${value}” is not an origin. Give a scheme, host, and port, such as http://127.0.0.1:4320.`,
|
|
@@ -473,7 +506,23 @@ Exit Codes: 0 no errors, 1 error findings, 2 usage or internal error.`,
|
|
|
473
506
|
importFailed: 'Import failed. Check the file encoding, whether the Word file is complete, and output directory permissions.',
|
|
474
507
|
imported: (documents, files, out) => `imported ${documents} documents and copied ${files} files into ${out}`,
|
|
475
508
|
importUnresolved: (note, link) => `${note}: the link ${link} leads to no note or file; it is plain text now`,
|
|
476
|
-
|
|
509
|
+
migrateHelp: `Usage: presmium migrate [<dir>] [--check]
|
|
510
|
+
|
|
511
|
+
Converts the documents of a workspace, or one document, from the older format to the current one, as Upgrade
|
|
512
|
+
does in Presmium Studio: lecture.json is renamed to ${DOCUMENT_ENTRY}, keeping the file’s history, translations too; each document records its place in the document tree, at the top level in
|
|
513
|
+
the order of documents in presmium.workspace.yaml, then by path; documents is removed from
|
|
514
|
+
presmium.workspace.yaml; paths that named lecture.json are rewritten. A document’s own settings stay as they
|
|
515
|
+
are. Running it again leaves upgraded documents alone.
|
|
516
|
+
|
|
517
|
+
--check only report what would change, without writing; exits with 1 when an upgrade is needed
|
|
518
|
+
-h, --help show this help`,
|
|
519
|
+
migrateRename: (from, to) => `rename ${from} → ${to}`,
|
|
520
|
+
migrateDocument: path => `upgrade ${path}`,
|
|
521
|
+
migrateWorkspaceFile: 'remove documents from presmium.workspace.yaml; each document records its own place',
|
|
522
|
+
migrateProblem: (path, reason) => `${path}: Couldn’t upgrade: ${reason}`,
|
|
523
|
+
migrateNothing: dir => `The documents in ${dir} already use the current format; nothing to upgrade.`,
|
|
524
|
+
migrateNeeded: (documents, workspaceFile) => `${documents} ${documents === 1 ? 'document needs' : 'documents need'} an upgrade${workspaceFile ? ', and so does presmium.workspace.yaml' : ''}. Run presmium migrate to upgrade.`,
|
|
525
|
+
migrateDone: documents => `Upgraded ${documents} ${documents === 1 ? 'document' : 'documents'}.`,
|
|
477
526
|
badVoice: value => `--voice takes “<service>:<voice>”, such as volcengine:zh_female_tianmeitaozi_uranus_bigtts, not “${value}”.`,
|
|
478
527
|
documentChanged: 'The document changed after it was read, so nothing was written. Run the command again.',
|
|
479
528
|
documentInvalid: detail => `The changed document is not valid, so nothing was written: ${detail}`,
|
package/src/migrate.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, renameSync } from 'node:fs'
|
|
3
|
+
import { join, relative, resolve } from 'node:path'
|
|
4
|
+
import { parseArgs } from 'node:util'
|
|
5
|
+
import { errorMessage } from '@lincyaw/kernel'
|
|
6
|
+
import { applyMigration, planMigration } from '@lincyaw/compiler/migrate'
|
|
7
|
+
import type { CliMessages } from './messages.ts'
|
|
8
|
+
|
|
9
|
+
/** A rename that keeps the file's history where git tracks it (`git mv`), a plain rename elsewhere. */
|
|
10
|
+
function renameKeepingHistory(root: string) {
|
|
11
|
+
return (from: string, to: string) => {
|
|
12
|
+
try { execFileSync('git', ['mv', '--', from, to], { cwd: root, stdio: 'ignore' }) }
|
|
13
|
+
catch { renameSync(join(root, from), join(root, to)) }
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `presmium migrate [<dir>] [--check]`: converts the documents of a workspace or one document to the current format. */
|
|
18
|
+
export async function runMigrateCommand(argv: string[], t: CliMessages): Promise<number> {
|
|
19
|
+
let parsed
|
|
20
|
+
try {
|
|
21
|
+
parsed = parseArgs({ args: argv, allowPositionals: true, options: { check: { type: 'boolean', default: false }, help: { type: 'boolean', short: 'h', default: false } } })
|
|
22
|
+
} catch (error) {
|
|
23
|
+
process.stderr.write(`presmium: ${errorMessage(error)}\n\n${t.migrateHelp}\n`)
|
|
24
|
+
return 2
|
|
25
|
+
}
|
|
26
|
+
const { values, positionals } = parsed
|
|
27
|
+
if (values.help) { process.stdout.write(`${t.migrateHelp}\n`); return 0 }
|
|
28
|
+
if (positionals.length > 1) { process.stderr.write(`presmium: ${t.unexpectedArgument(positionals[1]!)}\n`); return 2 }
|
|
29
|
+
const root = resolve(positionals[0] ?? '.')
|
|
30
|
+
if (!existsSync(root)) { process.stderr.write(`presmium: ${t.notFound(positionals[0] ?? '.')}\n`); return 2 }
|
|
31
|
+
const plan = planMigration(root)
|
|
32
|
+
const shown = (path: string) => relative(process.cwd(), join(root, path)) || '.'
|
|
33
|
+
for (const { from, to } of plan.renames) process.stdout.write(`${t.migrateRename(shown(from), shown(to))}\n`)
|
|
34
|
+
for (const path of plan.documents) process.stdout.write(`${t.migrateDocument(shown(path))}\n`)
|
|
35
|
+
if (plan.workspaceFile) process.stdout.write(`${t.migrateWorkspaceFile}\n`)
|
|
36
|
+
for (const problem of plan.problems) process.stderr.write(`${t.migrateProblem(shown(problem.path), problem.message)}\n`)
|
|
37
|
+
const pending = plan.documents.length > 0 || plan.workspaceFile || plan.writes.length > 0
|
|
38
|
+
if (!pending) {
|
|
39
|
+
process.stdout.write(`${t.migrateNothing(relative(process.cwd(), root) || '.')}\n`)
|
|
40
|
+
return plan.problems.length ? 1 : 0
|
|
41
|
+
}
|
|
42
|
+
if (values.check) {
|
|
43
|
+
process.stdout.write(`${t.migrateNeeded(plan.documents.length, plan.workspaceFile)}\n`)
|
|
44
|
+
return 1
|
|
45
|
+
}
|
|
46
|
+
applyMigration(plan, renameKeepingHistory(root))
|
|
47
|
+
process.stdout.write(`${t.migrateDone(plan.documents.length)}\n`)
|
|
48
|
+
return plan.problems.length ? 1 : 0
|
|
49
|
+
}
|
package/src/script.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { NarrationNode,
|
|
1
|
+
import type { NarrationNode, PresmiumDocument } from '@lincyaw/kernel'
|
|
2
2
|
|
|
3
|
-
// The narration of a document as one Markdown file to polish, and back;
|
|
3
|
+
// The narration of a document as one Markdown file to polish, and back; index.pmd stays the only source. Each step
|
|
4
4
|
// is a heading; under it each narration follows its marker line `<!-- n: <narration> -->` up to the next marker or
|
|
5
5
|
// heading. Each line break of a narration starts a new line of the file, and a blank line stands for a blank line
|
|
6
6
|
// (`\n\n`; several read back as one).
|
|
@@ -44,7 +44,7 @@ function linesToText(lines: readonly string[]): string {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/** The narrated steps in reading order, each with the narrations it is the first to say (a shared one is listed once). */
|
|
47
|
-
export function narratedSteps(document:
|
|
47
|
+
export function narratedSteps(document: PresmiumDocument): { section: string, title: string, step: string, stepTitle: string, narrations: string[] }[] {
|
|
48
48
|
const seen = new Set<string>()
|
|
49
49
|
return document.sectionOrder.flatMap(sectionId => {
|
|
50
50
|
const section = document.sections[sectionId]!
|
|
@@ -57,7 +57,7 @@ export function narratedSteps(document: TeachingDocument): { section: string, ti
|
|
|
57
57
|
})
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
export function exportScript(document:
|
|
60
|
+
export function exportScript(document: PresmiumDocument, instructions: string): { text: string, steps: number, narrations: number } {
|
|
61
61
|
const steps = narratedSteps(document)
|
|
62
62
|
const parts = [`# ${document.metadata.title}`, '', `<!-- ${instructions} -->`, '']
|
|
63
63
|
let page: string | undefined
|
|
@@ -130,7 +130,7 @@ function contentOf(id: string, text: string, before: NarrationNode[]): Narration
|
|
|
130
130
|
* Applies the file to the document: every narrated step's markers must be there exactly once; only the narrations whose
|
|
131
131
|
* text differs from what an untouched export reads back as are changed. Returns the same document when nothing changed.
|
|
132
132
|
*/
|
|
133
|
-
export function importScript(document:
|
|
133
|
+
export function importScript(document: PresmiumDocument, text: string): { document: PresmiumDocument, changed: string[] } {
|
|
134
134
|
const found = readScript(text)
|
|
135
135
|
const expected = narratedSteps(document).flatMap(step => step.narrations)
|
|
136
136
|
const unknown = [...found.keys()].filter(id => !expected.includes(id))
|
package/src/voiceover.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto'
|
|
2
2
|
import { relative } from 'node:path'
|
|
3
3
|
import { changeDocumentVoice, errorMessage, SPEECH, speechEngineId, speechFailureCode, VOICEOVER_EXECUTION } from '@lincyaw/kernel'
|
|
4
|
-
import type { SpeechCachePrune, SpeechEngineInfo, SpeechService, SpeechVoice,
|
|
5
|
-
import { Compiler } from '@lincyaw/compiler'
|
|
4
|
+
import type { SpeechCachePrune, SpeechEngineInfo, SpeechService, SpeechVoice, PresmiumDocument, VoiceoverExecution, VoiceoverRunResult, VoiceoverSentence, VoiceoverSentences, VoiceScope } from '@lincyaw/kernel'
|
|
5
|
+
import { Compiler, effectiveDocument } from '@lincyaw/compiler'
|
|
6
6
|
import type { Compilation } from '@lincyaw/compiler'
|
|
7
7
|
import { readDocumentFile, saveDocumentFile } from './document-file.ts'
|
|
8
8
|
import type { CliMessages } from './messages.ts'
|
|
@@ -52,7 +52,7 @@ export function voiceLabel(voice: SpeechVoice): string {
|
|
|
52
52
|
const sameVoice = (a: SpeechVoice, b: SpeechVoice) => a.voice === b.voice && speechEngineId(a.engine ?? '') === speechEngineId(b.engine ?? '')
|
|
53
53
|
|
|
54
54
|
/** The document's default voice, or why it has none. */
|
|
55
|
-
function defaultVoice(document:
|
|
55
|
+
function defaultVoice(document: PresmiumDocument): SpeechVoice | 'none' | undefined {
|
|
56
56
|
const source = document.production.defaults.voiceSource
|
|
57
57
|
return source?.type === 'tts' ? source.voice : source?.type === 'none' ? 'none' : undefined
|
|
58
58
|
}
|
|
@@ -132,7 +132,8 @@ async function voiceover(options: VoiceoverOptions, t: CliMessages, signal: Abor
|
|
|
132
132
|
const fallback = await speech?.defaultSynthesisEngine?.().catch(() => undefined)
|
|
133
133
|
if (fallback) engines.fallback = fallback
|
|
134
134
|
|
|
135
|
-
const
|
|
135
|
+
const effective = await effectiveDocument(options.entry, file.document)
|
|
136
|
+
const before = defaultVoice(effective)
|
|
136
137
|
const changesVoice = options.voice !== undefined && !(typeof before === 'object' && sameVoice(before, options.voice))
|
|
137
138
|
const planned = changesVoice ? changeDocumentVoice(file.document, { kind: 'document' }, { type: 'tts', voice: options.voice! }) : file.document
|
|
138
139
|
let listing: VoiceoverSentences = await execution.sentences(changesVoice ? { defaultVoice: options.voice! } : {}, signal)
|
|
@@ -140,7 +141,7 @@ async function voiceover(options: VoiceoverOptions, t: CliMessages, signal: Abor
|
|
|
140
141
|
|
|
141
142
|
const json: Record<string, unknown> = { document: compilation.deckDir }
|
|
142
143
|
say(`${t.voiceoverLabels.document} ${relative(process.cwd(), compilation.deckDir) || '.'}`)
|
|
143
|
-
const now = defaultVoice(planned)
|
|
144
|
+
const now = defaultVoice(changesVoice ? planned : effective)
|
|
144
145
|
const shown = typeof now === 'object' ? voiceLabel(now) : now === 'none' ? t.voiceoverNoVoice : t.voiceoverUnset
|
|
145
146
|
say(`${t.voiceoverLabels.voice} ${changesVoice ? t.voiceoverVoiceChange(shown, typeof before === 'object' ? voiceLabel(before) : before === 'none' ? t.voiceoverNoVoice : t.voiceoverUnset) : t.voiceoverVoiceDefault(shown)}`)
|
|
146
147
|
json.voice = { default: typeof now === 'object' ? now : null, changed: changesVoice }
|