Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions developer/src/common/web/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export { getFontFamily, getFontFamilySync } from './font-family.js';
export * as ValidIds from './valid-ids.js';

export * as ProjectLoader from './project-loader.js';

export { optionsManager, KeymanDeveloperOption, KeymanDeveloperOptions, KeymanDeveloperOptionsPath } from './keyman-developer-options.js';
128 changes: 128 additions & 0 deletions developer/src/common/web/utils/src/keyman-developer-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* User options for Keyman Developer. These are stored in options.json in the
* user profile; the location varies by operating system or may be stored in
* browser storage on web sites.
*
* The node-based loader is implemented in both kmc and Keyman Developer Server,
* in order to keep node dependencies out of the developer-utils module.
*/

/**
* The standard path under the user profile where options.json is stored; use
* `path.join(os.homedir(), ...KeymanDeveloperOptionsPath)` or similar
*/
export const KeymanDeveloperOptionsPath = [/* '~', */ '.keymandeveloper', 'options.json'];

/**
* The set of standard user options for Keyman Developer. Corresponds to
* TKeymanDeveloperOptions in the Keyman Developer TIKE source.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it would be better to state the filename?

Suggested change
* TKeymanDeveloperOptions in the Keyman Developer TIKE source.
* TKeymanDeveloperOptions in `developer/src/tike/main/KeymanDeveloperOptions.pas`

*/
export interface KeymanDeveloperOptions {
"use tab char": boolean;
"link font sizes": boolean;
"indent size": number;
"use old debugger": boolean;
"editor theme": string;
"debugger break when exiting line": boolean;
"debugger single step after break": boolean;
"debugger show store offset": boolean;
"debugger recompile with debug info": boolean;
"debugger auto reset before compilng": boolean;
"auto save before compiling": boolean;
"osk auto save before importing": boolean;
"web host port": number;
"server keep alive": boolean;
"server use local addresses": boolean;
"server ngrok token": string;
"server ngrok region": string;
"server use ngrok": boolean;
"server show console window": boolean;
"char map disable database lookups": boolean;
"char map auto lookup": boolean;
"open keyboard files in source view": boolean;
"display theme": string;
"external editor path": string;
"smtp server": string;
"test email addresses": string;
"web ladder length": number;
"default project path": string;
"automatically report errors": boolean;
"automatically report usage": boolean;
"toolbar visible": boolean;
"active project": string;
"prompt to upgrade projects": boolean;
};

/**
* A single Keyman Developer user option.
*/
export type KeymanDeveloperOption = keyof KeymanDeveloperOptions;

const DEFAULT_OPTIONS: KeymanDeveloperOptions = {
// Corresponds to KeymanDeveloperOptions.pas, TKeymanDeveloperOptions.Read

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit clearer might be:

Suggested change
// Corresponds to KeymanDeveloperOptions.pas, TKeymanDeveloperOptions.Read
// Corresponds to TKeymanDeveloperOptions.Read in KeymanDeveloperOptions.pas

"use tab char": false,
"link font sizes": true,
"indent size": 4,
"use old debugger": false,
"editor theme": '',
"debugger break when exiting line": true,
"debugger single step after break": false,
"debugger show store offset": false,
"debugger recompile with debug info": false,
"debugger auto reset before compilng": false,
"auto save before compiling": false,
"osk auto save before importing": false,
"web host port": 8008,
"server keep alive": false,
"server use local addresses": true,
"server ngrok token": '',
"server ngrok region": '',
"server use ngrok": false,
"server show console window": false,
"char map disable database lookups": false,
"char map auto lookup": true,
"open keyboard files in source view": false,
"display theme": 'Windows10',
"external editor path": '',
"smtp server": '',
"test email addresses": '',
"web ladder length": 100,
"default project path": '', // Note: this diverges from Delphi code, which uses CSIDL_PERSONAL on Windows, but it is not used in Server
"automatically report errors": true,
"automatically report usage": true,
"toolbar visible": true,
"active project": '',
"prompt to upgrade projects": true,
}


class KeymanDeveloperOptionsManager {
private options: KeymanDeveloperOptions = {...DEFAULT_OPTIONS};
constructor() {}

public load(blob: Uint8Array | null) {
this.options = {...DEFAULT_OPTIONS};
if(blob !== null && blob !== undefined) {
const data = JSON.parse(new TextDecoder('utf-8').decode(blob));
if(typeof data == 'object') {
// TODO: verify fields in options
this.options = {...DEFAULT_OPTIONS, ...data};
return true;
}
}
return false;
}

public get<T extends KeymanDeveloperOption>(valueName: T): KeymanDeveloperOptions[T] {
return this.options[valueName];
}

public clear() {
this.options = {...DEFAULT_OPTIONS};
}
}

export const optionsManager = new KeymanDeveloperOptionsManager();

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ProjectBuilder {

// Give a hint if the project is v1.0
if(this.project.options.version != '2.0') {
if(getOption("prompt to upgrade projects", true)) {
if(getOption("prompt to upgrade projects")) {
this.callbacks.reportMessage(InfrastructureMessages.Hint_ProjectIsVersion10());
}
}
Expand Down
2 changes: 1 addition & 1 deletion developer/src/kmc/src/util/KeymanSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class KeymanSentry {
return true;
}

return getOption('automatically report errors', true);
return getOption('automatically report errors');
}

static init(options?: SentryNodeOptions) {
Expand Down
85 changes: 24 additions & 61 deletions developer/src/kmc/src/util/options.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,32 @@
/*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Load Keyman Developer's options from the standard Options location. This
* small loader is duplicated in Keyman Developer Server, because we do not have
* a shared node-aware module at this time.
*/

import * as os from 'node:os';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { KeymanDeveloperOption, KeymanDeveloperOptions, KeymanDeveloperOptionsPath, optionsManager } from '@keymanapp/developer-utils';

export interface KeymanDeveloperOptions {
"use tab char"?: boolean;
"link font sizes"?: boolean;
"indent size"?: number;
"use old debugger"?: boolean;
"editor theme"?: string;
"debugger break when exiting line"?: boolean;
"debugger single step after break"?: boolean;
"debugger show store offset"?: boolean;
"debugger recompile with debug info"?: boolean;
"debugger auto reset before compilng"?: boolean;
"auto save before compiling"?: boolean;
"osk auto save before importing"?: boolean;
"web host port"?: number;
"server keep alive"?: boolean;
"server use local addresses"?: boolean;
"server ngrok token"?: string;
"server ngrok region"?: string;
"server use ngrok"?: boolean;
"server show console window"?: boolean;
"char map disable database lookups"?: boolean;
"char map auto lookup"?: boolean;
"open keyboard files in source view"?: boolean;
"display theme"?: string;
"external editor path"?: string;
"smtp server"?: string;
"test email addresses"?: string;
"web ladder length"?: number;
"default project path"?: string;
"automatically report errors"?: boolean;
"automatically report usage"?: boolean;
"toolbar visible"?: boolean;
"active project"?: string;
"prompt to upgrade projects"?: boolean;
};

type KeymanDeveloperOption = keyof KeymanDeveloperOptions;

// Default has no options set, and unit tests will use the defaults (won't call
// `loadOptions()`)
let options: KeymanDeveloperOptions = {};

// We only load the options from disk once on first use
let optionsLoaded = false;
let optionsLoaded: boolean = false;

export async function loadOptions(): Promise<KeymanDeveloperOptions> {
export async function loadOptions(): Promise<boolean> {
if(optionsLoaded) {
return options;
return true;
}
optionsLoaded = true;

options = {};
try {
const optionsFile = path.join(os.homedir(), '.keymandeveloper', 'options.json');
const optionsFile = path.join(os.homedir(), ...KeymanDeveloperOptionsPath);
if(fs.existsSync(optionsFile)) {
for(let i = 0; i < 5; i++) {
try {
const data = JSON.parse(fs.readFileSync(optionsFile, 'utf-8'));
if(typeof data == 'object') {
options = data;
}
break;
} catch(e) {
const data = fs.readFileSync(optionsFile) as Uint8Array;
return optionsManager.load(data);
} catch(e: any) {
if(e?.code == 'EBUSY') {
await new Promise(resolve => setTimeout(resolve, 500));
} else {
Expand All @@ -76,20 +39,20 @@ export async function loadOptions(): Promise<KeymanDeveloperOptions> {
} catch(e) {
// Nothing to report here, sadly -- because we cannot rely on Sentry at this
// low level.
options = {};
}
optionsLoaded = true;
return options;

optionsManager.clear();
return false;
}

export function getOption<T extends KeymanDeveloperOption>(valueName: T, defaultValue: KeymanDeveloperOptions[T]): KeymanDeveloperOptions[T] {
return options[valueName] ?? defaultValue;
export function getOption<T extends KeymanDeveloperOption>(valueName: T): KeymanDeveloperOptions[T] {
return optionsManager.get(valueName);
}

/**
* unit tests will clear options before running, for consistency
*/
export function clearOptions() {
options = {};
optionsLoaded = true;
optionsLoaded = false;
return optionsManager.clear();
}
2 changes: 1 addition & 1 deletion developer/src/server/src/KeymanSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class KeymanSentry {
return true;
}

return getOption('automatically report errors', true);
return getOption('automatically report errors');
}

static init(options?: SentryNodeOptions) {
Expand Down
48 changes: 0 additions & 48 deletions developer/src/server/src/config.ts

This file was deleted.

6 changes: 3 additions & 3 deletions developer/src/server/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from 'fs';
import { configuration } from './config.js';
import { standardPaths } from './standardPaths.js';
import { loadJsonFile } from './load-json-file.js';

export interface DebugObject {
Expand Down Expand Up @@ -97,7 +97,7 @@ export class SiteData {
}

private loadState() {
const state = loadJsonFile(configuration.cacheStateFilename);
const state = loadJsonFile(standardPaths.cacheStateFilename);
this.loadDebugObject(DebugKeyboard, state?.keyboards, this.keyboards);
this.loadDebugObject(DebugModel, state?.models, this.models);
this.loadDebugObject(DebugFont, state?.fonts, this.fonts);
Expand All @@ -106,7 +106,7 @@ export class SiteData {
}

public saveState() {
writeFileSync(configuration.cacheStateFilename, JSON.stringify(this, null, 2), 'utf-8');
writeFileSync(standardPaths.cacheStateFilename, JSON.stringify(this, null, 2), 'utf-8');
}
};

Expand Down
4 changes: 2 additions & 2 deletions developer/src/server/src/handlers/api/debugobject/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as express from 'express';
import { DebugObject, isValidId, simplifyId } from "../../../data.js";
import * as fs from 'fs';
import * as crypto from 'crypto';
import { configuration } from '../../../config.js';
import { standardPaths } from '../../../standardPaths.js';
import chalk from 'chalk';

// We allow only 12 objects of each type in the cache
Expand Down Expand Up @@ -41,7 +41,7 @@ export function apiRegisterFile<O extends DebugObject> (intf: new () => O, root:

o.lastUse = new Date();
o.id = id;
o.filename = configuration.cachePath + o.filenameFromId(id);
o.filename = standardPaths.cachePath + o.filenameFromId(id);
fs.writeFileSync(o.filename, file);
o.sha256 = crypto.createHash('sha256').update(file).digest('hex');

Expand Down
Loading