Skip to content

Instantly share code, notes, and snippets.

@wolfram77
Created December 25, 2023 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfram77/183959888a6b086d44624cb9880e16f2 to your computer and use it in GitHub Desktop.
Save wolfram77/183959888a6b086d44624cb9880e16f2 to your computer and use it in GitHub Desktop.
Create gist for PDF files in a folder : SCRIPT
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
// Main function.
function main() {
for (var file of fs.readdirSync('./')) {
// Skip if file is hidden or in node_modules.
if (/^\.|^node_modules/.test(file)) continue;
// Get file name and extension.
var ext = path.extname(file);
var name = path.basename(file, ext);
var desc = name.replace(/[\s:_]/g, ' ');
var stat = fs.statSync(file);
// Skip if file is not a pdf.
if (!stat.isFile() || ext!=='.pdf') continue;
// Get name of markdown file and data.
var md = 'document-' + _.kebabCase(desc) + '.md';
// Upload markdown file to gist.
console.log(`Uploading file ${name} as ${md} ...`);
fs.writeFileSync(md, `${name}.\n`);
var url = cp.execSync(`gh gist create -d "${name} : DOCUMENT" ${md}`).toString().trim();
fs.unlinkSync(md);
// Rename pdf file.
var pdf = 'document-' + _.kebabCase(desc) + '.pdf';
fs.renameSync(file, pdf);
// Upload pdf file to gist.
cp.execSync(`git clone ${url} gist`);
fs.copyFileSync(pdf, `gist/${pdf}`);
cp.execSync(`git add .`, {cwd: 'gist'});
cp.execSync(`git commit -m "Add ${pdf}"`, {cwd: 'gist'});
cp.execSync(`git push`, {cwd: 'gist'});
fs.rmSync('gist', {recursive: true});
fs.unlinkSync(pdf);
console.log();
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment