-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzedpack.js
More file actions
executable file
·71 lines (63 loc) · 1.73 KB
/
zedpack.js
File metadata and controls
executable file
·71 lines (63 loc) · 1.73 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
#! /usr/bin/env node
const argv = require('minimist')(process.argv.slice(2))
const fs = require('fs')
const strings = {
css: "",
js: "",
html: "",
regex: {}
}
function zedpack() {
getCSS()
}
function getCSS() {
getTextFromFile(argv.css, getJS, "css")
}
function getJS() {
getTextFromFile(argv.js, getHTML, "js")
}
function getHTML() {
getTextFromFile(argv.html, createOutput, "html")
}
function createOutput() {
const htmlLines = strings.html.split('\n')
for(let x = 0; x < htmlLines.length; x++) {
if(htmlLines[x].indexOf(strings.regex.css) !== -1) {
htmlLines[x] = `<style>${strings.css}</style>`
}
else if(htmlLines[x].indexOf(strings.regex.js) !== -1) {
htmlLines[x] = `<script>${strings.js}</script>`
}
}
if(argv.output) {
writeToFile(htmlLines)
}
else {
console.log(htmlLines.join('\n'))
}
}
function writeToFile(data) {
fs.writeFile(argv.output, data.join('\n'), function (err) {
if (err) {
throw err
console.log('Could not write to file. Try another filename, or leave out the filename and pipe from stdout')
}
})
}
function getTextFromFile(filename, nextFunction, string) {
if(filename) {
fs.readFile(filename, { encoding: 'UTF-8' }, (err, data) => {
if (err) {
console.log("Could not find ", filename)
process.exit(1)
}
strings[string] += "\n" + data
// strip the filepath and create a RegExp with the filename
strings.regex[string] = argv[string].substring(argv[string].lastIndexOf('/') + 1)
nextFunction()
})
}
else nextFunction()
}
// Start packing!
zedpack()