-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreateAsset.js
More file actions
112 lines (84 loc) · 3.32 KB
/
createAsset.js
File metadata and controls
112 lines (84 loc) · 3.32 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
import gameName from './currentGameName.js';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import pkg from 'jsonfile';
import {entityNameRegex} from "./constants.ts";
const gameDir = `src/Games/${gameName}`;
const { readFileSync } = pkg;
const assetType = process.argv[2];
if (!assetType) {
console.log('You need to specify an asset type, e.g. location.');
process.exit();
}
const assetName = process.argv[3];
if (!assetName) {
console.log('You need to specify an asset name, e.g. \'Cave\'.');
process.exit();
}
if (!entityNameRegex.exec(assetName)) {
console.log(`${assetName} is not a valid asset name! The name must start with a letter and contain only letters, numbers and '_'.`);
process.exit();
}
let baseSnippetKey = assetType.substring(0, 1).toUpperCase() + assetType.substring(1) + 's';
let snippetKey = baseSnippetKey.endsWith('ys') ? baseSnippetKey.substring(0, baseSnippetKey.length - 2) + 'ies' : baseSnippetKey;
const assetNameCapital = assetName.substring(0, 1).toUpperCase() + assetName.substring(1);
const snippets = readFileSync('CodeSnippets/StoryScriptSnippets.code-snippets');
if (!snippets[snippetKey]) {
if (!snippets[baseSnippetKey]) {
console.log(`No asset type ${assetType} exists.`);
process.exit();
}
snippetKey = baseSnippetKey;
}
const descriptionSnippet = snippets['Description'];
if (!descriptionSnippet) {
console.log('The description snippet doesn\'t exist.');
process.exit();
}
let includeDescription = !process.argv[4] || process.argv[4].toLowerCase() !== 'p';
includeDescription = (snippetKey === 'Locations' || snippetKey === 'Persons') || ((snippetKey === 'Items' || snippetKey === 'Keys' || snippetKey === 'Enemies') && includeDescription);
let conversationSnippet = null;
if (snippetKey === 'Persons') {
conversationSnippet = snippets['Conversation'];
if (!conversationSnippet) {
console.log('The conversation snippet doesn\'t exist.');
}
}
const snippet = snippets[snippetKey];
// Keys go into the items folder.
const dirName = snippetKey === 'Keys' ? 'Items' : snippetKey;
const assetDir = `${gameDir}/${dirName.toLowerCase()}`;
const assetBaseFileName = `${assetDir}/${assetName}`;
if (!existsSync(assetDir)){
mkdirSync(assetDir);
}
if (!includeDescription) {
const cleaned = [];
snippet.body = Object.keys(snippet.body).forEach(k => {
const l = snippet.body[k];
if (!l.match(/import description from \'\.\/\$\{TM_FILENAME_BASE\}\.html?raw';/g) && !l.match(/[\\t]{0,}description:/g)) {
cleaned.push(l);
}
});
snippet.body = cleaned;
}
const tsString = snippet.body
.join('\n')
.replace(/\${TM_FILENAME_BASE}/g, assetName)
.replace(/\${TM_FILENAME_BASE\/\(\.\*\)\/\${1:\/capitalize}\/}/g, assetNameCapital)
.replace(/\$[0-9]{1,}/g, '');
// Write ts file
writeFileSync(assetBaseFileName + '.ts', tsString);
if (!includeDescription) {
process.exit();
}
let htmlString = removePlaceholders(descriptionSnippet);
if (conversationSnippet) {
htmlString = htmlString + '\n' + removePlaceholders(conversationSnippet);
}
// Write html file
writeFileSync(assetBaseFileName + '.html', htmlString);
function removePlaceholders(value) {
return value.body
.join('\n')
.replace(/\$[0-9]{1,}/g, '');
}