UNPKG

10.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const command_1 = require("@oclif/command");
4// Constants
5const Mustache = require('mustache');
6const fs = require('fs');
7const path = require('path');
8var helper = require('../helper');
9// Global variables
10var exec = require('child_process').exec;
11var name;
12var validationErrors = new Set([]);
13// Type declarations
14var MainAdditions;
15(function (MainAdditions) {
16 MainAdditions["ImportPages"] = "ImportPages";
17 MainAdditions["ModelPages"] = "ModelPages";
18 MainAdditions["ViewPages"] = "ViewPages";
19 MainAdditions["UpdatePages"] = "UpdatePages";
20 MainAdditions["MsgPages"] = "MsgPages";
21 MainAdditions["SubscriptionPages"] = "SubscriptionPages";
22 MainAdditions["RoutePages"] = "RoutePages";
23 MainAdditions["ToSession"] = "ToSession";
24})(MainAdditions || (MainAdditions = {}));
25var RouteAdditions;
26(function (RouteAdditions) {
27 RouteAdditions["RouteToString"] = "RouteToString";
28 RouteAdditions["RouteParser"] = "RouteParser";
29 RouteAdditions["RouteType"] = "RouteType";
30})(RouteAdditions || (RouteAdditions = {}));
31var errorMessages = [
32 {
33 msg: "Add -- TO SESSION comment inside toSession function.",
34 additionType: MainAdditions.ToSession
35 },
36 {
37 msg: "Add -- ROUTE MAIN comment inside maybeRoute case of changeRouteTo function.",
38 additionType: MainAdditions.RoutePages
39 },
40 {
41 msg: "Add -- SUBSCRIPTIONS PAGES comment inside model case of subscriptions function.",
42 additionType: MainAdditions.SubscriptionPages
43 },
44 {
45 msg: "Add -- MSG PAGES comment inside type Msg sum type.",
46 additionType: MainAdditions.MsgPages
47 },
48 {
49 msg: "Add -- UPDATE PAGES comment inside (msg, model) case in update function.",
50 additionType: MainAdditions.UpdatePages
51 },
52 {
53 msg: "Add -- VIEW PAGES comment inside model case of view function.",
54 additionType: MainAdditions.ViewPages
55 },
56 {
57 msg: "Add -- MODEL PAGES comment inside type Model sum type.",
58 additionType: MainAdditions.ModelPages
59 },
60 {
61 msg: "Add -- IMPORT PAGES comment in top of the file bellow module Main line.",
62 additionType: MainAdditions.ImportPages
63 },
64 {
65 msg: "Add -- ROUTE TYPE comment inside type Route sum type.",
66 additionType: RouteAdditions.RouteType
67 },
68 {
69 msg: "Add -- ROUTE TO STRING comment inside case page of routeToString function.",
70 additionType: RouteAdditions.RouteToString
71 },
72 {
73 msg: "Add -- ROUTE PARSER comment inside oneOf list in parser function.",
74 additionType: RouteAdditions.RouteParser
75 }
76];
77// Function declarations
78function ucfirst(n) {
79 return n.charAt(0).toUpperCase() + n.slice(1);
80}
81function checkIfCommentExsists(addition, data) {
82 var doesExsist = data.join("\n").includes(addition.comment.trim());
83 if (doesExsist === false) {
84 validationErrors.add(addition.additionType);
85 }
86 ;
87}
88function renderMustache(name) {
89 var view = { name: ucfirst(name) };
90 fs.readFile(path.join(__dirname, '..', '/..', "/templates/page-template.mustache"), function (err, data) {
91 if (err)
92 helper.logError(err);
93 else {
94 var template = data.toString();
95 Mustache.parse(template);
96 var rendered = Mustache.render(template, view);
97 fs.writeFile("elm/Page/" + ucfirst(name) + ".elm", rendered, function (err) {
98 if (err) {
99 return helper.logError(err);
100 }
101 });
102 }
103 });
104}
105function oneAddition(search, text, data) {
106 var index = data.findIndex(function (v) {
107 return v.includes(search);
108 });
109 if (index === -1) {
110 return data.join("\n");
111 }
112 if (text === undefined) {
113 return data.join("\n");
114 }
115 data.splice(index + 1, 0, text);
116 helper.logInfo({ added: search });
117 return data.join("\n");
118}
119function writeToFile(addition, data, name) {
120 helper.logInfo("Writing to " + name + ".elm");
121 var text;
122 addition.map(x => text = oneAddition(x.comment, x.text, data));
123 text = data.join("\n");
124 fs.writeFile(process.cwd() + "\\" + name, text, function (err) {
125 if (err)
126 return helper.logInfo(err);
127 });
128}
129function maybeAddToMainAndRoute(additionsToMain, additionsToRoute) {
130 fs.readFile(process.cwd() + "\\elm/Main.elm", function (err, dataOld) {
131 helper.logInfo("Reading Main.elm");
132 let dataMain;
133 dataMain = dataOld.toString().split("\n");
134 additionsToMain.map(x => checkIfCommentExsists(x, dataMain));
135 if (dataMain.includes("import Page." + ucfirst(name) + " as " + ucfirst(name))) {
136 helper.logInfo("Page " + ucfirst(name) + " was already added!");
137 return;
138 }
139 fs.readFile(process.cwd() + "\\elm/Route.elm", function (err, dataOld) {
140 helper.logInfo("Reading Route.elm");
141 let dataRoute;
142 dataRoute = dataOld.toString().split("\n");
143 additionsToRoute.map(x => checkIfCommentExsists(x, dataRoute));
144 if (validationErrors.size > 0) {
145 showErrors(validationErrors);
146 return;
147 }
148 if (fs.existsSync("elm/Page/" + name + ".elm")) {
149 helper.logInfo("File elm/Page" + name + ".elm already exsists. Adding needed code to Main.elm and Route.elm...");
150 }
151 else {
152 helper.logInfo("Rendering " + name + ".elm " + "file");
153 renderMustache(name);
154 }
155 writeToFile(additionsToMain, dataMain, "elm/Main.elm");
156 writeToFile(additionsToRoute, dataRoute, "elm/Route.elm");
157 helper.execWithOutput(exec('elm-format elm/Main.elm --yes'));
158 helper.logSuccess("Page has successfully been added!");
159 });
160 });
161}
162function showErrors(errors) {
163 let filteredMessages = errorMessages.filter(x => errors.has(x.additionType));
164 var obj = [];
165 var data = {};
166 for (var i = 0; i < filteredMessages.length; i++) {
167 data = {
168 error: filteredMessages[i].msg
169 };
170 obj.push(data);
171 }
172 obj.map(x => helper.logError(x));
173}
174class NewPage extends command_1.Command {
175 async run() {
176 const { args, flags } = this.parse(NewPage);
177 name = flags.name;
178 var additionsToMain = [
179 {
180 comment: "IMPORT PAGES",
181 text: "import Page." + ucfirst(flags.name) + " as " + ucfirst(flags.name) + "\n",
182 additionType: MainAdditions.ImportPages
183 },
184 {
185 comment: "MODEL PAGES",
186 text: " | " + ucfirst(flags.name) + " " + ucfirst(flags.name) + ".Model",
187 additionType: MainAdditions.ModelPages
188 },
189 {
190 comment: "VIEW PAGES",
191 text: ' ' + ucfirst(flags.name) + ' ' + 'arg ->' + '\n' +
192 ' viewPage Page.Other Got' + ucfirst(flags.name) + 'Msg' + ' (' + ucfirst(flags.name) + '.view arg)' + '\n',
193 additionType: MainAdditions.ViewPages
194 },
195 {
196 comment: "UPDATE PAGES",
197 text: " ( Got" + ucfirst(flags.name) + "Msg subMsg, " + ucfirst(flags.name) + " arg ) ->\n" +
198 " " + ucfirst(flags.name) + ".update subMsg arg\n" +
199 " |> updateWith " + ucfirst(flags.name) + " Got" + ucfirst(flags.name) + "Msg model\n",
200 additionType: MainAdditions.UpdatePages
201 },
202 {
203 comment: "MSG PAGES",
204 text: " | " + 'Got' + ucfirst(flags.name) + 'Msg' + ' ' + ucfirst(flags.name) + ".Msg",
205 additionType: MainAdditions.MsgPages
206 },
207 {
208 comment: "SUBSCRIPTIONS PAGES",
209 text: " " + ucfirst(flags.name) + " arg ->\n" +
210 " " + "Sub.map " + "Got" + ucfirst(flags.name) + "Msg (" + ucfirst(flags.name) + ".subscriptions arg)\n",
211 additionType: MainAdditions.SubscriptionPages
212 },
213 {
214 comment: "ROUTE MAIN",
215 text: ' Just Route.' + ucfirst(flags.name) + '->\n' +
216 ' ' + ucfirst(flags.name) + '.init session\n' +
217 ' |> updateWith ' + ucfirst(flags.name) + ' Got' + ucfirst(flags.name) + 'Msg model\n',
218 additionType: MainAdditions.RoutePages
219 },
220 {
221 comment: "TO SESSION",
222 text: ' ' + ucfirst(flags.name) + ' arg ->\n' +
223 ' ' + ucfirst(flags.name) + '.toSession arg\n',
224 additionType: MainAdditions.ToSession
225 }
226 ];
227 var additionsToRoute = [
228 {
229 comment: "ROUTE TO STRING",
230 text: ' ' + ucfirst(flags.name) + ' ->\n' +
231 ' [ "' + flags.name.toLowerCase() + ' " ]',
232 additionType: RouteAdditions.RouteToString
233 },
234 {
235 comment: "ROUTE PARSER",
236 text: " , " + "Parser.map " + ucfirst(flags.name) + ' (s "' + flags.name.toLowerCase() + '")',
237 additionType: RouteAdditions.RouteParser
238 },
239 {
240 comment: "ROUTE TYPE",
241 text: " | " + ucfirst(flags.name),
242 additionType: RouteAdditions.RouteType
243 }
244 ];
245 maybeAddToMainAndRoute(additionsToMain, additionsToRoute);
246 }
247}
248NewPage.description = 'describe the command here';
249NewPage.flags = {
250 help: command_1.flags.help({ char: 'h' }),
251 // flag with a value (-n, --name=VALUE)
252 name: command_1.flags.string({ char: 'n', description: 'name to print' }),
253};
254NewPage.args = [{ name: 'file' }];
255exports.default = NewPage;
256// interactivno dal želimo dodat manualno ili rollbackat
257// dodavanje paketa
258// rollback moguća opcija
259// refaktor da data nisu globalne varijable nego parametri - 1
260// refaktor writeToMain i writeToRoute funkcija - 1
261// refaktorat da je validationErrors set a ne array -1
262// error msg i prettyfier - 1
263// izvuć console pretty u zaseban file +
264// elm format logging bug +
265// success message kod uspješnog dodavanja - console.info +