-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
158 lines (143 loc) · 4.14 KB
/
cli.js
File metadata and controls
158 lines (143 loc) · 4.14 KB
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
/*
* __ __ __ ______ ______
* /\ \/ / /\ \ /\ ___\ /\ ___\
* \ \ _"-.\ \ \\ \___ \\ \___ \
* \ \_\ \_\\ \_\\/\_____\\/\_____\
* \/_/\/_/ \/_/ \/_____/ \/_____/
*
* @author Matthieu Lassalvy
* @email sixertoy.github gmail
* @repository https://github.com/sixertoy/kiss-cli
*
* Install:
* npm i -g kiss-cli
*
* Usage:
* kiss --help
* kiss --list
* kiss <type> <./path/to/outputfile> <...>
* kiss <./path/to/outputfile.type> <...>
*
*/
const path = require('path');
const { KISS_DIRNAME, KISS_ROOTPATH } = require('./src/constants');
const { error, home } = require('./src/core');
const {
excludeNonExistingPath,
excludeSystemsFiles,
getTemplatesFilesInDirectory,
lookup,
mapTemplatesFilesToTypes,
} = require('./src/domain');
const {
checkFileIsAllowedType,
checkIsAllowedType,
checkIsFile,
exit,
getCliArguments,
outputAvailablesTypes,
outputHelp,
outputRawTemplate,
outputTemplateContent,
outputWelcomeMessage,
writeFile,
} = require('./src/cli-helpers');
const USE_DEBUG = true;
const USE_TTY = process.stderr.isTTY;
function getTemplatesList(search) {
// retrieve KISS templates files
// -> ./.kiss -> ~/.kiss -> ~/.npm/.kiss
const kissDirectories = [
path.join(KISS_ROOTPATH, KISS_DIRNAME), // Kiss Global Folder
path.join(home(), KISS_DIRNAME), // Kiss Home Folder
lookup(search), // Kiss Project Folder
];
const templates = kissDirectories
.filter(excludeNonExistingPath)
.reduce(getTemplatesFilesInDirectory, [])
.filter(excludeSystemsFiles)
.reduce(mapTemplatesFilesToTypes, {});
// returns an object/map { template-type: template-filepath }
return templates;
}
function shouldShowTemplates(args) {
if (!args || !args.length) return false;
return (
args.indexOf('-L') !== -1 ||
args.indexOf('-l') !== -1 ||
args.indexOf('--list') !== -1 ||
args.indexOf('-T') !== -1 ||
args.indexOf('-t') !== -1 ||
args.indexOf('--templates') !== -1
);
}
function shouldShowHelp(args) {
return (
args.indexOf('-H') !== -1 ||
args.indexOf('-h') !== -1 ||
args.indexOf('--help') !== -1 ||
!args ||
!args.length
);
}
function shouldOutputRawContent(args) {
if (!args || !args.length) return false;
return args.indexOf('-R') !== -1 || args.indexOf('--raw') !== -1;
}
try {
let type = null;
const args = getCliArguments();
const outputRawContent = shouldOutputRawContent(args);
const workingPath = (outputRawContent && args[2]) || process.cwd();
const templates = getTemplatesList(workingPath);
// NOTE output pour atom
if (outputRawContent) {
type = args.slice(1, 2).join('');
outputRawTemplate(type, templates);
process.exit(0);
}
// NOTE affichage du message d'acceuil
outputWelcomeMessage();
const showHelp = shouldShowHelp(args);
if (showHelp) {
outputHelp();
exit();
}
// NOTE affichage du contenu du template
const showTemplates = shouldShowTemplates(args);
if (showTemplates) {
outputAvailablesTypes(templates);
exit();
}
const [firstArgument, ...rest] = args;
const firstIsFile = checkIsFile(firstArgument);
const firstIsTemplateType = checkIsAllowedType(firstArgument, templates);
// NOTE affiche une erreur si il s'agit pas d'un type de template
// ou qu'il n'y aucun fichier a traiter
if (!firstIsTemplateType && !firstIsFile) {
const msg = `Argument ${firstArgument} is not a valid type`;
outputHelp();
exit(msg);
}
const fileIsValidType = checkFileIsAllowedType(firstArgument, templates);
if (!firstIsTemplateType && firstIsFile && !fileIsValidType) {
const msg = `File ${firstArgument} is not a valid filetype`;
outputHelp();
exit(msg);
}
if (firstIsTemplateType && !rest.length) {
outputTemplateContent(firstArgument, templates);
exit();
}
let files = [...args];
if (firstIsTemplateType) [type, ...files] = args;
files
.filter(checkIsFile)
.filter(f => !!firstIsTemplateType || checkFileIsAllowedType(f, templates))
.forEach(writeFile(templates, type));
exit();
} catch (e) {
if (USE_DEBUG) error(`error >>> ${e}\n`);
if (USE_TTY) error('\u001b[31m! Unexpected error has occurred\u001b[39m\n');
process.exit(1);
}