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

feat: migrate to ES modules #308

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// but the entry-point needs to be executable so we can't have it in src/index.ts directly
// because the resulting file won't have the executable flag and you can't properly use it that way.

require("./build/src/index");
import "./build/index.js";
12 changes: 12 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import("ts-jest").JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest/presets/default-esm",
testEnvironment: "node",
// We don't want to test nodecg, and without including this jest fails because it includes a invalid json
modulePathIgnorePatterns: ["/nodecg/"],
testMatch: ["<rootDir>/test/**/*.ts", "!**/*.util.ts"],
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
};
7 changes: 0 additions & 7 deletions jest.config.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.3.1",
"description": "The CLI to install and manage nodecg-io installations. Also helps you with nodecg-io bundle related development.",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsc -b",
"run": "tsc -b && node build/index.js",
Expand Down
14 changes: 9 additions & 5 deletions src/generate/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import CodeBlockWriter from "code-block-writer";
import { getServiceClientName } from "../nodecgIOVersions";
import { Installation } from "../utils/installation";
import { CodeLanguage, GenerationOptions } from "./prompt";
import { writeBundleFile } from "./utils";
import { getServiceClientName } from "../nodecgIOVersions.js";
import { Installation } from "../utils/installation.js";
import { CodeLanguage, GenerationOptions } from "./prompt.js";
import { writeBundleFile } from "./utils.js";

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

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

// imports
genImport(writer, "requireService", opts.corePackage.name, opts.language);
Expand Down
30 changes: 15 additions & 15 deletions src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { CommandModule } from "yargs";
import * as fs from "fs";
import { logger } from "../utils/log";
import { directoryExists } from "../utils/fs";
import { Installation, readInstallInfo } from "../utils/installation";
import { corePackages } from "../nodecgIOVersions";
import { GenerationOptions, promptGenerationOpts } from "./prompt";
import { runNpmBuild, runNpmInstall } from "../utils/npm";
import { genExtension } from "./extension";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
import { genDashboard, genGraphic } from "./panel";
import { genTsConfig } from "./tsConfig";
import { writeBundleFile, yellowInstallCommand } from "./utils";
import { genPackageJson } from "./packageJson";
import { promises as fs } from "fs";
import { logger } from "../utils/log.js";
import { directoryExists } from "../utils/fs.js";
import { Installation, readInstallInfo } from "../utils/installation.js";
import { corePackages } from "../nodecgIOVersions.js";
import { GenerationOptions, promptGenerationOpts } from "./prompt.js";
import { runNpmBuild, runNpmInstall } from "../utils/npm.js";
import { genExtension } from "./extension.js";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";
import { genDashboard, genGraphic } from "./panel.js";
import { genTsConfig } from "./tsConfig.js";
import { writeBundleFile, yellowInstallCommand } from "./utils.js";
import { genPackageJson } from "./packageJson.js";

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

// In case some re-executes the command in a already used bundle name we should not overwrite their stuff and error instead.
const filesInBundleDir = await fs.promises.readdir(opts.bundlePath);
const filesInBundleDir = await fs.readdir(opts.bundlePath);
if (filesInBundleDir.length > 0) {
throw new Error(
`Directory for bundle at ${opts.bundlePath} already exists and contains files.\n` +
Expand Down
12 changes: 6 additions & 6 deletions src/generate/packageJson.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GenerationOptions } from "./prompt";
import { logger } from "../utils/log";
import { getLatestPackageVersion } from "../utils/npm";
import { genNodeCGDashboardConfig, genNodeCGGraphicConfig } from "./panel";
import { GenerationOptions } from "./prompt.js";
import { logger } from "../utils/log.js";
import { getLatestPackageVersion } from "../utils/npm.js";
import { genNodeCGDashboardConfig, genNodeCGGraphicConfig } from "./panel.js";
import { SemVer } from "semver";
import { writeBundleFile } from "./utils";
import { Installation } from "../utils/installation";
import { writeBundleFile } from "./utils.js";
import { Installation } from "../utils/installation.js";

// Loaction where the development tarballs are hosted.
export const developmentPublishRootUrl = "https://codeoverflow-org.github.io/nodecg-io-publish/";
Expand Down
4 changes: 2 additions & 2 deletions src/generate/panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GenerationOptions } from "./prompt";
import { writeBundleFile } from "./utils";
import { GenerationOptions } from "./prompt.js";
import { writeBundleFile } from "./utils.js";

type PanelType = "graphic" | "dashboard";

Expand Down
18 changes: 9 additions & 9 deletions src/generate/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as semver from "semver";
import * as inquirer from "inquirer";
import semver from "semver";
import inquirer from "inquirer";
import * as path from "path";
import { directoryExists } from "../utils/fs";
import { Installation } from "../utils/installation";
import { getServicesFromInstall } from "../install/prompt";
import { yellowInstallCommand } from "./utils";
import { findNpmPackages, NpmPackage } from "../utils/npm";
import { corePackage } from "../nodecgIOVersions";
import { getNodeCGIODirectory } from "../utils/nodecgInstallation";
import { directoryExists } from "../utils/fs.js";
import { Installation } from "../utils/installation.js";
import { getServicesFromInstall } from "../install/prompt.js";
import { yellowInstallCommand } from "./utils.js";
import { findNpmPackages, NpmPackage } from "../utils/npm.js";
import { corePackage } from "../nodecgIOVersions.js";
import { getNodeCGIODirectory } from "../utils/nodecgInstallation.js";

/**
* Describes all options for bundle generation a user has answered with inside the inquirer prompt
Expand Down
4 changes: 2 additions & 2 deletions src/generate/tsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GenerationOptions } from "./prompt";
import { writeBundleFile } from "./utils";
import { GenerationOptions } from "./prompt.js";
import { writeBundleFile } from "./utils.js";

/**
* Generates a tsconfig.json for a bundle if the language was set to typescript.
Expand Down
12 changes: 6 additions & 6 deletions src/generate/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as path from "path";
import { logger } from "../utils/log";
import { directoryExists } from "../utils/fs";
import * as fs from "fs";
import * as chalk from "chalk";
import { logger } from "../utils/log.js";
import { directoryExists } from "../utils/fs.js";
import { promises as fs } from "fs";
import chalk from "chalk";

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

const str = typeof content === "string" ? content : JSON.stringify(content, null, 4);
await fs.promises.writeFile(finalPath, str);
await fs.writeFile(finalPath, str);
}
43 changes: 23 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import yargs from "yargs";
import { installModule } from "./install";
import { uninstallModule } from "./uninstall";
import { version } from "../package.json";
import { checkForCliUpdate, ensureMinimumNodeVersion } from "./utils/cli";
import { generateModule } from "./generate";
import { installModule } from "./install/index.js";
import { uninstallModule } from "./uninstall/index.js";
import { checkForCliUpdate, ensureMinimumNodeVersion, getCliVersion } from "./utils/cli.js";
import { generateModule } from "./generate/index.js";

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

const args = yargs(process.argv.slice(2))
.scriptName("nodecg-io")
.usage("$0 <cmd> [args]")
.version(version)
.command(installModule)
.command(uninstallModule)
.command(generateModule)
.option("disable-updates", { type: "boolean", description: "Disables check for nodecg-io-cli updates" })
.strict()
.demandCommand()
.parserConfiguration({
"dot-notation": false,
})
.parse();

ensureMinimumNodeVersion();
(async () => {
const cliVersion = await getCliVersion();

const args = yargs(process.argv.slice(2))
.scriptName("nodecg-io")
.usage("$0 <cmd> [args]")
.version(cliVersion)
.command(installModule)
.command(uninstallModule)
.command(generateModule)
.option("disable-updates", { type: "boolean", description: "Disables check for nodecg-io-cli updates" })
.strict()
.demandCommand()
.parserConfiguration({
"dot-notation": false,
})
.parse();

ensureMinimumNodeVersion();

const opts = await args;
if (!opts["disable-updates"]) {
checkForCliUpdate();
Expand Down
14 changes: 7 additions & 7 deletions src/install/development.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as chalk from "chalk";
import chalk from "chalk";
import * as git from "isomorphic-git";
import * as fs from "fs";
import * as http from "isomorphic-git/http/node";
import { directoryExists } from "../utils/fs";
import { DevelopmentInstallation, writeInstallInfo } from "../utils/installation";
import { logger } from "../utils/log";
import * as http from "isomorphic-git/http/node/index.js";
import { directoryExists } from "../utils/fs.js";
import { DevelopmentInstallation, writeInstallInfo } from "../utils/installation.js";
import { logger } from "../utils/log.js";
import * as path from "path";
import * as glob from "glob";
import { runNpmBuild, runNpmInstall } from "../utils/npm";
import glob from "glob";
import { runNpmBuild, runNpmInstall } from "../utils/npm.js";

type CloneRepository = "nodecg-io" | "nodecg-io-docs";
const nodecgIOCloneURL = "https://github.com/codeoverflow-org/nodecg-io.git";
Expand Down
18 changes: 9 additions & 9 deletions src/install/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { CommandModule } from "yargs";
import * as path from "path";
import * as fs from "fs";
import { directoryExists } from "../utils/fs";
import { createDevInstall } from "./development";
import { manageBundleDir } from "../utils/nodecgConfig";
import { promptForInstallInfo } from "./prompt";
import { readInstallInfo } from "../utils/installation";
import { createProductionInstall } from "./production";
import { logger } from "../utils/log";
import { requireNpmV7 } from "../utils/npm";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
import { directoryExists } from "../utils/fs.js";
import { createDevInstall } from "./development.js";
import { manageBundleDir } from "../utils/nodecgConfig.js";
import { promptForInstallInfo } from "./prompt.js";
import { readInstallInfo } from "../utils/installation.js";
import { createProductionInstall } from "./production.js";
import { logger } from "../utils/log.js";
import { requireNpmV7 } from "../utils/npm.js";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";

export interface InstallCommandOptions {
"nodecg-io-version"?: string;
Expand Down
12 changes: 6 additions & 6 deletions src/install/production.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductionInstallation, writeInstallInfo } from "../utils/installation";
import { ProductionInstallation, writeInstallInfo } from "../utils/installation.js";
import {
NpmPackage,
removeNpmPackage,
Expand All @@ -8,12 +8,12 @@ import {
downloadNpmPackage,
createNpmSymlinks,
getSubPackages,
} from "../utils/npm";
import { directoryExists, ensureDirectory } from "../utils/fs";
import { logger } from "../utils/log";
} from "../utils/npm.js";
import { directoryExists, ensureDirectory } from "../utils/fs.js";
import { logger } from "../utils/log.js";
import { promises as fs } from "fs";
import path = require("path");
import chalk = require("chalk");
import * as path from "path";
import chalk from "chalk";

export async function createProductionInstall(
requested: ProductionInstallation,
Expand Down
14 changes: 7 additions & 7 deletions src/install/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Installation } from "../utils/installation";
import * as inquirer from "inquirer";
import { getHighestPatchVersion, getMinorVersions, NpmPackage } from "../utils/npm";
import * as semver from "semver";
import { logger } from "../utils/log";
import { Installation } from "../utils/installation.js";
import inquirer from "inquirer";
import { getHighestPatchVersion, getMinorVersions, NpmPackage } from "../utils/npm.js";
import semver from "semver";
import { logger } from "../utils/log.js";
import {
corePackage,
corePackages,
Expand All @@ -11,8 +11,8 @@ import {
developmentVersion,
getServicesForVersion,
supportedNodeCGIORange,
} from "../nodecgIOVersions";
import { InstallCommandOptions } from ".";
} from "../nodecgIOVersions.js";
import { InstallCommandOptions } from "./index.js";

interface PromptInput {
version: string;
Expand Down
2 changes: 1 addition & 1 deletion src/nodecgIOVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as semver from "semver";
import semver from "semver";
import * as path from "path";

export const corePackage = "nodecg-io-core";
Expand Down
12 changes: 6 additions & 6 deletions src/uninstall/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from "path";
import * as fs from "fs";
import { promises as fs } from "fs";
import { CommandModule } from "yargs";
import { directoryExists } from "../utils/fs";
import { logger } from "../utils/log";
import { manageBundleDir } from "../utils/nodecgConfig";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation";
import { directoryExists } from "../utils/fs.js";
import { logger } from "../utils/log.js";
import { manageBundleDir } from "../utils/nodecgConfig.js";
import { findNodeCGDirectory, getNodeCGIODirectory } from "../utils/nodecgInstallation.js";

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

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

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