-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure-css-cli.js
91 lines (75 loc) · 2.07 KB
/
pure-css-cli.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
"use strict";
const fs = require('fs');
const pipe = (...funcs) => v => {
return funcs.reduce((res, func) => {
return func(res);
}, v);
};
let createTemplate = (name, callback) =>
fs.writeFile(`src/html/${name}.html`, `${name} is created!`, callback(`src/html/${name}.html`))
let createStyles = (name, callback) =>
fs.writeFile(`src/css/${name}.scss`, '', callback(`src/css/${name}.scss`));
let appendToIndex = (name) => {
let indexPath = 'src/html/index.html';
let readCallback = (err, data) => {
if (err) {
console.error(err);
process.exit(1);
}
let getHtml = (anchor) => (name) => `
<div class='list-item'>
//= ${name}.html
</div>
${anchor}
`
pipe(
anchor => ({ anchor, html: getHtml(anchor)(name) }),
({anchor, html}) => data.replace(anchor, html),
html => void fs.writeFile(indexPath, html, callbackFiles())
)('<!-- next-item -->');
}
fs.readFile(indexPath, 'utf8', readCallback);
}
let callbackFiles = (path) => (err) => {
if (err) {
console.log(err);
process.exit(1);
}
if (path !== undefined) {
console.log(`${path} is created successfully.`);
} else {
console.log('Successfull!')
}
};
let showHelp = () => void console.log(`
Usage:
- npm run g [arguments]
- npm run generate [arguments]
- node pure-css-cli.js [arguments]
Arguments:
help, h output usage information
file-name set new HTML and SCSS file name. Example: 'npm run generate example'.
`);
let getHelp = pipe(
argv => argv.findIndex(x => x === 'help' || x === 'h'),
index => {
if (index === -1) { return; }
showHelp();
process.exit(0);
}
);
let isValidName = (name) => name !== undefined && name.length > 0 && !name.includes('.');
void function() {
let argv = process.argv.slice(2);
getHelp(argv);
pipe(
name => isValidName(name) ? name : null,
name => {
if (name !== null) {
createTemplate(name, callbackFiles),
createStyles(name, callbackFiles),
appendToIndex(name)
}
}
)(argv[0])
}()