1 | const fs = require('fs')
|
2 | const async = require('async')
|
3 | const path = require('path');
|
4 | var config = ""
|
5 | var packconfig = ""
|
6 | var babelrc = ""
|
7 | module.exports = (name,option)=>{
|
8 | var itemName = name;
|
9 | console.log(option)
|
10 | async.series({
|
11 | ReadgulpHeader(next){
|
12 |
|
13 |
|
14 |
|
15 | fs.readFile(path.resolve(__dirname,'../gulpfile','gulpHeader.js'), 'utf-8', function(err, data) {
|
16 |
|
17 | if (err) {
|
18 | throw err;
|
19 | }
|
20 |
|
21 |
|
22 | config = data.toString()
|
23 |
|
24 | next()
|
25 | });
|
26 | },
|
27 | ReadgulpScss(next){
|
28 |
|
29 | if(option.scss =='y'||'Y'){
|
30 |
|
31 | fs.readFile(path.resolve(__dirname,'../gulpfile','cssHandle.js'), 'utf-8', function(err, data) {
|
32 |
|
33 | if (err) {
|
34 | throw err;
|
35 | }
|
36 | config += data.toString()
|
37 |
|
38 | next()
|
39 | });
|
40 | }
|
41 | },
|
42 | ReadgulpEs6(next){
|
43 |
|
44 | if(option.es6 =='y'||'Y'){
|
45 | fs.readFile(path.resolve(__dirname,'../gulpfile','jsHandle.js'), 'utf-8', function(err, data) {
|
46 |
|
47 | if (err) {
|
48 | throw err;
|
49 | }
|
50 | config += data.toString()
|
51 | async.series({
|
52 |
|
53 | ReadWebpack(next){
|
54 |
|
55 | fs.readFile(path.resolve(__dirname,'../gulpfile','webpack.config.js'), 'utf-8', function(err, data) {
|
56 |
|
57 | if (err) {
|
58 | throw err;
|
59 | }
|
60 |
|
61 | packconfig = data.toString();
|
62 | next()
|
63 | });
|
64 | },
|
65 | WriteWebpack(next){
|
66 | fs.writeFile('./webpack.config.js', packconfig, function(err) {
|
67 | if (err) {
|
68 | throw err;
|
69 | }
|
70 | next()
|
71 | })
|
72 | },
|
73 | RmWebpack(next){
|
74 | fs.rename('./webpack.config.js',`./${itemName}/webpack.config.js`, function (err) {
|
75 | if(err) {
|
76 | console.error(err);
|
77 | return;
|
78 | }
|
79 |
|
80 | next()
|
81 | });
|
82 | },
|
83 | ReadBabel(next){
|
84 |
|
85 |
|
86 | fs.readFile(path.resolve(__dirname,'../gulpfile','.babelrc'), 'utf-8', function(err, data) {
|
87 |
|
88 | if (err) {
|
89 | throw err;
|
90 | }
|
91 |
|
92 | babelrc = data.toString();
|
93 | next()
|
94 | });
|
95 | },
|
96 | WriteBabel(next){
|
97 | fs.writeFile('./.babelrc', babelrc, function(err) {
|
98 | if (err) {
|
99 | throw err;
|
100 | }
|
101 | next()
|
102 | })
|
103 | },
|
104 | RmBabel(next){
|
105 | fs.rename('./.babelrc',`./${itemName}/.babelrc`, function (err) {
|
106 | if(err) {
|
107 | console.error(err);
|
108 | return;
|
109 | }
|
110 |
|
111 | next()
|
112 | });
|
113 | },
|
114 | MkModule(next){
|
115 | fs.mkdir(`./module`, function (err) {
|
116 | if(err)
|
117 | throw err;
|
118 |
|
119 | next()
|
120 | });
|
121 | },
|
122 | RmModule(next){
|
123 | fs.rename('./module',`./${itemName}/src/module`, function (err) {
|
124 | if(err) {
|
125 | console.error(err);
|
126 | return;
|
127 | }
|
128 |
|
129 | next()
|
130 | });
|
131 | }
|
132 | })
|
133 |
|
134 |
|
135 | next()
|
136 | });
|
137 | }
|
138 | },
|
139 | WriteGulpWatch(next){
|
140 | let libs = `gulp.task('copy-libs',function(){
|
141 | gulp.src('./src/libs/**/*')
|
142 | .pipe(gulp.dest('./build/libs'));
|
143 | })`
|
144 | let images = `gulp.task('copy-images', function () {
|
145 | gulp.src('./src/images/**/*')
|
146 | .pipe(gulp.dest('./build/images/'));
|
147 | }); `
|
148 | let watch = `gulp.task('build',function(){
|
149 | watch('./src/styles/*.scss',batch(function (events, done) {
|
150 | gulp.start('scss', done);
|
151 | }));
|
152 | watch('./src/images/**/*',batch(function (events, done) {
|
153 | gulp.start('copy-images', done);
|
154 | }));
|
155 | watch('./src/scripts/*.js',batch(function (events, done) {
|
156 | gulp.start('webpackjs', done);
|
157 | }));
|
158 | watch('./src/libs/**/*',batch(function (events, done) {
|
159 | gulp.start('copy-libs', done);
|
160 | }));
|
161 | });`
|
162 | config+=`${libs}
|
163 | ${images}
|
164 | ${watch}`
|
165 | next()
|
166 | },
|
167 | WritegulpHeader(next){
|
168 | fs.writeFile(`./${itemName}/gulpfile.js`, config, function(err) {
|
169 | if (err) {
|
170 | throw err;
|
171 | }
|
172 | console.log('gulpfile 已生成')
|
173 | next()
|
174 | })
|
175 |
|
176 | }
|
177 | })
|
178 | }
|