From e75d98d974f4f620122603e622f8637d303786fe Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Mon, 28 Oct 2024 12:48:35 +1100 Subject: [PATCH] Build script fixes --- data/bso_items.json | 32 +++++++++ package.json | 2 +- scripts/build.ts | 148 ----------------------------------------- scripts/spritesheet.ts | 21 +++++- 4 files changed, 53 insertions(+), 150 deletions(-) delete mode 100644 scripts/build.ts diff --git a/data/bso_items.json b/data/bso_items.json index e34913711e..fafa9ff8b0 100644 --- a/data/bso_items.json +++ b/data/bso_items.json @@ -1617,7 +1617,39 @@ "73222": "Offhand dice plushie", "73223": "Tidal collector (i)", "73224": "Pernix components", + "73250": "Sinister crate (s7)", + "73251": "Sinister crate key (s7)", + "73252": "Mumpkin", + "73253": "Mumpkin (demonic)", + "73254": "Mumpkin (pumpkin)", + "73255": "Mumpkin (dead)", + "73256": "Puzzle box (kuro)", + "73257": "Pumpkinhead boxhat", + "73258": "White hween mask", + "73259": "Deathless victims", + "73260": "Pumpkin cloak", + "73261": "Venomous cloak", + "73262": "Bloody cloak", + "73263": "Purple cloak", + "73264": "Dark cloak", + "73265": "White cloak", + "73266": "Pumpkin sweater", + "73267": "Sinister sweater", + "73268": "Pumpkin carving knife", + "73270": "Deathtouched tart", + "73271": "Jack-o-lantern", + "73272": "Zombie cow plushie", + "73273": "Pumpkin parasol", + "73274": "Halloween cracker", + "73275": "Monster in a backpack", + "73276": "Vampyric hween mask", + "73277": "Demonic hween mask", + "73278": "Zombie hween mask", + "73279": "Heirloom pumpkin", + "73301": "Spookling token", + "73302": "Miniature pumpkin head", "88888": "Bucket of dung", + "88889": "Polterpup", "121234": "Burnt celebratory cake", "121521": "Celebratory cake with candle", "122001": "Lit celebratory cake", diff --git a/package.json b/package.json index ed6356f2e1..4bbc69a86c 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "creatables": "tsx ./scripts/renderCreatablesFile.ts", "generate:robochimp": "prisma generate --no-hints --schema prisma/robochimp.prisma", "============BUILDING": "============", - "build": "tsx ./scripts/build.ts", + "build": "yarn build:esbuild", "gen": "concurrently --raw \"prisma generate --no-hints\" \"yarn generate:robochimp\" && echo \"Generated Prisma Client\"", "build:tsc": "tsc -p src", "build:esbuild": "concurrently --raw \"yarn build:main\" \"yarn build:workers\"", diff --git a/scripts/build.ts b/scripts/build.ts deleted file mode 100644 index 6ccf1ea3b8..0000000000 --- a/scripts/build.ts +++ /dev/null @@ -1,148 +0,0 @@ -import '../src/lib/safeglobals'; - -import { createHash } from 'node:crypto'; -import { existsSync, readFileSync, writeFileSync } from 'node:fs'; -import path from 'node:path'; -import fg from 'fast-glob'; - -import { production } from '../src/config'; -import { BOT_TYPE } from '../src/lib/constants'; -import { customItems } from '../src/lib/customItems/util.js'; -import { getSystemInfo } from '../src/lib/systemInfo'; -import { itemNameFromID } from '../src/lib/util.js'; -import { renderCreatablesFile } from './renderCreatablesFile.js'; -import { execAsync, runTimedLoggedFn } from './scriptUtil.js'; - -const args = process.argv.slice(2); - -const hasArg = (arg: string) => args.includes(arg); - -const forceRebuild = hasArg('--clean'); - -if (!existsSync('./cache.json')) { - writeFileSync('./cache.json', `${JSON.stringify({}, null, ' ')}\n`); -} -const currentCache = JSON.parse(readFileSync('./cache.json', 'utf-8')); - -function doHash(string: string | Buffer) { - return createHash('sha256').update(string).digest('hex'); -} - -function getFileHash(filePath: string) { - try { - return doHash(readFileSync(filePath)); - } catch { - return null; - } -} - -function getCacheHash(cachePath: string, key: string): string | null { - if (!existsSync(cachePath)) return null; - const cache = JSON.parse(readFileSync(cachePath, 'utf-8')); - return cache[key] || null; -} - -function setCacheValue(key: string, value: string | number) { - if (process.env.TEST) return; - const cache = JSON.parse(readFileSync(cacheFilePath, 'utf-8')); - cache[key] = value; - writeFileSync(cacheFilePath, `${JSON.stringify(cache, null, ' ')}\n`); -} - -function shouldGeneratePrismaClient( - schemaPath: string, - cachePath: string, - cacheKey: string, - clientPath: string -): boolean { - if (!existsSync(clientPath)) return true; - const currentHash = getFileHash(schemaPath); - const cachedHash = getCacheHash(cachePath, cacheKey); - if (currentHash !== cachedHash) { - setCacheValue(cacheKey, currentHash!); - return true; - } - return false; -} - -const cacheFilePath = './cache.json'; - -async function handlePrismaClientGeneration() { - const prismaSchemaPaths = [ - { schema: 'prisma/robochimp.prisma', client: 'node_modules/@prisma/client', key: 'robochimpPrismaSchemaHash' }, - { schema: 'prisma/schema.prisma', client: 'node_modules/@prisma/robochimp', key: `${BOT_TYPE}SchemaHash` } - ]; - - let shouldRunGen = false; - for (const { schema, client, key } of prismaSchemaPaths) { - if (shouldGeneratePrismaClient(schema, cacheFilePath, key, client)) { - shouldRunGen = true; - break; - } - } - - if (shouldRunGen || forceRebuild) { - await Promise.all([ - execAsync('yarn prisma generate --no-hints --schema prisma/robochimp.prisma'), - execAsync('yarn prisma db push') - ]); - } -} - -async function checkForWipingDistFolder() { - const allTypescriptFiles = await fg('**/**/*.ts', { cwd: path.join('src'), onlyFiles: true }); - allTypescriptFiles.sort(); - const hash = doHash(allTypescriptFiles.join('\n')); - if (currentCache.typescriptFilesHash !== hash || forceRebuild) { - console.log(' Removing dist folder'); - await execAsync('yarn wipedist'); - setCacheValue('typescriptFilesHash', hash); - } -} - -async function handleTypescriptCompilation() { - await execAsync('yarn build:tsc'); -} - -async function handleCommandsJSON() { - const cmdFile = `data/${BOT_TYPE.toLowerCase()}.commands.json`; - const currentFileHash = getFileHash(cmdFile); - if (currentFileHash === null || currentCache.commandsHash !== currentFileHash) { - console.log(' Updating commands json file'); - const { commandsFile } = await import('./renderCommandsFile.js'); - await commandsFile(); - setCacheValue('commandsHash', getFileHash(cmdFile)!); - } -} - -async function main() { - if (production || process.env.NODE_ENV === 'production') { - throw new Error("Don't run build script in production!"); - } - console.log((await getSystemInfo()).singleStr); - await runTimedLoggedFn('Prisma Client / Wipe Dist', () => - Promise.all([handlePrismaClientGeneration(), checkForWipingDistFolder()]) - ); - await runTimedLoggedFn('Yarn Installation', () => execAsync('yarn')); - await runTimedLoggedFn('Typescript Compilation', handleTypescriptCompilation); - await runTimedLoggedFn('Post Build', () => Promise.all([handleCommandsJSON()])); - renderCreatablesFile(); - writeFileSync( - 'data/bso_items.json', - JSON.stringify( - customItems.reduce( - (acc, id) => { - acc[id] = itemNameFromID(id)!; - return acc; - }, - {} as Record - ), - null, - 4 - ), - 'utf-8' - ); - await execAsync('yarn lint'); -} - -runTimedLoggedFn('Build', main); diff --git a/scripts/spritesheet.ts b/scripts/spritesheet.ts index 7ea4c85bf0..2ded4262e4 100644 --- a/scripts/spritesheet.ts +++ b/scripts/spritesheet.ts @@ -1,4 +1,4 @@ -import { promises as fs } from 'node:fs'; +import { promises as fs, writeFileSync } from 'node:fs'; import * as path from 'node:path'; import { Stopwatch } from '@oldschoolgg/toolkit/structures'; import Spritesmith from 'spritesmith'; @@ -7,9 +7,11 @@ import '../src/lib/safeglobals'; import { isFunction, uniqueArr } from 'e'; import { Bank, Items } from 'oldschooljs'; import { ALL_OBTAINABLE_ITEMS } from '../src/lib/allObtainableItems'; +import { customItems } from '../src/lib/customItems/util'; import { allCLItems } from '../src/lib/data/Collections'; import Buyables from '../src/lib/data/buyables/buyables'; import Createables from '../src/lib/data/createables'; +import { itemNameFromID } from '../src/lib/util'; const stopwatch = new Stopwatch(); @@ -134,6 +136,23 @@ async function main() { ).catch(err => console.error(`Failed to make BSO spritesheet: ${err.message}`)); stopwatch.check('Finished'); + + writeFileSync( + 'data/bso_items.json', + JSON.stringify( + customItems.reduce( + (acc, id) => { + acc[id] = itemNameFromID(id)!; + return acc; + }, + {} as Record + ), + null, + 4 + ), + 'utf-8' + ); + process.exit(); }