Skip to content

Commit

Permalink
default to .config/TerminalOne/config.js as the config path (#50)
Browse files Browse the repository at this point in the history
* default to .config/TerminalOne/config.js as the config path

* bump version
  • Loading branch information
atinylittleshell committed Jan 21, 2024
1 parent 3435721 commit 79b33d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions apps/app/src/nativeBridge/modules/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { app } from 'electron';
export const getAppDirs = () => {
return {
userData: app.getPath('userData'),
home: app.getPath('home'),
};
};
37 changes: 29 additions & 8 deletions apps/app/src/nativeBridge/modules/configModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
resolveConfig,
ResolvedConfig,
} from '@terminalone/types';
import { BrowserWindow } from 'electron';
import { app, BrowserWindow } from 'electron';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs-extra';
import os from 'os';
import path from 'path';
Expand All @@ -19,6 +19,7 @@ import { Logger } from './common/logger';

@nativeBridgeModule('config')
export class ConfigModule extends NativeBridgeModule {
private configPath: string = '';
private config: ResolvedConfig = DEFAULT_CONFIG;

@moduleFunction()
Expand All @@ -28,22 +29,40 @@ export class ConfigModule extends NativeBridgeModule {

@moduleFunction()
public async getConfigPath(_mainWindow: BrowserWindow): Promise<string> {
return path.join(getAppDirs().userData, 'config.js');
return this.configPath;
}

public onRegistered(mainWindow: BrowserWindow): void {
const configDir = getAppDirs().userData;
const configPath = path.join(configDir, 'config.js');
if (!existsSync(configPath)) {
const configPathCandidates: string[] = [
path.join(
getAppDirs().home,
'.config',
app.getName().replace(/\s/g, ''),
'config.js',
),
path.join(getAppDirs().userData, 'config.js'),
];

// look for the first existing config file. if not found, use the first candidate
this.configPath = configPathCandidates[0];
for (const configPathCandidate of configPathCandidates) {
if (existsSync(configPathCandidate)) {
this.configPath = configPathCandidate;
break;
}
}

if (!existsSync(this.configPath)) {
const configDir = path.dirname(this.configPath);
mkdirSync(configDir, { recursive: true });
writeFileSync(
configPath,
this.configPath,
'// see https://github.com/atinylittleshell/TerminalOne/blob/main/packages/types/defaultConfig.ts for supported configuration values\n' +
'module.exports = {};',
);
}

const configContent = readFileSync(configPath, 'utf8');
const configContent = readFileSync(this.configPath, 'utf8');

try {
const script = new vm.Script(configContent, {
Expand All @@ -66,7 +85,9 @@ export class ConfigModule extends NativeBridgeModule {

Logger.getInstance().log(
'info',
`Loaded config from ${configPath}}: ${JSON.stringify(mod.exports)}`,
`Loaded config from ${this.configPath}}: ${JSON.stringify(
mod.exports,
)}`,
);

const resolved = resolveConfig(mod.exports, os.platform(), os.release());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@terminalone/monorepo",
"productName": "Terminal One",
"version": "1.5.1",
"version": "1.6.0",
"description": "A fast, elegant and intelligent cross-platform terminal.",
"author": "atinylittleshell <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit 79b33d0

Please sign in to comment.