generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 48
/
FilesHelper.ts
150 lines (145 loc) · 3.98 KB
/
FilesHelper.ts
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
142
143
144
145
146
147
148
149
150
import {setOutput as coreSetOutput, debug as coreDebug} from '@actions/core'
import {writeFileSync} from 'fs'
import {ChangedFiles} from 'typings/ChangedFiles'
import {GitHubFile} from 'typings/GitHubFile'
import {getErrorString} from './UtilsHelper'
/**
* @function sortChangedFiles
* @param files pass in array of GithubFile's to be sorted
* @returns ChangedFiles object that has .files, .added, .modified, .renamed, and .removed
*/
export function sortChangedFiles(files: GitHubFile[]): ChangedFiles {
try {
coreDebug(
`Here are the files I am changing: ${JSON.stringify(files, null, 2)}`
)
const changedFiles = {
files: [],
added: [],
removed: [],
renamed: [],
modified: []
} as ChangedFiles
files.forEach(f => {
changedFiles[f.status].push(
f.filename || f.added || f.removed || f.renamed || f.modified
)
changedFiles.files.push(
f.filename || f.added || f.removed || f.modified || f.renamed
)
})
return changedFiles
} catch (error) {
const eString = `There was an issue sorting files changed.`
throw new Error(
getErrorString(
error.name,
error.status,
sortChangedFiles.name,
eString,
JSON.stringify(error)
)
)
}
}
/**
* @function getFormatExt
* @param format output format 'json' = '.json' ',' = '.csv' anything else is '.txt'.
* @returns file extension, '.json', '.csv', or '.txt'
*/
export function getFormatExt(format: string): string {
let ext
switch (format.trim()) {
case 'json':
ext = '.json'
break
case ',':
ext = '.csv'
break
default:
ext = '.txt'
break
}
return ext
}
/**
* @function formatChangedFiles
* @param format output format 'json' will stringify anything else will files.join('string')
* @param files string list of files to format
* @returns string for output of changedFiles
*/
export function formatChangedFiles(format: string, files: string[]): string {
if (format === 'json') {
return JSON.stringify(files)
}
return files.join(format)
}
/**
* @function writeFiles
* @param format output format 'json' will stringify anything else will files.join('string')
* @param key changedFiles type added, modified, removed, or files
* @param files string list of files to format
* @returns string output to be stored in file
*/
export function writeFiles(format: string, key: string, files: string[]): void {
try {
const ext = getFormatExt(format)
const fileName = key === 'files' ? `${key}${ext}` : `files_${key}${ext}`
coreDebug(
`Writing output file ${
process.env.HOME
}/${fileName} with ${format} and files ${JSON.stringify(files, null, 2)}`
)
writeFileSync(
`${process.env.HOME}/${fileName}`,
formatChangedFiles(format, files),
'utf-8'
)
} catch (error) {
const eString = `There was an issue writing output files.`
throw new Error(
getErrorString(
error.name,
error.status,
writeFiles.name,
eString,
JSON.stringify(error)
)
)
}
}
/**
* @function writeOutput
* @param format output format 'json' will stringify anything else will files.join('string')
* @param key changedFiles type added, modified, removed, or files
* @param files string list of files to format
* @returns string output to be stored to action output
*/
export function writeOutput(
format: string,
key: string,
files: string[]
): void {
try {
const fileName = key === 'files' ? key : `files_${key}`
coreDebug(
`Writing output ${fileName} with ${format} and files ${JSON.stringify(
files,
null,
2
)}`
)
coreSetOutput(fileName, formatChangedFiles(format, files))
} catch (error) {
const eString = `There was an issue setting action outputs.`
throw new Error(
getErrorString(
error.name,
error.status,
writeOutput.name,
eString,
JSON.stringify(error)
)
)
}
}