Skip to content

Commit

Permalink
3.3.3
Browse files Browse the repository at this point in the history
- Changed CLI commands (serve, create)

-- Changed `addRemoteScripts` method for 'plugin' AND 'behavior' addon types

-- Changed `setRuntimeScripts` method for 'plugin' AND 'behavior' addon types
  • Loading branch information
dakln committed Dec 14, 2024
1 parent 02a6081 commit 4f32e2e
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 100 deletions.
9 changes: 7 additions & 2 deletions .addon_base/behavior/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ const BEHAVIOR_CLASS = SDK.Behaviors[config.addonId] = class LostBehavior extend
}
}
addRemoteScripts() {
_lostData.remoteScripts.forEach(url => {
this._info.AddRemoteScriptDependency(url);
_lostData.remoteScripts.forEach(script => {
if (script.type) {
this._info.AddRemoteScriptDependency(script.url, script.type);
}
else {
this._info.AddRemoteScriptDependency(script.url);
}
});
}
addUserFiles() {
Expand Down
8 changes: 6 additions & 2 deletions .addon_base/behavior/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ const BEHAVIOR_CLASS = SDK.Behaviors[config.addonId] = class LostBehavior extend
}

private addRemoteScripts() {
_lostData.remoteScripts.forEach(url => {
this._info.AddRemoteScriptDependency(url);
_lostData.remoteScripts.forEach(script => {
if (script.type) {
this._info.AddRemoteScriptDependency(script.url, script.type);
} else {
this._info.AddRemoteScriptDependency(script.url);
}
})
}

Expand Down
9 changes: 7 additions & 2 deletions .addon_base/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,13 @@ const PLUGIN_CLASS = SDK.Plugins[config.addonId] = class LostPlugin extends SDK.
}
}
addRemoteScripts() {
_lostData.remoteScripts.forEach(url => {
this._info.AddRemoteScriptDependency(url);
_lostData.remoteScripts.forEach(script => {
if (script.type) {
this._info.AddRemoteScriptDependency(script.url, script.type);
}
else {
this._info.AddRemoteScriptDependency(script.url);
}
});
}
addUserFiles() {
Expand Down
8 changes: 6 additions & 2 deletions .addon_base/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ const PLUGIN_CLASS = SDK.Plugins[config.addonId] = class LostPlugin extends SDK.
}

private addRemoteScripts() {
_lostData.remoteScripts.forEach(url => {
this._info.AddRemoteScriptDependency(url);
_lostData.remoteScripts.forEach(script => {
if (script.type) {
this._info.AddRemoteScriptDependency(script.url, script.type);
} else {
this._info.AddRemoteScriptDependency(script.url);
}
})
}

Expand Down
65 changes: 35 additions & 30 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ async function BuildAndWatch() {

async function main() {
const { _, ...flags } = parseArgs(Deno.args, {
boolean: ['drawing-plugin', 'plugin', 'behavior', 'watch', 'minify'],
alias: { dp: 'drawing-plugin', p: 'plugin', b: 'behavior', w: 'watch', m: 'minify'},
string: ['type', 'port'],
boolean: ['watch', 'minify'],
alias: { w: 'watch', m: 'minify' },
"--": true,
});

Expand All @@ -63,29 +64,27 @@ async function main() {
case 'version':
Logger.LogBetweenLines(dedent`
👾 ${Colors.bold(`Lost ➜ ${Colors.yellow(DenoJson.version)} by ${Colors.italic(Colors.magenta('lostinmind.'))}`)}
🌐 ${Colors.bold('https://github.com/lostinmind-dev/lost-c3')}
🌐 ${Colors.bold(`[GitHub] ${Paths.Links.GitHub}`)}
🌐 ${Colors.bold(`[JSR] ${Paths.Links.JSR}`)}
`)
break;
case 'create':
if (
!flags.plugin &&
!flags.behavior &&
!flags["drawing-plugin"]
) {
Logger.Log('🎓', Colors.blue(Colors.italic('Specify one of the available types of addon:')))
printCreate();
break;
} else {
if (flags.plugin) {
await createBareBones('plugin');
}
if (flags.type) {
const type = flags.type as AddonBareBonesType;

if (flags.behavior) {
await createBareBones('behavior');
}
if (flags["drawing-plugin"]) {
await createBareBones('drawing-plugin');
if (
type === 'plugin' ||
type === 'drawing-plugin' ||
type === 'behavior'
) {
await createBareBones(type);
} else {
Logger.Log('🎓', Colors.blue(Colors.italic('Specify one of the available types of addon:')))
printCreate();
}
} else {
Logger.Log('🎓', Colors.blue(Colors.italic('Specify one of the available types of addon:')))
printCreate();
}
break;
case 'build':
Expand All @@ -101,15 +100,19 @@ async function main() {
}
break;
case 'serve':
Serve(65432);
if (flags.port) {
const port = Number(flags['port']);
Serve(port);
} else {
Serve(65432);
}
break;
case 'types':
await installTypes();
break;
default:
Logger.Log(`❌ ${Colors.red(Colors.bold(`Unknown command:`)), Colors.italic(String(command))}`);
Logger.Log(Colors.blue(Colors.italic('Enter')), Colors.italic(Colors.bold(`${Colors.yellow('lost')} help`)), Colors.italic(Colors.blue('to get of available commands.')))
Deno.exit(1);
printHelp();
break;
}

}
Expand All @@ -128,7 +131,7 @@ async function installTypes() {
}

const fileContent = await response.text();
await Deno.writeTextFile(join(Paths.Root, 'Addon', 'Types', 'construct.d.ts'), fileContent);
await Deno.writeTextFile(join(Paths.ProjectFolders.Types, 'construct.d.ts'), fileContent);
Logger.Success(Colors.bold(`${Colors.green('Successfully')} installed construct types!`));
} catch (e) {
Logger.Error('cli', 'Error while installing construct types file', `Error: ${e}`);
Expand Down Expand Up @@ -167,6 +170,7 @@ async function cloneRepo(url: string) {
await installTypes();
} else {
Logger.Error('cli', 'Error occured while creating bare-bones.', `Error code: ${code}`);
Deno.exit(1);
}
}

Expand All @@ -182,15 +186,16 @@ function printHelp() {

Logger.Log(` ${Colors.yellow('build')}`);
Logger.Log(' ⚙️', Colors.gray(' --watch, -w'), Colors.italic(' Runs build command with auto-reload.'));
Logger.Log(' ⚙️', Colors.gray(' --minify, -m'), Colors.italic(' Runs build command with minification.'));
Logger.Log(' ⚙️', Colors.gray(' --minify, -m'), Colors.italic(' Runs build command with script minification.'));
Logger.Log(` ${Colors.yellow('serve')}`);
Logger.Log(` ${Colors.yellow('types')}`, Colors.italic(' Creates "properties.d.ts" of your plugin properties.'));
Logger.Log(' ⚙️', Colors.gray(' --port "{port}"'));
Logger.Log(` ${Colors.yellow('types')}`, Colors.italic(' Creates "construct.d.ts" for all Construct 3 declarations.'));
}

function printCreate() {
Logger.Log(' ⚙️', Colors.gray(' --plugin, -p'), Colors.italic(' Creates a bare-bones for "Plugin" addon type.'));
Logger.Log(' ⚙️', Colors.gray(' --drawing-plugin, -dp'), Colors.italic(' Creates a bare-bones for "Drawing Plugin" addon type.'));
Logger.Log(' ⚙️', Colors.gray(' --behavior, -b'), Colors.italic(' Creates a bare-bones for "Behavior" addon type.'));
Logger.Log(' ⚙️', Colors.gray(' --type "plugin"'), Colors.italic(' Creates a bare-bones for "Plugin" addon type.'));
Logger.Log(' ⚙️', Colors.gray(' --type "drawing-plugin"'), Colors.italic(' Creates a bare-bones for "Drawing Plugin" addon type.'));
Logger.Log(' ⚙️', Colors.gray(' --type "behavior"'), Colors.italic(' Creates a bare-bones for "Behavior" addon type.'));
// Logger.Log(' ⚙️', Colors.gray(' --theme, -t'), Colors.italic(' Creates a bare-bones for "theme" addon type.'));
// Logger.Log(' ⚙️', Colors.gray(' --effect, -e'), Colors.italic(' Creates a bare-bones for "effect" addon type.'));
}
6 changes: 6 additions & 0 deletions cli/serve-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function serveAddon(port: number) {
}

const handler = async (req: Request): Promise<Response> => {

try {
const url = new URL(req.url);
// let filePath = url.pathname;
Expand All @@ -33,12 +34,17 @@ export default function serveAddon(port: number) {
filePath = `${filePath}/index.html`;
}
} catch {
Logger.Error('serve', 'File not found')
return new Response("File not found", { status: 404 });
}

const file = await Deno.readFile(filePath);
const contentType = getContentType(filePath) || 'application/octet-stream';

// Logger.Info(`Sent file from path: "${Colors.yellow(url.pathname)}"`);
Logger.LogBetweenLines(
`📃 Sent file from path: "${Colors.yellow(url.pathname)}"`
)
return new Response(file, {
status: 200,
headers: {
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lost-c3/lib",
"version": "3.3.2",
"version": "3.3.3",
"exports": {
".": "./mod.ts",
"./cli": "./cli.ts",
Expand Down
85 changes: 54 additions & 31 deletions lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LostAddonData } from "./lost-addon-data.ts";
import { ProjectFolders } from "../shared/paths/project-folders.ts";
import { EditorScript, JsonFile, RuntimeScript } from "../shared/paths/addon-files.ts";
import { AddonFolders } from "../shared/paths/addon-folders.ts";
import type { RemoteScriptType, RuntimeScriptType } from "../shared/types.ts";

export abstract class Addon<A extends AddonType, I, T extends SDK.ITypeBase> {
protected readonly type: AddonType;
Expand All @@ -26,8 +27,8 @@ export abstract class Addon<A extends AddonType, I, T extends SDK.ITypeBase> {
protected readonly userDomSideScripts: AddonUserDomSideScriptFile[] = [];
protected readonly pluginProperties: PluginProperty<A, I, T>[] = [];
protected readonly categories: CategoryClassType[] = [];
protected readonly remoteScripts: string[] = [];
protected readonly runtimeScripts: string[] = [];
protected readonly remoteScripts: RemoteScriptType[] = [];
protected readonly runtimeScripts: RuntimeScriptType[] = [];

protected hasDefaultImage: boolean = false;

Expand Down Expand Up @@ -156,7 +157,9 @@ export abstract class Addon<A extends AddonType, I, T extends SDK.ITypeBase> {
`Addon icon was not detected, will be used default [SVG] icon`
);
} else {
Logger.Info(`Loaded [${this.icon.iconType}] addon icon`, `Filename: ${this.icon.originalName}`);
Logger.Info(
`Loaded [${this.icon.iconType}] addon icon with file name: ${this.icon.originalName}`
);
}
}

Expand Down Expand Up @@ -228,29 +231,16 @@ export abstract class Addon<A extends AddonType, I, T extends SDK.ITypeBase> {
entry.isFile &&
((entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) || entry.name.endsWith('.js'))
) {
if (this.#isFilePathInRuntimeScriptsArray('')) {
this.#addUserScript({
type: 'script',
originalName: entry.name,
originalPath: path,
localName: entry.name,
localPath: join(Paths.AddonFolders.Scripts, ...Paths.getFoldersAfterFolder(path, ProjectFolders.Scripts)),
dependencyType: 'external-runtime-script',
isTypescript: (entry.name.endsWith('.ts')) ? true : false,
finalPath: `${AddonFolders.Scripts}`
})
} else {
this.#addUserScript({
type: 'script',
originalName: entry.name,
originalPath: path,
localName: entry.name,
localPath: join(Paths.AddonFolders.Scripts, ...Paths.getFoldersAfterFolder(path, ProjectFolders.Scripts)),
dependencyType: 'external-dom-script',
isTypescript: (entry.name.endsWith('.ts')) ? true : false,
finalPath: `${AddonFolders.Scripts}`
})
}
this.#addUserScript({
type: 'script',
originalName: entry.name,
originalPath: path,
localName: entry.name.replace('.ts', '.js'),
localPath: join(Paths.AddonFolders.Scripts, ...Paths.getFoldersAfterFolder(path, ProjectFolders.Scripts)),
dependencyType: await this.#getScriptDependencyType(path, entry.name),
isTypescript: (entry.name.endsWith('.ts')) ? true : false,
finalPath: `${AddonFolders.Scripts}`
})
}
}
}
Expand Down Expand Up @@ -412,12 +402,45 @@ export abstract class Addon<A extends AddonType, I, T extends SDK.ITypeBase> {
}
}

#isFilePathInRuntimeScriptsArray(path: string) {
if (this.runtimeScripts.includes(path)) {
return true;
} else {
return false;
async #getScriptDependencyType(filePath: string, fileName: string): Promise<AddonScriptDependencyType> {
let dependencyType: AddonScriptDependencyType = 'external-dom-script';

for (const script of this.runtimeScripts) {
switch (script.type) {
case "file":
let convertedPath: string = `${fileName}`;

const folders = Paths.getFoldersAfterFolder(filePath, ProjectFolders.Scripts);

if (folders.length > 0) {
convertedPath = `${folders.join('/')}/${fileName}`
}

if (script.path === convertedPath) {
dependencyType = 'external-runtime-script';
}
break;
case "directory":
const readDir = async (path: string) => {
for await (const entry of Deno.readDir(path)) {
if (entry.isDirectory) {
await readDir(join(path, entry.name));
} else if (
entry.isFile &&
(!entry.name.endsWith('.d.ts') && entry.name.endsWith('.ts')) || entry.name.endsWith('.js')
) {
// console.log(entry.isDirectory)
dependencyType = 'external-runtime-script';
}
}
}

await readDir(join(Paths.ProjectFolders.Scripts, script.path))
break;
}
}

return dependencyType;
}

/**
Expand Down
Loading

0 comments on commit 4f32e2e

Please sign in to comment.