Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
feat(cli): create the 'delete-incident' command
Browse files Browse the repository at this point in the history
  • Loading branch information
juliomrqz committed Mar 12, 2019
1 parent b61bf05 commit 8aef1f9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"demo:dev": "cross-env STATUSFY_LERNA=true yarn workspace statusfy-demo dev",
"demo:generate": "cross-env STATUSFY_LERNA=true yarn workspace statusfy-demo generate",
"demo:start": "cross-env STATUSFY_LERNA=true yarn workspace statusfy-demo start",
"demo:delete-incident": "cross-env STATUSFY_LERNA=true yarn workspace statusfy-demo delete-incident",
"website:dev": "yarn workspace statusfy-website dev",
"website:generate": "yarn workspace statusfy-website generate",
"update-changelog": "conventional-changelog -p angular -r 2 -i CHANGELOG.md -s"
Expand Down
9 changes: 8 additions & 1 deletion packages/@statusfy/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (!semver.satisfies(process.version, requiredVersion)) {
process.exit(1)
}

const { init, dev, build, generate, start, newIncident } = require('@statusfy/core/lib')
const { init, dev, build, generate, start, newIncident, deleteIncident } = require('@statusfy/core/lib')
const sourceDir = path.resolve('.')

program
Expand Down Expand Up @@ -98,6 +98,13 @@ program
wrapCommand(newIncident)(sourceDir, { })
})

program
.command('delete-incident')
.description(`Creates a new incident after answering a few questions.`)
.action(() => {
wrapCommand(deleteIncident)(sourceDir, { })
})

// output help information on unknown commands
program
.arguments('<command>')
Expand Down
91 changes: 91 additions & 0 deletions packages/@statusfy/core/lib/delete-incident.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const inquirer = require("inquirer");

const { logger, fse, grayMatter, chalk, path } = require("@statusfy/common");
const loadConfig = require("./config/load");

/* eslint-disable require-await */
module.exports = async function deleteIncident(sourceDir, cliOptions = {}) {
process.env.NODE_ENV = "development";

const config = loadConfig(sourceDir).config;
const contentDir = path.join(sourceDir, config.content.dir);
const files = await fse.readdir(contentDir);
const incidentsList = [];

for (let i = 0; i < files.length; i++) {
const f = path.resolve(contentDir, files[i]);
const ext = path.extname(f);
const fileName = path.basename(f);

if (ext === ".md") {
const fileContent = await fse.readFile(f);
const { data } = grayMatter.parse(fileContent);

incidentsList.push(
`${fileName} > ${chalk.yellow(data.title)} (${chalk.green(
data.date.toUTCString()
)})`
);
}
}

const questions = [
{
type: "list",
name: "incident",
message: "What incident do you want to delete?",
paginated: true,
choices: incidentsList
},
{
type: "confirm",
name: "confirm",
message: "Are you sure you want to delete the incident?",
default: false
}
];

inquirer.prompt(questions).then(async answers => {
const { incident, confirm } = answers;

try {
if (confirm) {
const locales = config.locales.map(l => l.code);
const deletedFiles = [];

for (let j = 0; j < locales.length; j++) {
const incidentFileName = incident.split(">")[0].trim();
const locale = locales[j];
const localeIncidentPath = path.join(
contentDir,
config.defaultLocale !== locale ? locale : "",
incidentFileName
);
const exists = await fse.pathExists(localeIncidentPath);

if (exists) {
try {
await fse.remove(localeIncidentPath);
deletedFiles.push(localeIncidentPath);
} catch (error) {
logger.error(error);
}
} else {
logger.warn(`This file couldn't be found:\n${localeIncidentPath}`);
}
}

if (deletedFiles.length > 0) {
const prefix =
deletedFiles.length === 1
? "This file was successfully deleted"
: "These files were successfully deleted";

logger.success(`${prefix}: \n${deletedFiles.join("\n")}`);
}
}
} catch (error) {
logger.fatal(error);
}
});
};
1 change: 1 addition & 0 deletions packages/@statusfy/core/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ exports.build = require("./build");
exports.generate = require("./generate");
exports.start = require("./start");
exports.newIncident = require("./new-incident");
exports.deleteIncident = require("./delete-incident");
exports.Statusfy = require("./statusfy");
3 changes: 2 additions & 1 deletion packages/@statusfy/core/lib/init/template-package.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "statusfy build",
"start": "statusfy start",
"generate": "statusfy generate",
"new-incident": "statusfy new-incident"
"new-incident": "statusfy new-incident",
"delete-incident": "statusfy delete-incident"
},
"dependencies": {
"statusfy": "^<%= options.statusfyVersion %>"
Expand Down
3 changes: 2 additions & 1 deletion packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "npm run prepare && statusfy build",
"start": "npm run prepare && statusfy start",
"generate": "npm run prepare && statusfy generate",
"new-incident": "statusfy new-incident"
"new-incident": "statusfy new-incident",
"delete-incident": "statusfy delete-incident"
},
"author": {
"name": "Bazzite",
Expand Down

0 comments on commit 8aef1f9

Please sign in to comment.