Skip to content

Commit

Permalink
Creates required files if missing on launch.
Browse files Browse the repository at this point in the history
  • Loading branch information
zonble committed Mar 3, 2024
1 parent 9be73a3 commit 99d2ec2
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions src/pime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,22 @@ class PimeMcBopomofo {
this.inputController.reset();
}

readonly userDataPath: string = path.join(
readonly pimeUserDataPath: string = path.join(
process.env.APPDATA || "",
"PIME",
"PIME"
);

readonly mcBopomofoUserDataPath: string = path.join(
this.pimeUserDataPath,
"mcbopomofo"
);

readonly userPhrasesPath: string = path.join(this.userDataPath, "data.json");
readonly userPhrasesPath: string = path.join(
this.mcBopomofoUserDataPath,
"data.json"
);
readonly userSettingsPath: string = path.join(
this.userDataPath,
this.mcBopomofoUserDataPath,
"config.json"
);

Expand Down Expand Up @@ -184,10 +191,12 @@ class PimeMcBopomofo {
}

private addPhrase(key: string, phrase: string): void {
if (!fs.existsSync(this.userDataPath)) {
console.log("User data folder not found, creating " + this.userDataPath);
if (!fs.existsSync(this.mcBopomofoUserDataPath)) {
console.log(
"User data folder not found, creating " + this.mcBopomofoUserDataPath
);
console.log("Creating one");
fs.mkdirSync(this.userDataPath);
fs.mkdirSync(this.mcBopomofoUserDataPath);
}

fs.readFile(this.userPhrasesPath, (err, data) => {
Expand All @@ -204,10 +213,12 @@ class PimeMcBopomofo {
}

private writeUserPhrases(map: Map<string, string[]>): void {
if (!fs.existsSync(this.userDataPath)) {
console.log("User data folder not found, creating " + this.userDataPath);
if (!fs.existsSync(this.mcBopomofoUserDataPath)) {
console.log(
"User data folder not found, creating " + this.mcBopomofoUserDataPath
);
console.log("Creating one");
fs.mkdirSync(this.userDataPath);
fs.mkdirSync(this.mcBopomofoUserDataPath);
}

let string = "";
Expand Down Expand Up @@ -280,10 +291,12 @@ class PimeMcBopomofo {

/** Write settings to disk */
private writeSettings() {
if (!fs.existsSync(this.userDataPath)) {
console.log("User data folder not found, creating " + this.userDataPath);
if (!fs.existsSync(this.mcBopomofoUserDataPath)) {
console.log(
"User data folder not found, creating " + this.mcBopomofoUserDataPath
);
console.log("Creating one");
fs.mkdirSync(this.userDataPath);
fs.mkdirSync(this.mcBopomofoUserDataPath);
}

console.log("Writing user settings to " + this.userSettingsPath);
Expand Down Expand Up @@ -507,32 +520,41 @@ class PimeMcBopomofo {
}

const pimeMcBopomofo = new PimeMcBopomofo();
if (!fs.existsSync(pimeMcBopomofo.userDataPath)) {
fs.mkdirSync(pimeMcBopomofo.userDataPath);
}

if (!fs.existsSync(pimeMcBopomofo.userSettingsPath)) {
fs.writeFileSync(
pimeMcBopomofo.userSettingsPath,
JSON.stringify(defaultSettings)
);
}
try {
if (!fs.existsSync(pimeMcBopomofo.pimeUserDataPath)) {
fs.mkdirSync(pimeMcBopomofo.pimeUserDataPath);
}

fs.watch(pimeMcBopomofo.userSettingsPath, (event, filename) => {
if (filename) {
pimeMcBopomofo.loadSettings();
if (!fs.existsSync(pimeMcBopomofo.mcBopomofoUserDataPath)) {
fs.mkdirSync(pimeMcBopomofo.mcBopomofoUserDataPath);
}
});

if (!fs.existsSync(pimeMcBopomofo.userPhrasesPath)) {
fs.writeFileSync(pimeMcBopomofo.userSettingsPath, "");
}
if (!fs.existsSync(pimeMcBopomofo.userSettingsPath)) {
fs.writeFileSync(
pimeMcBopomofo.userSettingsPath,
JSON.stringify(defaultSettings)
);
}

fs.watch(pimeMcBopomofo.userPhrasesPath, (event, filename) => {
if (filename) {
pimeMcBopomofo.loadUserPhrases();
fs.watch(pimeMcBopomofo.userSettingsPath, (event, filename) => {
if (filename) {
pimeMcBopomofo.loadSettings();
}
});

if (!fs.existsSync(pimeMcBopomofo.userPhrasesPath)) {
fs.writeFileSync(pimeMcBopomofo.userPhrasesPath, "");
}
});

fs.watch(pimeMcBopomofo.userPhrasesPath, (event, filename) => {
if (filename) {
pimeMcBopomofo.loadUserPhrases();
}
});
} catch (e) {
console.error(e);
}

module.exports = {
textReducer(_: any, preState: any) {
Expand Down

0 comments on commit 99d2ec2

Please sign in to comment.