forked from ably/ably-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
198 lines (165 loc) · 6.04 KB
/
Gruntfile.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
'use strict';
var fs = require('fs');
var path = require('path');
var webpackConfig = require('./webpack.config');
var esbuild = require('esbuild');
var process = require('process');
var MochaServer = require('./test/web_server');
var esbuildConfig = require('./grunt/esbuild/build');
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-webpack');
var dirs = {
common: 'src/common',
browser: 'src/platform/web',
fragments: 'src/platform/web/fragments',
static: 'build',
dest: 'build',
};
async function execExternalPromises(cmd) {
grunt.log.ok('Executing ' + cmd);
return new Promise(function (resolve, reject) {
require('child_process').exec(cmd, function (err, stdout, stderr) {
if (err) {
grunt.fatal('Error executing "' + cmd + '":\nstderr:\n' + stderr + '\nstdout:\n' + stdout);
reject(err);
}
console.log(stdout);
stderr && console.error(stderr);
resolve();
});
});
}
function execExternal(cmd) {
return function () {
var done = this.async();
execExternalPromises(cmd)
.then(() => done())
.catch((error) => done(error));
};
}
var gruntConfig = {
dirs: dirs,
webpack: {
all: Object.values(webpackConfig),
browser: [webpackConfig.browser, webpackConfig.browserMin, webpackConfig.mochaJUnitReporterBrowser],
},
};
grunt.initConfig(gruntConfig);
grunt.registerTask('checkGitSubmodules', 'Check, if git submodules are properly installed', function () {
var done = this.async();
var pathToSubmodule = path.join(__dirname, 'test', 'common', 'ably-common');
fs.stat(pathToSubmodule, function (error, stats) {
if (error) {
grunt.log.writeln('%s : while checking submodule path!', error.message);
grunt.log.writeln('Probably, git submodule at %s are not initialized?', pathToSubmodule);
grunt.log.writeln('Please, initialize it with `git submodule init & git submodule update`!');
return done(false);
}
if (stats.isDirectory()) {
grunt.log.writeln('Git submodule at %s is found!', pathToSubmodule);
return done();
}
grunt.log.writeln('Git submodule at %s is not initialized!', pathToSubmodule);
grunt.log.writeln('Please, initialize it with `git submodule init & git submodule update`!');
return done(false);
});
});
grunt.registerTask('build', ['checkGitSubmodules', 'webpack:all', 'build:browser', 'build:node']);
grunt.registerTask('all', ['build', 'requirejs']);
grunt.registerTask('mocha:webserver', 'Run the Mocha web server', function () {
const done = this.async();
const server = new MochaServer();
server.listen();
process.on('SIGTERM', () => {
server.close();
done();
});
process.on('SIGINT', () => {
server.close();
done();
});
});
grunt.registerTask('build:node', function () {
const done = this.async();
esbuild
.build(esbuildConfig.nodeConfig)
.then(() => {
done(true);
})
.catch((err) => {
done(err);
});
});
grunt.registerTask('build:browser', function () {
var done = this.async();
Promise.all([
esbuild.build(esbuildConfig.webConfig),
esbuild.build(esbuildConfig.minifiedWebConfig),
esbuild.build(esbuildConfig.modularConfig),
])
.then(() => {
console.log('esbuild succeeded');
done(true);
})
.catch((err) => {
done(err);
});
});
grunt.registerTask('test:webserver', 'Launch the Mocha test web server on http://localhost:3000/', [
'build:browser',
'checkGitSubmodules',
'mocha:webserver',
]);
(function () {
const baseDir = path.join(__dirname, 'test', 'package', 'browser');
const buildDir = path.join(baseDir, 'build');
grunt.registerTask(
'test:package:browser:prepare-project',
'Prepare an app to be used for testing the NPM package in a browser environment',
function () {
const done = this.async();
(async function () {
if (grunt.file.exists(buildDir)) {
grunt.file.delete(buildDir);
}
// Create an app based on the template
grunt.file.copy(path.join(baseDir, 'template'), buildDir);
// Use `npm pack` to generate a .tgz NPM package
await execExternalPromises('npm run build');
await execExternalPromises('npm pack --pack-destination test/package/browser/build');
const version = grunt.file.readJSON('package.json').version;
const packFileName = `ably-${version}.tgz`;
// Configure app to consume the generated .tgz file
const pwd = process.cwd();
process.chdir(buildDir);
await execExternalPromises(`npm install ${packFileName}`);
// Install further dependencies required for testing the app
await execExternalPromises('npm run test:install-deps');
process.chdir(pwd);
})()
.then(() => done(true))
.catch((error) => done(error));
},
);
grunt.registerTask('test:package:browser:test', 'Test the NPM package in a browser environment', function () {
const done = this.async();
(async function () {
grunt.task.requires('test:package:browser:prepare-project');
const pwd = process.cwd();
process.chdir(buildDir);
// Perform type checking on TypeScript code that imports ably-js
await execExternalPromises('npm run typecheck');
// Build bundle including ably-js
await execExternalPromises('npm run build');
// Test that the code which exercises ably-js behaves as expected
await execExternalPromises('npm run test');
process.chdir(pwd);
})()
.then(() => done(true))
.catch((error) => done(error));
});
})();
grunt.registerTask('test:package:browser', ['test:package:browser:prepare-project', 'test:package:browser:test']);
grunt.registerTask('test:package', ['test:package:browser']);
grunt.registerTask('default', 'all');
};