Skip to content

Commit

Permalink
chore: convert project from commonjs to esm modules (#545)
Browse files Browse the repository at this point in the history
* fix e2e test. convert tsconfig module to nodenext for esm
  • Loading branch information
jgresham authored Apr 16, 2024
1 parent 4e86fe3 commit 9e29a7f
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 27 deletions.
4 changes: 3 additions & 1 deletion forge.config.ts → forge.config.cts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { FuseV1Options, FuseVersion } from '@electron/fuses';
import * as path from 'node:path';
import packageJson from './package.json';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const { version } = packageJson;

const iconDir = path.resolve(__dirname, 'assets', 'icons');
const iconDir = path.resolve('assets', 'icons');
console.log("forge.config.ts iconDir: ", iconDir);

const packagerConfig: ForgePackagerOptions = {
Expand Down
2 changes: 1 addition & 1 deletion forge.env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {}; // Make this a module
export type {}; // Make this a module

declare global {
// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Vite
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Run a node at home, the easy way.",
"homepage": "https://nicenode.xyz",
"productName": "NiceNode",
"type": "module",
"main": ".vite/build/main.js",
"scripts": {
"start": "electron-forge start",
Expand Down Expand Up @@ -75,7 +76,7 @@
"vite": "^5.2.8",
"vite-plugin-svgr": "^4.2.0",
"vitest": "^1.5.0",
"wdio-electron-service": "^6.3.1",
"wdio-electron-service": "^6.4.1",
"wdio-wait-for": "^3.0.11"
},
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/node/childProcess.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { type SpawnOptions, spawn } from 'node:child_process';
import sleep from 'await-sleep';
import { describe, expect, it } from 'vitest';
import * as url from 'node:url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

// setTimeout(10000);
describe('Nodejs process testing', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/main/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
spawn,
} from 'node:child_process';
import path from 'node:path';
import url from 'node:url';
import * as readline from 'node:readline';
import { Docker, Options } from 'docker-cli-js';

Expand All @@ -25,6 +26,9 @@ import * as dockerCompose from './docker-compose';
// stdin: undefined,
// };

export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));


const options = new Options(
undefined,
path.join(__dirname),
Expand Down
4 changes: 4 additions & 0 deletions src/main/files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { access, chmod, mkdir, readFile, rm } from 'node:fs/promises';
import path from 'node:path';
import url from 'node:url';
import checkDiskSpace from 'check-disk-space';

import { app } from 'electron';
Expand All @@ -12,6 +13,9 @@ logger.info(`App data dir: ${app.getPath('appData')}`);
logger.info(`User data dir: ${app.getPath('userData')}`);
logger.info(`logs dir: ${app.getPath('logs')}`);

export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));


export const getNNDirPath = (): string => {
// In packaged build...
// Linux: ~/.config/NiceNode
Expand Down
24 changes: 17 additions & 7 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `./src/main.js` using webpack. This gives us some performance wins.
*/
import path from 'node:path';
import url from 'node:url';
import * as Sentry from '@sentry/electron/main';
import dotenv from 'dotenv';
import { BrowserWindow, app, shell } from 'electron';
Expand Down Expand Up @@ -46,10 +47,11 @@ if (process.env.NODE_ENV === 'development') {
// require('wdio-electron-service/main');
// }

// todo: Turned off when switching to ESM modules. Do we need this?
// https://www.electronforge.io/config/makers/squirrel.windows#handling-startup-events
if (require('electron-squirrel-startup')) {
app.quit();
}
// if (require('electron-squirrel-startup')) {
// app.quit();
// }

// fixPathEnvVar();
logger.info(`NICENODE_ENV: ${process.env.NICENODE_ENV}`);
Expand Down Expand Up @@ -78,13 +80,21 @@ const isDevelopment =
// require('electron-debug')();
// }

export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

console.log('electron.app.getAppPath(): ', app.getAppPath());
const preloadPath = path.resolve(app.getAppPath(), '.vite/build/preload.js')
console.log('preloadPath:', preloadPath)

// __dirname = package.json dir or app.asar in build. works in dev and built app.
const RESOURCES_PATH = app.isPackaged
? path.join(__dirname)
: path.join(__dirname, '..', '..', 'assets'); // starting point: .vite/build/main.js
// electron.app.getAppPath() = ../../__dirname (I think .vite/build/__dirname)
// const RESOURCES_PATH = app.isPackaged
// ? path.join(__dirname)
// : path.join(__dirname, '..', '..', 'assets'); // starting point: .vite/build/main.js
const RESOURCES_PATH = __dirname

const getAssetPath = (...paths: string[]): string => {
logger.log('RESOURCES_PATH: ', RESOURCES_PATH);
logger.info('RESOURCES_PATH: ', RESOURCES_PATH);
return path.join(RESOURCES_PATH, ...paths);
};

Expand Down
3 changes: 3 additions & 0 deletions src/main/podman/podman-desktop/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import { spawn } from 'node:child_process';
import * as os from 'node:os';
import * as path from 'node:path';
import url from 'node:url';
import { getInstallationPath } from './podman-cli';

export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const windows = os.platform() === 'win32';
export function isWindows(): boolean {
return windows;
Expand Down
3 changes: 3 additions & 0 deletions src/main/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'node:path';
import { URL } from 'node:url';
import url from 'node:url';

export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export function resolveHtmlPath(htmlFileName: string) {
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
Expand Down
13 changes: 9 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"module": "nodenext",
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true,
"sourceMap": true,
"baseUrl": ".",
"outDir": "dist",
"moduleResolution": "node",
"moduleResolution": "nodenext",
"resolveJsonModule": true,

"jsx": "react-jsx"
"jsx": "react-jsx",

// not in vite template but was in previously
// "declaration": true,
Expand Down Expand Up @@ -43,7 +43,12 @@
// skip because github's macos-latest was checking lib types
// "skipLibCheck": true,
// todo: when using electron apis in e2e tests
// "types": ["@wdio/globals/types", "wdio-electron-service", "@wdio/types"],
"types": [
"@wdio/globals/types",
"wdio-electron-service",
"@wdio/types",
"mocha"
]
// "typeRoots": ["./node_modules", "./node_modules/@types", "./@types"]
},
// not in vite template but was in previously
Expand Down
4 changes: 2 additions & 2 deletions vite.main.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConfigEnv, UserConfig } from 'vite';
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, getBuildDefine, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, getBuildDefine, external, pluginHotRestart } from './vite.base.config.js';

console.log("vite.main.config.ts");

Expand All @@ -14,7 +14,7 @@ export default defineConfig((env) => {
lib: {
entry: forgeConfigSelf.entry!,
fileName: () => '[name].js',
formats: ['cjs'],
formats: ['es'],
},
rollupOptions: {
external,
Expand Down
2 changes: 1 addition & 1 deletion vite.preload.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConfigEnv, UserConfig } from 'vite';
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, external, pluginHotRestart } from './vite.base.config.js';

console.log("vite.preload.config.ts");

Expand Down
2 changes: 1 addition & 1 deletion vite.renderer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from 'vite';
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
import svgr from "vite-plugin-svgr";

import { pluginExposeRenderer } from './vite.base.config';
import { pluginExposeRenderer } from './vite.base.config.js';

console.log("vite.renderer.config.ts");

Expand Down
10 changes: 2 additions & 8 deletions wdio.conf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="wdio-electron-service" />
import type { Options } from '@wdio/types';
import path from 'node:path';
import process from 'node:process';

console.log("process.arch: ", process.arch);
const arch = process.arch || 'x64';
Expand All @@ -11,7 +12,7 @@ if (process.platform === 'darwin') {
} else if(process.platform === 'linux') {
appBinaryPath = `./out/NiceNode-linux-${arch}/nice-node`
} else {
appBinaryPath = path.join(__dirname, 'out', `NiceNode-win32-${arch}`, 'nice-node.exe')
appBinaryPath = path.join('out', `NiceNode-win32-${arch}`, 'nice-node.exe')
}

export const config: Options.Testrunner = {
Expand All @@ -21,13 +22,6 @@ export const config: Options.Testrunner = {
// ====================
// WebdriverIO supports running e2e tests as well as unit and component tests.
runner: 'local',
autoCompileOpts: {
autoCompile: true,
tsNodeOpts: {
project: './test/tsconfig.json',
transpileOnly: true,
},
},

//
// ==================
Expand Down

0 comments on commit 9e29a7f

Please sign in to comment.