-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
177 lines (158 loc) · 4.75 KB
/
mod.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
import { getLogger } from "https://deno.land/std@0.146.0/log/mod.ts";
import { Args, parse } from "https://deno.land/std@0.146.0/flags/mod.ts";
import { join, resolve } from "https://deno.land/std@0.146.0/path/mod.ts";
import {
checkExists,
getAppConfigDirectory,
getAppConfigFile,
getAppLogFile,
} from "./utils.ts";
import { setup } from "./setup.ts";
import { help } from "./help.ts";
import { error, setupLogs } from "./log.ts";
import { bootstrap } from "./bootstrap.ts";
import { render } from "./render.ts";
const appName = "readme-manager";
export const mod = async (args: Args) => {
const appLogFile = getAppLogFile(appName);
await setupLogs(appLogFile);
const logger = getLogger(args.debug && "debug");
if (args.debug) {
logger.debug(`Logs will be appended to ${appLogFile}`);
}
const errorHandler = error(appLogFile, args.debug);
const appConfigFile = getAppConfigFile(appName, errorHandler);
logger.info(`App config file will be ${appConfigFile}`);
const appConfigDirectory = getAppConfigDirectory(appName, errorHandler);
logger.info(`App config file will be ${appConfigDirectory}`);
if (args.setup) {
return setup(
appName,
appConfigFile,
logger,
errorHandler,
appConfigDirectory,
);
}
if (args.bootstrap) {
return bootstrap(appConfigDirectory, errorHandler);
}
let templates = args?.templates;
let match = args?.match;
if (!args.match || !args.templates) {
logger.debug("Checking configuration file.");
let appConfigFileContent;
if (!(await checkExists(appConfigFile, errorHandler))) {
appConfigFileContent = await setup(
appName,
appConfigFile,
logger,
errorHandler,
appConfigDirectory,
);
}
if (!appConfigFileContent) {
try {
appConfigFileContent = await Deno.readTextFile(appConfigFile);
} catch (e) {
errorHandler(`Something went wrong reading ${appConfigFile}.`, e);
}
if (!appConfigFileContent) {
appConfigFileContent = await setup(
appName,
appConfigFile,
logger,
errorHandler,
appConfigDirectory,
);
}
try {
appConfigFileContent = JSON.parse(appConfigFileContent);
} catch (e) {
errorHandler(
`Something went wrong parsing the json in ${appConfigFile}.`,
e,
);
}
if (
!appConfigFileContent ||
typeof appConfigFileContent !== "object" ||
Object.keys(appConfigFileContent).length === 0
) {
appConfigFileContent = JSON.parse(
await setup(
appName,
appConfigFile,
logger,
errorHandler,
appConfigDirectory,
),
);
}
match = appConfigFileContent.match;
if (args.match) match = args.match;
templates = appConfigFileContent.templates;
if (args.templates) templates = appConfigFileContent.templates;
}
}
let targetPath = args?._?.[0];
if (args._.length === 0) targetPath = Deno.cwd();
targetPath = `${targetPath}`; // cast to string
try {
for await (const file of Deno.readDir(targetPath)) {
logger.debug(`Checking ${file.name} in ${targetPath}`);
if (file.isFile && file.name === match) {
console.log(`Matched ${file.name}.`);
try {
const readme = await render(
resolve(join(targetPath, file.name)),
resolve(targetPath),
await Deno.readTextFile(resolve(join(targetPath, file.name))),
templates,
errorHandler,
);
if (readme) {
await Deno.writeTextFile(
resolve(join(targetPath, "README.md")),
readme,
);
} else {
errorHandler(
`Something went wrong, ${
resolve(join(targetPath, file.name))
} generated an empty render output.`,
);
}
} catch (e) {
errorHandler(
`Something went wrong rendering ${
resolve(
join(targetPath, "README.md"),
)
}`,
e,
);
}
console.log(`Generated ${join(targetPath, "README.md")}.`);
}
}
} catch (e) {
if (e instanceof Deno.errors.PermissionDenied) {
logger.error("PermissionDenied on a directory.");
logger.error(e.message);
} else {
errorHandler(
"Something went wrong reading the direcotries of the target path.",
e,
);
}
}
};
export const bin = (args: string[]): Promise<void | string | undefined> => {
const parsedArgs = parse(args);
if (parsedArgs.help) {
return help();
}
return mod(parsedArgs);
};
await bin(Deno.args);