-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
76 lines (64 loc) · 1.76 KB
/
index.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
const { promises: { readFile, writeFile } } = require('fs');
const { join } = require('path');
const execute = require('async-execute');
const exist = require('@does/exist');
const FILENAME = 'dangerfile.js';
const primed = join(process.cwd(), FILENAME);
/**
* Create a dangerfile if needed and run it
* @param {String} sourcedir Directory to look for dangerfile
* @param {Boolean} [optional.force=false] Prefer passed dir over process root
* @return {String}
*/
module.exports = async function dangerfile(sourcedir, { force = false } = {}) {
// Check is a local file exists *unless* false flag is on
const exists = force
? false
: await exist(primed)
;
let message = 'Using the existing Dangerfile';
// Create dangerfile only if local one is not available
if (!exists) {
message = await create(sourcedir);
}
// Run danger
const installed = await run();
return installed
? [ 'Installed danger.', message ].join(' ')
: message
;
};
/**
* Create a dangerfile
* @param {String} target [description]
* @return {[type]} [description]
*/
async function create(sourcedir) {
const target = join(sourcedir, FILENAME);
const exists = await exist(target);
const source = exists
? target
: join(__dirname, FILENAME)
;
const content = await readFile(source);
await writeFile(primed, content.toString());
return exists
? 'Creating a new Dangerfile'
: 'Creating a default Dangerfile'
;
}
/**
* Install danger and run it
* @return {Boolean}
*/
async function run() {
try {
await execute('./node_modules/.bin/danger ci', { pipe: true });
return false;
} catch (error) {
// don't throw yet
}
await execute('npm i danger --no-save', { pipe: true });
await execute('./node_modules/.bin/danger ci', { pipe: true });
return true;
}