Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 20c1345

Browse files
committed
feat: migrate to ES modules
1 parent df5e96b commit 20c1345

34 files changed

+207
-166
lines changed
File renamed without changes.

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
// but the entry-point needs to be executable so we can't have it in src/index.ts directly
66
// because the resulting file won't have the executable flag and you can't properly use it that way.
77

8-
require("./build/src/index");
8+
import "./build/index.js";

jest.config.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @type {import("ts-jest").JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest/presets/default-esm",
4+
testEnvironment: "node",
5+
// We don't want to test nodecg, and without including this jest fails because it includes a invalid json
6+
modulePathIgnorePatterns: ["/nodecg/"],
7+
testMatch: ["<rootDir>/test/**/*.ts", "!**/*.util.ts"],
8+
extensionsToTreatAsEsm: [".ts"],
9+
moduleNameMapper: {
10+
"^(\\.{1,2}/.*)\\.js$": "$1"
11+
},
12+
};

jest.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.3.1",
44
"description": "The CLI to install and manage nodecg-io installations. Also helps you with nodecg-io bundle related development.",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"build": "tsc -b",
89
"run": "tsc -b && node build/index.js",

src/generate/extension.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import CodeBlockWriter from "code-block-writer";
2-
import { getServiceClientName } from "../nodecgIOVersions";
3-
import { Installation } from "../utils/installation";
4-
import { CodeLanguage, GenerationOptions } from "./prompt";
5-
import { writeBundleFile } from "./utils";
2+
import { getServiceClientName } from "../nodecgIOVersions.js";
3+
import { Installation } from "../utils/installation.js";
4+
import { CodeLanguage, GenerationOptions } from "./prompt.js";
5+
import { writeBundleFile } from "./utils.js";
66

77
interface ServiceNames {
88
name: string;
@@ -39,6 +39,10 @@ export async function genExtension(opts: GenerationOptions, install: Installatio
3939
// the service names for each version are hardcoded and unknown for a development version.
4040
const services = install.dev === false ? opts.services.map((svc) => getServiceNames(svc, install.version)) : [];
4141

42+
// FIXME: Types and jest(running via node.js) require "new CodeBlockWriter()", but when running with node.js
43+
// this needs to be "new CodeBlockWriter.default()" to function, wtf?!.
44+
// Needs figuring out why this is happening.
45+
//@ts-ignore
4246
const writer = new CodeBlockWriter();
4347

4448
// imports

src/generate/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { CommandModule } from "yargs";
2-
import * as fs from "fs";
3-
import { logger } from "../utils/log";
4-
import { directoryExists } from "../utils/fs";
5-
import { Installation, readInstallInfo } from "../utils/installation";
6-
import { corePackages } from "../nodecgIOVersions";
7-
import { GenerationOptions, promptGenerationOpts } from "./prompt";
8-
import { runNpmBuild, runNpmInstall } from "../utils/npm";
9-
import { genExtension } from "./extension";
10-
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
11-
import { genDashboard, genGraphic } from "./panel";
12-
import { genTsConfig } from "./tsConfig";
13-
import { writeBundleFile, yellowInstallCommand } from "./utils";
14-
import { genPackageJson } from "./packageJson";
2+
import { promises as fs } from "fs";
3+
import { logger } from "../utils/log.js";
4+
import { directoryExists } from "../utils/fs.js";
5+
import { Installation, readInstallInfo } from "../utils/installation.js";
6+
import { corePackages } from "../nodecgIOVersions.js";
7+
import { GenerationOptions, promptGenerationOpts } from "./prompt.js";
8+
import { runNpmBuild, runNpmInstall } from "../utils/npm.js";
9+
import { genExtension } from "./extension.js";
10+
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";
11+
import { genDashboard, genGraphic } from "./panel.js";
12+
import { genTsConfig } from "./tsConfig.js";
13+
import { writeBundleFile, yellowInstallCommand } from "./utils.js";
14+
import { genPackageJson } from "./packageJson.js";
1515

1616
export const generateModule: CommandModule = {
1717
command: "generate",
@@ -64,11 +64,11 @@ export function ensureValidInstallation(install: Installation | undefined): inst
6464
export async function generateBundle(opts: GenerationOptions, install: Installation): Promise<void> {
6565
// Create dir if necessary
6666
if (!(await directoryExists(opts.bundlePath))) {
67-
await fs.promises.mkdir(opts.bundlePath);
67+
await fs.mkdir(opts.bundlePath);
6868
}
6969

7070
// In case some re-executes the command in a already used bundle name we should not overwrite their stuff and error instead.
71-
const filesInBundleDir = await fs.promises.readdir(opts.bundlePath);
71+
const filesInBundleDir = await fs.readdir(opts.bundlePath);
7272
if (filesInBundleDir.length > 0) {
7373
throw new Error(
7474
`Directory for bundle at ${opts.bundlePath} already exists and contains files.\n` +

src/generate/packageJson.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { GenerationOptions } from "./prompt";
2-
import { logger } from "../utils/log";
3-
import { getLatestPackageVersion } from "../utils/npm";
4-
import { genNodeCGDashboardConfig, genNodeCGGraphicConfig } from "./panel";
1+
import { GenerationOptions } from "./prompt.js";
2+
import { logger } from "../utils/log.js";
3+
import { getLatestPackageVersion } from "../utils/npm.js";
4+
import { genNodeCGDashboardConfig, genNodeCGGraphicConfig } from "./panel.js";
55
import { SemVer } from "semver";
6-
import { writeBundleFile } from "./utils";
7-
import { Installation } from "../utils/installation";
6+
import { writeBundleFile } from "./utils.js";
7+
import { Installation } from "../utils/installation.js";
88

99
// Loaction where the development tarballs are hosted.
1010
export const developmentPublishRootUrl = "https://codeoverflow-org.github.io/nodecg-io-publish/";

src/generate/panel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GenerationOptions } from "./prompt";
2-
import { writeBundleFile } from "./utils";
1+
import { GenerationOptions } from "./prompt.js";
2+
import { writeBundleFile } from "./utils.js";
33

44
type PanelType = "graphic" | "dashboard";
55

src/generate/prompt.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as semver from "semver";
2-
import * as inquirer from "inquirer";
1+
import semver from "semver";
2+
import inquirer from "inquirer";
33
import * as path from "path";
4-
import { directoryExists } from "../utils/fs";
5-
import { Installation } from "../utils/installation";
6-
import { getServicesFromInstall } from "../install/prompt";
7-
import { yellowInstallCommand } from "./utils";
8-
import { findNpmPackages, NpmPackage } from "../utils/npm";
9-
import { corePackage } from "../nodecgIOVersions";
10-
import { getNodeCGIODirectory } from "../utils/nodecgInstallation";
4+
import { directoryExists } from "../utils/fs.js";
5+
import { Installation } from "../utils/installation.js";
6+
import { getServicesFromInstall } from "../install/prompt.js";
7+
import { yellowInstallCommand } from "./utils.js";
8+
import { findNpmPackages, NpmPackage } from "../utils/npm.js";
9+
import { corePackage } from "../nodecgIOVersions.js";
10+
import { getNodeCGIODirectory } from "../utils/nodecgInstallation.js";
1111

1212
/**
1313
* Describes all options for bundle generation a user has answered with inside the inquirer prompt

src/generate/tsConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GenerationOptions } from "./prompt";
2-
import { writeBundleFile } from "./utils";
1+
import { GenerationOptions } from "./prompt.js";
2+
import { writeBundleFile } from "./utils.js";
33

44
/**
55
* Generates a tsconfig.json for a bundle if the language was set to typescript.

src/generate/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as path from "path";
2-
import { logger } from "../utils/log";
3-
import { directoryExists } from "../utils/fs";
4-
import * as fs from "fs";
5-
import * as chalk from "chalk";
2+
import { logger } from "../utils/log.js";
3+
import { directoryExists } from "../utils/fs.js";
4+
import { promises as fs } from "fs";
5+
import chalk from "chalk";
66

77
// Colored commands for logging purposes.
88
export const yellowInstallCommand = chalk.yellow("nodecg-io install");
@@ -20,9 +20,9 @@ export async function writeBundleFile(content: string | Record<string, unknown>,
2020
// Create directory if missing
2121
const parent = path.dirname(finalPath);
2222
if (!(await directoryExists(parent))) {
23-
await fs.promises.mkdir(parent);
23+
await fs.mkdir(parent);
2424
}
2525

2626
const str = typeof content === "string" ? content : JSON.stringify(content, null, 4);
27-
await fs.promises.writeFile(finalPath, str);
27+
await fs.writeFile(finalPath, str);
2828
}

src/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import yargs from "yargs";
2-
import { installModule } from "./install";
3-
import { uninstallModule } from "./uninstall";
4-
import { version } from "../package.json";
5-
import { checkForCliUpdate, ensureMinimumNodeVersion } from "./utils/cli";
6-
import { generateModule } from "./generate";
2+
import { installModule } from "./install/index.js";
3+
import { uninstallModule } from "./uninstall/index.js";
4+
import { checkForCliUpdate, ensureMinimumNodeVersion, getCliVersion } from "./utils/cli.js";
5+
import { generateModule } from "./generate/index.js";
76

87
// This file gets imported by the index.js file of the repository root.
98

10-
const args = yargs(process.argv.slice(2))
11-
.scriptName("nodecg-io")
12-
.usage("$0 <cmd> [args]")
13-
.version(version)
14-
.command(installModule)
15-
.command(uninstallModule)
16-
.command(generateModule)
17-
.option("disable-updates", { type: "boolean", description: "Disables check for nodecg-io-cli updates" })
18-
.strict()
19-
.demandCommand()
20-
.parserConfiguration({
21-
"dot-notation": false,
22-
})
23-
.parse();
24-
259
ensureMinimumNodeVersion();
2610
(async () => {
11+
const cliVersion = await getCliVersion();
12+
13+
const args = yargs(process.argv.slice(2))
14+
.scriptName("nodecg-io")
15+
.usage("$0 <cmd> [args]")
16+
.version(cliVersion)
17+
.command(installModule)
18+
.command(uninstallModule)
19+
.command(generateModule)
20+
.option("disable-updates", { type: "boolean", description: "Disables check for nodecg-io-cli updates" })
21+
.strict()
22+
.demandCommand()
23+
.parserConfiguration({
24+
"dot-notation": false,
25+
})
26+
.parse();
27+
28+
ensureMinimumNodeVersion();
29+
2730
const opts = await args;
2831
if (!opts["disable-updates"]) {
2932
checkForCliUpdate();

src/install/development.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as chalk from "chalk";
1+
import chalk from "chalk";
22
import * as git from "isomorphic-git";
33
import * as fs from "fs";
4-
import * as http from "isomorphic-git/http/node";
5-
import { directoryExists } from "../utils/fs";
6-
import { DevelopmentInstallation, writeInstallInfo } from "../utils/installation";
7-
import { logger } from "../utils/log";
4+
import * as http from "isomorphic-git/http/node/index.js";
5+
import { directoryExists } from "../utils/fs.js";
6+
import { DevelopmentInstallation, writeInstallInfo } from "../utils/installation.js";
7+
import { logger } from "../utils/log.js";
88
import * as path from "path";
9-
import * as glob from "glob";
10-
import { runNpmBuild, runNpmInstall } from "../utils/npm";
9+
import glob from "glob";
10+
import { runNpmBuild, runNpmInstall } from "../utils/npm.js";
1111

1212
type CloneRepository = "nodecg-io" | "nodecg-io-docs";
1313
const nodecgIOCloneURL = "https://github.com/codeoverflow-org/nodecg-io.git";

src/install/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { CommandModule } from "yargs";
22
import * as path from "path";
33
import * as fs from "fs";
4-
import { directoryExists } from "../utils/fs";
5-
import { createDevInstall } from "./development";
6-
import { manageBundleDir } from "../utils/nodecgConfig";
7-
import { promptForInstallInfo } from "./prompt";
8-
import { readInstallInfo } from "../utils/installation";
9-
import { createProductionInstall } from "./production";
10-
import { logger } from "../utils/log";
11-
import { requireNpmV7 } from "../utils/npm";
12-
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
4+
import { directoryExists } from "../utils/fs.js";
5+
import { createDevInstall } from "./development.js";
6+
import { manageBundleDir } from "../utils/nodecgConfig.js";
7+
import { promptForInstallInfo } from "./prompt.js";
8+
import { readInstallInfo } from "../utils/installation.js";
9+
import { createProductionInstall } from "./production.js";
10+
import { logger } from "../utils/log.js";
11+
import { requireNpmV7 } from "../utils/npm.js";
12+
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";
1313

1414
export interface InstallCommandOptions {
1515
"nodecg-io-version"?: string;

src/install/production.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ProductionInstallation, writeInstallInfo } from "../utils/installation";
1+
import { ProductionInstallation, writeInstallInfo } from "../utils/installation.js";
22
import {
33
NpmPackage,
44
removeNpmPackage,
@@ -8,12 +8,12 @@ import {
88
downloadNpmPackage,
99
createNpmSymlinks,
1010
getSubPackages,
11-
} from "../utils/npm";
12-
import { directoryExists, ensureDirectory } from "../utils/fs";
13-
import { logger } from "../utils/log";
11+
} from "../utils/npm.js";
12+
import { directoryExists, ensureDirectory } from "../utils/fs.js";
13+
import { logger } from "../utils/log.js";
1414
import { promises as fs } from "fs";
15-
import path = require("path");
16-
import chalk = require("chalk");
15+
import * as path from "path";
16+
import chalk from "chalk";
1717

1818
export async function createProductionInstall(
1919
requested: ProductionInstallation,

src/install/prompt.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Installation } from "../utils/installation";
2-
import * as inquirer from "inquirer";
3-
import { getHighestPatchVersion, getMinorVersions, NpmPackage } from "../utils/npm";
4-
import * as semver from "semver";
5-
import { logger } from "../utils/log";
1+
import { Installation } from "../utils/installation.js";
2+
import inquirer from "inquirer";
3+
import { getHighestPatchVersion, getMinorVersions, NpmPackage } from "../utils/npm.js";
4+
import semver from "semver";
5+
import { logger } from "../utils/log.js";
66
import {
77
corePackage,
88
corePackages,
@@ -11,8 +11,8 @@ import {
1111
developmentVersion,
1212
getServicesForVersion,
1313
supportedNodeCGIORange,
14-
} from "../nodecgIOVersions";
15-
import { InstallCommandOptions } from ".";
14+
} from "../nodecgIOVersions.js";
15+
import { InstallCommandOptions } from "./index.js";
1616

1717
interface PromptInput {
1818
version: string;

src/nodecgIOVersions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as semver from "semver";
1+
import semver from "semver";
22
import * as path from "path";
33

44
export const corePackage = "nodecg-io-core";

src/uninstall/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as path from "path";
2-
import * as fs from "fs";
2+
import { promises as fs } from "fs";
33
import { CommandModule } from "yargs";
4-
import { directoryExists } from "../utils/fs";
5-
import { logger } from "../utils/log";
6-
import { manageBundleDir } from "../utils/nodecgConfig";
7-
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
4+
import { directoryExists } from "../utils/fs.js";
5+
import { logger } from "../utils/log.js";
6+
import { manageBundleDir } from "../utils/nodecgConfig.js";
7+
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";
88

99
export const uninstallModule: CommandModule = {
1010
command: "uninstall",
@@ -37,7 +37,7 @@ export async function uninstall(): Promise<void> {
3737

3838
// Delete directory
3939
logger.debug(`Uninstalling nodecg-io from nodecg installation at ${nodecgDir}...`);
40-
await fs.promises.rm(nodecgIODir, { recursive: true, force: true });
40+
await fs.rm(nodecgIODir, { recursive: true, force: true });
4141

4242
logger.success("Successfully uninstalled nodecg-io.");
4343
}

0 commit comments

Comments
 (0)