Skip to content

Commit

Permalink
Add TypeScript boilerplate (#78)
Browse files Browse the repository at this point in the history
* Add support for TypeScript boilerplate generation

* lint
  • Loading branch information
marklundin authored Sep 26, 2024
1 parent d8c3452 commit 5bab6f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 42 additions & 3 deletions src/assets/createScript.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };

0 comments on commit 5bab6f2

Please sign in to comment.