Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Serialize data to ~/.black-screen/.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed May 31, 2016
1 parent ac40365 commit ddc1c89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Session.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Job> = [];
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<string> = {
directory: `String:${homeDirectory()}`,
Expand Down Expand Up @@ -100,9 +101,10 @@ export default class Session extends EmitterWithUniqueID {
values[key] = Serializer.serialize((<any>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 {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export async function statsIn(directoryPath: string): Promise<i.FileInfo[]> {
}));
}

export function mkdir(directoryPath: string): Promise<{}> {
return new Promise(resolve => fs.mkdir(directoryPath, resolve));
}

export function exists(filePath: string): Promise<boolean> {
return new Promise(resolve => fs.exists(filePath, resolve));
}
Expand All @@ -87,6 +91,32 @@ export async function isDirectory(directoryPath: string): Promise<boolean> {
}
}

async function ensureDirectoryExists(filePath: string): Promise<void> {
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<string> {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (error, buffer) => {
Expand Down

0 comments on commit ddc1c89

Please sign in to comment.