|
| 1 | +// Microsoft Store listing helpers for build-store-listing.mjs, split out so |
| 2 | +// src/lib/storeListingScripts.test.ts can check the texts without an export. |
| 3 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import Papa from 'papaparse'; |
| 7 | + |
| 8 | +const ROOT = fileURLToPath(new URL('..', import.meta.url)); |
| 9 | +const LISTINGS = join(ROOT, 'store', 'microsoft'); |
| 10 | +export const SOURCE_TAG = 'en-US'; |
| 11 | + |
| 12 | +export const LIMITS = { description: 10000, features: 20, featureChars: 200, keywords: 7, keywordChars: 40, keywordWords: 21 }; |
| 13 | + |
| 14 | +// CRLF from an editor would otherwise reach Partner Center as a trailing CR. |
| 15 | +const lf = (text) => text.replace(/\r\n?/g, '\n'); |
| 16 | +const lines = (text) => lf(text).split('\n').map((line) => line.trim()).filter(Boolean); |
| 17 | + |
| 18 | +export function readListings() { |
| 19 | + return readdirSync(LISTINGS, { withFileTypes: true }) |
| 20 | + .filter((entry) => entry.isDirectory()) |
| 21 | + .map(({ name: tag }) => { |
| 22 | + const read = (file) => readFileSync(join(LISTINGS, tag, file), 'utf8'); |
| 23 | + return { tag, description: lf(read('description.txt')).trimEnd(), features: lines(read('features.txt')), keywords: lines(read('keywords.txt')) }; |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +/** What Partner Center would reject, plus translations whose feature count drifted from the source. */ |
| 28 | +export function listingsProblems(listings) { |
| 29 | + const source = listings.find((l) => l.tag === SOURCE_TAG); |
| 30 | + if (!source) return [`no ${SOURCE_TAG} source listing`]; |
| 31 | + const problems = []; |
| 32 | + for (const listing of listings) { |
| 33 | + const check = (ok, message) => { |
| 34 | + if (!ok) problems.push(`${listing.tag}: ${message}`); |
| 35 | + }; |
| 36 | + check(listing.description.length > 0, 'empty description'); |
| 37 | + check(listing.description.length <= LIMITS.description, `description over ${LIMITS.description} chars`); |
| 38 | + check(listing.features.length === source.features.length, `${listing.features.length} features, ${SOURCE_TAG} has ${source.features.length}`); |
| 39 | + check(listing.features.length <= LIMITS.features, `more than ${LIMITS.features} features`); |
| 40 | + listing.features.forEach((f, i) => check(f.length <= LIMITS.featureChars, `feature ${i + 1} over ${LIMITS.featureChars} chars`)); |
| 41 | + check(listing.keywords.length <= LIMITS.keywords, `more than ${LIMITS.keywords} keywords`); |
| 42 | + listing.keywords.forEach((k, i) => check(k.length <= LIMITS.keywordChars, `keyword ${i + 1} over ${LIMITS.keywordChars} chars`)); |
| 43 | + check(listing.keywords.join(' ').split(/\s+/).length <= LIMITS.keywordWords, `keywords over ${LIMITS.keywordWords} words`); |
| 44 | + } |
| 45 | + return problems; |
| 46 | +} |
| 47 | + |
| 48 | +export function parseCsv(text) { |
| 49 | + const { data, errors } = Papa.parse(text.replace(/^\uFEFF/, ''), { delimiter: ',', skipEmptyLines: true }); |
| 50 | + if (errors.length) throw new Error(`malformed CSV: ${errors[0].message} (row ${errors[0].row})`); |
| 51 | + return data; |
| 52 | +} |
| 53 | + |
| 54 | +/** Writes the listings into a Partner Center export. Every text slot of their |
| 55 | + * languages is rewritten, the default column included: Partner Center fills an empty |
| 56 | + * language cell from it, so a stale default would resurface everywhere. */ |
| 57 | +export function fillExport(exportCsv, listings, screenshotPath) { |
| 58 | + const rows = parseCsv(exportCsv); |
| 59 | + const column = (tag) => rows[0].indexOf(tag.toLowerCase()); |
| 60 | + const row = (field) => { |
| 61 | + const found = rows.find((r) => r[0] === field); |
| 62 | + if (!found) throw new Error(`the export has no ${field} row`); |
| 63 | + return found; |
| 64 | + }; |
| 65 | + const slots = (prefix, count) => Array.from({ length: count }, (_, i) => row(`${prefix}${i + 1}`)); |
| 66 | + |
| 67 | + const source = listings.find((l) => l.tag === SOURCE_TAG); |
| 68 | + if (!source) throw new Error(`no ${SOURCE_TAG} listing to fill the default column from`); |
| 69 | + const targets = [['default', source], ...listings.map((l) => [l.tag, l])].map(([tag, listing]) => ({ tag, listing, col: column(tag) })); |
| 70 | + for (const { tag, col } of targets) if (col < 0) throw new Error(`the export has no ${tag} column; add the language in Partner Center first`); |
| 71 | + const description = row('Description'); |
| 72 | + const features = slots('Feature', LIMITS.features); |
| 73 | + const keywords = slots('SearchTerm', LIMITS.keywords); |
| 74 | + |
| 75 | + row('DesktopScreenshot1')[column('default')] = screenshotPath; |
| 76 | + for (const { listing, col } of targets) { |
| 77 | + description[col] = listing.description; |
| 78 | + features.forEach((r, i) => { r[col] = listing.features[i] ?? ''; }); |
| 79 | + keywords.forEach((r, i) => { r[col] = listing.keywords[i] ?? ''; }); |
| 80 | + } |
| 81 | + return `\uFEFF${Papa.unparse(rows, { newline: '\r\n' })}\r\n`; |
| 82 | +} |
0 commit comments