From be004c5e02b462d7d0f39f3540bbcfce7e7ece6b Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Mon, 11 May 2020 11:33:34 +0200 Subject: [PATCH] readme template --- lib/Helper.ts | 2 ++ lib/snippets/readme/Assets.ts | 33 +++++++++++++++++++++++++++++++++ lib/snippets/readme/index.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lib/snippets/readme/Assets.ts create mode 100644 lib/snippets/readme/index.ts diff --git a/lib/Helper.ts b/lib/Helper.ts index 651fb29..6f0eaa0 100644 --- a/lib/Helper.ts +++ b/lib/Helper.ts @@ -44,6 +44,8 @@ async function modify_json ( const file_path = path.join (process.cwd (), json_path); const content = JSON.parse (await fs.readFile (file_path, 'utf-8')); const new_obj = await func (content); + if (typeof new_obj === 'undefined' || new_obj === null) + return; await fs.writeFile (file_path, JSON.stringify (new_obj, null, 2)); } diff --git a/lib/snippets/readme/Assets.ts b/lib/snippets/readme/Assets.ts new file mode 100644 index 0000000..ff1e03b --- /dev/null +++ b/lib/snippets/readme/Assets.ts @@ -0,0 +1,33 @@ +/* eslint-disable max-len */ +export function get_readme ( + software: string, + description: string, + dev: boolean, + license: string, + author: string +): string { + return `# ${software} + +[![Package quality](https://packagequality.com/shield/${software}.svg)](https://packagequality.com/#?package=${software}) + +> ${description} + +## Installation + +npm: + +> npm i --save${dev ? '-dev' : ''} ${software} + +yarn: + +> yarn add ${dev ? '--dev' : ''} ${software} + +## Usage + +TODO: Add usage + +## License + +${license} © ${author} +`; +} diff --git a/lib/snippets/readme/index.ts b/lib/snippets/readme/index.ts new file mode 100644 index 0000000..598b201 --- /dev/null +++ b/lib/snippets/readme/index.ts @@ -0,0 +1,33 @@ +import { Snippet } from '../../Snippet'; +import { modify_json, apply_template } from '../../Helper'; +import { get_readme } from './Assets'; + +export default class Readme implements Snippet { + public async start (): Promise { + const dev = await new Confirm ({ + message: 'is the package used as dev dependency?', + default: false + }) + .run (); + const package_data = { + software: '', + description: '', + license: '', + author: '' + }; + await modify_json ((json) => { + package_data.software = json.name; + package_data.description = json.description; + package_data.license = json.license; + package_data.author = json.author; + }); + const readme = get_readme ( + package_data.software, + package_data.description, + dev, + package_data.license, + package_data.author + ); + await apply_template (readme, 'README.md'); + } +}