Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Simple build script for PICO-8 carts.
- * Looks for a config file in the directory it's run in and
- * tries to jam all the included lua files into the given cart.
- */
- const fs = require('fs');
- const START_MARKER = '__lua__';
- const END_MARKER = '__gfx__';
- const INCLUDE_MARKER = '#include ';
- const alreadyLoaded = [];
- let newCart = '';
- const isValidIndex = (value) => typeof value === 'number' && value >= 0;
- const loadForCart = (file) => {
- const lines = fs.readFileSync(file, 'utf-8').split('\n');
- lines.forEach(line => {
- const includeIndex = line.indexOf('#include');
- if (isValidIndex(includeIndex)) {
- // This file includes another, so go and load that first.
- const includedFile = line.slice(INCLUDE_MARKER.length, line.length).trim();
- if (!alreadyLoaded.includes(includedFile)) {
- loadForCart(includedFile);
- alreadyLoaded.push(includedFile);
- }
- }
- else {
- newCart += line + '\n';
- }
- });
- }
- /*
- * Loading the config here. The config must have three fields defined:
- * - output: The output DIRECTORY for the final cart.
- * - entryPoint: The name of the 'main' file for your cart.
- * - cartName: The name of your cart. The outputted file will be [cartName].p8.
- */
- const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
- if (!config.output || !config.entryPoint || !config.cartName) {
- console.log('ERROR :: Config must define an output directory, entry point and a cart name.');
- }
- // Get existing cart file.
- console.log(`INFO :: Reading existing cart...`);
- let existingCart = fs.readFileSync(`${config.output}/${config.cartName}.p8`, 'utf-8');
- if (!existingCart) {
- console.log('ERROR :: The cart file must have been created in PICO-8 first.');
- }
- const startOfCode = existingCart.indexOf(START_MARKER) + START_MARKER.length;
- const endOfCode = existingCart.indexOf(END_MARKER);
- // Amalgamate source files and create the new cart.
- console.log(`INFO :: Concatenating source files...`);
- newCart = existingCart.slice(0, startOfCode) + '\n';
- loadForCart(config.entryPoint);
- newCart += '\n' + existingCart.slice(endOfCode, existingCart.length);
- console.log(`INFO :: Saving new cart...`);
- fs.writeFileSync(`${config.output}/${config.cartName}.p8`, newCart, 'utf-8');
- console.log("INFO :: Done!");
Advertisement
Add Comment
Please, Sign In to add comment