Skip to content

Instantly share code, notes, and snippets.

@appieschot
Last active Jun 13, 2021
Embed
What would you like to do?
cli-m365-tweets
const fs = require('fs');
const yaml = require('js-yaml');
var Jimp = require('jimp');
const { HORIZONTAL_ALIGN_CENTER } = require('jimp');
async function textOverlay(textForImage, fileName) {
console.log('Generating image ' + textForImage)
const font = await Jimp.loadFont(Jimp.FONT_SANS_128_WHITE);
const image = await Jimp.read("C:/git/sbx/tweetgenerator/images/bg.png");
await image.print(font, 10, 10, {
text: textForImage,
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
}, 1024, 512);
await image.writeAsync(fileName);
}
try {
let docUrl = "https://pnp.github.io/cli-microsoft365/"
let rootPath = "C:/git/pnp/office365-cli/docs/"
let imagePath = "C:/git/sbx/tweetgenerator/images/";
let messages = ['Did you know there is a command that [description]? Checkout [url]! #CLIMicrosoft365',
'Needing to [description-s]? Use [command] Learn more at [url] #CLIMicrosoft365',
'Do you want to [description-s]? See [url] for more info! #CLIMicrosoft365',
'Did you know you can use the cli for Microsoft 365 to [description]? [url] #CLIMicrosoft365',
'Trying to [description-s]? Checkout [command] [url] #CLIMicrosoft365']
let navigationContents = fs.readFileSync((rootPath + "mkdocs.yml"), 'utf8');
let navData = yaml.safeLoad(navigationContents);
const re = /\s*cmd.*?(md)/g;
let links = JSON.stringify(navData).match(re);
links.forEach(link => {
console.log("Generating tweet for " + link);
let fileContents = fs.readFileSync((rootPath + "docs/" + link), 'utf8');
fileContents = fileContents.split("\n");
let commandName = fileContents[0].replace('#', '');
let commandDescription = fileContents[2];
let firstDescriptionWord = commandDescription.split(" ")[0];
let commandDescriptionWithoutS = '';
let commandUrl = docUrl + link.replace(".md", "");
if (firstDescriptionWord.endsWith("s")) {
firstDescriptionWord = firstDescriptionWord.substring(0, firstDescriptionWord.length - 1)
commandDescriptionWithoutS = (firstDescriptionWord + commandDescription.substring((firstDescriptionWord.length + 1), commandDescription.length));
}
let randomMessageNo = Math.floor(Math.random() * 5);
// todo ignore empty tweets
let tweet = messages[randomMessageNo].replace("[command]", commandName).replace("[url]", commandUrl.trim()).replace("[description]", commandDescription.trim()).replace("[description-s]", commandDescriptionWithoutS.trim());
let line = `'${commandName}','${commandDescription}','${commandUrl}'`
fs.appendFile('test.csv', tweet.replace(/(\r\n|\n|\r)/gm, "") + "\n", function (err) {
if (err) {
console.log(err)
} else {
}
})
let fileName = imagePath + link + '.png';
textOverlay(commandName, fileName);
})
console.log('Finished tweets')
} catch (e) {
console.log(e);
}
{
"name": "tweetgenerator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"canvas": "^2.6.1",
"jimp": "^0.16.1",
"js-yaml": "^3.14.0",
"md-2-json": "^1.0.6"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment