X Tutup
Commit 8cecdabd authored by Cosmo Myzrail Gorynych's avatar Cosmo Myzrail Gorynych 😼
Browse files

🐛 Fix inability to compile Script assets with Catnip code

parent 4ea9b0e2
Loading
Loading
Loading
Loading
+32 −12
Original line number Diff line number Diff line
import {ExporterError, highlightProblem} from './ExporterError';
import {coffeeScriptOptions} from './scriptableProcessor';
import {compile as compileCatnip} from '../catnip/compiler';

const compileCoffee = require('coffeescript').CoffeeScript.compile;
const typeScript = require('sucrase').transform;
@@ -8,14 +9,32 @@ export const stringifyScripts = (scripts: IScript[]): string =>
    scripts.reduce((acc, script) => {
        let code;
        try { // Apply converters to the user's code first
            code = script.language === 'coffeescript' ?
                compileCoffee(script.code, coffeeScriptOptions) :
                typeScript(script.code, {
            switch (script.language) {
            case 'typescript':
                ({code} = typeScript(script.code, {
                    transforms: ['typescript']
                }).code;
                }));
                break;
            case 'coffeescript':
                code = compileCoffee(script.code, coffeeScriptOptions);
                break;
            case 'catnip':
                code = compileCatnip(script.code as BlockScript, {
                    resourceId: script.uid,
                    resourceName: script.name,
                    resourceType: script.type,
                    eventKey: 'onRun'
                });
                break;
            default: throw new Error(`Unsupported script language: ${script.language}`);
            }
            return acc + `'${script.name}': function (options) {${code}},`;
        } catch (e) {
            const errorMessage = `${e.name || 'An error'} occured while compiling script ${script.name}`;
            if (e instanceof ExporterError) {
                // Passthrough already formatted errors, mainly coming from Catnip
                throw e;
            } else {
                const exporterError = new ExporterError(errorMessage, {
                    resourceId: script.uid,
                    resourceName: script.name,
@@ -25,6 +44,7 @@ export const stringifyScripts = (scripts: IScript[]): string =>
                }, e);
                throw exporterError;
            }
        }
    }, '');

export const getStartupScripts = (scripts: IScript[]): string => {
X Tutup