diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..1da3ac8 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,18 @@ +# .github/workflows/publish.yml + +name: Publish + +on: + push: + tags: + - '*' + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write # The OIDC ID token is used for authentication with JSR. + steps: + - uses: actions/checkout@v4 + - run: npx jsr publish diff --git a/LICENSE b/LICENSE index 1beb820..890ddfb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ MIT License +Copyright (c) 2024 Benjamin Jesuiter (for changes made to the original code, see github commit history) Copyright (c) 2021 Octo Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/README.md b/README.md index 224e6c0..afa1add 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,29 @@ -# asset_builder -Asset files (eg, text, image) bundle tool for Deno. +# Deno asset-builder for deno compile -# Install +Bundles asset files (eg, text, image) for deno compile into base64 encoded strings in a typescript file. +This TS file can be used with `deno compile` to load these assets at runtime. -```sh -deno install --allow-read https://deno.land/x/asset_builder/asset_builder.ts +# Usage - directly run from jsr +```sh # Import config file from default ./assets_config.json. -asset_builder >> asset.ts +deno run --allow-read jsr:@codemonument/asset-builder >> asset.ts # Set Import config file. -asset_builder --import-file my_assets_config.json >> asset.ts +deno run --allow-read jsr:@codemonument/asset-builder --import-file my_assets_config.json >> asset.ts ``` -# Usage +# Usage - install as a global binary ```sh -# Import config file from default ./assets_config.json. -deno run --allow-read https://deno.land/x/asset_builder/asset_builder.ts >> asset.ts +# change the name for this binary by passing a different -n argument +deno install --global --allow-read -n asset-builder jsr:@codemonument/asset-builder -# Set Import config file. -deno run --allow-read https://deno.land/x/asset_builder/asset_builder.ts --import-file my_assets_config.json >> asset.ts +# Use default config (./assets_config.json) +asset-builder >> asset.ts + +# Use a custom config file +asset-builder --import-file my_assets_config.json >> asset.ts ``` # Configuration @@ -30,12 +33,12 @@ Write as follows. ```json { - "files":[ - { - "importPath": "./sample.txt", - "calledName": "sample-text" - } - ] + "files": [ + { + "importPath": "./sample.txt", + "calledName": "sample-text" + } + ] } ``` @@ -43,13 +46,16 @@ Write as follows. The file created by asset_builder is used as follows. -```ts -import asset from './asset.ts' +```ts +import asset from './asset.ts'; for (const [key, value] of Object.entries(asset.files)) { - console.log(`key: ${key}, extension: ${value.extension}, content: ${new TextDecoder().decode(value.content) }`); + console.log( + `key: ${key}, extension: ${value.extension}, content: ${new TextDecoder().decode( + value.content + )}` + ); } // key: test-text, extension: txt, content: Hello World!! // key: test-text2, extension: txt, content: Hello World!! ``` - diff --git a/asset_builder.ts b/asset_builder.ts index b6de835..d6594b4 100644 --- a/asset_builder.ts +++ b/asset_builder.ts @@ -1,47 +1,49 @@ -import { parse, encode, ImportTargetFile, ImportedFile, exportBundledObject } from "./deps.ts" +import {parseArgs} from '@std/cli/parse-args'; +import {encodeBase64} from 'jsr:@std/encoding@~1.0.8'; +import {exportBundledObject} from './src/export_text.ts'; +import type {ImportedFile, ImportTargetFile} from './src/types.ts'; -const parsedArgs = parse(Deno.args); +const parsedArgs = parseArgs(Deno.args); -const importFileName = - typeof parsedArgs["import-file"] === "string" - ? parsedArgs["import-file"] - : "assets_config.json"; +const assetsConfigFileName = + typeof parsedArgs['import-file'] === 'string' ? parsedArgs['import-file'] : 'assets_config.json'; -let bundleList = ""; +let assetsConfigString = ''; try { - const readFile = Deno.readTextFileSync(importFileName); - bundleList = readFile; + assetsConfigString = Deno.readTextFileSync(assetsConfigFileName); } catch (error) { - if (error.name === "NotFound") { - console.error( - `Import Config file [${importFileName}] is not Found!!\nplease confirm.` - ); - Deno.exit(); - } - throw error; + if (error instanceof Deno.errors.NotFound) { + console.error( + `Assets Config file [${assetsConfigFileName}] is not Found!!\nPlease confirm path.` + ); + Deno.exit(); + } + throw error; } -const bundleListArr: [ImportTargetFile] = JSON.parse(bundleList).files; - -let bundledObject: { [key: string]: ImportedFile } = {}; - -bundleListArr.forEach((file) => { - try { - const content = encode(Deno.readFileSync(file.importPath)); - const extension = file.importPath.split(".").slice(-1)[0]; - bundledObject[`${file.calledName}`] = { content, extension }; - } catch (error) { - if (error.name === "NotFound") { - console.error( - `Import file [${file.importPath}] is not Found!!\nplease confirm.` - ); - Deno.exit(); - } - throw error; - } +// The list of asset files to be bundled +const assetFilesList: [ImportTargetFile] = JSON.parse(assetsConfigString).files; + +// The output object of bundled files +const bundledObject: {[key: string]: ImportedFile} = {}; + +assetFilesList.forEach(file => { + try { + const content = encodeBase64(Deno.readFileSync(file.importPath)); + const extension = file.importPath.split('.').slice(-1)[0]; + bundledObject[`${file.calledName}`] = {content, extension}; + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + console.error( + `Asset file [${file.importPath}] is not Found - Cannot bundle!!\nPlease confirm.` + ); + Deno.exit(); + } + throw error; + } }); -const exportText = exportBundledObject(bundledObject) +const exportText = exportBundledObject(bundledObject); console.log(exportText); diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..2ac1df1 --- /dev/null +++ b/deno.json @@ -0,0 +1,23 @@ +{ + "name": "@codemonument/asset-builder", + "description": "Compiles assets into a assets.ts file for deno.compile - forked from https://deno.land/x/asset_builder", + "version": "1.0.5", + "exports": { + ".": "./asset_builder.ts" + }, + "exclude": [ + "./example", + "./asset_config.json.sample" + ], + "tasks": { + "example": "deno run --allow-read ./asset_builder.ts --import-file ./example/asset_config.json > ./example/asset.ts", + "verify-example": "deno run --allow-read ./example/verify.ts", + "dry": "deno publish --dry-run", + "deploy": "deno publish", + "example-jsr": "deno run --allow-read jsr:@codemonument/asset-builder --import-file example/asset_config.json > example/asset.ts" + }, + "imports": { + "@std/cli": "jsr:@std/cli@^1.0.15", + "@std/encoding": "jsr:@std/encoding@^1.0.8" + } +} \ No newline at end of file diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..b8c33f2 --- /dev/null +++ b/deno.lock @@ -0,0 +1,27 @@ +{ + "version": "4", + "specifiers": { + "jsr:@std/cli@^1.0.15": "1.0.15", + "jsr:@std/encoding@^1.0.8": "1.0.8", + "jsr:@std/encoding@~1.0.8": "1.0.8" + }, + "jsr": { + "@std/cli@1.0.15": { + "integrity": "e79ba3272ec710ca44d8342a7688e6288b0b88802703f3264184b52893d5e93f" + }, + "@std/encoding@1.0.8": { + "integrity": "a6c8f3f933ab1bed66244f435d1dc0fd23a888e07195532122ddc3d5f8f0e6b4" + } + }, + "remote": { + "https://deno.land/std@0.66.0/_util/assert.ts": "e1f76e77c5ccb5a8e0dbbbe6cce3a56d2556c8cb5a9a8802fc9565af72462149", + "https://deno.land/std@0.66.0/flags/mod.ts": "11f51e7fec72bfe01d531acb4e0b0ae54115439367377b088b3c6e7e7142918f", + "https://deno.land/std@0.97.0/encoding/base64.ts": "eecae390f1f1d1cae6f6c6d732ede5276bf4b9cd29b1d281678c054dc5cc009e" + }, + "workspace": { + "dependencies": [ + "jsr:@std/cli@^1.0.15", + "jsr:@std/encoding@^1.0.8" + ] + } +} diff --git a/deps.ts b/deps.ts deleted file mode 100644 index dfd1128..0000000 --- a/deps.ts +++ /dev/null @@ -1,4 +0,0 @@ -export { parse } from "https://deno.land/std@0.66.0/flags/mod.ts"; -export { encode } from "https://deno.land/std@0.97.0/encoding/base64.ts"; -export type { ImportTargetFile, ImportedFile } from "./src/type.d.ts" -export { exportBundledObject } from "./src/export_text.ts" diff --git a/example/asset.ts b/example/asset.ts index 84a2fa5..eb33885 100644 --- a/example/asset.ts +++ b/example/asset.ts @@ -1,16 +1,17 @@ -import { decode } from "https://deno.land/std@0.97.0/encoding/base64.ts"; -const bundledObject = { - files:{ - "test-text":{ - content:decode("SGVsbG8gV29ybGQhIQ=="), - extension: "txt" - }, - "test-text2":{ - content:decode("SGVsbG8gV29ybGQhIQ=="), - extension: "txt" + import { decodeBase64 } from "jsr:@std/encoding@~1.0.8"; + + const bundledObject = { + files:{ + "test-text":{ + content:decodeBase64("SGVsbG8gV29ybGQhIQ=="), + extension: "txt" + }, + "test-text2":{ + content:decodeBase64("SGVsbG8gV29ybGQhIQ=="), + extension: "txt" } - } -} + } + } + export default bundledObject; -export default bundledObject diff --git a/example/example.ts b/example/verify.ts similarity index 100% rename from example/example.ts rename to example/verify.ts diff --git a/src/export_text.ts b/src/export_text.ts index c587db9..5bb0204 100644 --- a/src/export_text.ts +++ b/src/export_text.ts @@ -1,32 +1,30 @@ -import { ImportedFile } from "./type.d.ts"; +import type {ImportedFile} from './types.ts'; -const exportText = (exportText: string) => - `import { decode } from "https://deno.land/std@0.97.0/encoding/base64.ts"; +function exportTextFn(exportText: string): string { + return ` + import { decodeBase64 } from "jsr:@std/encoding@~1.0.8"; + + const bundledObject = { + files:{ + ${exportText} + } + } + export default bundledObject; +`; +} -const bundledObject = { - files:{ - ${exportText} - } -} +function exportObjectText(bundledObject: {[key: string]: ImportedFile}): string { + return Object.keys(bundledObject) + .map( + (key, index) => + `${index > 0 ? ' ' : ''} "${key}":{\r\n content:decodeBase64("${ + bundledObject[key].content + }"),\r\n extension: "${bundledObject[key].extension}"\r\n }` + ) + .join(',\r\n'); +} -export default bundledObject`; - -const exportObjectText = ( - bundledObject: { [key: string]: ImportedFile }, -) => { - return Object.keys(bundledObject) - .map( - (key, index) => - `${index>0 ? ' ':''} "${key}":{\r\n content:decode("${ - bundledObject[key].content - }"),\r\n extension: "${bundledObject[key].extension}"\r\n }`, - ) - .join(",\r\n"); -}; - -export const exportBundledObject = ( - bundledObject: { [key: string]: ImportedFile }, -) => { - const tmp = exportObjectText(bundledObject); - return exportText(tmp); +export const exportBundledObject = (bundledObject: {[key: string]: ImportedFile}) => { + const tmp = exportObjectText(bundledObject); + return exportTextFn(tmp); }; diff --git a/src/type.d.ts b/src/types.ts similarity index 100% rename from src/type.d.ts rename to src/types.ts