Expand file tree
/
Copy pathcreative-create-story.js
More file actions
153 lines (141 loc) · 5.59 KB
/
Copy pathcreative-create-story.js
File metadata and controls
153 lines (141 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
Manuscript,
Story,
StoryArchive,
} from '../.codex/creative/objects';
import { StoryModal } from '../.codex/creative/modals/StoryModal.js';
import { sceneTemplateSuffixForStage } from '../.codex/creative/controls/scene-template-helpers.js';
import { getPriorStage } from '../.codex/creative/controls/manuscript-pipelines.js';
import { Log } from '../.codex/utils/logger.js';
/**
* Unlike create-scene, this command's target folder isn't a scaffold-free
* destination -- it's the story's own already-cloned submodule root (a lean
* submodule containing just README/.github, activated via the vault's git
* workflow before this command runs), and everything below gets built inside
* it: the Story note itself, one Manuscript per pipeline stage, its
* scene Templater templates, the ARCHIVE dashboard and its DRAFTS/CHANGELOG
* folders.
* checkCallback (not callback) so the command is only enabled from within a
* resolvable folder -- checking=true just resolves/validates the folder and
* returns a boolean; checking=false does that same resolution again (cheap)
* and then fires the actual async modal + scaffold work below. Must use
* concise method syntax (not a lambda) or `this.app` is undefined -- see
* invocable-scripts.md.
*/
export function buildInvokeCommand(app) {
return {
name: 'Create Story',
id: 'creative-create-story',
checkCallback(checking) {
const folder = app.workspace.getActiveFile()?.parent?.path ?? '';
if (!folder) {
return false;
}
if (!checking) {
runCreateStory(app, folder);
}
return true;
}
};
};
async function runCreateStory(app, folder) {
new StoryModal(app, async (result) => {
try {
// Built (not yet written) first, so its storyTag/pipeline/title
// getters are available for wiring up Manuscript/StoryArchive.
// manuscriptLinks gets populated as each Manuscript is created
// below, then story.create() writes the file last -- correct on
// its one and only write, no follow-up re-save needed.
// Filename and title both take the name of the folder this
// command is run from (e.g. "Dark Side of the Moon/" ->
// "Dark Side of the Moon.md", title "Dark Side of the Moon")
// -- no longer collected via the modal.
const folderName = folder.split('/').pop();
const story = new Story(app, {
folder,
filename: folderName,
title: folderName,
...result
});
// title-abbv names this story's generated scene Templater
// templates below -- StoryModal marks it required, but has no
// built-in way to block an empty submit, so the actual
// enforcement lives here.
if (!story.titleAbbv) {
const error = new Error('Title abbreviation is required to scaffold a story -- it names the generated scene templates.');
Log.error(
'create-story',
error.message,
error
);
throw error;
}
const pipeline = story.pipeline; // throws if category has no defined pipeline
// Scene Templater templates -- created first (one to three .md
// files per Story.buildSceneTemplates(), placed at the story
// project's own root, not inside any stage folder) since each
// stage's Manuscript below needs a template's actual vault path
// to wire into its own longform.sceneTemplate.
const sceneTemplatePaths = {}; // suffix -> vault-relative path (with .md)
for (const {
filename,
suffix,
content
} of story.buildSceneTemplates()) {
const templateFile = await app.vault.create(`${ folder }/${ filename }`, content);
sceneTemplatePaths[suffix] = templateFile.path;
Log.log('create-story', `Scene template created: ${ filename }`);
}
for (const stage of pipeline) {
const stageFolder = `${ folder }/${ stage }`;
await app.vault.createFolder(stageFolder);
const manuscript = new Manuscript(app, {
folder: stageFolder,
filename: stage,
category: 'draft',
affiliations: story.affiliations,
// Manuscript doesn't auto-include the story's tag the way
// Story does for itself -- every dashboard query in both
// Manuscript.js and Story.js filters on #storyTag, so this
// has to be passed explicitly or the dashboards silently
// find nothing.
tags: [ story.storyTag ],
stage,
editorialStatus: '',
priorStage: getPriorStage(story.category, stage),
context: '',
storyTag: story.storyTag,
title: story.title,
sceneTemplate: sceneTemplatePaths[sceneTemplateSuffixForStage(stage)] ?? '',
});
const manuscriptFile = await manuscript.create();
story.manuscriptLinks[stage] = manuscriptFile.basename;
Log.log('create-story', `Manuscript created for stage "${ stage }".`);
}
const archiveFolder = `${ folder }/ARCHIVE`;
await app.vault.createFolder(archiveFolder);
await app.vault.createFolder(`${ archiveFolder }/DRAFTS`);
await app.vault.createFolder(`${ archiveFolder }/CHANGELOG`);
// Empty folders don't survive a git commit on their own.
await app.vault.create(`${ archiveFolder }/DRAFTS/.gitkeep`, '');
await app.vault.create(`${ archiveFolder }/CHANGELOG/.gitkeep`, '');
const storyArchive = new StoryArchive(app, {
folder: archiveFolder,
filename: 'ARCHIVE',
category: 'overview',
moduleTag: story.storyTag,
});
await storyArchive.create();
Log.log('create-story', 'ARCHIVE dashboard created.');
const storyFile = await story.create();
await app.workspace.getLeaf(false).openFile(storyFile);
} catch (error) {
Log.error(
'create-story',
'Story scaffold failed partway through -- check the vault for partially-created folders/notes before retrying.',
error
);
throw error;
}
}).open();
}