From 8f8a16d0258eaf4450a9f6b937a7eed378fb5f08 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 10 May 2023 15:30:51 -0400 Subject: [PATCH] add command add fix #2 --- CHANGELOG.yml | 7 +-- _templates/command/new/command.ejs.t | 17 +++++++ _templates/generator/help/index.ejs.t | 5 ++ _templates/generator/new/hello.ejs.t | 18 +++++++ _templates/generator/with-prompt/hello.ejs.t | 18 +++++++ _templates/generator/with-prompt/prompt.ejs.t | 14 +++++ _templates/init/repo/new-repo.ejs.t | 4 ++ package.json | 1 + src/changelord.js | 47 +++++++++-------- src/command/add.js | 51 +++++++++++++++++++ 10 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 _templates/command/new/command.ejs.t create mode 100644 _templates/generator/help/index.ejs.t create mode 100644 _templates/generator/new/hello.ejs.t create mode 100644 _templates/generator/with-prompt/hello.ejs.t create mode 100644 _templates/generator/with-prompt/prompt.ejs.t create mode 100644 _templates/init/repo/new-repo.ejs.t create mode 100644 src/command/add.js diff --git a/CHANGELOG.yml b/CHANGELOG.yml index 50de369..3746938 100644 --- a/CHANGELOG.yml +++ b/CHANGELOG.yml @@ -5,12 +5,13 @@ project: ticket_url: null releases: - version: NEXT - changes: + changes: - port the Perl changelord to JavaScript. change_types: - - title: '' + - title: "" level: minor - keywords: [''] + keywords: + - "" - title: Features level: minor keywords: diff --git a/_templates/command/new/command.ejs.t b/_templates/command/new/command.ejs.t new file mode 100644 index 0000000..34c6d11 --- /dev/null +++ b/_templates/command/new/command.ejs.t @@ -0,0 +1,17 @@ +--- +to: src/command/<%= name %>.js +--- + +const handler = async (config) => { + // DO SOMETHING +}; + +export default { + command: '<%= name %>', + desc : 'TODO', + builder: (yargs) => { + yargs + }, + handler, +} + diff --git a/_templates/generator/help/index.ejs.t b/_templates/generator/help/index.ejs.t new file mode 100644 index 0000000..90a29af --- /dev/null +++ b/_templates/generator/help/index.ejs.t @@ -0,0 +1,5 @@ +--- +message: | + hygen {bold generator new} --name [NAME] --action [ACTION] + hygen {bold generator with-prompt} --name [NAME] --action [ACTION] +--- \ No newline at end of file diff --git a/_templates/generator/new/hello.ejs.t b/_templates/generator/new/hello.ejs.t new file mode 100644 index 0000000..5680d96 --- /dev/null +++ b/_templates/generator/new/hello.ejs.t @@ -0,0 +1,18 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t +--- +--- +to: app/hello.js +--- +const hello = ``` +Hello! +This is your first hygen template. + +Learn what it can do here: + +https://github.com/jondot/hygen +``` + +console.log(hello) + + diff --git a/_templates/generator/with-prompt/hello.ejs.t b/_templates/generator/with-prompt/hello.ejs.t new file mode 100644 index 0000000..ba6abc5 --- /dev/null +++ b/_templates/generator/with-prompt/hello.ejs.t @@ -0,0 +1,18 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t +--- +--- +to: app/hello.js +--- +const hello = ``` +Hello! +This is your first prompt based hygen template. + +Learn what it can do here: + +https://github.com/jondot/hygen +``` + +console.log(hello) + + diff --git a/_templates/generator/with-prompt/prompt.ejs.t b/_templates/generator/with-prompt/prompt.ejs.t new file mode 100644 index 0000000..76ea532 --- /dev/null +++ b/_templates/generator/with-prompt/prompt.ejs.t @@ -0,0 +1,14 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/prompt.js +--- + +// see types of prompts: +// https://github.com/enquirer/enquirer/tree/master/examples +// +module.exports = [ + { + type: 'input', + name: 'message', + message: "What's your message?" + } +] diff --git a/_templates/init/repo/new-repo.ejs.t b/_templates/init/repo/new-repo.ejs.t new file mode 100644 index 0000000..08e7cff --- /dev/null +++ b/_templates/init/repo/new-repo.ejs.t @@ -0,0 +1,4 @@ +--- +setup: <%= name %> +force: true # this is because mostly, people init into existing folders is safe +--- diff --git a/package.json b/package.json index 0fcac03..c855518 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "dependencies": { + "@yanick/updeep-remeda": "^2.2.0", "consola": "^3.1.0", "fs-extra": "^11.1.1", "markdown-utils": "^1.0.0", diff --git a/src/changelord.js b/src/changelord.js index 42acf55..e1c4b80 100755 --- a/src/changelord.js +++ b/src/changelord.js @@ -1,29 +1,32 @@ #!/usr/bin/env node -import { hideBin } from 'yargs/helpers'; -import yargs from 'yargs'; -import { join } from 'path'; -import yaml from 'yaml'; -import fs from 'fs-extra'; +import { hideBin } from "yargs/helpers"; +import yargs from "yargs"; +import { join } from "path"; +import yaml from "yaml"; +import fs from "fs-extra"; +import consola from "consola"; -import print from './command/print.js' -import init from './command/init.js' -import schema from './command/schema.js'; -import consola from 'consola'; +import print from "./command/print.js"; +import init from "./command/init.js"; +import schema from "./command/schema.js"; +import add from "./command/add.js"; consola.raw = (...args) => console.log(...args); - yargs(hideBin(process.argv)) - .config({ - consola, - }) - .default('source', join( process.cwd(), 'CHANGELOG.yml' )) - .describe('source', 'changelog source') - .command({ - ...print, - command: '$0', - }) - .command(init) - .command(schema) - .command(print).help().parse(); + .config({ + consola, + }) + .default("source", join(process.cwd(), "CHANGELOG.yml")) + .describe("source", "changelog source") + .command({ + ...print, + command: "$0", + }) + .command(init) + .command(add) + .command(schema) + .command(print) + .help() + .parse(); diff --git a/src/command/add.js b/src/command/add.js new file mode 100644 index 0000000..4c48fe0 --- /dev/null +++ b/src/command/add.js @@ -0,0 +1,51 @@ +import fs from "fs-extra"; +import yaml from "yaml"; +import u from "@yanick/updeep-remeda"; + +const handler = async (config) => { + if (!config.change) + throw new Error("can't add a change without a description"); + + const entry = { + desc: config.change.join(" "), + }; + + if (config.ticket) entry.ticket = config.ticket; + if (config.type) entry.type = config.type; + + config.consola.start(`adding '${entry.desc}' to the changelog`); + + config.add_to_next(entry); + + config.consola.success("done!"); +}; + +export default { + command: "add [change...]", + desc: "add a change to the NEXT release", + builder: (yargs) => { + yargs + .string("ticket") + .string("type") + .middleware((argv) => { + argv.add_to_next = async (entry) => { + const changelog = yaml.parse(await fs.readFile(argv.source, "utf-8")); + + let next = changelog.releases.find(u.matches({ version: "NEXT" })); + if (!next) { + next = { version: "NEXT", changes: [] }; + changelog.releases.unshift(next); + } + + if (Object.keys(entry).length === 1) { + entry = entry.desc; + } + + next.changes.push(entry); + + return fs.writeFile(argv.source, yaml.stringify(changelog)); + }; + }); + }, + handler, +};