-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
132 lines (111 loc) · 3.7 KB
/
gulpfile.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
const gulp = require('gulp');
const yargs = require('yargs');
const path = require('path');
const through = require('through2');
const handlebars = require('gulp-compile-handlebars');
const rename = require('gulp-rename');
const merge = require('gulp-merge');
const replace = require('gulp-replace');
(function () {
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
};
String.prototype.pathMinusOneSubDir = function () {
return this.split('/').slice(0,-1).join('/');
}
}
)(this);
let modulePath;
//console arguments
const argv = yargs.argv;
function parsePath() {
return through.obj(function (file, enc, cb) {
modulePath = path.resolve(file.path);
cb();
});
}
/**
* Define as arguments
* ModuleName as --name (represent folder and file name)
* Path as --path (represent path to insert module)
*/
gulp.task('module', (cb) => {
const moduleName = argv.name;
const path = argv.path;
if(!moduleName || moduleName === undefined || moduleName == null) {
throw new Error('Module name can\'t be null');
}
if(!path || path === undefined || path == null) {
throw new Error('Path can\'t be null');
}
const templateData = {
moduleName: moduleName
};
const options = {
helpers : {
capitalize : function(str){
return str.capitalize();
}
}
};
return gulp.src('templates/module.hbs')
.pipe(handlebars(templateData,options))
.pipe(rename(moduleName+'Module.ts'))
.pipe(gulp.dest(path+'/'+moduleName+'Module'));
});
/**
* Define as argument:
* ModuleName as --module (name folder example appModule),
* ComponentName as --alias (name component example card),
*/
gulp.task('component',['searchPath'], (cb) => {
let moduleName = argv.module;
const componentName = argv.alias;
if(!componentName || componentName === undefined || componentName == null){
throw new Error('Component Name can\'t be null');
}
if(!moduleName || moduleName === undefined || moduleName == null) {
throw new Error('Module Name can\'t be null');
}
const templateData = {
componentName: componentName
};
const options = {
helpers : {
capitalize : function(str){
return str.capitalize();
}
}
};
console.log('modulePath ->', modulePath);
const createComponent =
gulp.src('templates/component.hbs')
.pipe(handlebars(templateData,options))
.pipe(rename(`${componentName}.component.ts`))
.pipe(gulp.dest(`${modulePath}/components/${componentName}Component`));
const createHtml =
gulp.src('templates/template.hbs')
.pipe(handlebars(templateData,options))
.pipe(rename(`${componentName}.component.html`))
.pipe(gulp.dest(`${modulePath}/components/${componentName}Component`));
const createSass =
gulp.src('templates/style.hbs')
.pipe(handlebars(templateData,options))
.pipe(rename(`${componentName}.component.scss`))
.pipe(gulp.dest(`${modulePath}/components/${componentName}Component`));
//TODO fix path
//const addAndImportComponentInModule =
//gulp.src(`src/**/*${moduleName}/${moduleName}.ts`)
//.pipe(replace('/*@addComponent*/', function () {
//return `${componentName.capitalize()}Component, /*@addComponent*/`;
//}))
//.pipe(replace('/*@addImport*/', `import {${componentName.capitalize()}Component} from './${componentName}Component/${componentName}.component' \n/*@addImport*/`))
//.pipe(gulp.dest(`${modulePathMinusOneSubDir}`));
return merge(createComponent, createHtml, createSass);
});
gulp.task('searchPath', ()=> {
let moduleName = argv.module;
console.log('moduleName ->', moduleName);
return gulp.src(`src/**/*${moduleName}/`)
.pipe(parsePath())
});