forked from emergentstories/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·130 lines (120 loc) · 4.11 KB
/
Copy pathcli.js
File metadata and controls
executable file
·130 lines (120 loc) · 4.11 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
import { compilePack, extractPack } from '@foundryvtt/foundryvtt-cli'
import fs from 'fs'
import path from 'path'
import logger from 'fancy-log'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
const COMPENDIUM_SOURCE = 'packs/_source'
const COMPENDIUM_DEST = 'packs'
function slugify(name) {
return name
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace("'", '')
.replace(/[^a-z0-9]+/gi, ' ')
.trim()
.replace(/\s+|-{2,}/g, '-')
}
function cleanEntry(entry, { clearSourceId = true, ownership = 0 } = {}) {
if (entry.ownership) entry.ownership = { default: ownership }
if (clearSourceId) {
delete entry._stats?.compendiumSource
delete entry.flags?.core?.sourceId
}
delete entry.flags?.importSource
delete entry.flags?.exportSource
if (entry._stats?.lastModifiedBy) entry._stats.lastModifiedBy = 'rloriteexporter0'
if (!entry.flags) entry.flags = {}
Object.entries(entry.flags).forEach(([key, contents]) => {
if (Object.keys(contents).length === 0) delete entry.flags[key]
})
if (entry.effects) entry.effects.forEach((i) => cleanEntry(i, { clearSourceId: false }))
}
async function extractCompendiums() {
const system = JSON.parse(fs.readFileSync('./module.json', { encoding: 'utf-8' }))
for (const pack of system.packs) {
const destPath = path.join(COMPENDIUM_SOURCE, pack.name)
const packPath = pack.path || `packs/${pack.name}`
logger.info(`Extrayendo el compendio ${pack.label}`)
const folders = {}
const containers = {}
await extractPack(packPath, destPath, {
log: false,
transformEntry: (e) => {
if (e._key.startsWith('!folders'))
folders[e._id] = { name: slugify(e.name), folder: e.folder }
else if (e.type === 'container')
containers[e._id] = {
name: slugify(e.name),
container: e.system?.container,
folder: e.folder,
}
return false
},
})
const buildPath = (collection, entry, parentKey) => {
let parent = collection[entry[parentKey]]
entry.path = entry.name
while (parent) {
entry.path = path.join(parent.name, entry.path)
parent = collection[parent[parentKey]]
}
}
Object.values(folders).forEach((f) => buildPath(folders, f, 'folder'))
Object.values(containers).forEach((c) => {
buildPath(containers, c, 'container')
const folder = folders[c.folder]
if (folder) c.path = path.join(folder.path, c.path)
})
await extractPack(packPath, destPath, {
yaml: true,
transformEntry: (entry) => cleanEntry(entry),
transformName: (entry) => {
if (entry._id in folders) return path.join(folders[entry._id].path, '_folder.yaml')
if (entry._id in containers) return path.join(containers[entry._id].path, '_container.yaml')
const outputName = slugify(entry.name)
const parent = containers[entry.system?.container] ?? folders[entry.folder]
return path.join(parent?.path ?? '', `${outputName}.yaml`)
},
})
}
}
async function compileCompendiums() {
const folders = fs
.readdirSync(COMPENDIUM_SOURCE, { withFileTypes: true })
.filter((file) => file.isDirectory())
for (const folder of folders) {
const src = path.join(COMPENDIUM_SOURCE, folder.name)
const dest = path.join(COMPENDIUM_DEST, folder.name)
logger.info(`Compilando el compendio ${folder.name}`)
await compilePack(src, dest, {
recursive: true,
yaml: true,
log: true,
transformEntry: cleanEntry,
})
}
}
yargs(hideBin(process.argv))
.command({
command: 'compendium [action]',
describe: 'Compilar y extraer compendios',
builder: (yargs) => {
yargs.positional('action', {
describe: 'La acción que se desea ejecutar sobre los compendios',
type: 'string',
choices: ['compile', 'extract'],
})
},
handler: async (argv) => {
const { action } = argv
switch (action) {
case 'extract':
return await extractCompendiums()
case 'compile':
return await compileCompendiums()
}
},
})
.parse()