diff --git a/package-lock.json b/package-lock.json index 6f60f8a..94960d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@playcanvas/editor-api", - "version": "1.0.38", + "version": "1.0.40", "license": "MIT", "devDependencies": { "@babel/core": "^7.23.7", diff --git a/src/assets/createScript.js b/src/assets/createScript.js index d92990b..71dedfa 100644 --- a/src/assets/createScript.js +++ b/src/assets/createScript.js @@ -1,4 +1,10 @@ const VALID_FILENAME = /^[^0-9.#<>$+%!`&='{}@\\/:*?"|\n][^#<>$+%!`&='{}@\\/:*?"|\n]*$/; +const extensionToBoilerplateMap = new Map([ + ['.js', createBoilerplate], + ['.mjs', createEsmBoilerplate], + ['.ts', createTsBoilerplate] +]); +const validExtensions = Array.from(extensionToBoilerplateMap.keys()); /** * Creates filename and script content from provided arguments. If the provide filename contains a '.mjs' @@ -49,11 +55,17 @@ function createScript(filename, text) { filename = `${scriptName}.js`; } - if (!/.js$/i.test(filename)) { + const hasValidExtension = validExtensions.some(ext => filename.endsWith(ext)); + if (!hasValidExtension) { filename += '.js'; } - const isEsm = filename.endsWith('.mjs'); - const boilerPlateGenerator = isEsm ? createEsmBoilerplate : createBoilerplate; + + // Extract extension from filename + const extension = filename.slice(filename.lastIndexOf('.')); + + // Get the correct boilerplate generator based on the file extension + const boilerPlateGenerator = extensionToBoilerplateMap.get(extension); + const content = text || boilerPlateGenerator(className, scriptName); return { @@ -112,4 +124,31 @@ export class ${className} extends Script { `.trim(); } +function createTsBoilerplate(className, scriptName) { + return ` +import { Script } from 'playcanvas'; + +/** + * The {@link https://api.playcanvas.com/classes/Engine.Script.html | Script} class is + * the base class for all PlayCanvas scripts. Learn more about writing scripts in the + * {@link https://developer.playcanvas.com/user-manual/scripting/ | scripting guide}. + */ +export class ${className} extends Script { + /** + * Called when the script is about to run for the first time. + */ + initialize(): void { + } + + /** + * Called for enabled (running state) scripts on each tick. + * + * @param {number} dt - The delta time in seconds since the last frame. + */ + update(dt: number): void { + } +} +`.trim(); +} + export { createScript };