Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/__snapshots__/metaverse.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ Object {
}
`;

exports[`metaverse hygen-extension: helpers-in-params new --name person 1`] = `
Object {
"actions": Array [
Object {
"status": "added",
"subject": "given/helpers-in-params/new.md",
"timing": -1,
"type": "add",
},
],
"success": true,
"time": 0,
}
`;

exports[`metaverse hygen-extension: helpers-in-prompt new 1`] = `
Object {
"actions": Array [
Object {
"status": "added",
"subject": "given/helpers-in-prompt/new.md",
"timing": -1,
"type": "add",
},
],
"success": true,
"time": 0,
}
`;

exports[`metaverse hygen-extension: hygen-js new 1`] = `
Object {
"actions": Array [
Expand Down
10 changes: 9 additions & 1 deletion src/__tests__/metaverse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ describe('metaverse', () => {
enquirer.prompt = failPrompt
})
metaverse('hygen-defaults', [['use-defaults']], { overwrite: true })
metaverse('hygen-extension', [['hygen-js', 'new']], { overwrite: true })
metaverse(
'hygen-extension',
[
['hygen-js', 'new'],
['helpers-in-prompt', 'new'],
['helpers-in-params', 'new', '--name', 'person'],
],
{ overwrite: true, name: 'messages' },
)

metaverse(
'hygen-templates',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
to: given/helpers-in-params/new.md
---
this demonstrates hygen loaded up .hygen.js and extended helpers in params.
<%= upperName %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
params: ({ args, h }) => {
return { upperName: h.extended(args.name) }
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
to: given/helpers-in-prompt/new.md
---
this demonstrates hygen loaded up .hygen.js and extended helpers in prompt.
<%= upperName %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
prompt: ({ prompter, h }) =>
prompter
.prompt({
type: 'input',
name: 'name',
message: "What's your name?",
})
.then(({ name }) => {
return { upperName: h.extended(name) }
}),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this demonstrates hygen loaded up .hygen.js and extended helpers in params.
PERSON
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this demonstrates hygen loaded up .hygen.js and extended helpers in prompt.
MESSAGES
21 changes: 8 additions & 13 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RunnerConfig } from './types'
import helpers from './helpers'
import { capitalize, createHelpers, inflection } from './helpers'

const localsToCapitalize = ['name']
const localsToPluralize = ['name']
Expand All @@ -11,14 +11,14 @@ const processLocals = (hsh, [key, value]) => {
hsh[key] = value

if (localsToCapitalize.includes(key)) {
hsh[helpers.capitalize(key)] = helpers.capitalize(value)
hsh[capitalize(key)] = capitalize(value)
}

if (localsToPluralize.includes(key)) {
hsh[helpers.inflection.pluralize(key)] = helpers.inflection.pluralize(value)
hsh[
helpers.capitalize(helpers.inflection.pluralize(key))
] = helpers.capitalize(helpers.inflection.pluralize(value))
hsh[inflection.pluralize(key)] = inflection.pluralize(value)
hsh[capitalize(inflection.pluralize(key))] = capitalize(
inflection.pluralize(value),
)
}

return hsh
Expand All @@ -33,17 +33,12 @@ const context = (locals: any, config: RunnerConfig = {}) => {
...config.localsDefaults,
...locals,
}
const configHelpers =
(config &&
(typeof config.helpers === 'function'
? config.helpers(locals, config)
: config.helpers)) ||
{}

return Object.assign(
localsWithDefaults,
processedLocals(localsWithDefaults),
{
h: { ...helpers, ...configHelpers },
h: createHelpers(locals, config),
},
)
}
Expand Down
25 changes: 19 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import inflection from 'inflection'
import changeCase from 'change-case'
import type { RunnerConfig } from './types'

// supports kebab-case to KebabCase
inflection.undasherize = (str) =>
Expand All @@ -9,14 +10,26 @@ inflection.undasherize = (str) =>
.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase())
.join('')

const helpers = {
capitalize(str) {
const toBeCapitalized = String(str)
return toBeCapitalized.charAt(0).toUpperCase() + toBeCapitalized.slice(1)
},
const capitalize = (str) => {
const toBeCapitalized = String(str)
return toBeCapitalized.charAt(0).toUpperCase() + toBeCapitalized.slice(1)
}

const globalHelpers = {
capitalize,
inflection,
changeCase,
path,
}

export default helpers
const createHelpers = (locals: any, config: RunnerConfig): any => {
const configHelpers =
(config &&
(typeof config.helpers === 'function'
? config.helpers(locals, config)
: config.helpers)) ||
{}
return { ...globalHelpers, ...configHelpers }
}

export { capitalize, createHelpers, inflection }
20 changes: 13 additions & 7 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ const resolvePositionals = async (templates: string, args: string[]) => {
}

const params = async (
{ templates, createPrompter }: RunnerConfig,
config: RunnerConfig,
externalArgv: string[],
): Promise<ParamsResult> => {
const { templates, createPrompter } = config
const argv = yargs(externalArgv)

const [generator, action, name] = await resolvePositionals(templates, argv._)
Expand All @@ -60,12 +61,17 @@ const params = async (
const actionfolder = path.join(templates, generator, mainAction)

const { _, ...cleanArgv } = argv
const promptArgs = await prompt(createPrompter, actionfolder, {
// NOTE we might also want the rest of the generator/action/etc. params here
// but theres no usecase yet
...(name ? { name } : {}),
...cleanArgv,
})
const promptArgs = await prompt(
createPrompter,
actionfolder,
{
// NOTE we might also want the rest of the generator/action/etc. params here
// but theres no usecase yet
...(name ? { name } : {}),
...cleanArgv,
},
config,
)

const args = Object.assign(
{
Expand Down
11 changes: 7 additions & 4 deletions src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs'
import type { Prompter } from './types'
import helpers from './helpers'
import type { Prompter, RunnerConfig } from './types'
import { createHelpers } from './helpers'

const hooksfiles = [
'index.js',
Expand All @@ -14,6 +14,7 @@ const prompt = async <Q, T>(
createPrompter: () => Prompter<Q, T>,
actionfolder: string,
args: Record<string, any>,
config: RunnerConfig,
): Promise<T | object> => {
const hooksfile = hooksfiles
.map((f) => path.resolve(path.join(actionfolder, f)))
Expand All @@ -34,8 +35,10 @@ const prompt = async <Q, T>(
hooksModule = hooksModule.default
}

const h = createHelpers({}, config)

if (hooksModule.params) {
return hooksModule.params({ args, h: helpers })
return hooksModule.params({ args, h })
}

// lazy loads prompter
Expand All @@ -46,7 +49,7 @@ const prompt = async <Q, T>(
prompter,
inquirer: prompter,
args,
h: helpers,
h,
})
}

Expand Down