This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgulpfile.js
209 lines (170 loc) · 5.86 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
'use strict';
const gulp = require( 'gulp' );
const ngAnnotate = require( 'gulp-ng-annotate' );
const uglify = require( 'gulp-uglify' );
const watch = require( 'gulp-watch' );
const concat = require( 'gulp-concat' );
const rename = require( 'gulp-rename' );
const insert = require( 'gulp-insert' );
const merge = require( 'gulp-merge' );
const fs = require( 'fs' );
const util = require( 'util' );
const exec = util.promisify( require('child_process').exec );
const karma = require( 'karma').Server;
const colors = require( 'colors/safe' );
const outputDirectory = 'dist';
const srcFiles = 'src/**/*.js';
const testFiles = 'test/**/*.js';
function buildAndWatch() {
log( '(WATCH) Building initial bundle' );
return build( true )
.then( () => watchAndBuild( srcFiles ) );
}
function runTestsAndWatch() {
build( true )
.then( runTests, () => {} )
.then( watchAndBuild( srcFiles, runTests ) )
.then( watchAndBuild( testFiles, runTests ) );
return new Promise( () => {} );
}
async function buildDistAndVersion() {
const version = getVersion();
const buildDate = new Date().toDateString();
const header = `/*! ngyn \r\n * Version: ${version}\r\n * Built: ${buildDate} \r\n */\r\n\r\n`;
console.log( '(VERSION) Adding file headers' );
await new Promise( resolve => {
merge(
gulp.src( 'dist/ngyn.js' ).pipe( insert.prepend( header ) ),
gulp.src( 'dist/ngyn.min.js' ).pipe( insert.prepend( header ) )
)
.pipe( gulp.dest( 'dist' ) )
.on( 'end', resolve )
} );
console.log( `(VERSION) Setting package version to ${version}` );
const {stdout, stderr} = await exec( `npm version --no-git-tag-version ${version}` );
console.log( `(NPM-VERSION) ${stdout}` );
console.log( `(NPM-VERSION) ${stderr}` );
return Promise.resolve();
};
async function publishToNpm() {
const localLoginExists = fs.existsSync('.npmrc' );
var authToken = process.env.NGYN_NPM_TOKEN;
if ( !localLoginExists ) {
fs.writeFileSync( '.npmrc', `//registry.npmjs.org/:_authToken=${authToken}` );
}
console.log( '(PUBLISH) Starting npm publish' )
const {stdout, stderr} = await exec( `npm publish` );
console.log( `(NPM-PUBLISH) ${stdout}` );
console.log( `(NPM-PUBLISH) ${stderr}` );
if ( !localLoginExists ) {
fs.unlinkSync( '.npmrc' );
}
return Promise.resolve();
}
/*
function nugetPack() {
const version = getVersion();
const p = Promise.all( [
new Promise( resolve => fs.mkdir( 'packages-build', resolve ) ),
new Promise( resolve => {
exec(
`build\\nuget.exe pack ngyn.nuspec -outputdirectory packages-build -verbosity detailed -version ${version}`,
( err, stdout, stderr ) => {
console.log( stdout );
console.log( stderr );
resolve();
}
);
} )
] );
return p;
}
*/
function getVersion() {
const versionArgumentName = '--ngyn-version';
const buildNumberArgumentName = '--ngyn-build';
const isReleaseArgumentName = '--ngyn-is-release';
let versionArg = process.argv.find( a => a.toLowerCase().startsWith( `${versionArgumentName}=` ) );
let buildNumberArg = process.argv.find( a => a.toLowerCase().startsWith( `${buildNumberArgumentName}=` ) );
let isReleaseArg = process.argv.find( a => a.toLowerCase().startsWith( `${isReleaseArgumentName}=` ) );
if ( !versionArg ) {
throw `Cannot find version argument. It must be passed in the format --ngyn-version=1.2.3-alpha2`;
}
let versionString = versionArg.substring( versionArgumentName.length + 1 );
const isRelease = isReleaseArg && isReleaseArg.substring( isReleaseArgumentName.length + 1 ) === 'true';
if ( !isRelease ) {
if ( !buildNumberArg ) {
throw `A buildNumber must be provided if '--ngyn-is-release' is not set.`;
}
buildNumberArg = buildNumberArg.substring( buildNumberArgumentName.length + 1 );
buildNumberArg = buildNumberArg.padStart( 4, '0' );
versionString += `-alpha${buildNumberArg}`;
}
return versionString;
}
function log( msg, startTime ) {
const date = new Date();
const timeTaken = !startTime ? '' : ' Took ' + ( date - startTime ) + ' ms';
console.log( `[${date.toLocaleTimeString()}] ${msg}${timeTaken}` );
}
function build( continueOnError ) {
log( '(BUILD) Building bundle' );
const start = new Date();
return new Promise( (resolve, reject) => {
gulp.src( srcFiles )
.pipe( concat( 'ngyn.js') )
.pipe( ngAnnotate() )
.on( 'error', function( ex ) {
if ( continueOnError ) {
log( colors.red( `(ANNOTATE) ${ex.message}` ) );
log( '(BUILD) Failed' )
reject();
}
} )
.pipe( gulp.dest( outputDirectory ) )
.pipe( uglify() )
.pipe( rename( 'ngyn.min.js' ) )
.pipe( gulp.dest( outputDirectory ) )
.on( 'end', () => {
log( '(BUILD) Finished.', start );
resolve();
} );
} );
}
function watchAndBuild( files, callback ) {
log( `(WATCH) Watching ${files}` );
watch( files, e => {
log( `(WATCH) ${e.history[0]} changed...` );
build( true ).then( () => {
callback && callback();
} );
} );
// watch never ends
return new Promise( () => {} );
}
function runTests() {
const p = new Promise( resolve => {
log( '(TEST) Test run starting' );
const karmaConfig = {
configFile: `${__dirname}/karma.conf.js`,
singleRun: true
};
if ( process.env['TEAMCITY_VERSION'] ) {
karmaConfig.reporters = ['teamcity'];
}
const start = new Date();
new karma( karmaConfig, () => {
log( '(TEST) Finished.', start );
resolve();
} ).start();
} );
return p;
}
const testTask = gulp.series( build, runTests );
const publishTask = gulp.series( testTask, buildDistAndVersion, publishToNpm );
exports.build = build;
exports.test = testTask;
exports.buildWatch = buildAndWatch;
exports.testWatch = runTestsAndWatch;
exports.publish = publishTask;
exports.default = runTestsAndWatch;