|
1 | 1 | import { CommandModule } from "yargs"; |
2 | | -import * as chalk from "chalk"; |
3 | | -import * as path from "path"; |
4 | 2 | import * as fs from "fs"; |
5 | 3 | import { logger } from "../utils/log"; |
6 | 4 | import { directoryExists } from "../utils/fs"; |
7 | 5 | import { Installation, ProductionInstallation, readInstallInfo } from "../utils/installation"; |
8 | 6 | import { corePackages } from "../nodecgIOVersions"; |
9 | 7 | import { GenerationOptions, promptGenerationOpts } from "./prompt"; |
10 | | -import { getLatestPackageVersion, runNpmBuild, runNpmInstall } from "../utils/npm"; |
| 8 | +import { runNpmBuild, runNpmInstall } from "../utils/npm"; |
11 | 9 | import { genExtension } from "./extension"; |
12 | | -import { findNodeCGDirectory, getNodeCGIODirectory, getNodeCGVersion } from "../utils/nodecgInstallation"; |
13 | | -import { SemVer } from "semver"; |
14 | | -import { genDashboard, genGraphic, genNodeCGDashboardConfig, genNodeCGGraphicConfig } from "./panel"; |
15 | | - |
16 | | -const defaultTsConfigJson = { |
17 | | - compilerOptions: { |
18 | | - target: "es2019", |
19 | | - sourceMap: true, |
20 | | - lib: ["es2019"], |
21 | | - alwaysStrict: true, |
22 | | - forceConsistentCasingInFileNames: true, |
23 | | - noFallthroughCasesInSwitch: true, |
24 | | - noImplicitAny: true, |
25 | | - noImplicitReturns: true, |
26 | | - noImplicitThis: true, |
27 | | - strictNullChecks: true, |
28 | | - skipLibCheck: true, |
29 | | - module: "CommonJS", |
30 | | - types: ["node"], |
31 | | - }, |
32 | | -}; |
33 | | - |
34 | | -export const yellowInstallCommand = chalk.yellow("nodecg-io install"); |
35 | | -const yellowGenerateCommand = chalk.yellow("nodecg-io generate"); |
| 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"; |
36 | 15 |
|
37 | 16 | export const generateModule: CommandModule = { |
38 | 17 | command: "generate", |
@@ -123,85 +102,9 @@ export async function generateBundle( |
123 | 102 | } |
124 | 103 | } |
125 | 104 |
|
126 | | -async function genPackageJson(nodecgDir: string, opts: GenerationOptions): Promise<void> { |
127 | | - const serviceDeps: [string, string][] = opts.servicePackages.map((pkg) => [pkg.name, addSemverCaret(pkg.version)]); |
128 | | - |
129 | | - const dependencies: [string, string][] = [["nodecg-io-core", addSemverCaret(opts.corePackage.version)]]; |
130 | | - |
131 | | - // When we use JS we only need core for requireService etc. and if we TS we also need nodecg, ts, types for node and |
132 | | - // each service for typings. |
133 | | - if (opts.language === "typescript") { |
134 | | - dependencies.push(...serviceDeps); |
135 | | - |
136 | | - logger.debug("Fetching latest typescript and @types/node versions..."); |
137 | | - const [nodecgVersion, latestNodeTypes, latestTypeScript] = await Promise.all([ |
138 | | - getNodeCGVersion(nodecgDir), |
139 | | - getLatestPackageVersion("@types/node"), |
140 | | - getLatestPackageVersion("typescript"), |
141 | | - ]); |
142 | | - |
143 | | - dependencies.push( |
144 | | - ["nodecg", addSemverCaret(nodecgVersion)], |
145 | | - ["@types/node", addSemverCaret(latestNodeTypes)], |
146 | | - ["typescript", addSemverCaret(latestTypeScript)], |
147 | | - ); |
148 | | - dependencies.sort(); |
149 | | - } |
150 | | - |
151 | | - const content = { |
152 | | - name: opts.bundleName, |
153 | | - version: opts.version.version, |
154 | | - private: true, |
155 | | - nodecg: { |
156 | | - compatibleRange: addSemverCaret("1.4.0"), |
157 | | - bundleDependencies: Object.fromEntries(serviceDeps), |
158 | | - graphics: genNodeCGGraphicConfig(opts), |
159 | | - dashboardPanels: genNodeCGDashboardConfig(opts), |
160 | | - }, |
161 | | - // These scripts are for compiling TS and thus are only needed when generating a TS bundle |
162 | | - scripts: |
163 | | - opts.language === "typescript" |
164 | | - ? { |
165 | | - build: "tsc -b", |
166 | | - watch: "tsc -b -w", |
167 | | - clean: "tsc -b --clean", |
168 | | - } |
169 | | - : undefined, |
170 | | - dependencies: Object.fromEntries(dependencies), |
171 | | - }; |
172 | | - |
173 | | - await write(content, opts.bundlePath, "package.json"); |
174 | | -} |
175 | | - |
176 | | -function addSemverCaret(version: string | SemVer): string { |
177 | | - return `^${version}`; |
178 | | -} |
179 | | - |
180 | | -async function genTsConfig(opts: GenerationOptions): Promise<void> { |
181 | | - // Only TS needs its tsconfig.json compiler configuration |
182 | | - if (opts.language === "typescript") { |
183 | | - await write(defaultTsConfigJson, opts.bundlePath, "tsconfig.json"); |
184 | | - } |
185 | | -} |
186 | | - |
187 | 105 | async function genGitIgnore(opts: GenerationOptions): Promise<void> { |
188 | 106 | const languageIgnoredFiles = opts.language === "typescript" ? ["/extension/*.js", "/extension/*.js.map"] : []; |
189 | 107 | const ignoreEntries = ["/node_modules/", "/.vscode/", "/.idea/", ...languageIgnoredFiles]; |
190 | 108 | const content = ignoreEntries.join("\n"); |
191 | | - await write(content, opts.bundlePath, ".gitignore"); |
192 | | -} |
193 | | - |
194 | | -export async function write(content: string | Record<string, unknown>, ...paths: string[]): Promise<void> { |
195 | | - const finalPath = path.join(...paths); |
196 | | - |
197 | | - logger.debug(`Writing file at ${finalPath}`); |
198 | | - |
199 | | - // Create directory if missing |
200 | | - const parent = path.dirname(finalPath); |
201 | | - if (!(await directoryExists(parent))) { |
202 | | - await fs.promises.mkdir(parent); |
203 | | - } |
204 | | - |
205 | | - const str = typeof content === "string" ? content : JSON.stringify(content, null, 4); |
206 | | - await fs.promises.writeFile(finalPath, str); |
| 109 | + await writeBundleFile(content, opts.bundlePath, ".gitignore"); |
207 | 110 | } |
0 commit comments