/
aqua.ts
206 lines (183 loc) · 5.4 KB
/
aqua.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isAbsolute, resolve } from "path";
import { cwd } from "process";
import { Command, Flags } from "@oclif/core";
import {
resolveCommonAquaCompilationFlags,
type ResolvedCommonAquaCompilationFlags,
} from "../lib/aqua.js";
import {
hasAquaToCompile,
compileAquaFromFluenceConfig,
compileAquaAndWatch,
} from "../lib/compileAquaAndWatch.js";
import {
INPUT_FLAG_EXPLANATION,
COMPILE_AQUA_PROPERTY_NAME,
COMMON_AQUA_COMPILATION_FLAGS,
NO_INPUT_FLAG,
FLUENCE_CONFIG_FULL_FILE_NAME,
INPUT_FLAG_NAME,
} from "../lib/const.js";
import { initCli, exitCli } from "../lib/lifeCycle.js";
import { validatePath } from "../lib/paths.js";
import { input, type InputArg } from "../lib/prompt.js";
/**
* This command doesn't extend BaseCommand like other commands do because it
* has a --watch flag which should keep cli alive
* This means we have to manually call exitCli() in all other cases before
* the final return statement
*/
export default class Aqua extends Command {
static override description = `Compile aqua defined in '${COMPILE_AQUA_PROPERTY_NAME}' property of ${FLUENCE_CONFIG_FULL_FILE_NAME}${INPUT_FLAG_EXPLANATION}`;
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
names: Flags.string({
description: `Comma-separated names of the configs from '${COMPILE_AQUA_PROPERTY_NAME}' property of ${FLUENCE_CONFIG_FULL_FILE_NAME} to compile. If not specified, all configs will be compiled`,
char: "n",
}),
...NO_INPUT_FLAG,
watch: Flags.boolean({
default: false,
description: "Watch aqua file or folder for changes and recompile",
char: "w",
}),
output: Flags.string({
description:
"Path to the output directory. Must be relative to the current working directory or absolute. Will be created if it doesn't exists",
helpValue: "<path>",
char: "o",
}),
air: Flags.boolean({
default: false,
description: "Generate .air file instead of .ts",
exclusive: ["js"],
}),
js: Flags.boolean({
default: false,
description: "Generate .js file instead of .ts",
exclusive: ["air"],
}),
...COMMON_AQUA_COMPILATION_FLAGS,
dry: Flags.boolean({
default: false,
description: "Checks if compilation succeeded, without output",
}),
};
async run(): Promise<void> {
const { flags, maybeFluenceConfig: fluenceConfig } = await initCli(
this,
await this.parse(Aqua),
);
const commonFlags = await resolveCommonAquaCompilationFlags(
flags,
fluenceConfig,
);
if (
flags[INPUT_FLAG_NAME] === undefined &&
hasAquaToCompile(fluenceConfig)
) {
await compileAquaFromFluenceConfig({
fluenceConfig,
imports: commonFlags.imports,
names: flags.names,
dry: flags.dry,
watch: flags.watch,
});
await exitCli();
return;
}
await compileAquaFromFlags({ ...commonFlags, ...flags });
}
}
async function compileAquaFromFlags({
input,
output,
...flags
}: ResolvedCommonAquaCompilationFlags & {
input: string | undefined;
output: string | undefined;
dry: boolean;
js: boolean;
air: boolean;
watch: boolean;
}) {
const filePath = await resolveAbsoluteAquaPathFromCwd({
maybePathFromFlags: input,
inputArg: {
message: `Enter path to an aqua file or an input directory that contains your aqua files`,
flagName: "input",
validate: validatePath,
},
});
const outputPathAbsolute = flags.dry
? undefined
: await resolveAbsoluteAquaPathFromCwd({
maybePathFromFlags: output,
inputArg: {
message:
"Enter path to the output directory. Will be created if it doesn't exists (press Enter to use the current working directory)",
flagName: "output",
},
});
const targetType = resolveTargetType(flags);
await compileAquaAndWatch({
filePath,
targetType,
outputPathAbsolute,
...flags,
});
if (!flags.watch) {
await exitCli();
}
}
type ResolveTargetTypeArgs = {
js: boolean;
air: boolean;
};
function resolveTargetType({
js,
air,
}: ResolveTargetTypeArgs): "ts" | "js" | "air" {
if (js) {
return "js";
}
if (air) {
return "air";
}
return "ts";
}
type ResolveAbsoluteAquaPathArg = {
maybePathFromFlags: string | undefined;
inputArg: InputArg;
};
async function resolveAbsoluteAquaPathFromCwd({
maybePathFromFlags,
inputArg,
}: ResolveAbsoluteAquaPathArg) {
if (maybePathFromFlags !== undefined) {
if (isAbsolute(maybePathFromFlags)) {
return maybePathFromFlags;
}
return resolve(maybePathFromFlags);
}
const pathFromUserInput = await input({ ...inputArg, default: cwd() });
if (isAbsolute(pathFromUserInput)) {
return pathFromUserInput;
}
return resolve(pathFromUserInput);
}