l7note
Version:
Access your notion notes quick
95 lines (77 loc) • 1.97 kB
text/typescript
import fs from 'fs';
import path from 'path';
import { configureSettings } from './configureSettings.js';
let userFolder;
if (process.env.APPDATA) {
userFolder = process.env.APPDATA;
} else if (process.platform == 'darwin') {
userFolder = process.env.HOME + '/Library/Preferences';
} else if (process.platform == 'android') {
userFolder = './';
} else {
userFolder = process.env.HOME + '/.local/share';
}
const settingsFolder = path.join(userFolder, 'l7Note');
const configPath = path.join(settingsFolder, 'config.json');
enum RunType {
LIST = 1,
ADD,
REMOVE,
CONFIG,
RESET,
HELP,
EXTRA,
}
type OptionalArgs = {
name: string;
value: string;
};
type GlobalConfig = {
configPath: string;
token: string;
dbId: string;
runType: RunType;
optionalArgs: OptionalArgs[];
};
let globalConfig: GlobalConfig = {
configPath: '',
token: '',
dbId: '',
runType: RunType.LIST,
optionalArgs: [],
};
const loadConfig = () => {
const myArgs = globalConfig.optionalArgs;
const runType = globalConfig.runType;
const content = fs.readFileSync(configPath, 'utf8');
globalConfig = JSON.parse(content);
globalConfig.configPath = configPath;
globalConfig.optionalArgs = myArgs;
globalConfig.runType = runType;
};
const saveConfig = () => {
fs.writeFileSync(configPath, JSON.stringify(globalConfig), 'utf-8');
};
const checkForFirstInstall = (): boolean => {
if (fs.existsSync(settingsFolder) == false) {
fs.mkdirSync(settingsFolder, { recursive: true });
return true;
}
if (fs.existsSync(configPath) == false) {
return true;
}
loadConfig();
if (!globalConfig.dbId || !globalConfig.token) {
return true;
}
return false;
};
const loadSetup = async () => {
if (globalConfig.runType == RunType.HELP) {
return;
}
if (checkForFirstInstall()) {
await configureSettings();
}
};
export { loadSetup, globalConfig, saveConfig, RunType };