Skip to content

Commit

Permalink
feat: separated types from executable code and modifying the build.mj…
Browse files Browse the repository at this point in the history
…s file accordingly (#105)
  • Loading branch information
Sadaf-A authored Mar 18, 2024
1 parent 0f5de04 commit 6c80b1d
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 271 deletions.
26 changes: 25 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 } })
Expand Down Expand Up @@ -151,10 +152,32 @@ function resetCommitHash ()
patchInitFile (commitHash, '<git_commit_hash_latest>')
}

// 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 ---'))
Expand All @@ -174,6 +197,7 @@ declare namespace Neutralino {
${definitions}
}
${typesSource}
${globals}
${
Expand Down
9 changes: 1 addition & 8 deletions src/api/app.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return sendMessage('app.exit', { code });
Expand Down
51 changes: 1 addition & 50 deletions src/api/computer.ts
Original file line number Diff line number Diff line change
@@ -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<MemoryInfo> {
return sendMessage('computer.getMemoryInfo');
Expand Down
7 changes: 1 addition & 6 deletions src/api/debug.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return sendMessage('debug.log', { message, type });
Expand Down
6 changes: 1 addition & 5 deletions src/api/extensions.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return new Promise(async (resolve: any, reject: any) => {
Expand Down
36 changes: 1 addition & 35 deletions src/api/filesystem.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return sendMessage('filesystem.createDirectory', { path });
Expand Down
5 changes: 1 addition & 4 deletions src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
85 changes: 2 additions & 83 deletions src/api/os.ts
Original file line number Diff line number Diff line change
@@ -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<ExecCommandResult> {
return sendMessage('os.execCommand', { command, ...options });
Expand Down
9 changes: 2 additions & 7 deletions src/api/updater.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
32 changes: 1 addition & 31 deletions src/api/window.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLElement, any> = 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<void> {
return sendMessage('window.setTitle', { title });
};
Expand Down
19 changes: 1 addition & 18 deletions src/browser/events.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
window.addEventListener(event, handler);
Expand Down
Loading

0 comments on commit 6c80b1d

Please sign in to comment.