diff --git a/src/Session.ts b/src/Session.ts index f8ddf3875..476fbcf9d 100644 --- a/src/Session.ts +++ b/src/Session.ts @@ -1,4 +1,4 @@ -import {readFileSync, writeFile} from "fs"; +import {readFileSync} from "fs"; import * as _ from "lodash"; import Job from "./Job"; import History from "./History"; @@ -8,15 +8,16 @@ import PluginManager from "./PluginManager"; import {Status} from "./Enums"; import ApplicationComponent from "./views/1_ApplicationComponent"; import Environment from "./Environment"; -import {homeDirectory, normalizeDirectory} from "./utils/Common"; +import {homeDirectory, normalizeDirectory, writeFileCreatingParents} from "./utils/Common"; import {remote} from "electron"; +import * as Path from "path"; export default class Session extends EmitterWithUniqueID { jobs: Array = []; readonly environment = new Environment(); history: typeof History; historicalCurrentDirectoriesStack: string[] = []; - private readonly stateFileName = `${homeDirectory()}/.black-screen-state`; + private readonly stateFileName = Path.join(homeDirectory(), ".black-screen", "state"); // The value of the dictionary is the default value used if there is no serialized data. private readonly serializableProperties: Dictionary = { directory: `String:${homeDirectory()}`, @@ -100,9 +101,10 @@ export default class Session extends EmitterWithUniqueID { values[key] = Serializer.serialize((this)[key]) ); - writeFile(this.stateFileName, JSON.stringify(values), (error: any) => { - if (error) throw error; - }); + writeFileCreatingParents(this.stateFileName, JSON.stringify(values)).then( + () => void 0, + (error: any) => { if (error) throw error; } + ); }; private deserialize(): void { diff --git a/src/utils/Common.ts b/src/utils/Common.ts index 29c78dd13..786a84bd8 100644 --- a/src/utils/Common.ts +++ b/src/utils/Common.ts @@ -75,6 +75,10 @@ export async function statsIn(directoryPath: string): Promise { })); } +export function mkdir(directoryPath: string): Promise<{}> { + return new Promise(resolve => fs.mkdir(directoryPath, resolve)); +} + export function exists(filePath: string): Promise { return new Promise(resolve => fs.exists(filePath, resolve)); } @@ -87,6 +91,32 @@ export async function isDirectory(directoryPath: string): Promise { } } +async function ensureDirectoryExists(filePath: string): Promise { + var directoryPath = Path.dirname(filePath); + if (await exists(directoryPath)) { + return; + } + await ensureDirectoryExists(directoryPath); + await mkdir(directoryPath); +} + +export async function writeFileCreatingParents(filePath: string, content: string): Promise<{}> { + await ensureDirectoryExists(filePath); + return writeFile(filePath, content); +} + +export function writeFile(filePath: string, content: string): Promise<{}> { + return new Promise((resolve, reject) => { + fs.writeFile(filePath, content, (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); +} + export function readFile(filePath: string): Promise { return new Promise((resolve, reject) => { fs.readFile(filePath, (error, buffer) => {