-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (116 loc) · 3.41 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
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
#!/usr/bin/env node
import fs from 'fs'
import {join} from 'path'
import chalk from 'chalk'
import meow from 'meow'
import open from 'open'
import yaml from 'js-yaml'
const {dim, blue, bold, red, yellow} = chalk
const cli = meow(
`
${bold('Usage')}
${blue(`action-reporting-parse-cli`)} ${yellow(`[options]`)}
${bold('Required options')}
${yellow(`--config`)}, ${yellow(`-c`)} Path to config.yml file.
${yellow(`--data`)}, ${yellow(`-d`)} Path to data directory to lookup ${dim('*actions.json')} files in.
${bold('Additional options')}
${yellow(`--open`)}, ${yellow(`-o`)} Open the generated CSV file ${dim('(Default: false)')}.
${bold('Helper options')}
${yellow(`--help`)}, ${yellow(`-h`)} Print this help message.
${yellow(`--version`)}, ${yellow(`-v`)} Print the CLI version.
${bold.yellow('Note:')} Report CSV file will be saved in ${dim(join(process.cwd(), 'uses.csv'))}`,
{
booleanDefault: undefined,
description: false,
hardRejection: false,
allowUnknownFlags: false,
importMeta: import.meta,
inferType: false,
input: [],
flags: {
help: {
type: 'boolean',
shortFlag: 'h',
},
version: {
type: 'boolean',
shortFlag: 'v',
},
config: {
type: 'string',
shortFlag: 'c',
required: true,
},
data: {
type: 'string',
shortFlag: 'd',
required: true,
},
open: {
type: 'boolean',
shortFlag: 'o',
default: false,
},
},
},
)
// run
;(async () => {
try {
const {config, data, open: opn, help, version} = cli.flags
help && cli.showHelp(0)
version && cli.showVers
if (!config) {
throw new Error('Missing required flag: --config')
}
if (!data) {
throw new Error('Missing required flag: --data')
}
// load config
const {orgs, exclude} = yaml.load(fs.readFileSync(config, 'utf8'))
// load *actions.json and merge into actions const
const actions = []
for (const file of fs.readdirSync(data)) {
if (file.indexOf('actions.json') === -1) continue
const json = JSON.parse(fs.readFileSync(join(data, file), 'utf8'))
actions.push(...json)
}
const tree = new Set()
for (const action of actions) {
const {owner, repo, uses} = action
if (!tree[owner]) {
tree[owner] = new Set()
}
if (!tree[owner][repo]) {
tree[owner][repo] = new Set()
}
for (const use of uses) {
if (use.indexOf('./') === 0) continue
const [o, _r] = use.split('/')
const [r] = _r.split('@')
if (exclude.includes(`${o}/${r}`)) continue
if (orgs.includes(o)) tree[owner][repo].add(`${o}/${r}`)
}
}
// create csv from tree
const csv = ['nwo,uses']
for (const owner of Object.keys(tree)) {
for (const repo of Object.keys(tree[owner])) {
for (const use of tree[owner][repo]) {
csv.push(`${owner}/${repo},${use}`)
}
}
}
// write csv to file if csv isn't empty
if (csv.length >= 2) {
console.log(`saving report CSV in ${blue('./uses.csv')}`)
fs.writeFileSync('./uses.csv', csv.join('\n'))
// open csv
if (opn) open('./uses.csv')
}
} catch (error) {
console.error(`\n ${red('ERROR: %s')}`, error.message)
// console.error(error.stack)
cli.showHelp(1)
}
})()