last update
This commit is contained in:
172
lib/itchpack.js
Normal file
172
lib/itchpack.js
Normal file
@@ -0,0 +1,172 @@
|
||||
// itch.io package of a project: the folder the itchiocreater skill writes
|
||||
// (<project>\docs\itchio, older projects: <project>\itchio) - read back for the
|
||||
// dashboard's copy view, and a console that starts Claude Code with the skill.
|
||||
// Nothing is uploaded anywhere.
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const { run, openConsole } = require('./proc')
|
||||
|
||||
const CANDIDATES = [['docs', 'itchio'], ['itchio']]
|
||||
const IMG_RE = /\.(png|jpe?g|gif|webp)$/i
|
||||
const MAX_HTML = 512 * 1024
|
||||
|
||||
function dir(projectPath) {
|
||||
for (const parts of CANDIDATES) {
|
||||
const d = path.join(projectPath, ...parts)
|
||||
try { if (fs.statSync(d).isDirectory()) return d } catch {}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// newest modification inside the folder (first level) - "created 3 days ago"
|
||||
function stamp(d) {
|
||||
let at = 0
|
||||
try {
|
||||
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
||||
if (!e.isFile()) continue
|
||||
try { at = Math.max(at, fs.statSync(path.join(d, e.name)).mtimeMs) } catch {}
|
||||
}
|
||||
} catch {}
|
||||
return at ? new Date(at).toISOString() : null
|
||||
}
|
||||
|
||||
// cheap: for /details
|
||||
function summary(projectPath) {
|
||||
const d = dir(projectPath)
|
||||
return d ? { exists: true, at: stamp(d), rel: path.relative(projectPath, d) } : { exists: false }
|
||||
}
|
||||
|
||||
// "**Tags**" / "`a`, `b` - comment" → shown text and the value worth copying:
|
||||
// a leading run of `code` items (the answer) without the comment after it,
|
||||
// otherwise the whole text
|
||||
function cell(raw) {
|
||||
const plain = raw.replace(/\*\*/g, '').trim()
|
||||
const lead = plain.match(/^`[^`]+`(\s*,\s*`[^`]+`)*/)
|
||||
const text = plain.replace(/`/g, '').trim()
|
||||
const placeholder = /^_[^_]+_$/.test(text) || text === '–' || text === '-'
|
||||
const copy = placeholder ? '' : lead ? [...lead[0].matchAll(/`([^`]+)`/g)].map((m) => m[1].trim()).join(', ') : text
|
||||
return { text, copy, placeholder }
|
||||
}
|
||||
|
||||
// all two+-column markdown tables of the .md files, except file lists
|
||||
function parseFields(d) {
|
||||
const fields = []
|
||||
const seen = new Set()
|
||||
let files = []
|
||||
try { files = fs.readdirSync(d).filter((f) => /\.md$/i.test(f)) } catch {}
|
||||
files.sort((a, b) => (a.toLowerCase() === 'readme.md' ? -1 : b.toLowerCase() === 'readme.md' ? 1 : a.localeCompare(b)))
|
||||
for (const f of files) {
|
||||
let text = ''
|
||||
try { text = fs.readFileSync(path.join(d, f), 'utf-8') } catch { continue }
|
||||
let header = null
|
||||
let skip = false
|
||||
for (const line of text.split(/\r?\n/)) {
|
||||
if (!line.trim().startsWith('|')) { header = null; continue }
|
||||
const cells = line.trim().replace(/^\||\|$/g, '').split('|').map((c) => c.trim())
|
||||
if (cells.length < 2) continue
|
||||
if (!header) { header = cells; skip = /^(datei|file)/i.test(cells[0].replace(/\*/g, '')); continue }
|
||||
if (cells.every((c) => /^:?-{2,}:?$/.test(c))) continue
|
||||
if (skip) continue
|
||||
const key = cells[0].replace(/\*\*/g, '').replace(/`/g, '').trim()
|
||||
if (!key || seen.has(key.toLowerCase())) continue
|
||||
seen.add(key.toLowerCase())
|
||||
fields.push({ key, ...cell(cells[1]) })
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
function kindOf(rel) {
|
||||
const n = rel.toLowerCase().replace(/\\/g, '/')
|
||||
const base = n.split('/').pop()
|
||||
if (/^cover/.test(base)) return 'cover'
|
||||
if (/^(banner|background|bg)/.test(base)) return 'theme'
|
||||
if (/^promo\//.test(n) || /^promo/.test(base)) return 'promo'
|
||||
if (/^(screenshots|screens|shots)\//.test(n) || /^(shot|screen)/.test(base)) return 'screenshot'
|
||||
return 'other'
|
||||
}
|
||||
|
||||
function images(d) {
|
||||
const out = []
|
||||
const add = (rel) => {
|
||||
try {
|
||||
const st = fs.statSync(path.join(d, rel))
|
||||
out.push({ rel, kind: kindOf(rel), mb: Math.round(st.size / 1024 / 1024 * 100) / 100 })
|
||||
} catch {}
|
||||
}
|
||||
let top = []
|
||||
try { top = fs.readdirSync(d, { withFileTypes: true }) } catch {}
|
||||
for (const e of top) {
|
||||
if (e.isFile() && IMG_RE.test(e.name)) add(e.name)
|
||||
else if (e.isDirectory() && !e.name.startsWith('.')) {
|
||||
try {
|
||||
for (const f of fs.readdirSync(path.join(d, e.name))) if (IMG_RE.test(f) && !/^raw_/i.test(f)) add(path.join(e.name, f))
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
const order = { cover: 0, theme: 1, promo: 2, screenshot: 3, other: 4 }
|
||||
return out.sort((a, b) => order[a.kind] - order[b.kind] || a.rel.localeCompare(b.rel))
|
||||
}
|
||||
|
||||
function readHtml(d, name) {
|
||||
const f = path.join(d, name)
|
||||
try { if (fs.statSync(f).size <= MAX_HTML) return fs.readFileSync(f, 'utf-8') } catch {}
|
||||
return null
|
||||
}
|
||||
|
||||
function read(projectPath) {
|
||||
const d = dir(projectPath)
|
||||
if (!d) return { exists: false }
|
||||
const fields = parseFields(d)
|
||||
const pick = (re) => fields.find((x) => re.test(x.key)) ?? null
|
||||
let other = []
|
||||
try { other = fs.readdirSync(d).filter((f) => /\.(mp4|webm|zip|exe)$/i.test(f)) } catch {}
|
||||
return {
|
||||
exists: true,
|
||||
rel: path.relative(projectPath, d),
|
||||
at: stamp(d),
|
||||
title: pick(/^title$/i),
|
||||
url: pick(/^project url$/i),
|
||||
tagline: pick(/^short description/i),
|
||||
fields: fields.filter((x) => !/^(title|project url|short description.*)$/i.test(x.key)),
|
||||
descriptionHtml: readHtml(d, 'description.html'),
|
||||
installHtml: readHtml(d, 'install.html'),
|
||||
images: images(d),
|
||||
media: other,
|
||||
}
|
||||
}
|
||||
|
||||
// a file inside the package folder (image), or null - guards against ..\
|
||||
function file(projectPath, rel) {
|
||||
const d = dir(projectPath)
|
||||
if (!d || typeof rel !== 'string' || !rel) return null
|
||||
const f = path.resolve(d, rel)
|
||||
if (!f.startsWith(d + path.sep)) return null
|
||||
try { if (fs.statSync(f).isFile()) return f } catch {}
|
||||
return null
|
||||
}
|
||||
|
||||
let claudeFound = null
|
||||
async function hasClaude() {
|
||||
if (claudeFound) return true
|
||||
try {
|
||||
const r = await run('where.exe', ['claude'], { timeout: 5000 })
|
||||
claudeFound = r.code === 0 && /claude/i.test(r.stdout ?? '')
|
||||
} catch { claudeFound = false }
|
||||
return claudeFound
|
||||
}
|
||||
|
||||
// Opens a console with Claude Code and the itchiocreater skill in the project.
|
||||
// Interactive on purpose: the skill asks a few questions (price, kind, …).
|
||||
async function launch(projectPath, name) {
|
||||
if (!(await hasClaude())) throw new Error('Claude Code nicht gefunden (Befehl „claude“). Installieren: https://claude.com/claude-code')
|
||||
const exists = !!dir(projectPath)
|
||||
// ASCII only: the .cmd file is read in the console's code page
|
||||
const prompt = exists
|
||||
? '/itchiocreater Check and complete the itch.io package of this project - do not overwrite anything without asking.'
|
||||
: '/itchiocreater Create the itch.io package for this project.'
|
||||
openConsole(`${name} itch.io`, projectPath, `claude "${prompt}"`)
|
||||
return { ok: true, update: exists }
|
||||
}
|
||||
|
||||
module.exports = { dir, summary, read, file, launch }
|
||||
Reference in New Issue
Block a user