Skip to content

Commit f8011b5

Browse files
committed
Export handlebar methods
1 parent 60e095e commit f8011b5

14 files changed

Lines changed: 88 additions & 23 deletions

File tree

examples/todo.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import type { Builder, Command, Describe, Handler } from 'landlubber'
22

3-
import { todo } from '@seamapi/smith'
3+
import { getHandlebarsPartials } from '@seamapi/smith'
44

55
interface Options {
6-
x: string
6+
root: string
77
}
88

9-
export const command: Command = 'todo x'
9+
export const command: Command = 'todo root'
1010

1111
export const describe: Describe = 'TODO'
1212

1313
export const builder: Builder = {
14-
x: {
14+
root: {
1515
type: 'string',
16-
default: 'TODO',
16+
default: 'test/fixtures/handlebars/partials',
1717
describe: 'TODO',
1818
},
1919
}
2020

21-
export const handler: Handler<Options> = async ({ x, logger }) => {
22-
logger.info({ data: todo(x) }, 'TODO')
21+
export const handler: Handler<Options> = async ({ root, logger }) => {
22+
logger.info({ data: getHandlebarsPartials(root) }, 'TODO')
2323
}

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
},
9090
"dependencies": {
9191
"@types/node": "^22.15.21",
92+
"change-case": "^5.4.4",
9293
"eslint-config-prettier": "^9.0.0",
9394
"eslint-config-standard": "^17.1.0",
9495
"eslint-config-standard-with-typescript": "^43.0.0",

src/lib/handlebars/helpers.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import test from 'ava'
2+
3+
import { add, eq, or, toCapitalCase } from './helpers.js'
4+
5+
test('eq: compares arguments', (t) => {
6+
t.true(eq(1, 1))
7+
t.false(eq(1, 2))
8+
})
9+
10+
test('or: compares arguments', (t) => {
11+
t.true(or(true, false))
12+
t.true(or(true, true))
13+
t.false(or(false, false))
14+
})
15+
16+
test('add: adds arguments', (t) => {
17+
t.is(add(1, 2), 3)
18+
})
19+
20+
test('toCapitalCase: capitalizes argument', (t) => {
21+
t.is(toCapitalCase('foo_bar'), 'Foo Bar')
22+
})

src/lib/handlebars/helpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { capitalCase } from 'change-case'
2+
3+
export const eq = (v1: unknown, v2: unknown): boolean => {
4+
return v1 === v2
5+
}
6+
7+
export const or = (...args: unknown[]): boolean => {
8+
// Remove the last argument, which is the Handlebars options object.
9+
args.pop()
10+
return args.some(Boolean)
11+
}
12+
13+
export const add = (v1: number, v2: number): number => {
14+
return v1 + v2
15+
}
16+
17+
export const toCapitalCase = (str: string): string => {
18+
return capitalCase(str)
19+
}

src/lib/handlebars/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * as handlebarsHelpers from './helpers.js'
2+
export * from './partials.js'

src/lib/handlebars/partials.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { parse } from 'node:path'
3+
4+
import { glob } from 'glob'
5+
6+
export const getHandlebarsPartials = async (
7+
rootPath: string,
8+
): Promise<Record<string, string>> => {
9+
const paths = await glob(`${rootPath}/**/*.hbs`)
10+
const keys = paths.map((p) => parse(p).name)
11+
const contents = await Promise.all(paths.map(async (p) => await readFile(p)))
12+
return Object.fromEntries(
13+
keys.map((_, i) => [keys[i], contents[i]?.toString()]),
14+
)
15+
}

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { todo } from './todo.js'
1+
export * from './handlebars/index.js'

src/lib/todo.test.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lib/todo.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)