Skip to content

Commit

Permalink
feat: initial setup (#1)
Browse files Browse the repository at this point in the history
* feat: add initial version of script

* fix: add fallback to undefined description and change default headers

* fix: use start and end tags to replace content

* refactor: move logic to inside generate folder

* feat: add CLI interactivity

* fix: properly handle errors when parsing snippets

* chore: change package name

* chore: add more config to package.json
  • Loading branch information
arthurdenner committed May 4, 2020
1 parent b22e6ed commit dfbb2f6
Show file tree
Hide file tree
Showing 6 changed files with 8,409 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
.DS_Store
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
save-exact=true
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "snippets-table",
"version": "0.0.0-semantically-released",
"description": "Tool to easily manage a table of snippets",
"main": "dist/cli.js",
"bin": {
"snippets-table": "dist/cli.js"
},
"files": [
"dist"
],
"scripts": {
"build": "kcd-scripts build",
"lint": "kcd-scripts lint"
},
"repository": {
"type": "git",
"url": "git+https://github.com/arthurdenner/snippets-table.git"
},
"keywords": [
"snippets-table"
],
"author": "Arthur Denner <arthurdenner7@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/arthurdenner/snippets-table/issues"
},
"homepage": "https://github.com/arthurdenner/snippets-table#readme",
"devDependencies": {
"kcd-scripts": "5.6.1"
},
"dependencies": {
"@babel/runtime": "7.9.2",
"chalk": "3.0.0",
"inquirer": "7.1.0",
"prettier": "2.0.2",
"yargs": "15.3.1"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
"rules": {
"no-process-exit": "off"
}
}
}
48 changes: 48 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

const inquirer = require('inquirer');
const yargs = require('yargs');
const generate = require('./generate');

const yargv = yargs
.help('help')
.alias('h', 'help')
.alias('v', 'version')
.version()
.recommendCommands()
.command('generate', 'Generate the table of snippets')
.usage('Usage: $0 generate')
.default('pathToREADME', 'README.md')
.default('pathToSnippets', 'snippets/snippets.json')
.default('headers', ['Prefix', 'Name', 'Description']);

function promptForCommand({ argv }) {
const questions = [
{
type: 'list',
name: 'command',
message: 'What do you want to do?',
choices: [
{
name: 'Re-generate the table of snippets',
value: 'generate',
},
],
when: !argv._[0],
default: 0,
},
];

return inquirer.prompt(questions).then(answers => {
return answers.command || argv._[0];
});
}

promptForCommand(yargv).then(command => {
switch (command) {
case 'generate':
return generate(yargv.parsed.argv);
default:
throw new Error(`Unknown command ${command}`);
}
});
58 changes: 58 additions & 0 deletions src/generate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable no-console */

const fs = require('fs');
const chalk = require('chalk');
const prettier = require('prettier');

const START_TAG =
'<!-- SNIPPETS-TABLE:START - Do not remove or modify this line -->';
const END_TAG = '<!-- SNIPPETS-TABLE:END -->';
const readFile = path => fs.promises.readFile(path, 'utf8');
const getPrefix = p => (Array.isArray(p) ? p.join(', ') : p);

function createTableLines(snippets, headers) {
const separatorLine = headers.reduce(acc => acc.concat(`--- | `), '\n| ');
const headersLine = headers.reduce((acc, h) => acc.concat(`${h} | `), '| ');
const bodyLines = Object.keys(snippets).reduce((acc, key) => {
const { description = '---', prefix } = snippets[key];
const newLine = `\n| \`${getPrefix(prefix)}\` | ${key} | ${description} |`;

return acc.concat(newLine);
}, '');
const table = headersLine.concat(separatorLine, bodyLines);

return prettier.format(table, { parser: 'markdown' }).trim();
}

async function generateTable({ pathToREADME, pathToSnippets, headers }) {
try {
const readme = await readFile(pathToREADME);
const regex = RegExp(`${START_TAG}([\\s\\S]*?)${END_TAG}`);

if (!regex.test(readme)) {
throw Error("Couldn't find start and end tags");
}

let snippets = await readFile(pathToSnippets);

try {
snippets = JSON.parse(snippets);
} catch (err) {
console.error(chalk.red(err.message));
throw Error(`There was an error parsing ${pathToSnippets}`);
}

const snippetsLines = createTableLines(snippets, headers);
const replace = START_TAG.concat('\n\n', snippetsLines, '\n\n', END_TAG);
const newReadme = readme.replace(regex, replace);

await fs.promises.writeFile(pathToREADME, newReadme, 'utf-8');

console.log(chalk.green(`${pathToREADME} updated!`));
} catch (err) {
console.error(chalk.red(err.message));
process.exit(1);
}
}

module.exports = generateTable;

0 comments on commit dfbb2f6

Please sign in to comment.