Skip to content

Commit

Permalink
feat: save in .npm dir
Browse files Browse the repository at this point in the history
  • Loading branch information
golota60 committed May 22, 2022
1 parent 3711d1e commit 0ac7ca9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/helpers/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

const checkSaves = async () => {
try {
const existingSavesArr = await fs.readdir('./termi-clicker');
const existingSavesArr = await fs.readdir('../../../termi-clicker');
return existingSavesArr;
} catch (err) {
return undefined;
Expand All @@ -30,7 +30,7 @@ const createProfilePrompt = async () => {
};

export const loadGame = async (fileName: string) => {
const file = await fs.readFile(`./termi-clicker/${fileName}.json`, {
const file = await fs.readFile(`../../../termi-clicker/${fileName}.json`, {
encoding: 'utf-8',
});
return parse(file);
Expand Down Expand Up @@ -80,18 +80,21 @@ export const initGame = async (): Promise<{
};

const actualSave = async (data: GameState) => {
await fs.writeFile(`./termi-clicker/${data.name}.json`, stringify(data));
await fs.writeFile(
`../../../termi-clicker/${data.name}.json`,
stringify(data)
);
};

export const createDirAndSaveGame = async () => {
try {
await fs.readdir('./termi-clicker');
await fs.readdir('../../../termi-clicker');

await actualSave(gameState);
} catch (err: any) {
const isNoPrevSave = err.code === 'ENOENT';
if (isNoPrevSave) {
await fs.mkdir('./termi-clicker');
await fs.mkdir('../../../termi-clicker');

await actualSave(gameState);
} else {
Expand Down

0 comments on commit 0ac7ca9

Please sign in to comment.