Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace smpltmpl dependency with internal function #111

Merged
merged 1 commit into from
Apr 8, 2020
Merged
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
23 changes: 17 additions & 6 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"pascal-case": "^3.1.1",
"pluralize": "^8.0.0",
"slash": "^3.0.0",
"smpltmpl": "^1.0.2",
"snake-case": "^3.0.3"
},
"nyc": {
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/

import { basename, join, isAbsolute, sep } from 'path'
import { template, templateFromFile } from 'smpltmpl'
import { StringTransformer } from './StringTransformer'
import { GeneratorFileOptions, GeneratorFileContract } from '../Contracts'
import { template, templateFromFile } from '../utils/template'

/**
* Exposes the API to construct the output file content, path
Expand Down
15 changes: 15 additions & 0 deletions src/utils/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { readFileSync } from 'fs'

export function template (tpl, data) {
return tpl.replace(/\$\{([a-zA-Z0-9_-]*)}/g, (_, p1: string) => {
if (!(p1 in data)) {
throw new Error(`Missing value for "${p1}"`)
}
return String(data[p1])
})
}

export function templateFromFile (file: string, data: object): string {
const contents = readFileSync(file, 'utf8')
return template(contents, data)
}
1 change: 1 addition & 0 deletions test/fixtures/template1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello ${value1}, ${value2}
48 changes: 48 additions & 0 deletions test/template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* @adonisjs/ace
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import test from 'japa'
import { join } from 'path'
import { template, templateFromFile } from '../src/utils/template'

test.group('template', () => {
test('template: interpolate valid template', (assert) => {
const result = template('${test} ${other}', {
test: 123,
other: 'hello',
})
assert.strictEqual(result, '123 hello')
})

test('template: error if a value is missing', (assert) => {
assert.throws(
() => template('${param}', {}),
'Missing value for "param"'
)
})

test('templateFromFile: interpolate valid template', (assert) => {
const result = templateFromFile(join(__dirname, 'fixtures/template1.txt'), {
value1: 'World',
value2: 42,
})

assert.strictEqual(result, 'Hello World, 42')
})

test('templateFromFile: error if a value is missing', (assert) => {
assert.throws(() => templateFromFile(join(__dirname, 'fixtures/template1.txt'), {
value1: 'World',
}), 'Missing value for "value2"')
})

test('templateFromFile: error if file is missing', (assert) => {
assert.throws(() => templateFromFile(join(__dirname, 'fixtures/i-do-not-exist'), {}))
})
})