Skip to content

Commit

Permalink
refactor: replace smpltmpl dependency with internal function (#111)
Browse files Browse the repository at this point in the history
smpltmpl depends on babel-code-frame, which, with its subdependencies
adds about 10 unnecessary packages to the tree in production.
Moreover, it interpolates the template by running it as code in a Node.js
vm, which is not that simple, for a "simple" library.
Replace it with a simple internal implementation.
  • Loading branch information
targos committed Apr 8, 2020
1 parent 6cddaff commit 0e00817
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
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 @@ -40,7 +40,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'), {}))
})
})

0 comments on commit 0e00817

Please sign in to comment.