-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcme.ts
153 lines (145 loc) · 4.13 KB
/
cme.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
import { parseArgs } from "@std/cli";
import { licenseText, releaseDate, releaseHash, version } from "./version.ts";
import { fmtHelp, cmeHelpText } from "./helptext.ts";
import { CodeMeta, CodeMetaTerms } from "./codemeta.ts";
import type { AttributeType } from "./codemeta.ts";
import { editCodeMetaTerm } from "./editor.ts";
const reUnquote = new RegExp(`^["'](.*)["']$`);
function getAttributeNames(terms: AttributeType[]): string[] {
let names: string[] = [];
for (let item of terms) {
names.push(item.name);
}
return names;
}
async function main() {
const appName = "cme";
const app = parseArgs(Deno.args, {
alias: {
help: "h",
license: "l",
version: "v",
format: "f",
editor: "e",
},
default: {
help: false,
version: false,
license: false,
editor: false,
attributes: false,
},
});
const args = app._;
if (app.help) {
console.log(fmtHelp(cmeHelpText, 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);
}
const codeMetaTermNames = getAttributeNames(CodeMetaTerms);
if (app.attributes) {
console.log('');
for (const term of CodeMetaTerms) {
console.log(`${term.name}
: ${term.help}
`);
}
Deno.exit(0);
}
if (args.length < 1) {
console.log(`USAGE: ${appName} [OPTIONS] INPUT_NAME [OUTPUT_NAME]`);
Deno.exit(1);
}
let inputName: string = (args.length > 0) ? `${args.shift()}` : "";
let attributeNames: string[] = [];
if (args.length === 0) {
attributeNames = codeMetaTermNames;
}
let attrValues: {[key: string]: string} = {};
for (const arg of args) {
// NOTE: arg can be a number or value due to process_args, should always be string.
let attr: string = `${arg}`;
if (attr.indexOf("=") > -1) {
let str = attr.substring(attr.indexOf('=')+1);
attr = attr.substring(0, attr.indexOf('='));
attrValues[attr] = str;
} else if (attributeNames.indexOf(attr) === -1) {
// Make sure the attirubute name makes sense, if not exits without doing anything.
if (codeMetaTermNames.indexOf(attr) === -1) {
console.log(`ERROR: "${attr}" is not a supported CodeMeta attribute, aborting`);
Deno.exit(1);
}
attributeNames.push(`${attr}`);
}
}
if (inputName === "") {
console.log("error: missing filepath to codemeta.json");
Deno.exit(1);
}
let src: string = '';
try {
src = await Deno.readTextFile(inputName);
} catch (err) {
console.log(`${err}`);
if (confirm(`Create ${inputName}?`)) {
src = '{}';
} else {
Deno.exit(1);
}
}
let obj: {[key: string]: any} = {};
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 (Object.keys(attrValues).length > 0) {
let obj: {[key: string]: string} = {};
for (let key of Object.keys(attrValues)) {
let val = attrValues[key];
obj[key] = val;
}
cm.patchObject(obj);
src = JSON.stringify(cm.toObject(), null, 2);
// Check if file exists then write out new version.
try {
await Deno.copyFile(inputName, `${inputName}.bak`);
} catch (err) {
// No file exists, skip backup.
}
await Deno.writeTextFile(inputName, src);
}
if (attributeNames.length > 0) {
for (let name of attributeNames) {
if (! await editCodeMetaTerm(cm, name, app.editor)) {
console.info(`INFO: using previous value ${name}`)
}
}
src = JSON.stringify(cm.toObject(), null, 2);
console.log(src);
if (confirm(`Write to ${inputName}`)) {
// Check if file exists then write out new version.
try {
await Deno.copyFile(inputName, `${inputName}.bak`);
} catch (err) {
// No file exists, skip backup.
}
await Deno.writeTextFile(inputName, src);
}
Deno.exit(0);
}
}
if (import.meta.main) main();