Skip to content

Commit

Permalink
Implement settings export
Browse files Browse the repository at this point in the history
  • Loading branch information
ijager committed Jan 6, 2022
1 parent 4974a90 commit 20f22bb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/app/core/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { from, BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';
import { PluginService, MatchRule } from './plugin.service';

import * as Fs from 'fs';
import { ElectronService } from '.';

const PREFERENCES_KEY = 'sw_preferences';

export interface Preferences {
Expand All @@ -19,7 +22,7 @@ export interface Preferences {
}

const defaults: Preferences = {
version: 1,
version: 2,
baudrate: 115200,
prevPort: undefined,
newlineChar: '\n',
Expand Down Expand Up @@ -57,6 +60,7 @@ const defaults: Preferences = {
})
export class SettingsService {
storage: typeof Storage;
fs: typeof Fs;

preferences: Preferences;

Expand All @@ -74,13 +78,16 @@ export class SettingsService {
}

constructor(
private pluginService: PluginService
private pluginService: PluginService,
private electronService: ElectronService
) {

console.log('Init settings service');
if (this.isElectron()) {

this.storage = window.require('electron-json-storage');
this.fs = window.require('fs');

console.log('json storage:', this.storage.getDataPath());
this.storage.has(PREFERENCES_KEY, (error: any, hasKey: boolean) => {
if (error) throw error;
Expand All @@ -94,7 +101,8 @@ export class SettingsService {
if ((this.preferences.version || 0) < defaults.version) {
// TODO migrate or replace
console.log('Preferences outdates, using defaults');
this.toDefaults();
this.migrate();
this.save();
}
this.preferencesLoaded$.next(true);
});
Expand Down Expand Up @@ -125,11 +133,38 @@ export class SettingsService {
return this.preferences;
}

migrate() {
if (!this.preferences.maxLines) {
this.preferences.maxLines = defaults.maxLines;
}
if (!this.preferences.rules) {
this.preferences.rules = defaults.rules;
}
this.preferences.version = defaults.version;
}

toDefaults() {
this.preferences = defaults;
this.save();
}

export(suggestedFilename) {
let filename = this.electronService.remote.dialog.showSaveDialogSync({
title: 'Export settings to File…',
message: 'For example, save the settings in your project repository',
defaultPath: suggestedFilename,
filters: [
{ name: 'JSON files', extensions: ['*.json'] }
]
});
if (filename) {
console.log("saving ", filename)
const content = JSON.stringify(this.preferences, null, 2);
this.fs.writeFileSync(filename, content);
}

}

save() {
this.storage.set(PREFERENCES_KEY, this.preferences, (error) => {
if (error) throw error;
Expand Down
20 changes: 20 additions & 0 deletions src/app/home/settings/settings.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@

<div class="contents">

<div class="settings-section">

<p>
Import an existing settings file (.json). For example one containing project specific matching rules.
</p>
<div class="p-inputgroup">
<span class="p-inputgroup-addon" style="line-height: 1;"> Import</span>
<input pInputText type="file">
<button pButton class="p-button-info" label="import settings" icon="pi pi-download" (click)="importSettings()"></button>
</div>

<p>
Export settings so you can reuse them later
</p>

<button pButton class="p-button-info" label="export settings" icon="pi pi-upload" (click)="exportSettings()"></button>

</div>

<div class="settings-section">

<h2> General </h2>


<div class="p-inputgroup">
<span class="p-inputgroup-addon" style="line-height: 1;"> Max lines in terminal:</span>
<input pInputText type="number" min=1 step=1 [(ngModel)]="maxLines">
Expand Down
4 changes: 4 additions & 0 deletions src/app/home/settings/settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ export class SettingsComponent implements OnInit {
this.settingsService.saveRules(this.rules);
}

exportFilename = "sw_settings.json"
exportSettings() {
this.settingsService.export(this.exportFilename);
}
}

0 comments on commit 20f22bb

Please sign in to comment.