-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
55 lines (47 loc) · 1.44 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
import { contextFinder, helpMessage } from "./src/context_finder.ts";
export const fileExists = async (filename: string): Promise<boolean> => {
try {
await Deno.stat(filename);
// successful, file or directory must exist
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// file or directory does not exist
return false;
} else {
// unexpected error, maybe permissions, pass it along
throw error;
}
}
};
async function runFromCommandLine() {
if (import.meta.main !== true) {
return false;
}
// Check if --help argument is used
const args = Deno.args;
if (args[0] === "--help") {
console.info(helpMessage);
Deno.exit(0);
}
// Check command line arguments are set (minimum is 5: node index.js <file> <file> <context>)
if (args.length < 3) {
console.error("Invalid arguments. See --help");
Deno.exit(1);
}
// Initialise data to be passed into function call
const fileToRead = args[0];
const fileToWriteTo = args[1];
const contexts = (args.slice(2)).filter((arg) => arg.indexOf("--") == -1);
// Check fileToRead exists
const fileToReadExists = await fileExists(fileToRead);
if (!fileToReadExists) {
console.error("File to read does not exist.");
Deno.exit(1);
}
// Call the function
contextFinder(contexts, fileToRead, fileToWriteTo);
}
/* If ran from the command line */
await runFromCommandLine();
export { contextFinder };