Skip to content

Commit b366fef

Browse files
committed
default directory and default note get created if they don't exist.
1 parent 1d0a2e1 commit b366fef

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

src/background.ts

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
'use strict'
22

3-
import { app, protocol, BrowserWindow} from 'electron'
3+
import { app, protocol, BrowserWindow, Config } from 'electron'
44
import {
55
createProtocol,
66
installVueDevtools
77
} from 'vue-cli-plugin-electron-builder/lib'
8+
import Configuration from './utility/Configuration';
89
const isDevelopment = process.env.NODE_ENV !== 'production'
9-
const nativeImage = require('electron').nativeImage;
10-
// var image = nativeImage.createFromPath(__dirname + '/assets/icon.png');
11-
// image.setTemplateImage(true);
12-
// console.log(__dirname + '/assets/icon.png')
1310
// Keep a global reference of the window object, if you don't, the window will
1411
// be closed automatically when the JavaScript object is garbage collected.
1512
let win;
1613

1714
// Standard scheme must be registered before the app is ready
1815
protocol.registerStandardSchemes(['app'], { secure: true })
19-
function createWindow () {
16+
function createWindow() {
2017
// Create the browser window.
21-
win = new BrowserWindow({
22-
minWidth:230,
23-
width:400,
24-
height:400,
25-
minHeight:400,
26-
autoHideMenuBar: true,
27-
transparent: true,
18+
win = new BrowserWindow({
19+
minWidth: 230,
20+
width: 400,
21+
height: 400,
22+
minHeight: 400,
23+
autoHideMenuBar: true,
24+
transparent: true,
2825
frame: false,
29-
resizable:true
30-
})
26+
resizable: true
27+
});
3128

3229
if (process.env.WEBPACK_DEV_SERVER_URL) {
3330
// Load the url of the dev server if in development mode
@@ -43,7 +40,42 @@ function createWindow () {
4340
win = null
4441
})
4542
}
43+
import * as fs from 'fs';
44+
import * as path from 'path';
45+
function ensureExists(callback) {
46+
Configuration.getConfig().then(c => {
47+
fs.exists(c.notePath, folderExists => {
48+
if (!folderExists) {
49+
createDirectory(c, _ => {
50+
createDefault(c, _ => callback());
51+
})
52+
} else {
53+
fs.exists(path.join(c.notePath, c.defaultNote), fileExists => {
54+
if (!fileExists) createDefault(c, _ => callback())
55+
else callback();
56+
})
57+
};
58+
})
59+
});
60+
}
61+
62+
function createDirectory(config: Configuration, callback) {
63+
fs.mkdir(config.notePath, e => {
64+
if (e){
65+
console.log(e);
66+
alert(e);
67+
process.exit();
68+
}else{
69+
callback(e);
70+
}
71+
});
72+
}
4673

74+
function createDefault(config: Configuration, callback) {
75+
fs.writeFile(path.join(config.notePath, config.defaultNote), "# Welcome to your new note!\nLet's write something awesome.", e => {
76+
callback(e)
77+
})
78+
}
4779

4880
// Quit when all windows are closed.
4981
app.on('window-all-closed', () => {
@@ -70,7 +102,10 @@ app.on('ready', async () => {
70102
// Install Vue Devtools
71103
await installVueDevtools()
72104
}
73-
createWindow()
105+
106+
ensureExists(function(){
107+
createWindow()
108+
})
74109
})
75110

76111
// Exit cleanly on request from parent process in development mode.

src/components/Editor.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<script lang="ts">
88
const HyperMD = require("hypermd");
9-
import { readFile, writeFile, rename } from "fs";
9+
import { readFile, writeFile, rename, exists, mkdir } from "fs";
1010
import { clearInterval } from "timers";
1111
import { Component, Prop, Vue } from "vue-property-decorator";
1212
import * as path from "path";
@@ -19,7 +19,7 @@ import VueEmitter from "@/utility/VueEmitter"
1919
export default class Editor extends VueEmitter {
2020
private internalEditor: any;
2121
private saveWatcher: any;
22-
private config;
22+
private config:Configuration;
2323
private file;
2424
2525
private filePath() {
@@ -63,6 +63,8 @@ export default class Editor extends VueEmitter {
6363
this.registerRootEvent("closing", _ => this.Save(_ => this.$emit("finished")));
6464
}
6565
66+
67+
6668
registerRootEvent(eventName, callback) {
6769
this.$root.$on(eventName, callback);
6870
}

0 commit comments

Comments
 (0)