This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
generate-tasks.js
80 lines (67 loc) · 2.44 KB
/
generate-tasks.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
const { existsSync, mkdirSync, writeFileSync, lstatSync, readdirSync } = require('fs');
const { join } = require('path');
const scanDirs = ['server', 'src'];
const isFile = f => lstatSync(f).isFile();
const isDirectory = source => lstatSync(source).isDirectory();
// given a relative directory, return an array containing all directories
// located under that directory, including the directory itself
const getDirectories = source =>
readdirSync(source)
.map(val => join(source, val))
.filter(isDirectory)
.reduce((acc, val) => acc.concat(getDirectories(val)).concat([val]), []);
const endsWithAnyOf = (ends, testVal) =>
ends.reduce((acc, val) => acc || testVal.endsWith(val), false);
const isFileWithEnding = (endings, file) =>
isFile(file) && endsWithAnyOf(endings, file);
const testFileSpec = () => ['.spec.', '.test.']
.reduce((acc, val) => acc.concat(['js', 'jsx']
.map(suf => val + suf)), []);
// given a directory, return true if that directory has files ending in
// .spec.js or .test.js
const hasTests = dir =>
readdirSync(dir)
.reduce((acc, val) => acc || isFileWithEnding(testFileSpec(), join(dir, val)), false);
// return a function that given a directory, returns a task with the given
// prefix, npm script, and bash file glob. The directory forms the suffix of
// the task name
function makeTask(prefix, cmd, match) {
return function(dir) {
return {
'name': prefix + '-' + dir.replace(/\//g, '-').toLowerCase(),
'commands': [
{
'func': 'preamble'
},
{
'func': 'npm',
'vars': {
'cmd': 'run-script ' + cmd + ' -- ' + dir + '/' + match
}
}
]
};
};
}
const dirs = scanDirs.reduce((acc, val) => acc.concat(getDirectories(val)), []).concat(scanDirs);
const testDirs = dirs.filter(hasTests);
console.log('Will run tests in: ', testDirs);
const testTasks = testDirs.map(makeTask('test', 'test:ci', '*.{spec,test}.js{,x}'));
const gt = {
'buildvariants': [
{
'name': 'ubuntu1604',
'tasks': testTasks.map(task => task.name)
}
],
'tasks': testTasks
};
console.log('Generating tasks with the following payload: ', JSON.stringify(gt, null, 2));
const outDir = join(__dirname, 'build');
const outFile = join(outDir, '.tasks.json');
if (!existsSync(outDir)) {
console.log('Making output directory: ', outDir);
mkdirSync(outDir);
}
console.log('Writing to: ', outFile);
writeFileSync(outFile, JSON.stringify(gt));