forked from eggjs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
61 lines (55 loc) · 1.67 KB
/
test.js
File metadata and controls
61 lines (55 loc) · 1.67 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const chalk = require('chalk');
const execSync = require('child_process').execSync;
const flag = process.argv[2] ? '-c' : '';
const exampleDirs = fs.readdirSync(__dirname)
.filter(name => [ '.git', 'node_modules', 'logs', 'run' ].indexOf(name) === -1)
.map(name => path.join(__dirname, name))
.filter(name => fs.statSync(name).isDirectory());
const skips = [];
const tests = [];
for (const dir of exampleDirs) {
const dirname = path.basename(dir);
if (!testExists(dir)) {
console.log('%s directory %s',
chalk.bgYellow('skip'),
chalk.gray(dirname)
);
skips.push(dirname);
continue;
}
console.log('%s directory %s',
chalk.bgGreen('test'),
chalk.gray(dirname)
);
const options = { cwd: dir, stdio: [ 0, 1, 2 ] };
execSync(`../node_modules/.bin/npminstall ${flag}`, options);
execSync('npm test', options);
console.log('%s success\n', chalk.green('✔'));
tests.push(dirname);
}
console.log('successed: %s\nskiped: %s',
chalk.cyan(tests.join(', ')),
chalk.yellow(skips.join(', '))
);
function testExists(dir) {
try {
const pkg = require(path.join(dir, 'package.json'));
if (!pkg.scripts.test) return false;
if (pkg.engines && pkg.engines.node && !semver.satisfies(process.version.slice(1), pkg.engines.node)) {
console.log('%s %s require node version %s, now is using node %s',
chalk.bgYellow('skip'),
chalk.gray(path.basename(dir)),
chalk.cyan(pkg.engines.node),
chalk.cyan(process.version)
);
return false;
}
return true;
} catch (_) {
return false;
}
}