template-file
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -9,5 +9,6 @@ import fs from 'fs' | ||
test('Shallow data is replaced when given string', t => { | ||
test('Data is replaced when given string', t => { | ||
// Should return the same without regard of consistent spacing | ||
const templateString = 'The {{ adjective1 }}, {{adjective2 }} {{ noun1}} jumped over the {{adjective3}} {{ noun2 }}.' | ||
const templateString = | ||
'The {{ adjective1 }}, {{adjective2 }} {{ person.title}} jumped over the {{adjective3}} {{ noun }}.' | ||
const templateData = { | ||
@@ -17,4 +18,6 @@ adjective1: 'cool', | ||
adjective3: 'silly', | ||
noun1: 'developer', | ||
noun2: 'laptop' | ||
noun: () => 'laptop', | ||
person: { | ||
title: 'developer' | ||
} | ||
} | ||
@@ -28,9 +31,39 @@ | ||
test('Shallow data is replaced when given file ', async t => { | ||
test('Data is replaced when given file path', async t => { | ||
const inputFile = path.resolve('./__tests__/helpers/testme.conf') | ||
const expectedFile = path.resolve('./__tests__/helpers/expected.conf') | ||
const mimeTypes = [ | ||
'application/atom+xml', | ||
'application/javascript', | ||
'application/json', | ||
'application/msword', | ||
'application/pdf', | ||
'application/postscript', | ||
'application/rtf', | ||
'application/vnd.ms-excel', | ||
'application/vnd.ms-fontobject', | ||
'application/vnd.ms-powerpoint', | ||
'application/vnd.wap.wml', | ||
'application/x-font-opentype', | ||
'application/x-font-ttf', | ||
'application/x-javascript', | ||
'application/xhtml+xml', | ||
'application/xml', | ||
'image/bmp', | ||
'image/svg+xml', | ||
'image/x-icon', | ||
'text/css', | ||
'text/javascript', | ||
'text/plain', | ||
'text/x-component', | ||
'text/xml' | ||
] | ||
const actual = await renderTemplateFile(inputFile, { | ||
aPath: '/this-is-a-test', | ||
domain: 'reallycooldomain.com' | ||
domain: 'reallycooldomain.com', | ||
gzip: { | ||
mimeTypes: () => mimeTypes.join(' ') | ||
} | ||
}) | ||
@@ -37,0 +70,0 @@ |
const pify = require('pify') | ||
const readFile = pify(require('fs').readFile) | ||
const valueForProperty = require('dot-prop').get | ||
function renderString(template, data) { | ||
return template.replace(/\{\{\s?(.*?)\s?\}\}/g, (match, captured) => { | ||
const replacement = data[captured.trim()] | ||
const replacement = valueForProperty(data, captured.trim()) | ||
@@ -8,0 +9,0 @@ // If a template variable is found but nothing is supplied to fill it, remove it |
{ | ||
"name": "template-file", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "🔀 Replace {{ variables }} in all your files", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"dot-prop": "^4.2.0", | ||
"glob": "^7.1.1", | ||
@@ -32,7 +33,7 @@ "meow": "^3.7.0", | ||
"devDependencies": { | ||
"ava": "^0.19.0", | ||
"eslint": "^3.17.1", | ||
"ava": "^0.21.0", | ||
"eslint": "^4.1.0", | ||
"eslint-config-standard": "^10.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^4.2.1", | ||
"eslint-plugin-node": "^5.0.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
@@ -39,0 +40,0 @@ "eslint-plugin-standard": "^3.0.1" |
@@ -14,4 +14,5 @@ # template-file | ||
- If the value of one of the keys is a function, the result of that function is used for replacement. | ||
- Deeply-nested keys can be used for replacements. | ||
**⚠️ NOTE:** As of right now, only shallow values can be used in files. We know this sucks, and there’s an open [issue](https://github.com/gsandf/template-file/issues/1) for it. However, we believe in releasing early and often. | ||
**⚠️ NOTE:** Keys with a period in the name will not be resolved. `{{ user.name }}` will look for `{ user: { name: '' }}` but not `{ 'user.name': ''}`. This would be easy to change, but we're leaving as-is for now for slightly better replacement performance (please open an issue if you would like the other behavior). | ||
@@ -32,3 +33,3 @@ ## Usage | ||
**ℹ️ TIP:** Remember to place single quotes around your arguments (if they contain asterisks, question marks, etc.) to keep your shell from expanding globs before `template-file` gets to consume them. | ||
**ℹ️ TIP:** Remember to place quotes around your arguments (if they contain asterisks, question marks, etc.) to keep your shell from expanding globs before `template-file` gets to consume them. | ||
@@ -47,3 +48,3 @@ Just handle one file: | ||
Compile all HTML files in `src/` to `dist/` using the result of a JavaScript module: | ||
Compile all HTML files in `src/` to `dist/` using the exported result of a JavaScript module: | ||
@@ -60,8 +61,10 @@ ```shell | ||
const data = { | ||
company: "GS&F", | ||
adjective: "cool" | ||
location: { | ||
name: 'Nashville' | ||
}, | ||
adjective: 'cool' | ||
} | ||
// Replace variables in string | ||
renderString('{{ company }} is {{ adjective }}.', data) // 'GS&F is cool.' | ||
renderString('{{ location.name }} is {{ adjective }}.', data) // 'Nashville is cool.' | ||
@@ -75,3 +78,3 @@ // Replace variables in a file | ||
With either [Yarn](https://yarnpkg.com/) or [npm](https://npmjs.org/) installed, run one of the following: | ||
With either [Yarn](https://yarnpkg.com/) or [npm](https://npmjs.org/) installed, run **one** of the following: | ||
@@ -82,3 +85,7 @@ ```shell | ||
# ...or install globally to use anywhere: | ||
# ...or install as development dependency: | ||
# (use this command if you're using `template-file` to build your project) | ||
yarn add --dev template-file | ||
# ...*or* install globally to use anywhere: | ||
yarn global add template-file | ||
@@ -89,3 +96,7 @@ | ||
# ...or install globally to use anywhere: | ||
# ...or install as development dependency: | ||
# (use this command if you're using `template-file` to build your project) | ||
npm install --save-dev template-file | ||
# ...*or* install globally to use anywhere: | ||
npm install --global template-file | ||
@@ -92,0 +103,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
125058
152
101
5
+ Addeddot-prop@^4.2.0
+ Addeddot-prop@4.2.1(transitive)
+ Addedis-obj@1.0.1(transitive)