autofront 2.2.2 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -3
  2. package/index.js +143 -68
  3. package/package.json +10 -1
package/README.md CHANGED
@@ -64,7 +64,7 @@ Other command options, that appear immediately below, are available. And to use
64
64
 
65
65
  ### Tasks
66
66
 
67
- Mainly the Gulp ones are the following:
67
+ The Gulp ones are the following:
68
68
 
69
69
  - `gulp` or `gulp serve` are for running a test server and develop with live reload.
70
70
  - `gulp build` only builds production code, the distributable application (`dist` folder).
@@ -123,8 +123,7 @@ Any asset (e.g. a PDF document) will keep the location path and, particularly at
123
123
 
124
124
  ## Pending
125
125
 
126
- Fixes and improvements to do:
126
+ Improvements to do:
127
127
 
128
- - Once the server watch for changes, synchronize also the deletion of source files.
129
128
  - Replace Bower as dependency manager.
130
129
  - Migrate AngularJS to new [Angular](https://angular.io).
package/index.js CHANGED
@@ -9,33 +9,39 @@ const gulp = require('gulp'),
9
9
  injStr = $.injectString,
10
10
  gulpSass = $.sass(require('sass')),
11
11
  notifyError = $.notify.onError(error => error.message),
12
- browserSync = require('browser-sync').create();
12
+ browserSync = require('browser-sync').create(),
13
+ deleteEmpty = require('delete-empty');
13
14
 
14
15
  let domain = undefined;
15
16
 
16
- const allFiles = getFiles(),
17
+ const allFiles = getGlob(),
17
18
  indexHtmlFile = 'index.html',
19
+ jsFiles = getGlob('js'),
18
20
  cssFilename = 'index',
19
- cssFullFilename = cssFilename + '.css';
20
- let stylesFolder = 'styles/',
21
+ cssFile = cssFilename + '.css';
22
+ let stylesDir = 'styles/',
21
23
  jsTemplatesFile = 'scripts/templates.js';
22
24
 
23
- const paths = {
25
+ const globs = {
24
26
  src: 'src/',
25
27
  tmp: '.tmp/',
26
28
  dist: 'dist/'
27
29
  };
28
- paths.srcIndexHtml = paths.src + indexHtmlFile;
29
- paths.srcCss = paths.src + stylesFolder + cssFullFilename;
30
- paths.srcSass = paths.src + getFiles('scss');
31
- paths.srcLess = paths.src + getFiles('less');
32
- paths.srcJs = paths.src + getFiles('js');
33
- paths.srcOthers = [paths.src + allFiles, '!' + paths.srcIndexHtml, '!' + paths.srcCss, '!' + paths.srcSass, '!' + paths.srcLess, '!' + paths.srcJs];
30
+ globs.srcIndexHtml = globs.src + indexHtmlFile;
31
+ globs.srcJs = globs.src + jsFiles;
32
+ globs.srcIndexAndSrcJs = [globs.srcIndexHtml, globs.srcJs];
33
+ globs.srcStyles = [globs.src + stylesDir + cssFile, globs.src + getGlob('less'), globs.src + getGlob('scss')];
34
+ globs.srcOthers = [globs.src + allFiles, ...[...globs.srcIndexAndSrcJs, ...globs.srcStyles].map(glob => '!' + glob)];
35
+ globs.tmpAllFiles = globs.tmp + allFiles;
34
36
 
35
37
  const nl = '\n',
36
38
  tab = ' ';
37
39
 
38
- gulp.task('check', () => {
40
+ function clean() {
41
+ return delDir(globs.tmp);
42
+ }
43
+
44
+ function manageDomain() {
39
45
  const pckg = require(path.resolve('package.json')),
40
46
  domains = pckg.domains;
41
47
  let domainIndex = args[0] || 'local';
@@ -49,63 +55,124 @@ gulp.task('check', () => {
49
55
  domain = domains[domainIndex];
50
56
  }
51
57
  const isMatched = domain !== undefined;
52
- return gulp.src(paths.src)
58
+ return gulp.src(globs.src, { read: false })
53
59
  .pipe($.notify(isMatched ? `Matching domain: "${domainIndex}".` : 'No domain matched.'));
54
- });
55
-
56
- gulp.task('del', () => delFolder(paths.tmp));
57
- gulp.task('index-build', () => gulp.src(paths.srcIndexHtml).pipe(injStr.after('<!-- endbuild -->', nl + tab + `<link rel="stylesheet" href="${stylesFolder}${cssFullFilename}">`)).pipe($.inject(gulp.src(paths.srcJs).pipe($.angularFilesort()), { relative: true })).on('error', notifyError).pipe($.wiredep()).pipe($.useref()).pipe(gulp.dest(paths.tmp)));
58
- gulp.task('index-domain', () => gulp.src(paths.tmp + getFiles('js')).pipe(injStr.replace('{{AUTOFRONT_DOMAIN}}', domain)).pipe(gulp.dest(paths.tmp)));
59
- gulp.task('index', gulp.series('index-build', 'index-domain'));
60
- gulp.task('styles', () => {
61
- return mergeStream(getCssStream('css'), getCssStream('scss', gulpSass, '@import "variables";'), getCssStream('less', $.less))
62
- .pipe($.concat(cssFullFilename))
63
- .pipe(gulp.dest(paths.tmp + stylesFolder))
60
+ }
61
+ manageDomain.displayName = 'manage-domain';
62
+
63
+ function buildIndex() {
64
+ return gulp.src(globs.srcIndexHtml)
65
+ .pipe(injStr.after('<!-- endbuild -->', nl + tab + `<link rel="stylesheet" href="${stylesDir + cssFile}">`))
66
+ .pipe($.inject(gulp.src(globs.srcJs).pipe($.angularFilesort()), { relative: true })).on('error', notifyError)
67
+ .pipe($.wiredep())
68
+ .pipe($.useref())
69
+ .pipe(gulp.dest(globs.tmp));
70
+ }
71
+ buildIndex.displayName = 'build-index';
72
+
73
+ function injectDomain() {
74
+ return gulp.src(globs.tmp + jsFiles)
75
+ .pipe(injStr.replace('{{AUTOFRONT_DOMAIN}}', domain))
76
+ .pipe(gulp.dest(globs.tmp));
77
+ }
78
+ injectDomain.displayName = 'inject-domain';
79
+
80
+ const indexAndJs = gulp.series(buildIndex, injectDomain);
81
+
82
+ function styles() {
83
+ return mergeStream(getStream('css'), getStream('less', $.less), getStream('scss', gulpSass, '@import "variables";'))
84
+ .pipe($.concat(cssFile))
85
+ .pipe(gulp.dest(globs.tmp + stylesDir))
64
86
  .pipe(browserSync.stream());
65
87
 
66
- function getCssStream(ext, process, extraCode) {
67
- let stream = gulp.src(paths.src + stylesFolder + cssFilename + '.' + ext, { allowEmpty: true });
88
+ function getStream(ext, process, extraCode) {
89
+ let stream = gulp.src(globs.src + stylesDir + cssFilename + '.' + ext, { allowEmpty: true });
68
90
  if (process)
69
- return stream.pipe(injStr.prepend((extraCode ? extraCode + nl : '') + '// bower:' + ext + nl + '// endbower' + nl)).pipe($.wiredep()).pipe(process()).on('error', notifyError);
91
+ return stream
92
+ .pipe(injStr.prepend((extraCode ? extraCode + nl : '') + '// bower:' + ext + nl + '// endbower' + nl))
93
+ .pipe($.wiredep())
94
+ .pipe(process()).on('error', notifyError);
70
95
  return stream;
71
96
  }
72
- });
73
- gulp.task('fonts', () => gulp.src(mainBowerFiles()).pipe(filter(['eot', 'otf', 'svg', 'ttf', 'woff', 'woff2'], true)).pipe(gulp.dest(paths.tmp + 'fonts/')));
74
- gulp.task('others', () => {
97
+ }
98
+
99
+ function fonts() {
100
+ return gulp.src(mainBowerFiles())
101
+ .pipe(filter(['eot', 'otf', 'svg', 'ttf', 'woff', 'woff2'], true))
102
+ .pipe(gulp.dest(globs.tmp + 'fonts/'));
103
+ }
104
+
105
+ function others() {
75
106
  const pugFilter = filter('pug');
76
- return gulp.src(paths.srcOthers)
107
+ return gulp.src(globs.srcOthers)
77
108
  .pipe(pugFilter).pipe($.pug()).on('error', notifyError).pipe(pugFilter.restore)
78
- .pipe(gulp.dest(paths.tmp));
79
- });
80
- gulp.task('about', () => gulp.src('package.json').pipe($.about()).pipe(gulp.dest(paths.tmp)));
81
- gulp.task('build:tmp', gulp.series('del', 'check', gulp.parallel('index', 'styles', 'fonts', 'others', 'about')));
82
- gulp.task('browser', gulp.series('build:tmp', (cb) => {
83
- browserSyncInit(paths.tmp);
109
+ .pipe(gulp.dest(globs.tmp));
110
+ }
111
+
112
+ function about() {
113
+ return gulp.src('package.json')
114
+ .pipe($.about())
115
+ .pipe(gulp.dest(globs.tmp));
116
+ }
117
+
118
+ const buildTmp = gulp.series(
119
+ gulp.parallel(clean, manageDomain),
120
+ gulp.parallel(indexAndJs, styles, fonts, others, about)
121
+ );
122
+
123
+ function browser(cb) {
124
+ browserSyncInit(globs.tmp);
84
125
  cb();
85
- }));
86
- gulp.task('serve', gulp.series('browser', () => {
87
- gulp.watch([paths.srcIndexHtml, paths.srcJs], gulp.task('index'));
88
- gulp.watch([paths.srcCss, paths.srcSass, paths.srcLess], gulp.task('styles'));
89
- gulp.watch(paths.srcOthers, gulp.task('others'));
90
- gulp.watch([paths.tmp + allFiles, '!' + paths.tmp + stylesFolder + cssFullFilename], function (cb) {
126
+ }
127
+
128
+ function watch() {
129
+ gulp.watch(globs.srcIndexAndSrcJs, indexAndJs);
130
+ gulp.watch(globs.srcStyles, styles);
131
+ gulp.watch(globs.srcOthers, others).on('unlink', function (path) {
132
+ gulp.src(path.replaceAll('\\', '/').replace(globs.src, globs.tmp).replace('.pug', '.html'), { read: false })
133
+ .pipe($.clean());
134
+ deleteEmpty(globs.tmp);
135
+ });
136
+ gulp.watch([globs.tmpAllFiles, '!' + globs.tmp + stylesDir + cssFile], function (cb) {
91
137
  browserSync.reload();
92
138
  cb();
93
139
  });
94
- }));
95
-
96
- gulp.task('del:dist', () => delFolder(paths.dist));
97
- gulp.task('copy', gulp.series('del:dist', () => gulp.src(paths.tmp + allFiles).pipe(gulp.dest(paths.dist))));
98
- gulp.task('templates-build', () => gulp.src([paths.dist + getFiles('html'), '!' + paths.dist + indexHtmlFile]).pipe($.cleanDest(paths.dist)).pipe(minifyHtml()).pipe($.angularTemplatecache(jsTemplatesFile, { module: 'app', transformUrl: function (url) { return url.slice(1); } })).pipe(gulp.dest(paths.dist)));
99
- gulp.task('templates-clean', () => require('delete-empty')(paths.dist));
100
- gulp.task('templates', gulp.series('templates-build', 'templates-clean'));
101
- gulp.task('build', gulp.series('build:tmp', 'copy', 'templates', () => {
140
+ }
141
+
142
+ gulp.task('serve', gulp.series(buildTmp, browser, watch));
143
+
144
+ function cleanDist() {
145
+ return delDir(globs.dist);
146
+ }
147
+ cleanDist.displayName = 'clean:dist';
148
+
149
+ function copy() {
150
+ return gulp.src(globs.tmpAllFiles)
151
+ .pipe(gulp.dest(globs.dist));
152
+ }
153
+
154
+ function buildTemplates() {
155
+ return gulp.src([globs.dist + getGlob('html'), '!' + globs.dist + indexHtmlFile])
156
+ .pipe($.cleanDest(globs.dist))
157
+ .pipe(minifyHtml())
158
+ .pipe($.angularTemplatecache(jsTemplatesFile, { module: 'app', transformUrl: function (url) { return url.slice(1); } }))
159
+ .pipe(gulp.dest(globs.dist));
160
+ }
161
+ buildTemplates.displayName = 'build-templates';
162
+
163
+ function cleanTemplates() {
164
+ return deleteEmpty(globs.dist);
165
+ }
166
+ cleanTemplates.displayName = 'clean-templates';
167
+
168
+ function finishBuild() {
102
169
  const indexHtmlFilter = filter('html'),
103
170
  cssFilter = filter('css'),
104
171
  jsFilter = filter('js'),
105
172
  cssAndJsFilter = filter(['css', 'js']),
106
173
  imgFilter = filter(['png', 'jpg', 'gif', 'svg']),
107
174
  jsonFilter = filter('json');
108
- return gulp.src(paths.dist + allFiles)
175
+ return gulp.src(globs.dist + allFiles)
109
176
  .pipe(indexHtmlFilter).pipe(injStr.before('</body>', `<script src="${jsTemplatesFile}"></script>` + nl)).pipe(minifyHtml()).pipe(indexHtmlFilter.restore)
110
177
  .pipe(cssFilter).pipe($.cssnano({ zindex: false })).pipe(cssFilter.restore)
111
178
  .pipe(jsFilter).pipe($.ngAnnotate()).pipe($.terser()).pipe(jsFilter.restore)
@@ -114,38 +181,46 @@ gulp.task('build', gulp.series('build:tmp', 'copy', 'templates', () => {
114
181
  .pipe(imgFilter).pipe($.imagemin()).pipe(imgFilter.restore)
115
182
  .pipe(jsonFilter).pipe($.jsonmin()).pipe(jsonFilter.restore)
116
183
  .pipe($.size({ showFiles: true }))
117
- .pipe(gulp.dest(paths.dist));
118
- }));
119
- gulp.task('serve:dist', gulp.series('build', () => browserSyncInit(paths.dist)));
184
+ .pipe(gulp.dest(globs.dist));
185
+ }
186
+ finishBuild.displayName = 'finish-build';
187
+
188
+ gulp.task('build', gulp.series(buildTmp, cleanDist, copy, buildTemplates, cleanTemplates, finishBuild));
189
+
190
+ function browserDist(cb) {
191
+ browserSyncInit(globs.dist);
192
+ cb();
193
+ }
194
+ browserDist.displayName = 'browser:dist';
195
+
196
+ gulp.task('serve:dist', gulp.series('build', browserDist));
120
197
 
121
198
  gulp.task('default', gulp.task('serve'));
122
199
 
123
200
  function html5Mode() {
124
- const pathPrefix = '/';
125
- stylesFolder = pathPrefix + stylesFolder;
126
- jsTemplatesFile = pathPrefix + jsTemplatesFile;
201
+ const prefix = '/';
202
+ stylesDir = prefix + stylesDir;
203
+ jsTemplatesFile = prefix + jsTemplatesFile;
127
204
  }
128
205
 
129
- function getFiles(ext = '*') {
130
- const isArray = typeof ext != 'string';
131
- return '**/*.' + (isArray ? '{' : '') + (isArray ? ext.join() : ext) + (isArray ? '}' : '');
206
+ function getGlob(ext = '*') {
207
+ const glob = '**/*.';
208
+ if (typeof ext == 'string')
209
+ return glob + ext;
210
+ return glob + '{' + ext.join() + '}';
132
211
  }
133
212
 
134
- function delFolder(path) {
135
- return gulp.src(path, { allowEmpty: true, read: false })
213
+ function delDir(glob) {
214
+ return gulp.src(glob, { allowEmpty: true, read: false })
136
215
  .pipe($.clean());
137
216
  }
138
217
 
139
218
  function filter(ext, isUnrestored) {
140
- return $.filter(getFiles(ext), { restore: !isUnrestored });
219
+ return $.filter(getGlob(ext), { restore: !isUnrestored });
141
220
  }
142
221
 
143
222
  function browserSyncInit(path) {
144
- browserSync.init({
145
- server: {
146
- baseDir: path
147
- }
148
- });
223
+ browserSync.init({ server: path });
149
224
  }
150
225
 
151
226
  function minifyHtml() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autofront",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "Automatisation of front-end by Gulp and Bower.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -62,5 +62,14 @@
62
62
  "main-bower-files": "^2.13.1",
63
63
  "merge-stream": "^1.0.1",
64
64
  "sass": "^1.38.2"
65
+ },
66
+ "domains": {
67
+ "local": "http://localhost:8081/",
68
+ "development": "http://dev.mydomain/",
69
+ "production": "http://mydomain/"
70
+ },
71
+ "domainAliases": {
72
+ "prod": "production",
73
+ "dev": "development"
65
74
  }
66
75
  }