From 2f7e2e33e208a82e1beba300a847763e9f465829 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 10 May 2023 10:28:23 -0400 Subject: [PATCH] LORD-5: add init command --- CHANGELOG.yml | 27 ++++++++++++++++++++++++ package.json | 12 ++++++++++- src/changelord.js | 18 ++++++++++++++++ src/command/init.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/command/print.js | 16 ++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.yml create mode 100755 src/changelord.js create mode 100644 src/command/init.js create mode 100644 src/command/print.js diff --git a/CHANGELOG.yml b/CHANGELOG.yml new file mode 100644 index 0000000..2dd90d9 --- /dev/null +++ b/CHANGELOG.yml @@ -0,0 +1,27 @@ +project: + name: null + homepage: null + with_stats: true + ticket_url: null +releases: + - version: NEXT + changes: [] +change_types: + - title: Features + level: minor + keywords: + - feat + - title: Bug fixes + level: patch + keywords: + - fix + - title: Package maintenance + level: patch + keywords: + - chore + - maint + - refactor + - title: Statistics + level: patch + keywords: + - stats diff --git a/package.json b/package.json index 66b03e7..3763c9e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,11 @@ "name": "changelord", "version": "0.0.1", "description": "cli-based changelog manager", - "main": "dist/index.js", + "type": "module", + "main": "src/index.js", + "bin": { + "changelord": "./src/changelord.js" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -12,6 +16,12 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "dependencies": { + "consola": "^3.1.0", + "fs-extra": "^11.1.1", + "yaml": "^2.2.2", "yargs": "^17.7.2" + }, + "devDependencies": { + "prettier": "^2.8.8" } } diff --git a/src/changelord.js b/src/changelord.js new file mode 100755 index 0000000..454a2c0 --- /dev/null +++ b/src/changelord.js @@ -0,0 +1,18 @@ +#!/usr/bin/env node + +import { hideBin } from 'yargs/helpers'; +import yargs from 'yargs'; +import { join } from 'path'; + +import print from './command/print.js' +import init from './command/init.js' + +yargs(hideBin(process.argv)) + .default('source', join( process.cwd(), 'CHANGELOG.yml' )) + .describe('source', 'changelog source') + .command({ + ...print, + command: '$0', + }) + .command(init) + .command(print).help().parse(); diff --git a/src/command/init.js b/src/command/init.js new file mode 100644 index 0000000..6db14b6 --- /dev/null +++ b/src/command/init.js @@ -0,0 +1,50 @@ +import fs from 'fs-extra'; +import { consola } from 'consola'; +import { stringify } from 'yaml'; + +const change_types = [ + { title: 'Features' , level: 'minor', keywords: [ 'feat' ] } , + { title : 'Bug fixes' , level : 'patch', keywords : [ 'fix' ] }, + { title : 'Package maintenance' , level : 'patch', keywords : [ 'chore', 'maint', 'refactor' ] }, + { title : 'Statistics' , level : 'patch', keywords : [ 'stats' ] }, + ]; + + +const base_changelog = { + project: { + name: null, + homepage: null, + with_stats: true, + ticket_url: null, + }, + releases: [ + { version: 'NEXT', changes: [] } + ], + change_types, +}; + +const handler = async (config) => { + if( await fs.pathExists(config.source) ) { + consola.error(`${config.source} already exist, aborting.`); + process.exit(); + } + + consola.start(`creating ${config.source}...`); + + await fs.writeFile( config.source, stringify(base_changelog) ); + + consola.success('done!'); + +}; + +export default { + command: 'init', + desc : 'initialize new changelog source file', + builder: (yargs) => { + yargs.boolean('json') + .boolean('next') + .default('json',false) + .default('next',true); + }, + handler, +} diff --git a/src/command/print.js b/src/command/print.js new file mode 100644 index 0000000..f90ee01 --- /dev/null +++ b/src/command/print.js @@ -0,0 +1,16 @@ + +const handler = (...args) => { + console.log('hi!',args); +}; + +export default { + command: 'print', + desc : 'render the changelog', + builder: (yargs) => { + yargs.boolean('json') + .boolean('next') + .default('json',false) + .default('next',true); + }, + handler, +}