Skip to content

Instantly share code, notes, and snippets.

@peterpeterparker
Last active November 2, 2023 10:37
Show Gist options
  • Save peterpeterparker/e4bbf657e17edf1b57bf39b07651b5a9 to your computer and use it in GitHub Desktop.
Save peterpeterparker/e4bbf657e17edf1b57bf39b07651b5a9 to your computer and use it in GitHub Desktop.
Bundle static files in Motoko canisters
#!/usr/bin/env node
import {writeFile} from 'fs/promises';
import {Blob} from 'node:buffer';
import fetch from 'node-fetch';
const fetchData = async (url) => {
const response = await fetch(url);
if (!response || !response.ok) {
throw new Error(`Data ${url} cannot be fetched.`);
}
return response.text();
};
const template = ({variable, content}) => `module {
public let ${variable}: [Nat8] = [${content}];
}`;
const generate = async ({url, dest, variable}) => {
const data = await fetchData(url);
const blob = new Blob([data], {
type: 'text/plain'
});
await writeFile(dest, template({variable, content: [...new Uint8Array(await blob.arrayBuffer())]}));
};
(async () => {
try {
const assets = [
{
url: 'https://raw.githubusercontent.com/deckgo/ic-kit/main/dist/robots.txt',
dest: './canisters/src/storage/assets/storage.robots.txt.mo',
variable: 'robots_txt'
},
{
url: 'https://raw.githubusercontent.com/deckgo/ic-kit/main/dist/build/index.css',
dest: './canisters/src/storage/assets/storage.index.css.mo',
variable: 'index_css'
}
];
await Promise.all(assets.map((asset) => generate(asset)));
} catch (err) {
console.error(err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment