-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
248 lines (213 loc) · 7.86 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const _ = require('lodash');
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const process = require('process');
const helpers = require('./lib/helpers');
const colores = require('./lib/colores').colores;
module.exports.colores = colores;
const packageLabel = colores.packageLabel;
const cwd = process.cwd();
const logger = {
success: function(...args) {
console.log(packageLabel, chalk.green(args.join(' ')));
},
info: function(...args) {
console.log(packageLabel, args.join(' '));
},
warn: function(...args) {
console.log(packageLabel, chalk.yellow(args.join(' ')));
},
error: function(...args) {
console.log(packageLabel, chalk.red(args.join(' ')));
},
file: function(...args) {
console.log(packageLabel, chalk.yellow(args.join(' ')), 'file created!');
}
}
//! install Command
function install(component, variant) {
if (!alloyProject()) return;
if (!component) {
return prompts();
} else if (component === 'all') {
fs.readdirSync(path.resolve(__dirname, 'dist')).forEach(component => {
if (component !== 'lib') fs.copySync(path.resolve(__dirname, `dist/${component}`), `${cwd}/app/views/tikit/${component}`);
});
postInstall(chalk.yellow('./app/views/tikit'));
} else if (component === 'lib' || component === 'module') {
installKitUI();
} else if (!variant) {
let source = path.resolve(__dirname, `dist/${component}`);
if (!fs.existsSync(source)) return console.log(chalk.yellow(component), 'Folder does not exists!');
fs.copySync(source, `${cwd}/app/views/tikit/${component}`);
postInstall(chalk.yellow(`./app/views/tikit/${component}`));
} else {
let source = path.resolve(__dirname, `dist/${component}`);
if (!fs.existsSync(source)) return console.log(chalk.yellow(component), 'Folder does not exists!');
if (!variant.includes('.xml')) variant += '.xml';
source = path.resolve(__dirname, `dist/${component}/${variant}`);
if (!fs.existsSync(source)) return console.log(`Component ${chalk.yellow(variant)} does not exists!`);
fs.copySync(source, `${cwd}/app/views/tikit/${component}/${variant}`);
postInstall(chalk.yellow(`./app/views/tikit/${component}/${variant}`));
}
}
exports.install = install;
//! list Command
function list(folder) {
if (folder) {
logger.success('List of variants for', chalk.yellow(folder));
walkSync(path.resolve(__dirname, `dist/${folder}`), (viewPath, spaces) => {
if (viewPath.length > 0) {
logger.info(spaces + '└─ ' + viewPath.join(', '));
}
});
} else {
logger.success('Available components and their variants:');
walkSync(path.resolve(__dirname, `dist`), (viewPath, spaces) => {
if (viewPath.length > 0) {
logger.info(spaces + '└─ ' + viewPath.join(', '));
}
});
}
console.log('\n' + packageLabel, 'Run', chalk.yellow('`tikit install`'), 'and follow an interactive select prompt');
// logger.info('Run', chalk.yellow('`tikit install all`'), 'to install the entire component library');
// logger.info('Run', chalk.yellow('`tikit install [componet]`'), 'to install all files and variants of a component');
// logger.info('Run', chalk.yellow('`tikit install [componet/variant]`'), 'to install all files of a component’s variant');
// logger.info('Run', chalk.yellow('`tikit install [componet/variant] [name]`'), 'to install a single file of a component’s variant');
}
exports.list = list;
//! help Command
function help(component) {
if (component) {
logger.info('Showing:', component);
} else {
logger.info('Please run:', chalk.yellow('`tikit help <component>`'), 'to learn more about it!');
const publicComponents = require(path.resolve(__dirname, 'core/scripts/public-components'));
const availableComponents = _.flatMap(publicComponents.components, (data, component) => component).join(', ');
logger.info('Avaliable components:', chalk.yellow(availableComponents));
}
}
exports.help = help;
//! helper Functions
function alloyProject() {
if (!fs.existsSync(cwd + '/app/views')) {
logger.error('Please make sure you’re running TiKit inside an Alloy Project.');
return false;
}
return true;
}
function prompts() {
// https://github.com/terkelg/prompts
const prompts = require('prompts');
(async () => {
let publicComponents = require(path.resolve(__dirname, 'core/scripts/public-components'));
let availableComponents = _.flatMap(publicComponents.components, (data, component) => component).sort().map(
(component) => {
return {
title: component,
value: component
};
}
);
if (availableComponents.length > 1) availableComponents.unshift({ title: 'all components', value: 'all' });
let selectedComponent = await prompts({
name: 'value',
type: 'select',
choices: availableComponents,
message: 'Choose a component to install'
});
if (selectedComponent.value) {
if (selectedComponent.value === 'all') {
install('all');
} else {
let availableVariants = publicComponents.components[selectedComponent.value].templates.sort().map((variant) => {
return {
title: variant,
value: variant
};
});
if (availableVariants.length > 1) availableVariants.unshift({ title: 'all variants', value: 'all' });
let selectedVariant = await prompts({
name: 'value',
type: 'select',
choices: availableVariants,
message: 'Choose a variant',
});
if (selectedVariant.value) {
if (selectedVariant.value === 'all') {
install(selectedComponent.value);
} else {
let type = publicComponents.components[selectedComponent.value].base;
let availableFiles = publicComponents.components[selectedComponent.value].properties.sort(
(a, b) => a.name.localeCompare(b.name)
).map((file) => {
return {
title: file.name,
value: file.name
};
});
if (availableFiles.length > 1) availableFiles.unshift({ title: `all ${type}s`, value: 'all' });
let selectedFile = await prompts({
name: 'value',
type: 'select',
choices: availableFiles,
message: `Choose a ${type}`,
});
if (selectedFile.value) {
if (selectedFile.value === 'all') {
install(`${selectedComponent.value}/${selectedVariant.value}`);
} else {
install(`${selectedComponent.value}/${selectedVariant.value}`, `${selectedFile.value}`);
}
}
}
}
}
}
})();
}
function postInstall(message) {
if (!fs.existsSync(cwd + '/app/lib')
|| !fs.existsSync(cwd + '/app/lib/tikit.ui.js')
|| fs.statSync(path.resolve(__dirname, 'dist/lib/tikit.ui.js')).mtime > fs.statSync(cwd + '/app/lib/tikit.ui.js').mtime) {
installKitUI();
}
logger.info('Components installed in', message);
checkForFontAwesome();
}
function walkSync(currentDirPath, callback, spaces = '') {
let files = fs.readdirSync(currentDirPath);
let actualFiles = [];
files.forEach(name => {
let filePath = path.join(currentDirPath, name);
let stat = fs.statSync(filePath);
if (stat.isFile()) {
actualFiles.push(path.basename(filePath).split('.').slice(0, -1).join('.'));
} else if (stat.isDirectory()) {
logger.info(spaces + '└─ ' + chalk.yellow(name));
walkSync(filePath, callback, spaces + ' ');
}
});
callback(actualFiles, spaces);
}
function installKitUI() {
fs.copySync(path.resolve(__dirname, 'dist/lib/tikit.ui.js'), `${cwd}/app/lib/tikit.ui.js`);
logger.info(chalk.yellow('tikit.ui'), 'module installed in', chalk.yellow(`./app/lib/tikit.ui.js`));
}
function checkForFontAwesome() {
if (!fs.existsSync(cwd + '/app/assets/fonts')) {
installFontAwesomeMessage();
} else {
let files = fs.readdirSync(cwd + '/app/assets/fonts');
let fontAwesome = files.filter((file) => file.includes('FontAwesome'));
if (fontAwesome.length === 0) {
installFontAwesomeMessage();
}
}
}
function installFontAwesomeMessage() {
console.log();
logger.warn('Some components use FontAwesome Icons!');
logger.warn('Please install them with:', chalk.green(`purgetss icon-library -v=fa`));
}