Skip to content

Commit

Permalink
convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Apr 1, 2024
1 parent 0b40d3e commit 4c1a6ff
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 45 deletions.
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "module",
"dependencies": {
"simple-node-logger": "^21.8.12",
"base64-js": "^1.5.1",
Expand Down
9 changes: 9 additions & 0 deletions app/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// experimental
//import config from './config.json' with { type: "json" };

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
import fs from 'fs';
const config = JSON.parse(fs.readFileSync(require.resolve('./config.json'), 'utf8'));

export default config;
15 changes: 7 additions & 8 deletions app/src/converter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import open from 'open';
import os from "os";
import path from "path";
import fs from "node:fs";

const os = require("os");
const path = require('path');
const fs = require("node:fs");

const logger = require('./logger');
const rpc = require('./weh-rpc');
import logger from "./logger.js";
import rpc from "./weh-rpc.js";

const exec_dir = path.dirname(process.execPath);

Expand Down Expand Up @@ -99,7 +98,7 @@ function ExecConverter(args) {
});
}

exports.star_listening = () => {
export const star_listening = () => {

const convertChildren = new Map();

Expand Down Expand Up @@ -334,7 +333,7 @@ exports.star_listening = () => {
});
};

exports.info = () => {
export const info = () => {
return new Promise((resolve, reject) => {
let convProcess = spawn(ffmpeg, ["-h"]);
let done = false;
Expand Down
10 changes: 5 additions & 5 deletions app/src/downloads.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path');
const fs = require('node:fs');
const rpc = require('./weh-rpc');
const os = require("os");
const got = require('got');
import path from "path";
import fs from "node:fs";
import rpc from "./weh-rpc.js";
import os from "os";
import got from "got";

let downloadFolder = path.join(os.homedir(), "dwhelper");

Expand Down
17 changes: 8 additions & 9 deletions app/src/logger.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
let simplelogger = require("simple-node-logger");
import * as simplelogger from "simple-node-logger";

let logfile = process.env.WEH_NATIVE_LOGFILE;
const logfile = process.env.WEH_NATIVE_LOGFILE;

if (!logfile) {
module.exports = {
export default !logfile ? (
{
info: () => {},
error: () => {},
warn: () => {},
log: () => {},
};
} else {
let logger = simplelogger.createSimpleFileLogger(logfile);
module.exports = logger;
}
}
) : (
simplelogger.createSimpleFileLogger(logfile);
);
31 changes: 19 additions & 12 deletions app/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const config = require('config.json');
const converter = require('./converter');
const os = require("os");
import config from './config.js';

import os from 'os';

let converter = null;

function info() {
let result = {
Expand All @@ -27,9 +29,9 @@ function info() {
}

if (process.argv[2] == "install") {
require("./native-autoinstall").install();
(await import("./native-autoinstall.js")).install();
} else if (process.argv[2] == "uninstall") {
require("./native-autoinstall").uninstall();
(await import("./native-autoinstall.js")).uninstall();
} else if (process.argv[2] == "--version") {
console.log(config.meta.version);
} else if (process.argv[2] == "--info") {
Expand All @@ -54,17 +56,22 @@ Options:
console.log(help);
} else {

require('./native-messaging');
const logger = require('./logger');
const rpc = require('./weh-rpc');
// note: this disables the exception handler
// so exceptions will silently fail
// for (let e of ["exit", "SIGINT", "SIGTERM", "uncaughtException"]) {
converter = await import('./converter.js');

await import('./native-messaging.js');
const logger = await import('./logger.js');
const rpc = await import('./weh-rpc.js');

rpc.setLogger(logger);
rpc.setDebugLevel(2);

require('./file');
require('./downloads');
require('./request');
require('./vm');
await import('./file.js');
await import('./downloads.js');
await import('./request.js');
await import('./vm.js');

converter.star_listening();

Expand Down
23 changes: 13 additions & 10 deletions app/src/native-autoinstall.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const os = require("os");
const path = require("path");
const { spawn, exec } = require('child_process');
const config = require('config.json');
import os from "os";
import path from "path";
import { spawn, exec } from "child_process";
import config from "./config.js";

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

let fs;
if (process.versions.node.startsWith("10")) {
fs = require('fs').promises;
fs = await require('fs').promises;
} else {
fs = require('node:fs/promises');
fs = await require('node:fs/promises');
}

const STORES = Object.keys(config.store);
Expand Down Expand Up @@ -170,12 +173,12 @@ async function install_uninstall(uninstall = false) {
}
}

exports.install = () => {
export const install = async () => {
console.log("Installing…");
install_uninstall();
await install_uninstall();
};

exports.uninstall = () => {
export const uninstall = async () => {
console.log("Uninstalling…");
install_uninstall(true);
await install_uninstall(true);
};
2 changes: 1 addition & 1 deletion app/src/weh-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ class RPC {

}

module.exports = new RPC();
export default new RPC();

0 comments on commit 4c1a6ff

Please sign in to comment.