Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to module #106

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
fail-fast: false
matrix:
os: [macOS-latest, ubuntu-latest, windows-latest]
node: ["16", "18", "20", "22"]
node: ["20", "22"]

runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.vscode/
.vscode/*
!.vscode/extensions.json
dist/
node_modules
npm-debug.log*
Expand Down
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint"]
}
50 changes: 50 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import globals from "globals";

import js from "@eslint/js";

import babelParser from "@babel/eslint-parser";

export default [
js.configs.recommended,

{
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: {
...globals.browser,
...globals.node,

// TODO: remove when moved to ES modules in client
$: "readonly",
CodeMirror: "readonly",
Mousetrap: "readonly",
Handlebars: "readonly",
_: "readonly",
fileExtension: "readonly",
screenfull: "readonly",
Uppie: "readonly",
PhotoSwipe: "readonly",
PhotoSwipeUI_Default: "readonly",
pdfjsLib: "readonly",
Plyr: "readonly",
},
parser: babelParser,
parserOptions: {
requireConfigFile: false,
babelOptions: {
babelrc: false,
configFile: false,
plugins: ["@babel/plugin-syntax-import-assertions"],
},
},
},
rules: {
"no-unused-vars": "warn",
"no-undef": "warn",
},
},
{
ignores: ["packages/svgstore/tests"],
},
];
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@
"yazl": "2.5.1"
},
"devDependencies": {
"@babel/core": "^7",
"@babel/eslint-parser": "^7.25.1",
"@babel/plugin-syntax-import-assertions": "^7.25.6",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.11.1",
"babel-jest": "^27.1.1",
"clean-css": "5.1.5",
"eslint": "7.32.0",
"eslint-config-silverwind": "18.0.2",
"eslint-plugin-unicorn": "21.0.0",
"eslint": "9.11.1",
"eslint-config-standard": "^17.1.0",
"globals": "^15.9.0",
"html-minifier": "4.0.0",
"husky": "^7.0.2",
"jest": "^27.1.1",
Expand Down Expand Up @@ -93,7 +98,7 @@
],
"browserslist": "defaults",
"volta": {
"node": "18.20.4",
"node": "20.17.0",
"yarn": "1.22.10"
},
"lint-staged": {
Expand Down
94 changes: 57 additions & 37 deletions packages/cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

"use strict";

const fs = require("fs");
const untildify = require("untildify");
const path = require("path");
const util = require("util");
import fs from "fs";
import untildify from "untildify";
import path from "path";
import util from "util";
import { fileURLToPath } from "url";

const pkg = require("../package.json");
import pkg from "../package.json" with { type: "json" };

const {server, paths, resources, log, cfg, db} = require("@droppyjs/server");
import { server, paths, resources, log, cfg, db } from "@droppyjs/server";

import minimist from "minimist";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

util.inspect.defaultOptions.depth = 4;

const argv = require("minimist")(process.argv.slice(2), {
boolean: ["color", "d", "daemon", "dev"]
const argv = minimist(process.argv.slice(2), {
boolean: ["color", "d", "daemon", "dev"],
});

if (!argv.dev) {
Expand All @@ -36,7 +42,8 @@ const cmds = {
};

const opts = {
configdir: "-c, --configdir <dir> Config directory. Default: ~/.droppy/config",
configdir:
"-c, --configdir <dir> Config directory. Default: ~/.droppy/config",
filesdir: "-f, --filesdir <dir> Files directory. Default: ~/.droppy/files",
daemon: "-d, --daemon Daemonize (background) process",
log: "-l, --log <file> Log to file instead of stdout",
Expand All @@ -60,7 +67,9 @@ if (argv.configdir || argv.filesdir || argv.c || argv.f) {

if (argv.log || argv.l) {
try {
log.setLogFile(fs.openSync(untildify(path.resolve(argv.log || argv.l)), "a", "644"));
log.setLogFile(
fs.openSync(untildify(path.resolve(argv.log || argv.l)), "a", "644")
);
} catch (err) {
console.error(`Unable to open log file for writing: ${err.message}`);
process.exit(1);
Expand All @@ -81,7 +90,7 @@ switch (cmd) {
break;

case "start":
server(null, true, argv.dev, err => {
server(null, true, argv.dev, (err) => {
if (err) {
log.error(err);
process.exit(1);
Expand All @@ -91,28 +100,30 @@ switch (cmd) {

case "stop": {
const ps = require("ps-node");
ps.lookup({command: pkg.name}, async (err, procs) => {
ps.lookup({ command: pkg.name }, async (err, procs) => {
if (err) {
log.error(err);
process.exit(1);
} else {
procs = procs.filter(proc => Number(proc.pid) !== process.pid);
procs = procs.filter((proc) => Number(proc.pid) !== process.pid);
if (!procs.length) {
log.info("No processes found");
process.exit(0);
}

const pids = await Promise.all(procs.map(proc => {
return new Promise(resolve => {
ps.kill(proc.pid, err => {
if (err) {
log.error(err);
return process.exit(1);
}
resolve(proc.pid);
const pids = await Promise.all(
procs.map((proc) => {
return new Promise((resolve) => {
ps.kill(proc.pid, (err) => {
if (err) {
log.error(err);
return process.exit(1);
}
resolve(proc.pid);
});
});
});
}));
})
);

if (pids.length) {
console.info(`Killed PIDs: ${pids.join(", ")}`);
Expand All @@ -125,7 +136,7 @@ switch (cmd) {

case "build":
console.info("Building resources ...");
resources.build(err => {
resources.build((err) => {
console.info(err || "Resources built successfully");
process.exit(err ? 1 : 0);
});
Expand All @@ -138,14 +149,19 @@ switch (cmd) {
case "config": {
const ourPaths = paths.get();
const edit = () => {
findEditor(editor => {
if (!editor) return console.error(`No suitable editor found, please edit ${ourPaths.cfgFile}`);
require("child_process").spawn(editor, [ourPaths.cfgFile], {stdio: "inherit"});
findEditor((editor) => {
if (!editor)
return console.error(
`No suitable editor found, please edit ${ourPaths.cfgFile}`
);
require("child_process").spawn(editor, [ourPaths.cfgFile], {
stdio: "inherit",
});
});
};
fs.stat(ourPaths.cfgFile, err => {
fs.stat(ourPaths.cfgFile, (err) => {
if (err && err.code === "ENOENT") {
fs.mkdir(ourPaths.config, {recursive: true}, async () => {
fs.mkdir(ourPaths.config, { recursive: true }, async () => {
try {
await cfg.init(null);
edit();
Expand Down Expand Up @@ -193,13 +209,13 @@ switch (cmd) {
function printHelp() {
let help = `Usage: ${pkg.name} command [options]\n\n Commands:`;

Object.keys(cmds).forEach(command => {
Object.keys(cmds).forEach((command) => {
help += `\n ${cmds[command]}`;
});

help += "\n\n Options:";

Object.keys(opts).forEach(option => {
Object.keys(opts).forEach((option) => {
help += `\n ${opts[option]}`;
});

Expand All @@ -211,16 +227,20 @@ function printUsers(users) {
if (Object.keys(users).length === 0) {
console.info("No users defined. Use 'add' to add one.");
} else {
console.info(`Current Users:\n${Object.keys(users).map(user => {
return ` - ${user}`;
}).join("\n")}`);
console.info(
`Current Users:\n${Object.keys(users)
.map((user) => {
return ` - ${user}`;
})
.join("\n")}`
);
}
}

function findEditor(cb) {
const editors = ["vim", "nano", "vi", "npp", "pico", "emacs", "notepad"];
const basename = require("path").basename;
const which = require("which");
const editors = ["vim", "nano", "vi", "npp", "pico", "emacs", "notepad"];
const basename = require("path").basename;
const which = require("which");
const userEditor = basename(process.env.VISUAL || process.env.EDITOR);

if (!editors.includes(userEditor)) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"homepage": "https://github.com/droppyjs/droppy#readme",
"license": "BSD-2-Clause",
"main": "lib/cli.js",
"type": "module",
"dependencies": {
"@droppyjs/server": "1.0.1",
"daemonize-process": "^3.0.0",
Expand Down
Loading
Loading