-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-meta.ts
100 lines (86 loc) · 2.7 KB
/
get-meta.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
import { parseYaml, readArgs } from "./deps.ts";
const isString = (d: unknown): d is string => Boolean(d && String(d) === d);
const error = {
noSource: "source folder is not defined",
};
const getSourceFolder = (args: Record<string, unknown>) => {
const sourceFolder = Deno.args[0] ||
(isString(args.src) ? args.src : undefined);
if (!sourceFolder) {
throw error.noSource;
}
return sourceFolder;
};
interface Config {
author: string;
filename: string;
language: string;
publisher: string;
source: string;
title: string;
verbose: boolean;
cover?: string;
}
const readConfig = (data: Record<string, unknown>): Partial<Config> => ({
author: isString(data.author) ? data.author : undefined,
publisher: isString(data.publisher) ? data.publisher : undefined,
title: isString(data.title) ? data.title : undefined,
language: isString(data.language) ? data.language : undefined,
source: isString(data.source) ? data.source : undefined,
filename: isString(data.filename) ? data.filename : undefined,
verbose: Boolean(data.verbose),
});
const readYaml = async (file: string) => {
try {
const content = await Deno.readTextFile(file);
return parseYaml(content);
} catch {
return {};
}
};
const readConfigFile = (
args: Record<string, unknown>,
sourceFolder: string,
): Promise<Partial<Config>> => {
const configFile = isString(args.config)
? args.config
: `${sourceFolder}/meta.yaml`;
// @ts-ignore ?
return readYaml(configFile);
};
const getConfig = async (
args: Record<string, unknown>,
id: string,
sourceFolder: string,
): Promise<Config> => {
const fromFile = await readConfigFile(args, sourceFolder);
const fromArgs = readConfig(args);
return {
author: fromArgs.author || fromFile.author || "Unknown author",
title: fromArgs.title || fromFile.title || "Unknown title",
publisher: fromArgs.publisher || fromFile.publisher || "Unknown publisher",
language: fromArgs.language || fromFile.language || "en-UK",
source: fromArgs.source || fromFile.source || "Unknown source",
filename: fromArgs.filename || fromFile.filename || id,
verbose: Boolean(fromArgs.verbose) || Boolean(fromFile.verbose) || false,
cover: fromArgs.cover || fromFile.cover,
};
};
const createDestinationFolder = async (id: string) => {
const folder = `tmp-${id}`;
await Deno.mkdir(folder);
return folder;
};
export const getMeta = async () => {
const args = readArgs();
const id = crypto.randomUUID();
const sourceFolder = getSourceFolder(args);
const config = await getConfig(args, id, sourceFolder);
const destinationFolder = await createDestinationFolder(id);
return {
id,
destinationFolder,
sourceFolder,
...config,
};
};