|
1 | | -import * as ansis from "ansis"; |
2 | 1 | import { watch } from "chokidar"; |
3 | | -import * as console from "node:console"; |
4 | | -import { dirname, join } from "path"; |
| 2 | +import { dirname, join } from "node:path"; |
5 | 3 |
|
6 | | -import { type BuildConfig } from "@lib/config"; |
7 | | -import { type Input, getDirectoryInput } from "@lib/input"; |
8 | | -import { getWatchInput } from "@lib/input"; |
9 | | -import { PackageManager, PackageManagerFactory } from "@lib/package-manager"; |
| 4 | +import { type BuildConfig, type Config } from "@lib/config"; |
| 5 | +import { type Input, getDirectoryInput, getWatchInput } from "@lib/input"; |
| 6 | +import { PackageManagerFactory, PackageManagerName } from "@lib/package-manager"; |
10 | 7 | import { Messages } from "@lib/ui"; |
11 | 8 |
|
12 | 9 | import { getCwd } from "@utils/path"; |
| 10 | +import { runSafe } from "@utils/run-safe"; |
13 | 11 |
|
14 | 12 | import { getConfig } from "~/action/common/config"; |
15 | 13 |
|
16 | | -import { AbstractAction } from "../abstract.action"; |
| 14 | +import { AbstractAction, type HandleResult } from "../abstract.action"; |
17 | 15 |
|
18 | | -interface BuildPart { |
| 16 | +interface BuildTarget { |
| 17 | + name: string; |
19 | 18 | entry: string; |
20 | 19 | output: string; |
21 | | - target: "client" | "server"; |
| 20 | + platform: "browser" | "node"; |
22 | 21 | } |
23 | 22 |
|
24 | 23 | export class BuildAction extends AbstractAction { |
25 | | - public async handle(_args: Input, options: Input) { |
26 | | - console.info(Messages.BUILD_START); |
27 | | - console.info(); |
| 24 | + protected startMessage = Messages.BUILD_START; |
| 25 | + protected successMessage = Messages.BUILD_SUCCESS; |
| 26 | + protected failureMessage = Messages.BUILD_FAILED; |
| 27 | + |
| 28 | + public async handle(_args: Input, options: Input): Promise<HandleResult> { |
| 29 | + const directory = getDirectoryInput(options); |
| 30 | + const config = await getConfig(options, directory); |
| 31 | + const isWatch = getWatchInput(options); |
| 32 | + |
| 33 | + const targets = this.resolveTargets(config, options); |
| 34 | + const results = await this.buildAll(targets, directory, isWatch); |
28 | 35 |
|
29 | | - try { |
30 | | - const directory = getDirectoryInput(options); |
31 | | - const config = await getConfig(options, directory); |
32 | | - const watch = getWatchInput(options); |
| 36 | + if (isWatch) { |
| 37 | + return this.enterWatchMode(); |
| 38 | + } |
| 39 | + |
| 40 | + return { success: results.every(Boolean) }; |
| 41 | + } |
33 | 42 |
|
34 | | - const client = getPart( |
| 43 | + private resolveTargets(config: Config, options: Input): BuildTarget[] { |
| 44 | + const targets: BuildTarget[] = [ |
| 45 | + this.createTarget( |
| 46 | + "Client", |
35 | 47 | config.client.build, |
| 48 | + "browser", |
36 | 49 | options.get("clientDirectory")?.value as string | undefined, |
37 | | - "client", |
38 | | - ); |
39 | | - let res = await buildPart("Client", client, directory, { watch }); |
| 50 | + ), |
| 51 | + ]; |
40 | 52 |
|
41 | | - if (config.server.enable) { |
42 | | - const server = getPart( |
| 53 | + if (config.server.enable) { |
| 54 | + targets.push( |
| 55 | + this.createTarget( |
| 56 | + "Server", |
43 | 57 | config.server.build, |
| 58 | + "node", |
44 | 59 | options.get("serverDirectory")?.value as string | undefined, |
45 | | - "server", |
46 | | - ); |
47 | | - res = (await buildPart("Server", server, directory, { watch })) ? res : false; |
48 | | - } |
49 | | - |
50 | | - console.info(); |
51 | | - |
52 | | - if (watch) { |
53 | | - console.info(Messages.BUILD_WATCH_START); |
54 | | - console.info(); |
55 | | - return; |
56 | | - } |
57 | | - |
58 | | - if (!res) { |
59 | | - console.info(Messages.BUILD_FAILED); |
60 | | - process.exit(1); |
61 | | - } |
62 | | - console.info(Messages.BUILD_SUCCESS); |
63 | | - process.exit(0); |
64 | | - } catch (e) { |
65 | | - console.error(e); |
66 | | - process.exit(1); |
| 60 | + ), |
| 61 | + ); |
67 | 62 | } |
| 63 | + |
| 64 | + return targets; |
| 65 | + } |
| 66 | + |
| 67 | + private createTarget( |
| 68 | + name: string, |
| 69 | + config: BuildConfig, |
| 70 | + platform: "browser" | "node", |
| 71 | + outDirOverride?: string, |
| 72 | + ): BuildTarget { |
| 73 | + return { |
| 74 | + name, |
| 75 | + entry: config.entryFile, |
| 76 | + output: outDirOverride || config.outDir, |
| 77 | + platform, |
| 78 | + }; |
68 | 79 | } |
69 | | -} |
70 | 80 |
|
71 | | -const getPart = ( |
72 | | - config: BuildConfig, |
73 | | - directoryOption: string | undefined, |
74 | | - target: "client" | "server", |
75 | | -): BuildPart => { |
76 | | - return { |
77 | | - entry: config.entryFile, |
78 | | - output: directoryOption || config.outDir, |
79 | | - target: target, |
80 | | - }; |
81 | | -}; |
82 | | - |
83 | | -const buildPart = async ( |
84 | | - name: string, |
85 | | - part: BuildPart, |
86 | | - directory: string, |
87 | | - options?: { watch?: boolean }, |
88 | | -) => { |
89 | | - const packageManagerName = PackageManager.LOCAL_BUN; |
90 | | - |
91 | | - const packageManager = PackageManagerFactory.create(packageManagerName); |
92 | | - |
93 | | - const build = async (watch = false) => { |
94 | | - try { |
95 | | - return await packageManager.build( |
96 | | - name, |
97 | | - directory, |
98 | | - part.entry, |
99 | | - part.output, |
100 | | - [ |
101 | | - "--asset-naming", |
102 | | - "[name].[ext]", |
103 | | - "--target", |
104 | | - part.target === "client" ? "browser" : "node", |
105 | | - ], |
106 | | - watch, |
| 81 | + private async buildAll( |
| 82 | + targets: BuildTarget[], |
| 83 | + directory: string, |
| 84 | + isWatch: boolean, |
| 85 | + ): Promise<boolean[]> { |
| 86 | + const results: boolean[] = []; |
| 87 | + for (const target of targets) { |
| 88 | + const result = await this.buildTarget(target, directory, isWatch); |
| 89 | + results.push(result); |
| 90 | + } |
| 91 | + return results; |
| 92 | + } |
| 93 | + |
| 94 | + private async buildTarget( |
| 95 | + target: BuildTarget, |
| 96 | + directory: string, |
| 97 | + isWatch: boolean, |
| 98 | + ): Promise<boolean> { |
| 99 | + const packageManager = PackageManagerFactory.create(PackageManagerName.LOCAL_BUN); |
| 100 | + |
| 101 | + const executeBuild = (rebuild = false) => |
| 102 | + runSafe( |
| 103 | + () => |
| 104 | + packageManager.build( |
| 105 | + target.name, |
| 106 | + directory, |
| 107 | + target.entry, |
| 108 | + target.output, |
| 109 | + ["--asset-naming", "[name].[ext]", "--target", target.platform], |
| 110 | + rebuild, |
| 111 | + ), |
| 112 | + false, |
107 | 113 | ); |
108 | | - } catch (error: any) { |
109 | | - if (error && error.message) { |
110 | | - console.error(ansis.red(error.message)); |
111 | | - } |
112 | | - return false; |
| 114 | + |
| 115 | + if (isWatch) { |
| 116 | + this.watchDirectory(directory, target.entry, () => executeBuild(true)); |
113 | 117 | } |
114 | | - }; |
115 | 118 |
|
116 | | - if (options?.watch) |
117 | | - watch(dirname(join(getCwd(directory), part.entry))).on("change", () => build(true)); |
| 119 | + const result = await executeBuild(); |
| 120 | + return result !== false; |
| 121 | + } |
118 | 122 |
|
119 | | - return await build(); |
120 | | -}; |
| 123 | + private watchDirectory(directory: string, entry: string, onChange: () => void): void { |
| 124 | + const watchPath = dirname(join(getCwd(directory), entry)); |
| 125 | + watch(watchPath).on("change", onChange); |
| 126 | + } |
| 127 | + |
| 128 | + private enterWatchMode(): HandleResult { |
| 129 | + console.info(); |
| 130 | + console.info(Messages.BUILD_WATCH_START); |
| 131 | + console.info(); |
| 132 | + return { keepAlive: true }; |
| 133 | + } |
| 134 | +} |
0 commit comments