-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
118 lines (92 loc) · 3.07 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
const Transform = require('./lib/transform');
const Sitemap = require('./lib/sitemap');
const Global = require('./lib/global');
const Observey = require('observey');
const Porty = require('porty');
const Path = require('path');
const Fsep = require('fsep');
const Nety = require('nety');
const LB = Global.lb;
const IGNOREABLES = Global.ignoreables;
const { Server, File, Normalize, Preflight } = Nety.HttpServer;
const directory = async function (input, output, options) {
let before = [];
let after = [];
const paths = await Fsep.walk({
path: input,
ignoreDot: true,
filters: IGNOREABLES
});
paths.forEach(path => {
if (LB.test(path)) {
before.push(path);
} else {
after.push(path);
}
});
await Promise.all(before.map(async path => {
return Transform(Path.join(input, path), Path.join(output, path), options);
}));
await Promise.all(after.map(async path => {
return Transform(Path.join(input, path), Path.join(output, path), options);
}));
};
const file = async function (input, output, options) {
await Transform(input, output, options);
};
exports.packer = async function (input, output, options) {
input = Path.resolve(process.cwd(), input);
if (!Fsep.existsSync(input)) {
throw new Error(`Input path does not exist ${input}`);
}
output = Path.resolve(process.cwd(), output);
Global.input = input; // TODO find a way to remove this
const stat = await Fsep.stat(input);
if (stat.isFile()) {
await file(input, output, options);
} else if (stat.isDirectory()) {
await directory(input, output, options);
} else {
throw new Error(`Input is not a file or direcotry ${input}`);
}
};
exports.watcher = async function (path, options) {
const observer = new Observey({ path: path });
await observer.open();
return observer;
};
exports.server = async function (folder, options) {
const { spa } = options || {};
const port = await Porty.find(8080);
const file = new File();
const normalize = new Normalize();
const preflight = new Preflight();
const server = new Server({ port, debug: true, host: 'localhost' });
await server.add(normalize);
await server.add(preflight);
await server.add(file);
await server.get(async context => context.file({ spa, folder, path: context.url.pathname }));
await server.open();
return server;
};
exports.encamp = async function (input, output) {
const data = await Fsep.readFile(input);
await Fsep.scaffold(output, JSON.parse(data));
};
exports.map = async function (input, output, domain) {
const paths = await Fsep.walk({
path: input,
ignoreDot: true,
filters: IGNOREABLES
});
const sitemap = await Sitemap(paths, domain);
const path = Path.join(output, 'sitemap.xml');
await Fsep.outputFile(path, sitemap);
};
exports.sass = async function () {
return await Terminal({
cmd: 'npm',
args: [ 'i', '--no-save', 'node-sass' ],
cwd: __dirname
});
};