LewisClark

PICO-8 Node Build Script

Sep 25th, 2023
1,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.32 KB | Software | 0 0
  1. /*
  2.  * Simple build script for PICO-8 carts.
  3.  * Looks for a config file in the directory it's run in and
  4.  * tries to jam all the included lua files into the given cart.
  5. */
  6. const fs = require('fs');
  7.  
  8. const START_MARKER = '__lua__';
  9. const END_MARKER = '__gfx__';
  10. const INCLUDE_MARKER = '#include ';
  11.  
  12. const alreadyLoaded = [];
  13. let newCart = '';
  14. const isValidIndex = (value) => typeof value === 'number' && value >= 0;
  15. const loadForCart = (file) => {
  16.    const lines = fs.readFileSync(file, 'utf-8').split('\n');
  17.    lines.forEach(line => {
  18.       const includeIndex = line.indexOf('#include');
  19.       if (isValidIndex(includeIndex)) {
  20.          // This file includes another, so go and load that first.
  21.          const includedFile = line.slice(INCLUDE_MARKER.length, line.length).trim();
  22.          if (!alreadyLoaded.includes(includedFile)) {
  23.             loadForCart(includedFile);
  24.             alreadyLoaded.push(includedFile);
  25.          }
  26.       }
  27.       else {
  28.          newCart += line + '\n';
  29.       }
  30.    });
  31. }
  32.  
  33. /*
  34. * Loading the config here. The config must have three fields defined:
  35. * - output: The output DIRECTORY for the final cart.
  36. * - entryPoint: The name of the 'main' file for your cart.
  37. * - cartName: The name of your cart. The outputted file will be [cartName].p8.
  38. */
  39. const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
  40. if (!config.output || !config.entryPoint || !config.cartName) {
  41.    console.log('ERROR :: Config must define an output directory, entry point and a cart name.');
  42. }
  43.  
  44. // Get existing cart file.
  45. console.log(`INFO :: Reading existing cart...`);
  46. let existingCart = fs.readFileSync(`${config.output}/${config.cartName}.p8`, 'utf-8');
  47. if (!existingCart) {
  48.    console.log('ERROR :: The cart file must have been created in PICO-8 first.');
  49. }
  50. const startOfCode = existingCart.indexOf(START_MARKER) + START_MARKER.length;
  51. const endOfCode = existingCart.indexOf(END_MARKER);
  52.  
  53. // Amalgamate source files and create the new cart.
  54. console.log(`INFO :: Concatenating source files...`);
  55. newCart = existingCart.slice(0, startOfCode) + '\n';
  56. loadForCart(config.entryPoint);
  57. newCart += '\n' + existingCart.slice(endOfCode, existingCart.length);
  58.  
  59. console.log(`INFO :: Saving new cart...`);
  60. fs.writeFileSync(`${config.output}/${config.cartName}.p8`, newCart, 'utf-8');
  61. console.log("INFO :: Done!");
  62.  
Tags: pico-8
Advertisement
Add Comment
Please, Sign In to add comment