-
Notifications
You must be signed in to change notification settings - Fork 1
/
ys.ts
129 lines (125 loc) · 4.2 KB
/
ys.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
import { Command, EnumType, globToRegExp } from "./deps.ts";
import { run } from "./entry.ts";
import log from "./log.ts";
import { EntryOptions } from "./interface.ts";
import { LevelName } from "./_interface.ts";
import pkg from "./pkg.json" assert { type: "json" };
import {
absolutePathToRelativePath,
getFilesFromGlob,
getPublicContext,
getUniqueStrings,
} from "./util.ts";
const setLogLevel = (options: Record<string, LevelName>) => {
let logLevel: LevelName = "info";
if (options.verbose) {
logLevel = "debug";
} else {
logLevel = options.logLevel;
}
log.setLevel(logLevel);
};
if (import.meta.main) {
const runCommand = new Command()
.description("run files")
.arguments("[file...:string]").option(
"-A, --all",
"run all .ys.yml files in current working directory",
).globalOption("-v, --verbose", "Enable verbose output.")
.option("--dist <dist>", "dist directory.")
.option(
"-d, --dir <folde:string>",
"run all **/*.ys.yml files in the specified directory, use , to separate multiple directories.",
{ collect: true },
)
.action(async (options, args) => {
setLogLevel(options as unknown as Record<string, LevelName>);
let files: string[] = await getFilesFromGlob(args || []);
// concat dir
if (options.dir && Array.isArray(options.dir)) {
const dir = options.dir;
for (const dirname of dir) {
const dirRemoveEndSlash = dirname.replace(/\/$/, "");
files.push(
...await getFilesFromGlob([`${dirRemoveEndSlash}/**/*.ys.yml`]),
);
}
}
if (options.all) {
files.push(...await getFilesFromGlob(["**/*.ys.yml"]));
}
files = getUniqueStrings(files).map(absolutePathToRelativePath);
const dist = options.dist || "dist";
log.debug("files:", files);
const runOptions: EntryOptions = {
files: files as string[],
public: await getPublicContext(),
isRun: true,
verbose: options.verbose,
dist,
};
await run(runOptions);
});
const buildCommand = new Command()
.arguments("[file...:string]")
.description("build yaml file to js file")
.option("--dist <dist>", "dist directory.")
.option("--runtime", "also build runtime mode.")
.option(
"-A, --all",
"run all .ys.yml files in current working directory",
)
.option(
"-d, --dir <folde:string>",
"run all **/*.ys.yml files in the specified directory, use , to separate multiple directories.",
{ collect: true },
).globalOption("-v, --verbose", "Enable verbose output.")
.action(async (options, args) => {
setLogLevel(options as unknown as Record<string, LevelName>);
log.debug("cli options:", options);
log.debug("cli args:", args);
let files: string[] = await getFilesFromGlob(args || []);
// concat dir
if (options.dir && Array.isArray(options.dir)) {
const dir = options.dir;
for (const dirname of dir) {
const dirRemoveEndSlash = dirname.replace(/\/$/, "");
files.push(
...await getFilesFromGlob([`${dirRemoveEndSlash}/**/*.ys.yml`]),
);
}
}
if (options.all) {
files.push(...await getFilesFromGlob(["**/*.ys.yml"]));
}
files = getUniqueStrings(files).map(absolutePathToRelativePath);
// transform to relative path
log.debug("files:", files);
const dist = options.dist || "dist";
const verbose = options.verbose || false;
const runOptions: EntryOptions = {
files: files,
isRun: false,
shouldBuildRuntime: options.runtime,
public: await getPublicContext(),
dist,
verbose: verbose,
};
await run(runOptions);
});
await new Command()
.name(pkg.bin)
.version(pkg.version)
.description(pkg.description)
.type("log-level", new EnumType(["debug", "info", "warn", "error"]))
.globalOption("-v, --verbose", "Enable verbose output.")
.action(function () {
this.showHelp();
}).example(
"run file",
"ys run file.ys.yml\n\nys run **/*.ys.yml",
)
.command("run", runCommand)
.command("build", buildCommand)
.parse(Deno.args);
}