-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmt.ts
128 lines (123 loc) · 3.52 KB
/
cmt.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
import { parseArgs } from "@std/cli";
import { licenseText, releaseDate, releaseHash, version } from "./version.ts";
import { fmtHelp, helpText } from "./helptext.ts";
import { CodeMeta } from "./codemeta.ts";
import { getFormatFromExt, isSupportedFormat, transform } from "./transform.ts";
async function main() {
const appName = "cmt";
const app = parseArgs(Deno.args, {
alias: {
help: "h",
license: "l",
version: "v",
format: "f",
deno: "d",
},
default: {
help: false,
version: false,
license: false,
format: "",
deno: false,
},
});
const args = app._;
if (app.help) {
console.log(fmtHelp(helpText, appName, version, releaseDate, releaseHash));
Deno.exit(0);
}
if (app.license) {
console.log(licenseText);
Deno.exit(0);
}
if (app.version) {
console.log(`${appName} ${version} ${releaseDate} ${releaseHash}`);
Deno.exit(0);
}
if (args.length < 1) {
console.log(`USAGE: ${appName} [OPTIONS] INPUT_NAME [OUTPUT_NAME]`);
Deno.exit(1);
}
let format = app.format;
let inputName: string = (args.length > 0) ? `${args.shift()}` : "";
let outputNames: string[] = [];
for (let outputName of args) {
outputNames.push(`${outputName}`);
}
if (inputName === "") {
console.log("error: missing filepath to codemeta.json");
Deno.exit(1);
}
let src: string = await Deno.readTextFile(inputName);
let obj: object = {};
try {
obj = JSON.parse(src);
} catch (err) {
console.log(err);
Deno.exit(1);
}
let cm = new CodeMeta();
if (cm.fromObject(obj) === false) {
console.log(`failed to process ${inputName} object`);
Deno.exit(1);
}
if (outputNames.length === 0) {
if (!isSupportedFormat(format)) {
console.log(`unsupported format, "${format}"`);
Deno.exit(1);
}
let txt: string | undefined = await transform(cm, format);
if (txt === undefined) {
console.log(`unsupported transform, "${format}"`);
Deno.exit(1);
}
console.log(txt);
Deno.exit(0);
}
let denoTasks: {[ key: string]: string } = {};
for (let outputName of outputNames) {
format = getFormatFromExt(outputName, app.format);
if (!isSupportedFormat(format)) {
console.log(`unsupported format, "${format}"`);
Deno.exit(1);
}
let txt: string | undefined = await transform(cm, format);
if (txt === undefined) {
console.log(`unsupported transform, "${format}"`);
Deno.exit(1);
}
Deno.writeTextFile(outputName, txt);
if (app.deno) {
denoTasks[outputName] = `${appName} ${inputName} ${outputName}`;
}
}
// Handle updating the deno.json file.
if (app.deno) {
let src = await Deno.readTextFile("deno.json");
if (src === undefined) {
src = `{"tasks":{}}`;
}
let denoJSON = JSON.parse(src);
let genCodeTasks: string[] = [];
if (denoJSON.tasks === undefined) {
denoJSON.tasks = {};
}
for (let taskName of Object.keys(denoTasks)) {
denoJSON.tasks[taskName] = denoTasks[taskName];
genCodeTasks.push(`deno task ${taskName}`);
}
if (genCodeTasks.length > 0) {
denoJSON.tasks["gen-code"] = genCodeTasks.join(' ; ');
}
// Update deno.json file.
//FIXME: backup deno.json if exists.
try {
await Deno.copyFile("deno.json", "deno.json.bak");
} catch(err) {
console.log(`failed to backup deno.json aborting, ${err}`);
Deno.exit(1);
}
Deno.writeTextFile("deno.json", JSON.stringify(denoJSON, null, 2));
}
}
if (import.meta.main) main();