forked from HadoukenIO/js-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
264 lines (237 loc) · 7.81 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const path = require('path');
const manifestPath = path.resolve('test', 'app.json');
const httpServer = require('http-server');
const ps = require('ps-node');
const os = require('os');
const { exec } = require('child_process');
const webpack = require('webpack');
const { buildCore, resolveRuntimeVersion } = require('./coreUtils');
const outDir = path.resolve('out');
const webpackConfig = {
mode: 'development',
entry: path.resolve(__dirname, 'src/of-main.ts'),
module: {
rules: [{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}]
},
resolve: {
alias: { events: path.resolve(__dirname, 'node_modules/events/events.js') },
extensions: ['.ts', '.js']
},
output: {
path: path.resolve(__dirname, 'out'),
filename: 'js-adapter.js'
}
};
const serverParams = {
root: path.resolve('html'),
open: false,
logLevel: 0,
port: 8689,
ignore: path.resolve('html')
};
module.exports = function (grunt) {
const version = grunt.option('ver');
const corePath = grunt.option('core');
const remote = grunt.option('remote');
const rvmDir = grunt.option('rvmDir');
const target = grunt.option('target');
const shouldBuildCore = grunt.option('build-core');
const uuid = 'testapp';
const args = '--inspect=9222';
process.env.OF_VER = version;
process.env.TEST_SERVER_PORT = serverParams.port;
process.env.RVM_DIR = rvmDir || path.resolve('' + process.env.localAppData, 'OpenFin');
grunt.initConfig({
shell: {
build: {
command: 'tsc',
preferLocal: true
}
},
tslint: {
default: {
src: ['src/**/*.ts', 'repl/*.ts', 'test/*.ts']
},
options: {
configuration: grunt.file.readJSON('tslint.json'),
rulesDirectory: 'node_modules/tslint-microsoft-contrib',
force: false
}
},
mochaTest: {
default: {
src: 'out/test/**/*.js',
timeout: 30000
}
},
copy: {
resources: {
files: [{ src: 'resources/**', dest: 'out/' }]
}
}
});
grunt.registerTask('check-version', function () {
if (!version) {
grunt.fail.fatal('No version given, please provide a target version');
}
});
grunt.registerTask('clean', function () {
grunt.file.delete(outDir);
grunt.log.ok('out directory deleted');
});
grunt.registerTask('start-server', function () {
const server = httpServer.createServer(serverParams);
server.listen(serverParams.port);
});
grunt.registerTask('start-repl', function () {
const finRepl = require(path.join(outDir, 'repl', 'index.js'));
const done = this.async();
finRepl.startRepl();
});
grunt.registerTask('deploy', function () {
if (!target) {
grunt.log.writeln('no deploy target specified, skipping deploy step');
} else {
const deployPath = path.join(target, 'js-adapter', 'js-adapter.js');
grunt.file.copy(webpackConfig.output.filename, deployPath);
grunt.log.ok(`deployed to ${deployPath}`);
}
});
//using the node webpack API: https://webpack.js.org/api/node/
//will use Task Error or Warning grunt codes based on webpack results: https://gruntjs.com/exit-codes
grunt.registerTask('webpack', function () {
const done = this.async();
webpack(webpackConfig, (err, stats) => {
if (err) {
if (err.details) {
grunt.fail.fatal(err.details, 3);
}
} else if (stats.hasErrors()) {
const info = stats.toJson();
grunt.fail.fatal(info.errors, 3);
} else if (stats.hasWarnings()) {
const info = stats.toJson();
info.warnings.forEach((warning) => {
grunt.fail.warn(warning, 6);
});
} else {
grunt.log.ok('webpack task done');
done();
}
});
});
grunt.registerTask('kill-processes', function () {
ps.lookup({
command: 'openfin.exe'
},
(err, processList) => {
if (err) {
throw new Error(err);
}
processList.forEach(i => ps.kill(i.pid));
}
);
});
grunt.registerTask('publish-docs', () => {
exec(`cd docs && git commit -am "committed new update for node-adapter documentation." && git push ${remote} master`,
function (err, stdout, stderr) {
if (err) {
grunt.log.error(err);
} else {
grunt.log.ok(
'published new documentation for node-adapter.'
);
}
}
);
});
grunt.registerTask('launch-openfin', function () {
const { launch } = require(path.join(outDir, 'src', 'main.js'));
const done = this.async();
launch({
manifestUrl: manifestPath
}).then(done).catch(done);
});
grunt.registerTask('update-manifest', function () {
const manifest = {
runtime: {
arguments: args,
version
},
startup_app: {
uuid,
autoShow: true,
url: `http://localhost:${serverParams.port}/index.html`,
nonPersistent: true,
saveWindowState: false,
experimental: {
v2Api: true
}
}
};
grunt.file.write(manifestPath, JSON.stringify(manifest));
});
grunt.registerTask('core', function () {
if (!shouldBuildCore) {
grunt.log.ok('skipping core build task');
return;
}
if (version) {
let done = this.async();
resolveRuntimeVersion(version).then(v => {
if (process.platform === 'win32') {
// Windows
if (!grunt.file.exists(`${process.env.localAppData}/OpenFin/runtime/${v}`)) {
grunt.log.error('WARNING: The specified version of runtime does not exist. The core wil not be deployed.');
} else {
buildCore(corePath || '', `${process.env.localAppData}/OpenFin/runtime/${v}/OpenFin/resources`);
}
} else {
// *nix system
// TODO deploy to appropriate directory
grunt.log.error('WARNING: Core deployment on ' + process.platform + ' is not supported. Core wil not be deployed.');
}
done();
});
} else {
buildCore(corePath || '');
}
});
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-tslint');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('lint', ['tslint']);
grunt.registerTask('openfin', ['update-manifest', 'launch-openfin']);
grunt.registerTask('build', [
'clean',
'shell',
'webpack',
'copy:resources'
]);
grunt.registerTask('default', [
'lint',
'build',
'deploy'
]);
grunt.registerTask('test', [
'check-version',
'core',
'default',
'start-server',
'openfin',
'mochaTest',
'kill-processes'
]);
grunt.registerTask('repl', [
'check-version',
'default',
'start-server',
'openfin',
'start-repl'
]);
};