Skip to content

Commit

Permalink
Merge pull request #269 from vitalygashkov/next
Browse files Browse the repository at this point in the history
Fixed storage saving
  • Loading branch information
vitalygashkov authored Dec 7, 2024
2 parents 490cc79 + d11b2bf commit 4660157
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/cli
Submodule cli updated from 0bfe75 to cac01b
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions packages/core/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { join } from 'node:path';
import { writeFileSync } from 'node:fs';
import { renameSync, unlinkSync, writeFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { fs, initDir } from './fs';
import { http } from './http';
import { getSettings } from './settings';
import { importCookies } from './cookies';
import { logger } from './log';

const createStorePath = (name: string) => {
// TODO: Make new store filename format
// const storePath = join(getSettings().servicesDir, `${name}.json`);
// const storePath = join(getSettings().servicesDir, `${name}.storage.json`);
// if (fs.exists(storePath)) return storePath;

// Old store support
const oldStoreDir = initDir(join(getSettings().servicesDir, name));
const oldStorePath = join(oldStoreDir, 'config.json');
const newStorePath = join(oldStoreDir, `${name}.storage.json`);
// Migrate from old store to new store
// if (fs.exists(oldStorePath)) {
// fs.readText(oldStorePath).then((data) => fs.writeText(storePath, data));
// }
return oldStorePath;
if (fs.exists(oldStorePath)) {
renameSync(oldStorePath, newStorePath);
unlinkSync(oldStorePath);
}
return newStorePath;
};

const exitQueue: (() => void)[] = [];

const onExit = () => {
logger.debug('Exiting...');
for (const fn of exitQueue) fn();
exitQueue.length = 0;
};
Expand All @@ -40,7 +44,7 @@ process.on('SIGTERM', () => {

// Catch uncaught exceptions
process.on('uncaughtException', () => {
onExit;
onExit();
process.exit(1);
});

Expand Down Expand Up @@ -186,6 +190,7 @@ export const createStorage = async (name: string) => {

export const createStore = (name: string) => {
const storePath = createStorePath(name);
logger.debug(`Store path: ${storePath}`);
const state = {} as Record<string, any>;
const getState = async <T = any>(cookiesKey: string | null = 'cookies') => {
const data = (await fs.readJson<any>(storePath).catch(() => {})) || {};
Expand Down

0 comments on commit 4660157

Please sign in to comment.