add command add

fix #2
This commit is contained in:
Yanick Champoux 2023-05-10 15:30:51 -04:00
parent 0ed4ca9cab
commit 8f8a16d025
10 changed files with 157 additions and 25 deletions

View File

@ -5,12 +5,13 @@ project:
ticket_url: null ticket_url: null
releases: releases:
- version: NEXT - version: NEXT
changes: changes:
- port the Perl changelord to JavaScript. - port the Perl changelord to JavaScript.
change_types: change_types:
- title: '' - title: ""
level: minor level: minor
keywords: [''] keywords:
- ""
- title: Features - title: Features
level: minor level: minor
keywords: keywords:

View File

@ -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,
}

View File

@ -0,0 +1,5 @@
---
message: |
hygen {bold generator new} --name [NAME] --action [ACTION]
hygen {bold generator with-prompt} --name [NAME] --action [ACTION]
---

View File

@ -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)

View File

@ -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)

View File

@ -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?"
}
]

View File

@ -0,0 +1,4 @@
---
setup: <%= name %>
force: true # this is because mostly, people init into existing folders is safe
---

View File

@ -16,6 +16,7 @@
"author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)", "author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@yanick/updeep-remeda": "^2.2.0",
"consola": "^3.1.0", "consola": "^3.1.0",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"markdown-utils": "^1.0.0", "markdown-utils": "^1.0.0",

View File

@ -1,29 +1,32 @@
#!/usr/bin/env node #!/usr/bin/env node
import { hideBin } from 'yargs/helpers'; import { hideBin } from "yargs/helpers";
import yargs from 'yargs'; import yargs from "yargs";
import { join } from 'path'; import { join } from "path";
import yaml from 'yaml'; import yaml from "yaml";
import fs from 'fs-extra'; import fs from "fs-extra";
import consola from "consola";
import print from './command/print.js' import print from "./command/print.js";
import init from './command/init.js' import init from "./command/init.js";
import schema from './command/schema.js'; import schema from "./command/schema.js";
import consola from 'consola'; import add from "./command/add.js";
consola.raw = (...args) => console.log(...args); consola.raw = (...args) => console.log(...args);
yargs(hideBin(process.argv)) yargs(hideBin(process.argv))
.config({ .config({
consola, consola,
}) })
.default('source', join( process.cwd(), 'CHANGELOG.yml' )) .default("source", join(process.cwd(), "CHANGELOG.yml"))
.describe('source', 'changelog source') .describe("source", "changelog source")
.command({ .command({
...print, ...print,
command: '$0', command: "$0",
}) })
.command(init) .command(init)
.command(schema) .command(add)
.command(print).help().parse(); .command(schema)
.command(print)
.help()
.parse();

51
src/command/add.js Normal file
View File

@ -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,
};