Initial Commit

This commit is contained in:
Donovan Daniels 2024-05-30 05:04:54 -05:00
commit a8786c3e64
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA
8 changed files with 3767 additions and 0 deletions

8
.eslintrc.json Normal file
View File

@ -0,0 +1,8 @@
{
"extends": ["@uwu-codes/eslint-config/esm"],
"rules": {
"unicorn/prevent-abbreviations": "off",
"unicorn/no-process-exit": "off",
"unicorn/import-style": "off"
}
}

175
.gitignore vendored Normal file
View File

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

13
README.md Normal file
View File

@ -0,0 +1,13 @@
## Pack Formatter
To use this, you must have the [Bun runtime](https://bun.sh) installed. Simply run `bun install`, `bun compile`, and you'll find the `build-packs` binary in the `dist` folder.
## Usage
```sh
build-packs
```
### Arguments
* `-i` or `--indir` - The input directory. Defaults to the current working directory.
* `-o` or `--outdir` - The directory to output the compiled packs to. Defaults to dist.
* `-c` or `--common` - The common directory containing shared files. Defailts to common.
* `-r` or `--revision` - The revision/version. Defaults to `git rev-parse --short HEAD` if .git exists, else required.

BIN
bun.lockb Executable file

Binary file not shown.

145
index.ts Normal file
View File

@ -0,0 +1,145 @@
import AdmZip from "adm-zip";
import prettyBytes from "pretty-bytes";
import { parseArgs } from "node:util";
import {
access,
copyFile,
cp,
lstat,
mkdir,
mkdtemp,
rm,
stat,
writeFile
} from "node:fs/promises";
import type { PathLike } from "node:fs";
import { tmpdir } from "node:os";
import { cwd } from "node:process";
import { resolve } from "node:path";
interface Info {
description: string;
exports: Array<["all"] | Array<string>>;
name: string;
url: string;
versions: Array<[mcVersion: string, packFormat: number]>;
}
const { values: { indir: inDir, common: commonDir, revision: rev, outdir: outDir } } = parseArgs({
args: Bun.argv,
options: {
indir: {
type: "string",
short: "i",
default: cwd()
},
common: {
type: "string",
short: "c",
default: "common"
},
revision: {
type: "string",
short: "r"
},
outdir: {
type: "string",
short: "o",
default: "dist"
}
},
strict: true,
allowPositionals: true
});
const directoryExists = (path: PathLike) => access(path).then(() => lstat(path).then(d => d.isDirectory())).catch(() => false);
const dir = resolve(inDir!);
console.log("Building packs for:", dir);
if (!dir) {
console.log("No directory specified");
process.exit(1);
}
if (!await directoryExists(dir)) {
console.log(`Specified input directory "${inDir}" (${dir}) does not exist.`);
process.exit(1);
}
const info = await Bun.file(`${dir}/info.json`).json() as Info;
const common = resolve(dir, commonDir!);
const dist = resolve(dir, outDir!);
if (!await directoryExists(common)) {
console.log(`Specified common directory "${commonDir}" (${common}) does not exist.`);
process.exit(1);
}
let revision = rev;
if (!revision) {
if (await directoryExists(`${dir}/.git`)) {
revision = (await Bun.$`git rev-parse --short HEAD`.cwd(dir).text().catch(() => "")).slice(0, -1);
if (!revision) {
console.log("Failed to get git revision.");
process.exit(1);
}
} else {
console.log("No revision specified and no git directory found.");
process.exit(1);
}
}
const tempDir = await mkdtemp(`${tmpdir()}/${info.name.toLowerCase().replaceAll(" ", "-")}`);
console.log("Temporary Directory:", tempDir);
let totalSize = 0;
async function createVersion(mcVersion: string, packFormat: number) {
console.log("Building %s (pf: %d)", mcVersion, packFormat);
const mcmeta = {
pack: {
pack_format: packFormat,
description: info.description,
version: {
git: revision,
mc: mcVersion,
url: info.url
}
}
};
await mkdir(`${tempDir}/${mcVersion}`);
await writeFile(`${tempDir}/${mcVersion}/pack.mcmeta`, JSON.stringify(mcmeta, null, 4));
await copyFile(`${common}/pack.png`, `${tempDir}/${mcVersion}/pack.png`);
await cp(`${common}/assets`, `${tempDir}/${mcVersion}/assets`, { recursive: true });
const zip = new AdmZip();
await zip.addLocalFolderPromise(`${tempDir}/${mcVersion}`, {});
await zip.writeZipPromise(`${dist}/${info.name.replaceAll(" ", "")}-${mcVersion}.zip`);
const { size } = await stat(`${dist}/${info.name.replaceAll(" ", "")}-${mcVersion}.zip`);
totalSize += size;
console.log("%s built, size: %s", mcVersion, prettyBytes(size));
}
if (await directoryExists(dist)) {
await rm(dist, { recursive: true, force: true });
}
await mkdir(dist);
for (const [mcVersion, packFormat] of info.versions) {
await createVersion(mcVersion, packFormat);
}
await rm(tempDir, { recursive: true, force: true });
console.log("Total size: %s", prettyBytes(totalSize));
for (const ex of info.exports) {
const name = (ex[0] === "all" ? info.name : `${info.name}-${ex.join("-")}`).replaceAll(" ", "");
console.log("Exporting %s", name);
const zip = new AdmZip();
const versions = ex[0] === "all" ? info.versions.map(([mcVersion]) => mcVersion) : ex;
for (const version of versions) {
zip.addLocalFile(`${dist}/${info.name.replaceAll(" ", "")}-${version}.zip`);
}
await zip.writeZipPromise(`${dist}/${name}.zip`);
const { size } = await stat(`${dist}/${name}.zip`);
console.log("%s exported, size: %s", name, prettyBytes(size));
}
console.log("Done");

3383
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "pack-formatter",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/adm-zip": "^0.5.5",
"@types/bun": "latest",
"@types/node": "^20.12.13",
"@uwu-codes/eslint-config": "^1.1.28"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"adm-zip": "^0.5.12",
"pretty-bytes": "^6.1.1"
},
"scripts": {
"compile": "bun build ./index.ts --compile --outfile dist/build-packs"
}
}

22
tsconfig.json Normal file
View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
/* Linting */
"skipLibCheck": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}