Skip to content

Commit

Permalink
feat: add docs and improve config (#2)
Browse files Browse the repository at this point in the history
* docs: add example folder

* docs: add README file

* chore: add TravisCI config and tweak other configs

* ci: rename Travis CI config file
  • Loading branch information
arthurdenner authored Jul 17, 2020
1 parent dfbb2f6 commit 5bad453
Show file tree
Hide file tree
Showing 9 changed files with 2,028 additions and 1,788 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
cache: yarn
notifications:
email: false
node_js:
- 10
- 12
install: yarn --frozen-lockfile
script: yarn validate
after_success:
- npx codecov --disable=gcov
- test $TRAVIS_BRANCH = "master" && test $TRAVIS_PULL_REQUEST = "false" && npx semantic-release
branches:
only:
- develop
- master
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# snippets-table

## Overview

Tool to easily manage a table of snippets on a README file.

## Installation

Install the package:

`npm i --dev snippets-table` or `yarn add -D snippets-table`

## Usage

Add the following lines to the README where the table of snippets should be created:

```markdown
<!-- SNIPPETS-TABLE:START - Do not remove or modify this line -->

<!-- SNIPPETS-TABLE:END -->
```

Run the command:

`npm run snippets-table generate` or `yarn snippets-table generate`

Alternatively, run it with `npx`:

`npx snippets-table generate`

## License

MIT © [Arthur Denner](https://github.com/arthurdenner/)
13 changes: 13 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# snippets-table-example

Example of usage of the package. Check the raw version [here](https://raw.githubusercontent.com/arthurdenner/snippets-table/master/example/README.md).

## How to test it

- Check the `snippets/snippets.json` file;
- Run the `update-readme` script;
- See the ✨magic ✨.

<!-- SNIPPETS-TABLE:START - Do not remove or modify this line -->

<!-- SNIPPETS-TABLE:END -->
10 changes: 10 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "snippets-table-example",
"version": "0.0.0",
"main": "index.js",
"author": "Arthur Denner <arthurdenner7@gmail.com>",
"license": "MIT",
"scripts": {
"update-readme": "node ../src/cli.js"
}
}
32 changes: 32 additions & 0 deletions example/snippets/snippets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Sing ABC": {
"description": "Song by Jackson 5",
"prefix": "abc",
"body": [
"A, B, C",
"Easy as 1, 2, 3",
"Or simple as Do-Re-Mi",
"A, B, C, 1, 2, 3, baby you and me, girl!"
]
},
"Sing Take on Me": {
"description": "Song by a-ha",
"prefix": "takeOnMe",
"body": [
"Take on me (take on me)",
"Take me on (take on me)",
"I'll be gone",
"In a day or two"
]
},
"Sing Help": {
"description": "Song by The Beatles",
"prefix": "help",
"body": [
"I need somebody",
"(Help!) not just anybody",
"(Help!) you know I need someone",
"Help!"
]
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"bin": {
"snippets-table": "dist/cli.js"
},
"engines": {
"node": ">=10 <11 || >=12"
},
"files": [
"dist"
],
"scripts": {
"validate": "yarn lint && yarn build",
"build": "kcd-scripts build",
"lint": "kcd-scripts lint"
},
Expand Down Expand Up @@ -39,6 +43,7 @@
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
"rules": {
"no-console": "off",
"no-process-exit": "off"
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function promptForCommand({ argv }) {
message: 'What do you want to do?',
choices: [
{
name: 'Re-generate the table of snippets',
name: 'Generate the table of snippets',
value: 'generate',
},
],
Expand All @@ -33,12 +33,12 @@ function promptForCommand({ argv }) {
},
];

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

promptForCommand(yargv).then(command => {
promptForCommand(yargv).then((command) => {
switch (command) {
case 'generate':
return generate(yargv.parsed.argv);
Expand Down
11 changes: 5 additions & 6 deletions src/generate/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/* 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);
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 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 keys = Object.keys(snippets).sort();
const bodyLines = keys.reduce((acc, key) => {
const { description = '---', prefix } = snippets[key];
const newLine = `\n| \`${getPrefix(prefix)}\` | ${key} | ${description} |`;

Expand Down
Loading

0 comments on commit 5bad453

Please sign in to comment.