|
| 1 | +import { CommandModule } from "yargs"; |
| 2 | +import * as chalk from "chalk"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as fs from "fs"; |
| 5 | +import { logger } from "../log"; |
| 6 | +import { directoryExists, findNodeCGDirectory, getNodeCGIODirectory } from "../fsUtils"; |
| 7 | +import { ProductionInstallation, readInstallInfo } from "../installation"; |
| 8 | +import { corePackages } from "../install/nodecgIOVersions"; |
| 9 | +import { GenerationOptions, promptGenerationOpts } from "./prompt"; |
| 10 | +import { manageBundleDir } from "../nodecgConfig"; |
| 11 | + |
| 12 | +export const yellowInstallCommand = chalk.yellow("nodecg-io install"); |
| 13 | +const yellowGenerateCommand = chalk.yellow("nodecg-io generate"); |
| 14 | + |
| 15 | +export const generateModule: CommandModule = { |
| 16 | + command: "generate", |
| 17 | + // FIXME rework description |
| 18 | + describe: "generates bundles that use nodecg-io services", |
| 19 | + |
| 20 | + handler: async () => { |
| 21 | + const nodecgDir = await findNodeCGDirectory(); |
| 22 | + logger.debug(`Detected nodecg installation at ${nodecgDir}.`); |
| 23 | + const nodecgIODir = getNodeCGIODirectory(nodecgDir); |
| 24 | + const install = await readInstallInfo(nodecgIODir); |
| 25 | + if (install === undefined) { |
| 26 | + logger.error("nodecg-io is not installed to your local nodecg install."); |
| 27 | + logger.error(`Please install it first using this command: ${yellowInstallCommand}`); |
| 28 | + process.exit(1); |
| 29 | + } else if (install.dev) { |
| 30 | + logger.error(`You cannot use ${yellowGenerateCommand} together with a development installation.`); |
| 31 | + process.exit(1); |
| 32 | + } else if (install.packages.length === corePackages.length) { |
| 33 | + // just has core packages without any services installed. |
| 34 | + logger.error(`You first need to have at least one service installed to generate a bundle.`); |
| 35 | + logger.error(`Please install a service using this command: ${yellowInstallCommand}`); |
| 36 | + process.exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + const opts = await promptGenerationOpts(nodecgDir, install); |
| 40 | + |
| 41 | + try { |
| 42 | + await generateBundle(nodecgDir, opts); |
| 43 | + } catch (e) { |
| 44 | + logger.error(`Couldn't generate bundle: ${e}`); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + logger.success(`Successfully generated bundle ${opts.bundleName}.`); |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +async function generateBundle(nodecgDir: string, opts: GenerationOptions): Promise<void> { |
| 53 | + // Create dir if necessary |
| 54 | + const bundlePath = path.join(opts.bundleDir, opts.bundleName); |
| 55 | + if (!(await directoryExists(bundlePath))) { |
| 56 | + await fs.promises.mkdir(bundlePath); |
| 57 | + } |
| 58 | + |
| 59 | + const filesInBundleDir = await fs.promises.readdir(bundlePath); |
| 60 | + if (filesInBundleDir.length > 0) { |
| 61 | + logger.error(`Directory for bundle at ${bundlePath} already exists and contains files.`); |
| 62 | + logger.error("Please make sure that you don't have a bundle with the same name already."); |
| 63 | + logger.error( |
| 64 | + `Also you cannot use this tool to add nodecg-io to a already existing bundle. It only supports generating new ones.`, |
| 65 | + ); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + await generatePackageJson(bundlePath, opts); |
| 70 | +} |
| 71 | + |
| 72 | +async function generatePackageJson(bundlePath: string, opts: GenerationOptions): Promise<void> { |
| 73 | + // This shouldn't happen... |
| 74 | + if (!opts.servicePackages) throw new Error("servicePackages undefined"); |
| 75 | + if (!opts.corePackage) throw new Error("corePackage undefined"); |
| 76 | + |
| 77 | + const serviceDeps = Object.fromEntries(opts.servicePackages.map((pkg) => [pkg.name, "^" + pkg.version])); |
| 78 | + const dependencies = { |
| 79 | + "@types/node": "^15.0.1", // TODO: maybe fetch newest version automatically |
| 80 | + nodecg: "^1.8.1", // TODO: get actual nodecg version, |
| 81 | + typescript: "^4.2.4", |
| 82 | + "nodecg-io-core": "^" + opts.corePackage.version, |
| 83 | + ...serviceDeps, |
| 84 | + }; |
| 85 | + |
| 86 | + const content = { |
| 87 | + name: opts.bundleName, |
| 88 | + version: opts.version, |
| 89 | + private: true, |
| 90 | + nodecg: { |
| 91 | + compatibleRange: "^1.4.0", |
| 92 | + bundleDependencies: serviceDeps, |
| 93 | + }, |
| 94 | + scripts: { |
| 95 | + build: "tsc -b", |
| 96 | + watch: "tsc -b -w", |
| 97 | + clean: "tsc -b --clean", |
| 98 | + }, |
| 99 | + dependencies, |
| 100 | + }; |
| 101 | + |
| 102 | + await write(content, bundlePath, "package.json"); |
| 103 | +} |
| 104 | + |
| 105 | +async function write(content: string | Record<string, unknown>, ...paths: string[]): Promise<void> { |
| 106 | + const finalPath = path.join(...paths); |
| 107 | + |
| 108 | + logger.debug(`Writing file at ${finalPath}`); |
| 109 | + |
| 110 | + // Create directory if missing |
| 111 | + const parent = path.dirname(finalPath); |
| 112 | + if (!(await directoryExists(parent))) { |
| 113 | + await fs.promises.mkdir(parent); |
| 114 | + } |
| 115 | + |
| 116 | + const str = typeof content === "string" ? content : JSON.stringify(content, null, 4); |
| 117 | + await fs.promises.writeFile(finalPath, str); |
| 118 | +} |
0 commit comments