Skip to content

Commit

Permalink
unify behavior of commands and cli with regards to local templates/re…
Browse files Browse the repository at this point in the history
…cipes
  • Loading branch information
loreanvictor committed Jan 5, 2024
1 parent e8f1709 commit 39a612b
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 43 deletions.
6 changes: 6 additions & 0 deletions e2e/fixtures/local-use/recipe/.tmplr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
steps:
- read: name
eval: World

- copy: ./halo.txt
to: ../halo.txt
1 change: 1 addition & 0 deletions e2e/fixtures/local-use/recipe/halo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Halo {{tmplr.name}}!
5 changes: 5 additions & 0 deletions e2e/fixtures/local/template/.tmplr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
steps:
- read: name
eval: World

- update: ./halo.txt
1 change: 1 addition & 0 deletions e2e/fixtures/local/template/halo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Halo {{ tmplr.name }}!
15 changes: 15 additions & 0 deletions e2e/local.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { scenario } from './util.mjs'


scenario('local', async (run, { ls, read }) => {
await run('local:../template')
expect(await ls()).toIncludeSameMembers([ '.tmplr.yml', 'halo.txt' ])
expect(await read('halo.txt')).toBe('Halo World!')
}, { root: 'target' })


scenario('local-use', async (run, { ls, read }) => {
await run('use', 'local:../recipe')
expect(await ls()).toIncludeSameMembers([ 'halo.txt' ])
expect(await read('halo.txt')).toBe('Halo World!')
}, { root: 'target' })
12 changes: 8 additions & 4 deletions e2e/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { execa, $ } from 'execa'
import { getBinPath } from 'get-bin-path'


export function scenario(name, testFn) {
export function scenario(name, testFn, options) {
options ??= {}
options.root ??= '.'

test('scenario: ' + name, async () => {
const fixsrc = name.split(':')[0]
const fixture = join('e2e', 'fixtures', fixsrc)
Expand All @@ -19,9 +22,10 @@ export function scenario(name, testFn) {
const dir = await mkdtemp('test-')
await cp(fixture, dir, { recursive: true })

const cmd = $({ cwd: dir })
const cwd = join(dir, options.root)
const cmd = $({ cwd })
const bin = await getBinPath()
const run = (...args) => execa(bin, args, { cwd: dir })
const run = (...args) => execa(bin, args, { cwd })

try {
await testFn(run, {
Expand All @@ -32,7 +36,7 @@ export function scenario(name, testFn) {
return stdout.split('\n').filter(x => x !== '.' && x !== '..')
},
read: async file => {
return await readFile(join(dir, file), 'utf8')
return await readFile(join(cwd, file), 'utf8')
}
})
} finally {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tmplr",
"version": "0.3.1",
"version": "0.3.2",
"description": "Easy templating for repositories",
"bin": "dist/src/index.js",
"engines": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"homepage": "https://github.com/loreanvictor/tmplr#readme",
"dependencies": {
"@tmplr/core": "^0.7.4",
"@tmplr/node": "^0.2.0",
"@tmplr/node": "^0.2.2",
"@tmplr/react": "^0.1.6",
"@tmplr/yaml-parser": "^0.1.8",
"chalk": "^4.1.2",
Expand Down
14 changes: 3 additions & 11 deletions src/app/template/env.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { useAsync } from 'react-use'
import { cp, mkdir } from 'fs/promises'
import { mkdir } from 'fs/promises'

import { createRuntime } from '../runtime'
import { degitAndRun, runLocalRecipe } from '../../recipes'
import { degitAndRun } from '../../recipes'


export function useTemplateEnv(workdir: string, target: string) {
const { value, loading, error } = useAsync(async () => {
const local = target.startsWith('local:')
const path = local ? target.slice(6) : target

await mkdir(workdir, { recursive: true })
if (local) {
await cp(path, workdir, { recursive: true })
}

return createRuntime(
workdir,
async () => {
return local ? runLocalRecipe() : degitAndRun(target)
}
async () => degitAndRun(target)
)
})

Expand Down
22 changes: 4 additions & 18 deletions src/app/use/env.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import { useAsync } from 'react-use'
import { join } from 'path'
import { access, mkdir, cp } from 'fs/promises'
import { access } from 'fs/promises'

import { createRuntime } from '../runtime'
import { useRecipe, runAndClean } from '../../recipes'
import { useRecipe } from '../../recipes'


export function useUseEnv(workdir: string, target: string) {
const {value, loading, error} = useAsync(async () => {
const local = target.startsWith('local:')
await access(workdir)

if (local) {
const path = local ? target.slice(6) : target
const dirname = '.use-' + Math.random().toString(36).slice(2)
const recipedir = join(workdir, dirname)

await mkdir(recipedir, { recursive: true })
await cp(path, recipedir, { recursive: true })

return await createRuntime(workdir, async () => runAndClean(join(dirname, '.tmplr.yml')))
} else {
await access(workdir)

return await createRuntime(workdir, async () => useRecipe(target))
}
return await createRuntime(workdir, async () => useRecipe(target))
}, [workdir, target])

return { runtime: value, loading, error }
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"moduleResolution": "Node16",
"jsx": "react",
"esModuleInterop": true,
Expand Down

0 comments on commit 39a612b

Please sign in to comment.