Diskernet/args.js

140 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-11-03 04:51:04 +00:00
import os from 'os';
import path from 'path';
import fs from 'fs';
const server_port = process.env.PORT || process.argv[2] || 22120;
const mode = process.argv[3] || 'save';
const chrome_port = process.argv[4] || 9222;
const Pref = {};
const pref_file = path.resolve(os.homedir(), '.22120.config.json');
const cacheId = Math.random();
loadPref();
let BasePath = Pref.BasePath;
const archive_root = () => path.resolve(BasePath, '22120-arc');
const no_file = () => path.resolve(archive_root(), 'no.json');
const temp_browser_cache = () => path.resolve(archive_root(), 'temp-browser-cache' + cacheId);
const library_path = () => path.resolve(archive_root(), 'public', 'library');
const cache_file = () => path.resolve(library_path(), 'cache.json');
const index_file = () => path.resolve(library_path(), 'index.json');
2021-12-21 06:27:26 +00:00
const fts_index_dir = () => path.resolve(library_path(), 'fts');
const flex_fts_index_dir = base => path.resolve(base || fts_index_dir(), 'flex');
const ndx_fts_index_dir = base => path.resolve(base || fts_index_dir(), 'ndx');
const fuzzy_fts_index_dir = base => path.resolve(base || fts_index_dir(), 'fuzzy');
2021-12-16 14:04:19 +00:00
const results_per_page = 10;
2021-11-03 04:51:04 +00:00
console.log(`Args usage: <server_port> <save|serve> <chrome_port> <library_path>`);
updateBasePath(process.argv[5] || Pref.BasePath || os.homedir());
const args = {
mode,
server_port,
chrome_port,
updateBasePath,
getBasePath,
library_path,
no_file,
temp_browser_cache,
cache_file,
2021-12-16 05:15:19 +00:00
index_file,
2021-12-16 14:04:19 +00:00
fts_index_dir,
2021-12-21 06:27:26 +00:00
flex_fts_index_dir,
2021-12-18 04:13:10 +00:00
ndx_fts_index_dir,
fuzzy_fts_index_dir,
2021-12-16 14:04:19 +00:00
results_per_page
2021-11-03 04:51:04 +00:00
};
export default args;
function updateBasePath(new_base_path, {force:force = false} = {}) {
2021-11-03 04:51:04 +00:00
new_base_path = path.resolve(new_base_path);
if ( !force && (BasePath == new_base_path) ) {
2021-11-03 04:51:04 +00:00
return false;
}
console.log(`Updating base path from ${BasePath} to ${new_base_path}...`);
BasePath = new_base_path;
if ( !fs.existsSync(library_path()) ) {
console.log(`Archive directory (${library_path()}) does not exist, creating...`);
fs.mkdirSync(library_path(), {recursive:true});
console.log(`Created.`);
}
if ( !fs.existsSync(cache_file()) ) {
console.log(`Cache file does not exist, creating...`);
fs.writeFileSync(cache_file(), JSON.stringify([]));
console.log(`Created!`);
}
if ( !fs.existsSync(index_file()) ) {
console.log(`Index file does not exist, creating...`);
fs.writeFileSync(index_file(), JSON.stringify([]));
console.log(`Created!`);
}
2021-12-21 06:27:26 +00:00
if ( !fs.existsSync(flex_fts_index_dir()) ) {
console.log(`FTS Index directory does not exist, creating...`);
2021-12-21 06:27:26 +00:00
fs.mkdirSync(flex_fts_index_dir(), {recursive:true});
console.log(`Created!`);
}
2021-12-18 04:13:10 +00:00
if ( !fs.existsSync(ndx_fts_index_dir()) ) {
console.log(`NDX FTS Index directory does not exist, creating...`);
fs.mkdirSync(ndx_fts_index_dir(), {recursive:true});
console.log(`Created!`);
}
2021-12-21 05:07:43 +00:00
if ( !fs.existsSync(fuzzy_fts_index_dir()) ) {
console.log(`FUZZY FTS Index directory does not exist, creating...`);
fs.mkdirSync(fuzzy_fts_index_dir(), {recursive:true});
2021-12-21 06:27:26 +00:00
fs.writeFileSync(path.resolve(fuzzy_fts_index_dir(), 'docs.fzz'), JSON.stringify([]));
console.log('Also creating FUZZY FTS Index docs file...');
console.log(`Created all!`);
2021-12-21 05:07:43 +00:00
}
2021-12-21 06:27:26 +00:00
2021-11-03 04:51:04 +00:00
console.log(`Base path updated to: ${BasePath}. Saving to preferences...`);
Pref.BasePath = BasePath;
savePref();
console.log(`Saved!`);
return true;
}
function getBasePath() {
return BasePath;
}
function loadPref() {
if ( fs.existsSync(pref_file) ) {
try {
Object.assign(Pref, JSON.parse(fs.readFileSync(pref_file)));
} catch(e) {
console.warn("Error reading from preferences file", e);
}
} else {
console.log("Preferences file does not exist. Creating one...");
savePref();
}
}
function savePref() {
try {
fs.writeFileSync(pref_file, JSON.stringify(Pref,null,2));
} catch(e) {
console.warn("Error writing preferences file", pref_file, Pref, e);
}
}