Skip to content

Instantly share code, notes, and snippets.

@appieschot
Last active January 3, 2022 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appieschot/3fb6ffd8cf2183e5ac708d77c689c0fa to your computer and use it in GitHub Desktop.
Save appieschot/3fb6ffd8cf2183e5ac708d77c689c0fa to your computer and use it in GitHub Desktop.
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 #Microsoft365dev #m365pnp',
'Needing to [description-s]? Use [command] Learn more at [url] #CLIMicrosoft365 #Microsoft365dev #m365pnp',
'Do you want to [description-s]? See [url] for more info! #CLIMicrosoft365 #Microsoft365dev #m365pnp',
'Did you know you can use the cli for Microsoft 365 to [description]? [url] #CLIMicrosoft365 #Microsoft365dev #m365pnp',
'Trying to [description-s]? Checkout [command] [url] #CLIMicrosoft365 #Microsoft365dev #m365pnp']
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