forked from vuejs-br/docs-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag-alert-blocks.js
42 lines (35 loc) · 940 Bytes
/
tag-alert-blocks.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
#!/usr/bin/env node
const { exec } = require('child_process')
/**
* Execute a command and return stdout as string.
* @param {string} command
* @returns {Promise<string>}
*/
function run(command) {
return new Promise((resolve, reject) => {
exec(command, { encoding: 'utf-8' }, (error, stdout) =>
error ? reject(error) : resolve(stdout)
)
})
}
const ALERT_BLOCK = /^\+\s*:::\s?(\w+)/m
async function isUsingAlertBlock(base = 'origin/master') {
const result = await run(`git diff --name-only ${base}`)
const files = (
await Promise.all(
result
.trim()
.split(/\r?\n/)
.map(file =>
run(`git diff ${base} -- ${file}`)
.then(diff => ALERT_BLOCK.test(diff))
.then(usesAlertBlock => (usesAlertBlock ? file : ''))
)
)
).filter(Boolean)
if (files.length) {
return true
}
return false
}
module.exports = { isUsingAlertBlock }