Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 30, 2024
1 parent c859f05 commit e2bd09a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"imports": {
"@cross/env": "jsr:@cross/env@^1.0.0",
"@cross/fs": "jsr:@cross/fs@^0.0.7",
"@cross/fs": "jsr:@cross/fs@^0.0.8",
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@cross/utils": "jsr:@cross/utils@^0.8.2",
"@cross/utils": "jsr:@cross/utils@^0.9.1",
"@std/assert": "jsr:@std/assert@^0.221.0",
"@std/path": "jsr:@std/path@^0.221.0"
},
Expand Down
12 changes: 7 additions & 5 deletions lib/managers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import { exists, mktempdir, writeFile } from "@cross/fs";
import { dirname } from "@std/path";
import { resolvedExecPath } from "@cross/utils/execpath";
import { join } from "@std/path";
import type { InstallServiceOptions, UninstallServiceOptions } from "../service.ts";
import type { ServiceInstallResult, ServiceManualStep, ServiceUninstallResult } from "../result.ts";
Expand Down Expand Up @@ -66,11 +68,11 @@ class InitService {
* @param {InstallServiceOptions} config - Options for the installService function.
* @returns {string} - The configuration file content.
*/
generateConfig(config: InstallServiceOptions): string {
const denoPath = Deno.execPath();
async generateConfig(config: InstallServiceOptions): Promise<string> {
const runtimePath = await resolvedExecPath();
const runtimeDir = dirname(runtimePath);
const command = config.cmd;
const servicePath = `${config.path?.join(":")}:${denoPath}:${config.home}/.deno/bin`;

const servicePath = config.path?.length ? `${config.path?.join(":")}:${runtimeDir}` : runtimeDir;
let initScriptContent = initScriptTemplate.replace(/{{name}}/g, config.name);
initScriptContent = initScriptContent.replace("{{command}}", command);
initScriptContent = initScriptContent.replace("{{path}}", servicePath);
Expand All @@ -96,7 +98,7 @@ class InitService {
throw new Error(`Service '${config.name}' already exists in '${initScriptPath}'.`);
}

const initScriptContent = this.generateConfig(config);
const initScriptContent = await this.generateConfig(config);

if (onlyGenerate) {
return {
Expand Down
11 changes: 6 additions & 5 deletions lib/managers/launchd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { exists, mkdir, unlink, writeFile } from "@cross/fs";
import { dirname } from "@std/path";
import { cwd } from "@cross/utils";
import { cwd, resolvedExecPath } from "@cross/utils";
import type { ServiceInstallResult, ServiceManualStep, ServiceUninstallResult } from "../result.ts";
import type { InstallServiceOptions, UninstallServiceOptions } from "../service.ts";
const plistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -38,10 +38,11 @@ class LaunchdService {
* @param {InstallServiceOptions} options - The options used to generate the Launchd plist configuration file.
* @returns {string} The generated Launchd plist configuration file content as a string.
*/
generateConfig(options: InstallServiceOptions): string {
const denoPath = Deno.execPath();
async generateConfig(options: InstallServiceOptions): Promise<string> {
const runtimePath = await resolvedExecPath();
const runtimeDir = dirname(runtimePath);
const servicePath = options.path?.length ? `${options.path?.join(":")}:${runtimeDir}` : runtimeDir;
const commandArgs = options.cmd.split(" ");
const servicePath = `${options.path?.join(":")}:${denoPath}:${options.home}/.deno/bin`;
const workingDirectory = options.cwd ? options.cwd : cwd();

let plistContent = plistTemplate.replace(/{{name}}/g, options.name);
Expand Down Expand Up @@ -82,7 +83,7 @@ class LaunchdService {
throw new Error(`Service '${config.name}' already exists.`);
}

const plistContent = this.generateConfig(config);
const plistContent = await this.generateConfig(config);

if (onlyGenerate) {
return {
Expand Down
12 changes: 6 additions & 6 deletions lib/managers/systemd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { exists, mkdir, mktempdir, unlink, writeFile } from "@cross/fs";
import { dirname, join } from "@std/path";
import { cwd, spawn } from "@cross/utils";
import { cwd, resolvedExecPath, spawn } from "@cross/utils";
import type { ServiceInstallResult, ServiceManualStep, ServiceUninstallResult } from "../result.ts";
import type { InstallServiceOptions, UninstallServiceOptions } from "../service.ts";

Expand Down Expand Up @@ -63,7 +63,7 @@ class SystemdService {
}
}

const serviceFileContent = this.generateConfig(config);
const serviceFileContent = await this.generateConfig(config);

if (onlyGenerate) {
return {
Expand Down Expand Up @@ -177,10 +177,10 @@ class SystemdService {
* @param {InstallServiceOptions} options - The options used to generate the systemd service configuration file.
* @returns {string} The generated systemd service configuration file content as a string.
*/
generateConfig(options: InstallServiceOptions): string {
const denoPath = Deno.execPath();
const defaultPath = `PATH=${denoPath}:${options.home}/.deno/bin`;
const envPath = options.path ? `${defaultPath}:${options.path.join(":")}` : defaultPath;
async generateConfig(options: InstallServiceOptions): Promise<string> {
const runtimePath = await resolvedExecPath();
const runtimeDir = dirname(runtimePath);
const envPath = "PATH=" + (options.path?.length ? `${options.path?.join(":")}:${runtimeDir}` : runtimeDir);
const workingDirectory = options.cwd ? options.cwd : cwd();

let serviceFileContent = serviceFileTemplate.replace("{{name}}", options.name);
Expand Down
14 changes: 7 additions & 7 deletions lib/managers/upstart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { exists, mktempdir, unlink, writeFile } from "@cross/fs";
import { join } from "@std/path";
import { cwd } from "@cross/utils";
import { dirname, join } from "@std/path";
import { cwd, resolvedExecPath } from "@cross/utils";
import type { InstallServiceOptions, UninstallServiceOptions } from "../service.ts";
import type { ServiceInstallResult, ServiceManualStep, ServiceUninstallResult } from "../result.ts";

Expand Down Expand Up @@ -41,10 +41,10 @@ class UpstartService {
* @param config - The configuration options for the service.
* @returns The generated Upstart configuration file content.
*/
generateConfig(config: InstallServiceOptions): string {
const denoPath = Deno.execPath();
const defaultPath = `${denoPath}:${config.home}/.deno/bin`;
const envPath = config.path ? `${defaultPath}:${config.path.join(":")}` : defaultPath;
async generateConfig(config: InstallServiceOptions): Promise<string> {
const runtimePath = await resolvedExecPath();
const runtimeDir = dirname(runtimePath);
const envPath = config.path?.length ? `${config.path?.join(":")}:${runtimeDir}` : runtimeDir;
const workingDirectory = config.cwd ? config.cwd : cwd();

let upstartFileContent = upstartFileTemplate.replace(
Expand Down Expand Up @@ -88,7 +88,7 @@ class UpstartService {
throw new Error(`Service '${config.name}' already exists in '${upstartFilePath}'.`);
}

const upstartFileContent = this.generateConfig(config);
const upstartFileContent = await this.generateConfig(config);

if (onlyGenerate) {
return {
Expand Down
5 changes: 3 additions & 2 deletions lib/managers/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WindowsService {
throw new Error(`Service '${config.name}' already exists in '${serviceBatchPath}'.`);
}

const batchFileContent = this.generateConfig(config);
const batchFileContent = await this.generateConfig(config);

if (onlyGenerate) {
return {
Expand Down Expand Up @@ -127,7 +127,7 @@ class WindowsService {
* @param {InstallServiceOptions} options - The options used to generate the batch file.
* @returns {string} The generated batch file content as a string.
*/
generateConfig(options: InstallServiceOptions): string {
async generateConfig(options: InstallServiceOptions): Promise<string> {
const defaultPath = `%PATH%;`;
const envPath = options.path ? `${defaultPath};${options.path.join(";")}` : defaultPath;
const workingDirectory = options.cwd ? options.cwd : cwd();
Expand All @@ -144,6 +144,7 @@ class WindowsService {
}

batchFileContent += `"deno run -A --allow-ffi --unstable https://deno.land/x/[email protected]/run.ts --serviceName ${options.name} -- ${options.cmd}\n`;
await true;

return batchFileContent;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface UninstallServiceOptions {
interface ServiceManagerImplementation {
install(options: InstallServiceOptions, onlyGenerate: boolean): Promise<ServiceInstallResult>;
uninstall(options: UninstallServiceOptions): Promise<ServiceUninstallResult>;
generateConfig(options: InstallServiceOptions): string;
generateConfig(options: InstallServiceOptions): Promise<string>;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/managers/systemd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals, assertStringIncludes } from "@std/assert";
import { test } from "@cross/test";
import type { ServiceInstallResult } from "../../lib/result.ts";

test("generateConfig should create a valid service configuration", () => {
test("generateConfig should create a valid service configuration", async () => {
const options: InstallServiceOptions = {
name: "test-service",
cmd: "deno run --allow-net server.ts",
Expand All @@ -14,7 +14,7 @@ test("generateConfig should create a valid service configuration", () => {
path: ["/usr/local/bin"],
};
const systemdService = new SystemdService();
const generatedConfig = systemdService.generateConfig(options);
const generatedConfig = await systemdService.generateConfig(options);

assertStringIncludes(generatedConfig, "Description=test-service (Deno Service)");
assertStringIncludes(generatedConfig, 'ExecStart=/bin/sh -c "deno run --allow-net server.ts"');
Expand Down Expand Up @@ -48,7 +48,7 @@ test("install should create and display service configuration in user mode (dry-
assertStringIncludes(installResult.serviceFileContent, 'ExecStart=/bin/sh -c "deno run --allow-net server.ts"');
});

test("generateConfig should contain multi-user.target in system mode", () => {
test("generateConfig should contain multi-user.target in system mode", async () => {
const options: InstallServiceOptions = {
name: "test-service",
cmd: "deno run --allow-net server.ts",
Expand All @@ -58,7 +58,7 @@ test("generateConfig should contain multi-user.target in system mode", () => {
path: ["/usr/local/bin"],
};
const systemdService = new SystemdService();
const generatedConfig = systemdService.generateConfig(options);
const generatedConfig = await systemdService.generateConfig(options);

assertStringIncludes(generatedConfig, "WantedBy=multi-user.target");
});

0 comments on commit e2bd09a

Please sign in to comment.