UNPKG

4.24 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const Operations = require('./operations');
4const Package = require('../package');
5const Muleify = require('../index');
6const Parse = require('./parse');
7const Cliy = require('cliy');
8const Path = require('path');
9const Fs = require('fs');
10
11(async function() {
12
13 const program = new Cliy();
14
15 await program.setup({
16 name: Package.name,
17 version: Package.version,
18 operations: [
19 {
20 key: 'p',
21 name: 'pack',
22 description: 'Packs folder or file and muleifies',
23 method: async function (argument, options) {
24 const data = await Parse(argument);
25
26 program.log('\nMuleify Packing\n', ['underline', 'cyan']);
27
28 await Muleify.pack(data.input, data.output, options);
29
30 if (options.watch) {
31 const watcher = await Muleify.watcher(data.input, data.output, options);
32
33 watcher.on('error', function (error) {
34 program.log(error.stack, ['red']);
35 });
36
37 watcher.on('change', function (path) {
38 Promise.resolve().then(function () {
39 return Muleify.pack(data.input, data.output, options);
40 }).then(function () {
41 program.log('Changed: ' + path, ['magenta']);
42 }).catch(function (error) {
43 program.log(error.stack, ['red']);
44 });
45 });
46 }
47
48 program.log(`Input: ${data.input}`, ['magenta']);
49 program.log(`Output: ${data.output}`, ['magenta']);
50 },
51 operations: [
52 Operations.path,
53 Operations.watch,
54 Operations.bundle,
55 Operations.minify,
56 Operations.transpile
57 ]
58 },
59 {
60 key: 's',
61 name: 'serve',
62 description: 'Serves folder and muleifies',
63 method: async function (argument, options) {
64 const data = await Parse(argument, false);
65
66 program.log('\nMuleify Serving\n', ['underline', 'cyan']);
67
68 if (data.output) {
69 await Muleify.pack(data.input, data.output, options);
70 }
71
72 const server = await Muleify.server(data.input, data.output, options);
73
74 program.log(`Served: ${server.hostname}:${server.port}`, ['green']);
75 program.log(`Input: ${data.input}`, ['magenta']);
76
77 if (data.output) {
78 program.log(`Output: ${data.output}\n`, ['magenta']);
79 }
80
81 if (options.watch) {
82 const watcher = await Muleify.watcher(data.input, data.output, options);
83
84 watcher.on('error', function (error) {
85 program.log(error.stack, ['red']);
86 });
87
88 watcher.on('change', function (path) {
89 Promise.resolve().then(function () {
90 if (data.output) {
91 return Muleify.pack(data.input, data.output, options);
92 }
93 }).then(function () {
94 program.log('Changed: ' + path, ['magenta']);
95 }).catch(function (error) {
96 program.log(error.stack, ['red']);
97 });
98 });
99
100 }
101
102 },
103 operations: [
104 Operations.spa,
105 Operations.cors,
106 Operations.path,
107 Operations.watch,
108 Operations.bundle,
109 Operations.minify,
110 Operations.transpile
111 ]
112 },
113 {
114 key: 'm',
115 name: 'map',
116 description: 'Creates XML sitemap',
117 method: async function (argument, options) {
118 const data = await Parse(argument);
119
120 program.log('\nMuleify Mapping\n', ['underline', 'cyan']);
121
122 await Muleify.map(data.input, data.output, options);
123
124 program.log(`Input: ${data.input}`, ['magenta']);
125 program.log(`Output: ${data.output}`, ['magenta']);
126 },
127 operations: [ Operations.domain ]
128 },
129 {
130 key: 'e',
131 name: 'encamp',
132 description: 'Creates folders and files from a json file',
133 method: async function (argument, options) {
134 const data = await Parse(argument);
135
136 program.log('\nMuleify Encamping\n', ['underline', 'cyan']);
137
138 await Muleify.encamp(data.input, data.output);
139
140 program.log(`Input: ${data.input}`, ['magenta']);
141 program.log(`Output: ${data.output}`, ['magenta']);
142 }
143 },
144 {
145 key: 'i',
146 name: 'install-sass',
147 description: 'Installs sass/scss compiler (might require sudo)',
148 method: async function (argument, options) {
149 program.log('Installing...', ['white']);
150 const result = await Muleify.sass();
151 program.log(result, ['white']);
152 }
153 }
154 ]
155 });
156
157 await program.run(process.argv);
158
159}()).catch(function (error) {
160 console.error(error);
161});