1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var changeCase = require('change-case');
|
4 | var karma = require('karma').server;
|
5 | var lazypipe = require('lazypipe');
|
6 | var del = require('del');
|
7 | var vinylPaths = require('vinyl-paths');
|
8 | var stylish = require('jshint-stylish');
|
9 | var conventionalChangelog = require('conventional-changelog');
|
10 |
|
11 | var jshint = require('gulp-jshint');
|
12 | var sass = require('gulp-sass');
|
13 | var cssmin = require('gulp-cssmin');
|
14 | var ngAnnotate = require('gulp-ng-annotate');
|
15 | var header = require('gulp-header');
|
16 | var footer = require('gulp-footer');
|
17 | var concat = require('gulp-concat');
|
18 | var uglify = require('gulp-uglify');
|
19 | var rename = require('gulp-rename');
|
20 | var html2js = require('gulp-ng-html2js');
|
21 |
|
22 | var PREFIX_FILE = 'component.prefix',
|
23 | SUFFIX_FILE = 'component.suffix',
|
24 | HEADER_FILE = 'header.txt',
|
25 | CHANGELOG_FILE = 'CHANGELOG.md',
|
26 | KARMA_CONF_FILE = 'karma.conf.js',
|
27 | SOURCE_SOFA_JS_FILE = 'src/sofa.js',
|
28 | SOURCE_JS_FILES = 'src/**/*.js',
|
29 | SOURCE_SASS_FILES = 'src/*.scss',
|
30 | SOURCE_TEMPLATE_FILES = 'src/**/*.tpl.html',
|
31 | DIST_DIR = 'dist';
|
32 |
|
33 | var banner = fs.readFileSync(path.join(__dirname, HEADER_FILE), 'utf-8');
|
34 | var componentPrefix = fs.readFileSync(PREFIX_FILE, 'utf-8');
|
35 | var componentSuffix = fs.readFileSync(SUFFIX_FILE, 'utf-8');
|
36 |
|
37 | module.exports = function (gulp, config) {
|
38 |
|
39 | var componentName = changeCase.camelCase(config.pkg.name.replace('angular-', ''));
|
40 | var jshintTasks = lazypipe()
|
41 | .pipe(jshint)
|
42 | .pipe(jshint.reporter, stylish)
|
43 | .pipe(jshint.reporter, 'fail');
|
44 |
|
45 | gulp.task('clean', function () {
|
46 | return gulp.src(DIST_DIR)
|
47 | .pipe(vinylPaths(del));
|
48 | });
|
49 |
|
50 | gulp.task('jshint:src', function () {
|
51 | return gulp.src(['src/**/*.js', '!src/**/*.tpl.js'])
|
52 | .pipe(jshintTasks());
|
53 | });
|
54 |
|
55 | gulp.task('jshint:specs', function () {
|
56 | return gulp.src('test/**/*.spec.js')
|
57 | .pipe(jshintTasks());
|
58 | });
|
59 |
|
60 | gulp.task('jshint', [
|
61 | 'jshint:src',
|
62 | 'jshint:specs'
|
63 | ]);
|
64 |
|
65 | gulp.task('test', function (done) {
|
66 | karma.start({
|
67 | configFile: path.join(config.baseDir, KARMA_CONF_FILE)
|
68 | }, done);
|
69 | });
|
70 |
|
71 | gulp.task('test:continuous', ['templates'], function (done) {
|
72 | karma.start({
|
73 | configFile: path.join(config.baseDir, KARMA_CONF_FILE),
|
74 | singleRun: true
|
75 | }, function () {
|
76 | done();
|
77 | });
|
78 | });
|
79 |
|
80 | gulp.task('test:debug', function (done) {
|
81 | karma.start({
|
82 | configFile: path.join(config.baseDir, KARMA_CONF_FILE),
|
83 | singleRun: false
|
84 | }, done);
|
85 | });
|
86 |
|
87 | gulp.task('templates', function () {
|
88 | return gulp.src(SOURCE_TEMPLATE_FILES)
|
89 | .pipe(html2js())
|
90 | .pipe(gulp.dest('src'));
|
91 | });
|
92 |
|
93 | gulp.task('changelog', function (done) {
|
94 |
|
95 | var file = path.join(config.baseDir, CHANGELOG_FILE);
|
96 |
|
97 | conventionalChangelog({
|
98 | repository: config.pkg.repository.url,
|
99 | version: config.pkg.version
|
100 | }, function (err, log) {
|
101 | fs.writeFileSync(file, log);
|
102 | done()
|
103 | });
|
104 | });
|
105 |
|
106 | gulp.task('build', ['scripts', 'styles']);
|
107 | gulp.task('default', ['build']);
|
108 |
|
109 | gulp.task('scripts', ['clean', 'jshint', 'test:continuous'], function () {
|
110 | return gulp.src([
|
111 | SOURCE_SOFA_JS_FILE,
|
112 | SOURCE_JS_FILES
|
113 | ])
|
114 | .pipe(ngAnnotate({
|
115 | single_quote: true,
|
116 | add: true
|
117 | }))
|
118 | .pipe(concat(componentName + '.js'))
|
119 | .pipe(header(componentPrefix))
|
120 | .pipe(footer(componentSuffix))
|
121 | .pipe(header(banner, {
|
122 | pkg: config.pkg,
|
123 | date: new Date()
|
124 | }))
|
125 | .pipe(gulp.dest(DIST_DIR))
|
126 | .pipe(uglify())
|
127 | .pipe(rename({extname: '.min.js'}))
|
128 | .pipe(header(banner, {
|
129 | pkg: config.pkg,
|
130 | date: new Date()
|
131 | }))
|
132 | .pipe(gulp.dest(DIST_DIR));
|
133 | });
|
134 |
|
135 | gulp.task('styles', function () {
|
136 | return gulp.src(SOURCE_SASS_FILES)
|
137 | .pipe(sass())
|
138 | .pipe(cssmin())
|
139 | .pipe(gulp.dest(DIST_DIR));
|
140 | });
|
141 |
|
142 | gulp.task('watch', ['build'], function () {
|
143 | gulp.watch([SOURCE_JS_FILES], ['scripts']);
|
144 | gulp.watch([SOURCE_SASS_FILES], ['styles']);
|
145 | gulp.watch([SOURCE_TEMPLATE_FILES], ['templates']);
|
146 | });
|
147 | };
|