-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_doc.ts
88 lines (77 loc) · 2.34 KB
/
new_doc.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
// Copyright © 2022 Dpm Land. All Rights Reserved.
import Ask from 'https://deno.land/x/[email protected]/mod.ts';
import * as colors from 'https://deno.land/[email protected]/fmt/colors.ts';
import { join } from 'https://deno.land/[email protected]/path/mod.ts';
import { dracoFiles } from 'https://deno.land/x/[email protected]/mod.ts';
// Utilities
const ask = new Ask({
suffix: '?',
prefix: '>',
});
// Mini logger
const info = (msg: string) => {
console.log(colors.cyan(msg));
};
const error = (msg: string) => {
console.log(colors.red(msg));
Deno.exit(2);
};
const warn = (msg: string) => {
console.log(colors.yellow(msg));
};
// Start the helper!
const answers = await ask.prompt([
{
name: 'name',
message:
'Name of documentation to create: ( Answers like: about.info or about )',
},
{
name: 'current',
message:
'Create this in the current directory or work with folders: ( Answers like: y -> directory or n -> folders )',
type: 'confirm',
},
]);
let docName = answers.name?.toString().split('.');
docName = (typeof docName == 'undefined') ? ['notExists'] : docName;
if (docName[0] == 'notExists') {
error('Is necessary an argument!');
}
let name = docName[1]?.toUpperCase() || 'NO_NAME_PROVIDED';
if (answers.current) {
if (docName.length == 1 && docName[0] != 'notExists') {
name = docName[0]?.toUpperCase() || 'NO_NAME_PROVIDED';
if (!dracoFiles.exists(`${docName[0]}.md`)) {
if (docName[0] == '') {
warn('Creating the file with the default name!');
}
await Deno.writeTextFile(
`${join(Deno.cwd(), name)}.md`,
'',
);
info('Writed successfully the new documentation file!');
} else {
error('This file already exists!');
}
}
}
if (docName.length == 2) {
if (dracoFiles.exists(docName[0])) {
info(`The ${docName[0]} directory exists!`);
await Deno.writeTextFile(
`${join(Deno.cwd(), docName[0], name)}.md`,
'',
);
info('Writed successfully the new documentation file!');
} else {
warn(`Not found the ${docName[0]} directory!`);
await Deno.mkdir(`${join(Deno.cwd(), docName[0])}`, { recursive: true });
info(`Created succesfully the new directory! ${docName[0]}`);
await Deno.writeTextFile(
`${join(Deno.cwd(), docName[0], name)}.md`,
'',
);
info('Writed successfully the new documentation file!');
}
}