-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·387 lines (339 loc) · 11.8 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env node
'use strict';
/*
The MIT License (MIT)
Copyright (c) Alexis Puret <puret.alexis@gmail.com> (slayug.fr)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var writeFile = require('write');
var ArgumentParser = require('argparse').ArgumentParser;
var prompt = require('prompt-sync')();
var read = require('read-file');
var colors = require('colors');
const fileExists = require('file-exists');
const TAG_LOG = "[tnsg] ";
const BREAK_LINE = '\n';
const TAB_CHAR = ' ';
const APP_PATH = './app/'
const DEFAULT_PATH_PAGE = 'pages/';
const DEFAULT_PATH_SERVICE = 'shared/'
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
//COMMAND DESCRIPTION
var parser = new ArgumentParser({
version: '0.1.7',
addHelp: true,
description: 'tnsg'
});
parser.addArgument(
[ '-page', '-p', ],
{
help: '-p my-new-page b',
nargs: '*'
}
);
parser.addArgument(
[ '-service', '-s' ],
{
help: '-s my-new-service',
nargs: '*'
}
);
parser.addArgument(
[ '-class', '-c' ],
{
help: '-c my-new-class',
nargs: '*'
}
);
parser.addArgument(
[ '-view', '-vi' ],
{
help: '-vi my-new-view',
nargs: '*'
}
);
var args = parser.parseArgs();
if( args.page != null ){
for(var p = 0; p < args.page.length; p++){
var result = getPathAndName( args.page[ p ] );
var name = result.name;
var path = result.path;
log( 'creating page: ' + adaptName( name ) + "..." );
createPage( name, path );
}
}
if( args.class != null ){
for(var c = 0; c < args.class.length; c++){
log( "creating class " + args.class[ c ].capitalize() + "..." );
createClass( args.class[ c ] );
}
}
if( args.service != null ){
for(var p = 0; p < args.service.length; p++){
log( 'creating service: ' + args.service[ p ] + "..");
createService( args.service[ p ]);
}
}
if( args.view != null ){
for(var v = 0; v < args.view.length; v++){
var result = getPathAndName( args.view[ v ] );
var name = result.name;
var path = result.path;
log( 'creating view: ' + adaptName( name ) + "..");
createView( name, path);
}
}
function getPathAndName( inputName ){
let name = inputName;
let path = '';
if( inputName.indexOf( '/' ) != -1 ){
var pageNameSplitted = inputName.split( '/' );
name = pageNameSplitted[ pageNameSplitted.length - 1 ];
for(var s = 0; s < pageNameSplitted.length - 1; s++ ){
path += pageNameSplitted[ s ] + "/";
}
}
return { name: name, path: path };
}
function adaptName( name ){
if( name.indexOf( '-' ) != -1 ){
var newName = '';
var nameSplitted = name.split( '-' );
for(var n = 0; n < nameSplitted.length; n++){
newName += nameSplitted[ n ].capitalize();
}
return newName;
}
return name.capitalize();
}
/**
* Create the page with the name passed
* Create the following folders: app/pages/pageName
* Then create the following files:
* - pageName.android.css
* - pageName.ios.css
* - pageName-common.css
* - pageName.component.ts
* - pageName.html
* In last time update the app.module.ts
* @param {String} 'pageName' page name will be created
* @param {String} 'path' path of the page
**/
function createPage( pageName, path ){
//create page with intermediates folders
writeFileWithCheck( APP_PATH + path +pageName + '/' +
pageName + '.android.css' );
writeFileWithCheck( APP_PATH + path + pageName + '/' +
pageName + '.ios.css' );
writeFileWithCheck( APP_PATH + path + pageName + '/' +
pageName + '-common.css' );
writeFileWithCheck( APP_PATH + path + pageName + '/' +
pageName + '.component.ts', generateContentComponent( pageName ) );
writeFileWithCheck( APP_PATH + path + pageName + '/' +
pageName + '.html', "<Label text='hello world, i am " + pageName +
" page.'></Label>");
insertComponentInModule( pageName, path );
}
function createView( viewName, path ){
//create view with intermediates folders
writeFileWithCheck( APP_PATH + path + viewName + '/' +
viewName + '.css' );
writeFileWithCheck( APP_PATH + path + viewName + '/' +
viewName + '.xml', generateContentView( viewName ) );
writeFileWithCheck( APP_PATH + path + viewName + '/' +
viewName + '.js', generateContentLogicView( viewName ) );
}
function generateContentLogicView( viewName ){
return 'exports.' + adaptName( viewName ) + 'Loaded = function() {' + BREAK_LINE +
TAB_CHAR + TAB_CHAR + 'console.log(" ' + adaptName( viewName ) + ' Loaded ");' + BREAK_LINE + '};';
}
function generateContentView( viewName ){
return '<Page loaded="' + adaptName( viewName ) + 'Loaded">' +
BREAK_LINE + '</Page>';
}
/**
*
* First check if the file exist, if it is, ask the user if the file should
* be rewrite, then do it or not.
* @param {String} 'filePath' path of the file with his name
* @param {String} 'content' optionnel, content who will be writed in the file
**/
function writeFileWithCheck( filePath, content ){
if( fileExists.sync( filePath ) ){
//prompt if should rewrite the file
var shouldContinue = prompt( TAG_LOG + "file " + filePath.underline +
" exists, rewrite ? [n|y] (n):", "n");
if( shouldContinue === 'n' ){
//assign if undefined
return;
}
}
content = content || '';
writeFile(filePath, content, function(err) {
if (err) throw new Error("Cannot writeFile " + filePath +
BREAK_LINE + err );
});
createdX( filePath );
}
/**
* generate the content of a component
* i.e with pageName = Login
* import { Component, OnInit } from "@angular/core";
* @Component({
* selector: "my-login",
* templateUrl: "pages/login/login.html",
* styleUrls: ["pages/login/login-common.css", "pages/login/login.css"]
* })
* export class LoginComponent implements OnInit {
* constructor() {}
*
* ngOnInit() {}
** }
* @param {String} 'pageName' name of the page, it will be the name of the
* component: PageNameComponent
**/
function generateContentComponent( pageName ){
const DEFAULT_IMPORT = 'import { Component, OnInit } from "@angular/core";';
const COMPONENT_PART = '@Component({' + BREAK_LINE +
TAB_CHAR + 'selector: "page-' + pageName + '",' + BREAK_LINE +
TAB_CHAR + 'templateUrl: "' + DEFAULT_PATH_PAGE + pageName + '/' +
pageName + '.html",' + BREAK_LINE +
TAB_CHAR + 'styleUrls: ["' + DEFAULT_PATH_PAGE + pageName + '/' +
pageName + '-common.css", "' + DEFAULT_PATH_PAGE +
pageName + '/' + pageName + '.css"]' + BREAK_LINE +
'})' + BREAK_LINE;
const EXPORT_CLASS = 'export class ' + adaptName( pageName ) + 'Component implements OnInit {' + BREAK_LINE;
const CONSTRUCTOR = TAB_CHAR + 'constructor(){}' + BREAK_LINE;
const NG_ON_INIT = TAB_CHAR + 'ngOnInit(){}' + BREAK_LINE;
const CLOSE_BRACKET = '}';
return DEFAULT_IMPORT +
BREAK_LINE + BREAK_LINE +
COMPONENT_PART + BREAK_LINE +
EXPORT_CLASS + BREAK_LINE +
CONSTRUCTOR + BREAK_LINE +
NG_ON_INIT + BREAK_LINE +
CLOSE_BRACKET;
}
/**
* i.e with className = user
* export class User{
* }
* @param {String} 'className' name of the class, it will be the name of the
* class: ClassName
**/
function generateContentClass( className ){
return "export class " + adaptName( pageName ) + "{" + BREAK_LINE + "}";
}
/**
* Write the file, with the default content of the class
* @param {String} 'className' name of the class, it will be the name of the
* class: ClassName
**/
function createClass( className ){
writeFileWithCheck( APP_PATH + DEFAULT_PATH_SERVICE + className +
'/' + className + '.ts', generateContentClass( className )
);
}
/**
* Update app.module.ts, insert the import of the component,
* then insert the name of the component in array declarations.
* @param {String} 'componentName' name of the component.
*
**/
function insertComponentInModule( componentName, path ){
if( ! fileExists.sync( './app/app.module.ts' ) ){
logError( './app/app.module.ts'.underline + ' not found.' );
return;
}
var content = read.sync('./app/app.module.ts', 'utf8');
//check if Component already inserted in app.module.js
if( content.indexOf( adaptName( componentName ) + "Component" ) !== -1 ){
return;
}
//inserting import
var headerIndex = content.indexOf( '@NgModule' ) - 1;
var firstPart = content.substring( 0, headerIndex );
firstPart += 'import { ' + adaptName( componentName ) +
'Component } from \'./' + path + componentName + '/' + componentName +
'.component\';' +
BREAK_LINE + BREAK_LINE;
content = firstPart + content.substring( headerIndex + 1, content.length );
//inserting declarations
var preIndex = content.indexOf( 'declarations' );
var commaIndex = preIndex + content.substring( preIndex ).indexOf( ']' );
firstPart = content.substring( 0, commaIndex );
firstPart += ',' + BREAK_LINE + TAB_CHAR + TAB_CHAR +
adaptName( componentName ) + "Component";
var secondPart = content.substring( commaIndex, content.length );
content = firstPart + secondPart;
//update app.module
writeFile('./app/app.module.ts', content, function(err) {
if (err) log(err);
});
updatedX( APP_PATH + 'app.module.ts' );
}
/**
* i.e with serviceName = user
* import { Injectable } from '@angular/core';
*
* @Injectable()
* export class UserService {
* constructor() { }
* }
*
* @param {String} 'serviceName' name of the service, it will be the name of the
* service: ServiceNameService
**/
function generateContentService( serviceName ){
return "import { Injectable } from '@angular/core';" + BREAK_LINE +
BREAK_LINE +
"@Injectable()" + BREAK_LINE +
"export class " + adaptName( serviceName ) + "Service {"+ BREAK_LINE +
TAB_CHAR + "constructor() { }" + BREAK_LINE + "}";
}
/**
* Write the file, with the default content of service
* @param {String} 'className' name of the service, it will be the name of the
* service: ServiceNameService
**/
function createService( serviceName ){
writeFileWithCheck( APP_PATH + DEFAULT_PATH_SERVICE + serviceName +
'/' + serviceName + '.service.ts',
generateContentService( serviceName )
);
warning( 'Service is generated but not provided, it must be provided to be used.' );
}
function log( msg ){
console.log( TAG_LOG + msg );
}
function createdX( x ){
log( "created: ".green + x );
}
function updatedX( x ){
log( "updated: ".yellow + x );
}
function logSuccess( msg ){
log( msg.green );
}
function logError( msg ){
log( msg.red );
}
function warning( warning ){
log( 'WARNING'.yellow + ' ' + warning );
}