From 49b85011d43f04bda10d32a72c24e14dc0ec8881 Mon Sep 17 00:00:00 2001 From: sadaf Date: Wed, 6 Mar 2024 22:28:13 +0530 Subject: [PATCH] feat: separated types from executable code and modifying the build.mjs file accordingly --- build.mjs | 26 +++++- src/api/app.ts | 9 +- src/api/computer.ts | 51 +---------- src/api/debug.ts | 7 +- src/api/extensions.ts | 6 +- src/api/filesystem.ts | 36 +------- src/api/init.ts | 5 +- src/api/os.ts | 85 +------------------ src/api/updater.ts | 9 +- src/api/window.ts | 32 +------ src/browser/events.ts | 19 +---- src/index.ts | 25 +----- types/api/app.d.ts | 7 ++ types/api/computer.d.ts | 49 +++++++++++ types/api/extensions.d.ts | 4 + types/api/filesystem.d.ts | 34 ++++++++ types/api/init.d.ts | 3 + types/api/os.d.ts | 66 ++++++++++++++ .../protocol.ts => types/api/protocol.d.ts | 0 types/api/updater.d.ts | 5 ++ types/api/window.d.ts | 30 +++++++ types/enums.d.ts | 47 ++++++++++ types/events.d.ts | 18 ++++ 23 files changed, 302 insertions(+), 271 deletions(-) create mode 100644 types/api/app.d.ts create mode 100644 types/api/computer.d.ts create mode 100644 types/api/extensions.d.ts create mode 100644 types/api/filesystem.d.ts create mode 100644 types/api/init.d.ts create mode 100644 types/api/os.d.ts rename src/api/protocol.ts => types/api/protocol.d.ts (100%) create mode 100644 types/api/updater.d.ts create mode 100644 types/api/window.d.ts create mode 100644 types/enums.d.ts create mode 100644 types/events.d.ts diff --git a/build.mjs b/build.mjs index 0b6887a..da2d800 100644 --- a/build.mjs +++ b/build.mjs @@ -11,7 +11,7 @@ // @ts-check -import { readFileSync, writeFile, writeFileSync, mkdirSync, existsSync } from 'fs' +import { readFileSync, writeFile, writeFileSync, mkdirSync, existsSync, readdirSync } from 'fs' import { exec } from 'child_process' import { join as joinPath } from 'path' import { rollup } from 'rollup' @@ -45,6 +45,7 @@ rollup ({ ...config, // rollup-plugin-ts produce an empty map, maybe we will find a solution in the future. // declarationMap: devmode + include: ['src/**/*.ts', 'types/**/*.d.ts'] }) }), devmode ? cleanup({comments: 'none'}) : Minify ({ format: { comments: false } }) @@ -151,10 +152,32 @@ function resetCommitHash () patchInitFile (commitHash, '') } +// Function to fetch all .d.ts files from the typings directory +const getTypeDefinitionFiles = () => { + const typingsDir = './types'; // Path to your typings directory + try { + const files = readdirSync(typingsDir); + return files.filter(file => file.endsWith('.d.ts')); + } catch (error) { + console.error('Error reading typings directory:', error); + return []; + } +}; + +// Fetch all .d.ts files from the typings directory +const typeFiles = getTypeDefinitionFiles(); + const writeDts = (filepath, definitions) => { // A 'declare' modifier cannot be used in an already ambient context. definitions = definitions.replaceAll ('declare namespace', 'namespace') definitions = definitions.replaceAll ('declare function', 'function') + + // Read the type definition files + let typesSource = '' + typeFiles.forEach(file => { + typesSource += readFileSync (`./types/${file}`, { encoding: 'utf8' }) + '\n\n'; + }); + let globalsSource = readFileSync ('./src/index.ts', { encoding: 'utf8' }) let globals = globalsSource.substring(globalsSource.indexOf('// --- globals ---'), globalsSource.lastIndexOf('// --- globals ---')) @@ -174,6 +197,7 @@ declare namespace Neutralino { ${definitions} } +${typesSource} ${globals} ${ diff --git a/src/api/app.ts b/src/api/app.ts index a4e050a..29ba1e9 100644 --- a/src/api/app.ts +++ b/src/api/app.ts @@ -1,13 +1,6 @@ import { sendMessage } from '../ws/websocket'; import * as os from './os'; - -export interface OpenActionOptions { - url: string; -} - -export interface RestartOptions { - args: string; -} +import type { RestartOptions } from "../../types/api/app" export function exit(code?: number): Promise { return sendMessage('app.exit', { code }); diff --git a/src/api/computer.ts b/src/api/computer.ts index 7c9773b..ea368ff 100644 --- a/src/api/computer.ts +++ b/src/api/computer.ts @@ -1,54 +1,5 @@ import { sendMessage } from '../ws/websocket'; - -export interface MemoryInfo { - physical: { - total: number; - available: number; - }, - virtual: { - total: number; - available: number; - } -} - -export interface KernelInfo { - variant: string; - version: string; -} - -export interface OSInfo { - name: string; - description: string; - version: string; -} - -export interface CPUInfo { - vendor: string; - model: string; - frequency: number; - architecture: string; - logicalThreads: number; - physicalCores: number; - physicalUnits: number; -} - -export interface Display { - id: number; - resolution: Resolution; - dpi: number; - bpp: number; - refreshRate: number; -} - -interface Resolution { - width: number; - height: number; -} - -interface MousePosition { - x: number; - y: number; -} +import type { MemoryInfo, KernelInfo, OSInfo, CPUInfo, Display, MousePosition} from '../../types/api/computer'; export function getMemoryInfo(): Promise { return sendMessage('computer.getMemoryInfo'); diff --git a/src/api/debug.ts b/src/api/debug.ts index 4b3945e..a9108b1 100644 --- a/src/api/debug.ts +++ b/src/api/debug.ts @@ -1,10 +1,5 @@ import { sendMessage } from '../ws/websocket'; - -export enum LoggerType { - WARNING = 'WARNING', - ERROR = 'ERROR', - INFO = 'INFO' -}; +import type { LoggerType } from '../../types/enums'; export function log(message: string, type?: LoggerType): Promise { return sendMessage('debug.log', { message, type }); diff --git a/src/api/extensions.ts b/src/api/extensions.ts index 2489de8..d98662f 100644 --- a/src/api/extensions.ts +++ b/src/api/extensions.ts @@ -1,9 +1,5 @@ import * as websocket from '../ws/websocket'; - -export interface ExtensionStats { - loaded: string[]; - connected: string[]; -} +import type { ExtensionStats } from '../../types/api/extensions'; export function dispatch(extensionId: string, event: string, data?: any): Promise { return new Promise(async (resolve: any, reject: any) => { diff --git a/src/api/filesystem.ts b/src/api/filesystem.ts index a63166f..61c1f1a 100644 --- a/src/api/filesystem.ts +++ b/src/api/filesystem.ts @@ -1,40 +1,6 @@ import { sendMessage } from '../ws/websocket'; import { base64ToBytesArray } from '../helpers'; - -export interface DirectoryEntry { - entry: string; - path: string; - type: string; -} - -export interface FileReaderOptions { - pos: number; - size: number; -} - -export interface DirectoryReaderOptions { - recursive: boolean; -} - -export interface OpenedFile { - id: number; - eof: boolean; - pos: number; - lastRead: number; -} - -export interface Stats { - size: number; - isFile: boolean; - isDirectory: boolean; - createdAt: number; - modifiedAt: number; -} - -export interface Watcher { - id: number; - path: string; -} +import type {DirectoryEntry, DirectoryReaderOptions, FileReaderOptions, OpenedFile, Stats, Watcher } from '../../types/api/filesystem'; export function createDirectory(path: string): Promise { return sendMessage('filesystem.createDirectory', { path }); diff --git a/src/api/init.ts b/src/api/init.ts index 35e2b71..af3acda 100644 --- a/src/api/init.ts +++ b/src/api/init.ts @@ -2,13 +2,10 @@ import { version } from '../../package.json'; import * as websocket from '../ws/websocket'; import * as debug from './debug'; import * as events from './events'; +import type { InitOptions } from '../../types/api/init'; let initialized = false; -export interface InitOptions { - exportCustomMethods?: boolean; -} - export function init(options: InitOptions = {}): void { options = { exportCustomMethods: true ,...options }; diff --git a/src/api/os.ts b/src/api/os.ts index d41a8d6..0e12e21 100644 --- a/src/api/os.ts +++ b/src/api/os.ts @@ -1,87 +1,6 @@ import { sendMessage } from '../ws/websocket'; - -export interface ExecCommandOptions { - stdIn?: string; - background?: boolean; - cwd?: string; -} - -export interface ExecCommandResult { - pid: number; - stdOut: string; - stdErr: string; - exitCode: number; -} - -export interface SpawnedProcess { - id: number; - pid: number; -} - -export interface Envs { - [key: string]: string; -} - -export interface OpenDialogOptions { - multiSelections?: boolean; - filters?: Filter[]; - defaultPath?: string; -} - -export interface FolderDialogOptions { - defaultPath?: string; -} - -export interface SaveDialogOptions { - forceOverwrite?: boolean; - filters?: Filter[]; - defaultPath?: string; -} - -export interface Filter { - name: string; - extensions: string[]; -} - -export interface TrayOptions { - icon: string; - menuItems: TrayMenuItem[]; -} - -export interface TrayMenuItem { - id?: string; - text: string; - isDisabled?: boolean; - isChecked?: boolean; -} - -export enum Icon { - WARNING = 'WARNING', - ERROR = 'ERROR', - INFO = 'INFO', - QUESTION = 'QUESTION' -}; - -export enum MessageBoxChoice { - OK = 'OK', - OK_CANCEL = 'OK_CANCEL', - YES_NO = 'YES_NO', - YES_NO_CANCEL = 'YES_NO_CANCEL', - RETRY_CANCEL = 'RETRY_CANCEL', - ABORT_RETRY_IGNORE = 'ABORT_RETRY_IGNORE' -}; - -export type KnownPath = - 'config' | - 'data' | - 'cache' | - 'documents' | - 'pictures' | - 'music' | - 'video' | - 'downloads' | - 'savedGames1' | - 'savedGames2' +import type { MessageBoxChoice, Icon } from '../../types/enums'; +import type { Envs, ExecCommandOptions, ExecCommandResult, FolderDialogOptions, KnownPath, OpenDialogOptions, SaveDialogOptions, SpawnedProcess, TrayOptions } from '../../types/api/os'; export function execCommand(command: string, options?: ExecCommandOptions): Promise { return sendMessage('os.execCommand', { command, ...options }); diff --git a/src/api/updater.ts b/src/api/updater.ts index a2e1a77..0171b0f 100644 --- a/src/api/updater.ts +++ b/src/api/updater.ts @@ -1,11 +1,6 @@ import * as filesystem from './filesystem'; -import { Error } from './protocol'; - -export interface Manifest { - applicationId: string; - version: string; - resourcesURL: string; -} +import { Error } from '../../types/api/protocol'; +import { Manifest } from '../../types/api/updater'; let manifest: Manifest = null; diff --git a/src/api/window.ts b/src/api/window.ts index 7bdb1ec..79d026a 100644 --- a/src/api/window.ts +++ b/src/api/window.ts @@ -1,39 +1,9 @@ import { sendMessage } from '../ws/websocket'; import * as os from './os'; +import { WindowOptions, WindowPosOptions, WindowSizeOptions } from '../../types/api/window'; const draggableRegions: WeakMap = new WeakMap(); -export interface WindowOptions extends WindowSizeOptions, WindowPosOptions { - title?: string; - icon?: string; - fullScreen?: boolean; - alwaysOnTop?: boolean; - enableInspector?: boolean; - borderless?: boolean; - maximize?: boolean; - hidden?: boolean; - maximizable?: boolean; - useSavedState?: boolean; - exitProcessOnClose?: boolean; - extendUserAgentWith?: string; - processArgs?: string; -} - -export interface WindowSizeOptions { - width?: number; - height?: number; - minWidth?: number; - minHeight?: number; - maxWidth?: number; - maxHeight?: number; - resizable?: boolean; -} - -export interface WindowPosOptions { - x: number; - y: number; -} - export function setTitle(title: string): Promise { return sendMessage('window.setTitle', { title }); }; diff --git a/src/browser/events.ts b/src/browser/events.ts index 8258801..073e1fa 100644 --- a/src/browser/events.ts +++ b/src/browser/events.ts @@ -1,21 +1,4 @@ -export interface Response { - success: boolean; - message: string; -} - -export type Builtin = - 'ready' | - 'trayMenuItemClicked' | - 'windowClose' | - 'serverOffline' | - 'clientConnect' | - 'clientDisconnect' | - 'appClientConnect' | - 'appClientDisconnect' | - 'extClientConnect' | - 'extClientDisconnect' | - 'extensionReady' | - 'neuDev_reloadApp' +import type { Response } from '../../types/events'; export function on(event: string, handler: (ev: CustomEvent) => void): Promise { window.addEventListener(event, handler); diff --git a/src/index.ts b/src/index.ts index e9ed99d..be055a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,25 +1,4 @@ -export enum Mode { - window, - browser, - cloud, - chrome -} - -export enum OperatingSystem { - Linux, - Windows, - Darwin, - FreeBSD, - Unknown -} - -export enum Architecture { - x64, - arm, - itanium, - ia32, - unknown -} +import { Architecture, Mode, OperatingSystem } from '../types/enums'; declare global { interface Window { @@ -78,4 +57,4 @@ export * as clipboard from './api/clipboard'; export * as custom from './api/custom'; export { init } from './api/init'; -export { Error, ErrorCode } from './api/protocol'; +export { Error, ErrorCode } from '../types/api/protocol'; diff --git a/types/api/app.d.ts b/types/api/app.d.ts new file mode 100644 index 0000000..ea07d58 --- /dev/null +++ b/types/api/app.d.ts @@ -0,0 +1,7 @@ +export interface OpenActionOptions { + url: string; +} + +export interface RestartOptions { + args: string; +} \ No newline at end of file diff --git a/types/api/computer.d.ts b/types/api/computer.d.ts new file mode 100644 index 0000000..a951b43 --- /dev/null +++ b/types/api/computer.d.ts @@ -0,0 +1,49 @@ +export interface MemoryInfo { + physical: { + total: number; + available: number; + }, + virtual: { + total: number; + available: number; + } +} + +export interface KernelInfo { + variant: string; + version: string; +} + +export interface OSInfo { + name: string; + description: string; + version: string; +} + +export interface CPUInfo { + vendor: string; + model: string; + frequency: number; + architecture: string; + logicalThreads: number; + physicalCores: number; + physicalUnits: number; +} + +export interface Display { + id: number; + resolution: Resolution; + dpi: number; + bpp: number; + refreshRate: number; +} + +export interface Resolution { + width: number; + height: number; +} + +export interface MousePosition { + x: number; + y: number; +} \ No newline at end of file diff --git a/types/api/extensions.d.ts b/types/api/extensions.d.ts new file mode 100644 index 0000000..8721266 --- /dev/null +++ b/types/api/extensions.d.ts @@ -0,0 +1,4 @@ +export interface ExtensionStats { + loaded: string[]; + connected: string[]; +} diff --git a/types/api/filesystem.d.ts b/types/api/filesystem.d.ts new file mode 100644 index 0000000..edfb95f --- /dev/null +++ b/types/api/filesystem.d.ts @@ -0,0 +1,34 @@ +export interface DirectoryEntry { + entry: string; + path: string; + type: string; +} + +export interface FileReaderOptions { + pos: number; + size: number; +} + +export interface DirectoryReaderOptions { + recursive: boolean; +} + +export interface OpenedFile { + id: number; + eof: boolean; + pos: number; + lastRead: number; +} + +export interface Stats { + size: number; + isFile: boolean; + isDirectory: boolean; + createdAt: number; + modifiedAt: number; +} + +export interface Watcher { + id: number; + path: string; +} \ No newline at end of file diff --git a/types/api/init.d.ts b/types/api/init.d.ts new file mode 100644 index 0000000..45b517e --- /dev/null +++ b/types/api/init.d.ts @@ -0,0 +1,3 @@ +export interface InitOptions { + exportCustomMethods?: boolean; +} \ No newline at end of file diff --git a/types/api/os.d.ts b/types/api/os.d.ts new file mode 100644 index 0000000..b875af6 --- /dev/null +++ b/types/api/os.d.ts @@ -0,0 +1,66 @@ +export interface ExecCommandOptions { + stdIn?: string; + background?: boolean; + cwd?: string; +} + +export interface ExecCommandResult { + pid: number; + stdOut: string; + stdErr: string; + exitCode: number; +} + +export interface SpawnedProcess { + id: number; + pid: number; +} + +export interface Envs { + [key: string]: string; +} + +export interface OpenDialogOptions { + multiSelections?: boolean; + filters?: Filter[]; + defaultPath?: string; +} + +export interface FolderDialogOptions { + defaultPath?: string; +} + +export interface SaveDialogOptions { + forceOverwrite?: boolean; + filters?: Filter[]; + defaultPath?: string; +} + +export interface Filter { + name: string; + extensions: string[]; +} + +export interface TrayOptions { + icon: string; + menuItems: TrayMenuItem[]; +} + +export interface TrayMenuItem { + id?: string; + text: string; + isDisabled?: boolean; + isChecked?: boolean; +} + +export type KnownPath = + 'config' | + 'data' | + 'cache' | + 'documents' | + 'pictures' | + 'music' | + 'video' | + 'downloads' | + 'savedGames1' | + 'savedGames2' \ No newline at end of file diff --git a/src/api/protocol.ts b/types/api/protocol.d.ts similarity index 100% rename from src/api/protocol.ts rename to types/api/protocol.d.ts diff --git a/types/api/updater.d.ts b/types/api/updater.d.ts new file mode 100644 index 0000000..4b34721 --- /dev/null +++ b/types/api/updater.d.ts @@ -0,0 +1,5 @@ +export interface Manifest { + applicationId: string; + version: string; + resourcesURL: string; +} \ No newline at end of file diff --git a/types/api/window.d.ts b/types/api/window.d.ts new file mode 100644 index 0000000..1108d7f --- /dev/null +++ b/types/api/window.d.ts @@ -0,0 +1,30 @@ +export interface WindowOptions extends WindowSizeOptions, WindowPosOptions { + title?: string; + icon?: string; + fullScreen?: boolean; + alwaysOnTop?: boolean; + enableInspector?: boolean; + borderless?: boolean; + maximize?: boolean; + hidden?: boolean; + maximizable?: boolean; + useSavedState?: boolean; + exitProcessOnClose?: boolean; + extendUserAgentWith?: string; + processArgs?: string; + } + + export interface WindowSizeOptions { + width?: number; + height?: number; + minWidth?: number; + minHeight?: number; + maxWidth?: number; + maxHeight?: number; + resizable?: boolean; + } + + export interface WindowPosOptions { + x: number; + y: number; + } \ No newline at end of file diff --git a/types/enums.d.ts b/types/enums.d.ts new file mode 100644 index 0000000..474fffb --- /dev/null +++ b/types/enums.d.ts @@ -0,0 +1,47 @@ +//debug +export enum LoggerType { + WARNING = 'WARNING', + ERROR = 'ERROR', + INFO = 'INFO' + } + +//OS +export enum Icon { + WARNING = 'WARNING', + ERROR = 'ERROR', + INFO = 'INFO', + QUESTION = 'QUESTION' +} + +export enum MessageBoxChoice { + OK = 'OK', + OK_CANCEL = 'OK_CANCEL', + YES_NO = 'YES_NO', + YES_NO_CANCEL = 'YES_NO_CANCEL', + RETRY_CANCEL = 'RETRY_CANCEL', + ABORT_RETRY_IGNORE = 'ABORT_RETRY_IGNORE' +} + +//NL_GLOBALS +export enum Mode { + window, + browser, + cloud, + chrome +} + +export enum OperatingSystem { + Linux, + Windows, + Darwin, + FreeBSD, + Unknown +} + +export enum Architecture { + x64, + arm, + itanium, + ia32, + unknown +} diff --git a/types/events.d.ts b/types/events.d.ts new file mode 100644 index 0000000..7ca35f6 --- /dev/null +++ b/types/events.d.ts @@ -0,0 +1,18 @@ +export interface Response { + success: boolean; + message: string; + } + + export type Builtin = + 'ready' | + 'trayMenuItemClicked' | + 'windowClose' | + 'serverOffline' | + 'clientConnect' | + 'clientDisconnect' | + 'appClientConnect' | + 'appClientDisconnect' | + 'extClientConnect' | + 'extClientDisconnect' | + 'extensionReady' | + 'neuDev_reloadApp' \ No newline at end of file