This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
/
new-incident.js
143 lines (123 loc) · 3.57 KB
/
new-incident.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
142
143
const fs = require('fs')
const inquirer = require('inquirer')
const opener = require('opener')
const { logger, fse, grayMatter, slugify, path } = require('@statusfy/common')
const loadConfig = require('./config/load')
function generateIncident (data, content, format) {
let matterContent = grayMatter.stringify(content, data, format)
if (['json', 'toml'].includes(format)) {
matterContent = matterContent.replace(/^---/, `---${format}`)
}
return matterContent
}
module.exports = async function newIncident (sourceDir, cliOptions = {}) {
process.env.NODE_ENV = 'development'
// Generate configuration
const config = loadConfig(sourceDir).config
const questions = [
{
type: 'input',
name: 'title',
message: 'What is the cause of the incident?',
validate: (value) => {
if (value.length > 0) {
return true
}
return 'You must have a cause title!'
}
},
{
type: 'list',
name: 'severity',
message: 'What is the severity of the incident?',
choices: [
'under-maintenance',
'degraded-performance',
'partial-outage',
'major-outage'
]
},
{
type: 'checkbox',
name: 'affected',
message: 'What are the affected systems?',
choices: config.content.systems,
validate: (value) => {
if (value.length > 0) {
return true
}
return 'You must have an affected system!'
}
}, {
type: 'input',
name: 'description',
message: 'Add a concise description of the incident.'
},
{
type: 'confirm',
name: 'open',
message: 'Open the incident for editing?',
default: false
}
]
if (!config.content.frontMatterFormat) {
questions.unshift({
type: 'list',
name: 'format',
default: 'yaml',
message: 'What is the format of the Front Matter of the Incident?',
choices: [
'yaml',
'toml',
'json'
]
})
}
logger.start('Create New Incident')
inquirer.prompt(questions).then((answers) => {
const date = new Date().toISOString()
const frontMatter = {
title: answers.title,
date,
severity: answers.severity,
affectedsystems: answers.affected,
resolved: false
}
const content = generateIncident(
frontMatter,
answers.description,
config.content.frontMatterFormat || answers.format
)
try {
const fileName = `${date.split('T')[0]}_${slugify(answers.title, '-')}`
const createdFiles = []
let error = false
config.locales.forEach(locale => {
const contentPath = path.join(sourceDir, config.content.dir)
let filePath
if (locale.code === config.defaultLocale) {
filePath = path.join(contentPath, fileName + '.md')
} else {
filePath = path.join(contentPath, locale.code, fileName + '.md')
}
if (fs.existsSync(filePath)) {
logger.error('An incident with a similar title already exists.', filePath)
error = true
} else {
fse.outputFileSync(filePath, `${content}\n<!--- language code: ${locale.code} -->\n`)
createdFiles.push(path.relative(contentPath, filePath))
if (answers.open) {
opener(filePath)
}
}
})
if (!error) {
logger.success('The Incident was successfully created.', createdFiles.join('\n'))
} else {
logger.warn('There was an issue in creating the Incident.')
}
} catch (error) {
logger.fatal(error)
}
})
}