|
| 1 | +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, ProductionInstallation, 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, yellowGenerateCommand, yellowInstallCommand } from "./utils"; |
| 14 | +import { genPackageJson } from "./packageJson"; |
| 15 | + |
| 16 | +export const generateModule: CommandModule = { |
| 17 | + command: "generate", |
| 18 | + describe: "generates nodecg bundles that use nodecg-io services", |
| 19 | + |
| 20 | + handler: async () => { |
| 21 | + try { |
| 22 | + const nodecgDir = await findNodeCGDirectory(); |
| 23 | + logger.debug(`Detected nodecg installation at ${nodecgDir}.`); |
| 24 | + const nodecgIODir = getNodeCGIODirectory(nodecgDir); |
| 25 | + const install = await readInstallInfo(nodecgIODir); |
| 26 | + |
| 27 | + // Will throw when install is not valid for generating bundles |
| 28 | + if (!ensureValidInstallation(install)) return; |
| 29 | + |
| 30 | + const opts = await promptGenerationOpts(nodecgDir, install); |
| 31 | + |
| 32 | + await generateBundle(nodecgDir, opts, install); |
| 33 | + |
| 34 | + logger.success(`Successfully generated bundle ${opts.bundleName}.`); |
| 35 | + } catch (e) { |
| 36 | + logger.error(`Couldn't generate bundle:\n${e.message ?? e.toString()}`); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Ensures that a installation can be used to generate bundles, meaning nodecg-io is actually installed, |
| 44 | + * is not a dev install and has some services installed that can be used. |
| 45 | + * Throws an error if the installation cannot be used to generate a bundle with an explanation. |
| 46 | + */ |
| 47 | +export function ensureValidInstallation(install: Installation | undefined): install is ProductionInstallation { |
| 48 | + if (install === undefined) { |
| 49 | + throw new Error( |
| 50 | + "nodecg-io is not installed to your local nodecg install.\n" + |
| 51 | + `Please install it first using this command: ${yellowInstallCommand}`, |
| 52 | + ); |
| 53 | + } else if (install.dev) { |
| 54 | + throw new Error(`You cannot use ${yellowGenerateCommand} together with a development installation.`); |
| 55 | + } else if (install.packages.length <= corePackages.length) { |
| 56 | + // just has core packages without any services installed. |
| 57 | + throw new Error( |
| 58 | + `You first need to have at least one service installed to generate a bundle.\n` + |
| 59 | + `Please install a service using this command: ${yellowInstallCommand}`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +export async function generateBundle( |
| 67 | + nodecgDir: string, |
| 68 | + opts: GenerationOptions, |
| 69 | + install: ProductionInstallation, |
| 70 | +): Promise<void> { |
| 71 | + // Create dir if necessary |
| 72 | + if (!(await directoryExists(opts.bundlePath))) { |
| 73 | + await fs.promises.mkdir(opts.bundlePath); |
| 74 | + } |
| 75 | + |
| 76 | + // In case some re-executes the command in a already used bundle name we should not overwrite their stuff and error instead. |
| 77 | + const filesInBundleDir = await fs.promises.readdir(opts.bundlePath); |
| 78 | + if (filesInBundleDir.length > 0) { |
| 79 | + throw new Error( |
| 80 | + `Directory for bundle at ${opts.bundlePath} already exists and contains files.\n` + |
| 81 | + "Please make sure that you don't have a bundle with the same name already.\n" + |
| 82 | + `Also you cannot use this tool to add nodecg-io to a already existing bundle. It only supports generating new ones.`, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + // All of these calls only generate files if they are set accordingly in the GenerationOptions |
| 87 | + await genPackageJson(nodecgDir, opts); |
| 88 | + await genTsConfig(opts); |
| 89 | + await genGitIgnore(opts); |
| 90 | + await genExtension(opts, install); |
| 91 | + await genGraphic(opts); |
| 92 | + await genDashboard(opts); |
| 93 | + logger.info("Generated bundle successfully."); |
| 94 | + |
| 95 | + logger.info("Installing dependencies..."); |
| 96 | + await runNpmInstall(opts.bundlePath, false); |
| 97 | + |
| 98 | + // JavaScript does not to be compiled |
| 99 | + if (opts.language === "typescript") { |
| 100 | + logger.info("Compiling bundle..."); |
| 101 | + await runNpmBuild(opts.bundlePath); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +async function genGitIgnore(opts: GenerationOptions): Promise<void> { |
| 106 | + // When typescript we want to ignore compilation results. |
| 107 | + const languageIgnoredFiles = opts.language === "typescript" ? ["/extension/*.js", "/extension/*.js.map"] : []; |
| 108 | + // Usual editors and node_modules directory |
| 109 | + const ignoreEntries = ["/node_modules/", "/.vscode/", "/.idea/", ...languageIgnoredFiles]; |
| 110 | + const content = ignoreEntries.join("\n"); |
| 111 | + await writeBundleFile(content, opts.bundlePath, ".gitignore"); |
| 112 | +} |
0 commit comments