Clean up of launcher and added new Node and Electron functions
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const child_process = require('child_process');
|
||||
const { ipcRenderer } = require('electron');
|
||||
|
||||
// import psList from 'ps-list';
|
||||
// const ps_list = require('ps-list');
|
||||
|
||||
let home_directory = require('os').homedir();
|
||||
console.log('Home: '+home_directory);
|
||||
|
||||
@@ -77,13 +81,26 @@ exports.load_config = function () {
|
||||
//close();
|
||||
}
|
||||
|
||||
config.home_directory = home_directory; // From the OS platform
|
||||
config.tmp_directory = tmp_directory; // From the OS platform
|
||||
|
||||
config.host_file_cache_path = config.host_file_cache_path.replace('[home]', home_directory);
|
||||
config.host_file_cache_path = config.host_file_cache_path.replace('[tmp]', tmp_directory);
|
||||
console.log(config.host_file_cache_path);
|
||||
// if (fs.existsSync(config.host_file_cache_path)) {
|
||||
// } else {
|
||||
// fs.mkdirSync(config.host_file_cache_path);
|
||||
// console.log(`Host file cache directory created: ${config.host_file_cache_path}`);
|
||||
// }
|
||||
|
||||
config.host_file_temp_path = config.host_file_temp_path.replace('[home]', home_directory);
|
||||
config.host_file_temp_path = config.host_file_temp_path.replace('[tmp]', tmp_directory);
|
||||
console.log(config.host_file_temp_path);
|
||||
// if (fs.existsSync(config.host_file_temp_path)) {
|
||||
// } else {
|
||||
// fs.mkdirSync(config.host_file_temp_path);
|
||||
// console.log(`Host file temp directory created: ${config.host_file_temp_path}`);
|
||||
// }
|
||||
|
||||
let import_config_to_ipc_result = ipcRenderer.invoke('import_config', config).then((result) => {
|
||||
console.log('IPC import config finished');
|
||||
@@ -97,7 +114,7 @@ exports.load_config = function () {
|
||||
|
||||
|
||||
// Check for local file
|
||||
// Updated 2022-03-10
|
||||
// Updated 2022-05-06
|
||||
exports.check_local_file = async function ({local_file_path, filename}) {
|
||||
console.log('*** Electron framework export: check_local_file() ***');
|
||||
// console.log('Check for local file');
|
||||
@@ -116,7 +133,7 @@ exports.check_local_file = async function ({local_file_path, filename}) {
|
||||
|
||||
|
||||
// Check local hash file cache
|
||||
// Updated 2022-03-09
|
||||
// Updated 2022-05-06
|
||||
exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
|
||||
console.log('*** Electron framework export: check_hash_file_cache() ***');
|
||||
// console.log('Check local hash file cache');
|
||||
@@ -124,7 +141,15 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
|
||||
|
||||
let hash_filename = `${hash}.file`;
|
||||
|
||||
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
|
||||
let subdirectory = hash_filename.substring(0,2);
|
||||
let subdirectory_path = path.join(host_file_cache_path, subdirectory);
|
||||
if (fs.existsSync(subdirectory_path)) {
|
||||
} else {
|
||||
console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
let hash_file_cache_path = path.join(subdirectory_path, hash_filename);
|
||||
// console.log(hash_file_cache_path);
|
||||
|
||||
if (fs.existsSync(hash_file_cache_path)) {
|
||||
@@ -138,7 +163,7 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
|
||||
|
||||
|
||||
// Download hash file to cache
|
||||
// Updated 2022-03-09
|
||||
// Updated 2022-05-06
|
||||
exports.download_hash_file_to_cache = async function ({host_file_cache_path, event_file_id=null, hash=null}) {
|
||||
console.log('*** Electron framework export: download_hash_file_to_cache() ***');
|
||||
// console.log('Download hash file to cache');
|
||||
@@ -147,7 +172,16 @@ exports.download_hash_file_to_cache = async function ({host_file_cache_path, eve
|
||||
let endpoint = `/event/file/${event_file_id}/download`;
|
||||
|
||||
let hash_filename = `${hash}.file`;
|
||||
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
|
||||
|
||||
let subdirectory = hash_filename.substring(0,2);
|
||||
let subdirectory_path = path.join(host_file_cache_path, subdirectory);
|
||||
if (fs.existsSync(subdirectory_path)) {
|
||||
} else {
|
||||
fs.mkdirSync(subdirectory_path);
|
||||
console.log(`Subdirectory directory created: ${subdirectory_path}`);
|
||||
}
|
||||
|
||||
let hash_file_cache_path = path.join(subdirectory_path, hash_filename);
|
||||
// console.log(hash_file_cache_path);
|
||||
|
||||
let download_file_result = await ipcRenderer.invoke('download_file', api_base_url, endpoint, hash_file_cache_path).then((result) => {
|
||||
@@ -169,13 +203,21 @@ exports.download_hash_file_to_cache = async function ({host_file_cache_path, eve
|
||||
|
||||
|
||||
// Open cached hash file after copying to temp directory
|
||||
// Updated 2022-03-09
|
||||
// Updated 2022-05-06
|
||||
exports.open_hash_file_to_temp = async function ({host_file_cache_path, hash, host_file_temp_path, filename}) {
|
||||
console.log('*** Electron framework export: open_hash_file_to_temp() ***');
|
||||
// console.log('Open cached hash file after copying to temp directory');
|
||||
console.log(`Host File Cache Path: ${host_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`);
|
||||
|
||||
let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', host_file_cache_path, hash, host_file_temp_path, filename).then((result) => {
|
||||
let subdirectory = hash.substring(0,2);
|
||||
let subdirectory_path = path.join(host_file_cache_path, subdirectory);
|
||||
if (fs.existsSync(subdirectory_path)) {
|
||||
} else {
|
||||
console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', subdirectory_path, hash, host_file_temp_path, filename).then((result) => {
|
||||
console.log('IPC open hash file to temp finished');
|
||||
console.log(result);
|
||||
return true;
|
||||
@@ -384,7 +426,36 @@ exports.check_file_cache_and_open_local_file = async function ({host_file_cache_
|
||||
|
||||
return open_local_file_result;
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Kill processes
|
||||
// Updated 2022-05-06
|
||||
exports.kill_processes = async function ({process_name = null}) {
|
||||
console.log('*** Electron framework export: kill_processes() ***');
|
||||
console.log(process_name); // process_name or grep pattern
|
||||
|
||||
let command = `pkill ${process_name}`;
|
||||
child_process.exec(command, (err, stdout, stdin) => {
|
||||
if (err) throw err;
|
||||
console.log(stdout);
|
||||
});
|
||||
console.log('Killed processes?');
|
||||
|
||||
// let command = `ps -aux | grep ${process_name}`;
|
||||
// child_process.exec(command, (err, stdout, stdin) => {
|
||||
// if (err) throw err;
|
||||
// console.log(stdout);
|
||||
// });
|
||||
|
||||
// console.log(await psList());
|
||||
// console.log(await ps_list());
|
||||
|
||||
// let signal = 'SIGTERM'; // 'SIGTERM', 'SIGINT', 'SIGHUP'
|
||||
|
||||
// process.kill(pid, signal);
|
||||
|
||||
// process.kill(pid, 0); // Special case test if process exists
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user