-
Notifications
You must be signed in to change notification settings - Fork 35
/
gulpfile.js
227 lines (189 loc) · 8.11 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
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
const { dest, parallel, series, src, watch } = require('gulp');
const argv = require('yargs').argv;
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const del = require('del');
const version = require('./package.json').version;
const concat = require('gulp-concat');
const minify = require('gulp-minify');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const tap = require('gulp-tap');
const apply = require('postcss-apply');
const importCss = require('postcss-import');
const nested = require('postcss-nested');
const simpleVars = require('postcss-simple-vars');
const distDirectory = 'style';
const capitalize = string =>
string
.split('-')
.map(part => part.charAt(0).toUpperCase() + part.substring(1).toLowerCase())
.join(' ');
/**
* ============================================================
* TAREFAS PARA A GERAÇÃO DOS PACKAGES DOS TEMAS
* ============================================================
*/
const cleanTemp = () => del('./.temp');
const cleanThemeDir = () => del('./dist');
const copyThemeAssets = () =>
src('./src/assets/**/*.*').pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/`));
const copyThemePackageJson = () =>
src('package.json')
.pipe(
tap(file => {
const contents = JSON.parse(file.contents.toString());
delete contents.devDependencies;
delete contents.scripts;
contents.name = `@po-ui/style${argv.theme ? '-' + argv.theme : ''}`;
contents.description = `PO UI - Theme${argv.theme ? ' ' + capitalize(argv.theme) : ''}`;
file.contents = Buffer.from(JSON.stringify(contents, null, 2), 'utf-8');
})
)
.pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/`));
const copyThemeReadme = () =>
src('src/css/README.md').pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/`));
const copyThemeVariablesCss = () =>
src(`./src/css/themes/po-theme-default.css`)
.pipe(rename(`po-theme-default-variables.css`))
.on('error', err => {
console.log(err.toString());
this.emit('end');
})
.pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/css/`));
const prepareThemeCss = () => src('./src/**/*.css').pipe(dest('./.temp'));
const buildThemeCss = modern =>
src(`./.temp/css/index${modern ? '-modern' : ''}.css`)
.pipe(
tap(file => {
const contents = file.contents.toString().replace(/\${theme}/, argv.theme || 'default');
file.contents = Buffer.from(contents, 'utf-8');
})
)
.pipe(postcss([importCss(), apply(), nested(), simpleVars(), autoprefixer(), cssnano()]))
.pipe(rename(modern ? `css/po-theme-core.min.css` : `css/po-theme-default.min.css`))
.on('error', err => {
console.log(err.toString());
this.emit('end');
})
.pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/`));
const buildThemeVariablesCss = () =>
src(`./.temp/css/themes/po-theme-${argv.theme ? argv.theme : 'default'}.css`)
.pipe(postcss([cssnano()]))
.pipe(rename(`po-theme-${argv.theme ? argv.theme : 'default'}-variables.min.css`))
.on('error', err => {
console.log(err.toString());
this.emit('end');
})
.pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/css/`));
// Tarefa otimizada para desenvolvimento
const buildDevThemeCss = () =>
src('./.temp/css/index.css')
.pipe(
tap(file => {
const contents = file.contents.toString().replace(/\${theme}/, argv.theme || 'default');
file.contents = Buffer.from(contents, 'utf-8');
})
)
.pipe(postcss([importCss(), apply(), nested()]))
.pipe(rename(`css/po-theme-default.min.css`))
.on('error', err => {
console.log(err.toString());
this.emit('end');
})
.pipe(dest(`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/`));
const buildThemeCssModern = () => buildThemeCss(true);
const buildThemeCssLegacy = () => buildThemeCss(false);
const buildTheme = series(
cleanTemp,
parallel(copyThemeAssets, copyThemePackageJson, copyThemeReadme, copyThemeVariablesCss, prepareThemeCss),
parallel(buildThemeCssModern, buildThemeVariablesCss, buildThemeCssLegacy),
cleanTemp
);
buildTheme.displayName = 'build';
/**
* ============================================================
* TAREFAS PARA A GERAÇÃO DO APP DE TESTES DOS TEMAS
* ============================================================
*/
const cleanAppDir = () => del('./app-dist');
const copyApp = () => src('./src/app/*.*').pipe(dest('./app-dist'));
const copyAppAssets = () => src('./src/app/assets/**/*.*').pipe(dest('./app-dist/assets'));
const copyAppComponents = () => src(['./src/css/**/*.html', './src/css/**/*.js']).pipe(dest('./app-dist/css'));
const copyThemeToApp = () =>
src([`./dist/${distDirectory}${argv.theme ? '-' + argv.theme : ''}/**/*.*`, '!./dist/*.json']).pipe(
dest('./app-dist/assets/')
);
const buildAppJs = () =>
src(['./src/app/js/*.js', '!./src/app/js/po-chart.js'])
.pipe(concat('app.js'))
.pipe(
minify({
ext: {
min: '.min.js'
},
noSource: true
})
)
.pipe(
tap(file => {
file.contents = Buffer.from(`var version = '${version}';` + file.contents.toString(), 'utf-8');
})
)
.pipe(dest('./app-dist/js'));
const buildApp = series(
cleanAppDir,
parallel(copyApp, copyAppAssets, copyAppComponents, buildAppJs),
buildTheme,
copyThemeToApp
);
buildApp.displayName = 'build:app';
const buildCli = () => src([`./src/cli/**/*.*`, '!./src/cli/node_modules/**/*.*']).pipe(dest('./dist/cli/'));
buildCli.displayName = 'build:cli';
/**
* ============================================================
* TAREFAS GENÉRICAS
* ============================================================
*/
const clean = parallel(cleanThemeDir, cleanAppDir, cleanTemp);
const configThemeName = () => (argv.theme || 'default').substring(0, 29).padEnd(30);
const watchers = () => {
console.warn('\n');
console.warn(' ╔═════════════════════════════════════════════════╗');
console.warn(' ║ ║');
console.warn(' ║ NÃO ESQUEÇA DE INICIAR O SEU SERVIDOR HTTP! ║');
console.warn(' ║ ║');
console.warn(' ║ Execute em outro terminal (http-server): ║');
console.warn(' ║ - npm run dev ║');
console.warn(' ║ ║');
console.warn(' ║ Ou use o servidor que você achar melhor: ║');
console.warn(' ║ - live-server ║');
console.warn(' ║ - tanto faz ... ║');
console.warn(' ║ ║');
console.warn(' ║ Ao atualizar um arquivo CSS ou HTML a sua ║');
console.warn(' ║ aplicação será carregada automaticamente! ║');
console.warn(' ║ ║');
console.warn(' ║ TEMA UTILIZADO: ' + configThemeName() + '║');
console.warn(' ║ ║');
console.warn(' ╚═════════════════════════════════════════════════╝');
watch('./src/**/*.html', copyAppComponents);
watch('./src/**/*.css', series(prepareThemeCss, buildDevThemeCss, copyThemeToApp));
watch('./src/**/*.js', buildAppJs);
};
/**
* ============================================================
* EXPORT DAS TAREFAS DO GULP
* ============================================================
*/
// gulp clean
exports.clean = clean;
// gulp build
exports.build = series(clean, buildTheme);
// gulp build:app
exports.buildApp = buildApp;
// gulp build:cli
exports.buildCli = buildCli;
// gulp
exports.default = series(clean, buildApp);
// gulp watch
exports.watch = series(clean, buildApp, watchers);