UNPKG

2.92 kBJavaScriptView Raw
1'use strict';
2
3const Transform = require('./lib/transform');
4const Sitemap = require('./lib/sitemap');
5const Global = require('./lib/global');
6const Observey = require('observey');
7const Servey = require('servey');
8const Porty = require('porty');
9const Path = require('path');
10const Fsep = require('fsep');
11
12const LB = Global.lb;
13const IGNOREABLES = Global.ignoreables;
14
15const directory = async function (input, output, options) {
16 let beforePaths = [];
17 let afterPaths = [];
18
19 const paths = await Fsep.walk({
20 path: input,
21 ignoreDot: true,
22 filters: IGNOREABLES
23 });
24
25 paths.forEach(function (path) {
26 if (LB.test(path)) {
27 beforePaths.push(path);
28 } else {
29 afterPaths.push(path);
30 }
31 });
32
33 await Promise.all(beforePaths.map(async function (path) {
34 await Transform(Path.join(input, path), Path.join(output, path), options);
35 }));
36
37 await Promise.all(afterPaths.map(async function (path) {
38 await Transform(Path.join(input, path), Path.join(output, path), options);
39 }));
40
41};
42
43const file = async function (input, output, options) {
44 await Transform(input, output, options);
45};
46
47exports.pack = async function (input, output, options) {
48
49 input = Path.resolve(process.cwd(), input);
50
51 if (!Fsep.existsSync(input)) {
52 throw new Error(`Input path does not exist ${input}`);
53 }
54
55 output = Path.resolve(process.cwd(), output);
56
57 Global.input = input; // TODO find a way to remove this
58
59 const stat = await Fsep.stat(input);
60
61 if (stat.isFile()) {
62 await file(input, output, options);
63 } else if (stat.isDirectory()) {
64 await directory(input, output, options);
65 } else {
66 throw new Error(`Input is not a file or direcotry ${input}`);
67 }
68};
69
70exports.encamp = async function (input, output) {
71 const data = await Fsep.readFile(input);
72 data = JSON.parse(data);
73 await Fsep.scaffold(output, data);
74};
75
76exports.map = async function (input, output, options) {
77 const paths = await Fsep.walk({
78 path: input,
79 ignoreDot: true,
80 filters: IGNOREABLES
81 });
82
83 const sitemap = await Sitemap(paths, options.domain);
84 const path = Path.join(output, 'sitemap.xml');
85
86 await Fsep.outputFile(path, sitemap);
87};
88
89exports.sass = async function () {
90 return await Terminal({
91 cmd: 'npm',
92 args: ['i', '--no-save', 'node-sass'],
93 cwd: __dirname
94 });
95};
96
97exports.watcher = async function (input, output, options) {
98
99 const observer = new Observey({
100 path: options.path || input
101 });
102
103 await observer.open();
104
105 return observer;
106};
107
108exports.server = async function (input, output, options) {
109 const port = await Porty.find(8080);
110
111 const server = new Servey({
112 port: port,
113 cache: false,
114 cors: options.cors,
115 hostname: 'localhost',
116 routes: [
117 {
118 path: '*',
119 method: 'get',
120 handler: async function (context) {
121 return await context.tool.static({
122 spa: options.spa,
123 folder: output || input,
124 path: context.url.pathname
125 });
126 }
127 }
128 ]
129 });
130
131 await server.open();
132
133 return server;
134};