template-file
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -1,8 +0,8 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import test from 'ava' | ||
import pify from 'pify' | ||
import { renderString, renderTemplateFile } from '..' | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import pify from 'pify'; | ||
import { renderString, renderTemplateFile } from '..'; | ||
const readFile = pify(fs.readFile) | ||
const readFile = pify(fs.readFile); | ||
@@ -12,3 +12,3 @@ test('Data is replaced when given string', t => { | ||
const templateString = | ||
'The {{ adjective1 }}, {{adjective2 }} {{ person.title}} jumped over the {{adjective3}} {{ noun }}.' | ||
'The {{ adjective1 }}, {{adjective2 }} {{ person.title}} jumped over the {{adjective3}} {{ noun }}.'; | ||
const templateData = { | ||
@@ -22,13 +22,14 @@ adjective1: 'cool', | ||
} | ||
} | ||
}; | ||
const actual = renderString(templateString, templateData) | ||
const expected = 'The cool, pizza-loving developer jumped over the silly laptop.' | ||
const actual = renderString(templateString, templateData); | ||
const expected = | ||
'The cool, pizza-loving developer jumped over the silly laptop.'; | ||
t.is(actual, expected) | ||
}) | ||
t.is(actual, expected); | ||
}); | ||
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 inputFile = path.resolve('./__tests__/helpers/testme.conf'); | ||
const expectedFile = path.resolve('./__tests__/helpers/expected.conf'); | ||
@@ -60,3 +61,3 @@ const mimeTypes = [ | ||
'text/xml' | ||
] | ||
]; | ||
@@ -69,7 +70,7 @@ const actual = await renderTemplateFile(inputFile, { | ||
} | ||
}) | ||
}); | ||
const expected = await readFile(expectedFile, 'utf8') | ||
const expected = await readFile(expectedFile, 'utf8'); | ||
t.is(actual, expected) | ||
}) | ||
t.is(actual, expected); | ||
}); |
45
cli.js
#!/usr/bin/env node | ||
const fs = require('fs') | ||
const path = require('path') | ||
const pify = require('pify') | ||
const glob = pify(require('glob')) | ||
const meow = require('meow') | ||
const pLimit = require('p-limit')(64) | ||
const { renderTemplateFile } = require('.') | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const pify = require('pify'); | ||
const glob = pify(require('glob')); | ||
const meow = require('meow'); | ||
const pLimit = require('p-limit')(64); | ||
const { renderTemplateFile } = require('.'); | ||
const writeFile = pify(fs.writeFile) | ||
const writeFile = pify(fs.writeFile); | ||
@@ -27,25 +27,28 @@ const cli = meow(` | ||
$ template-file stuff.json 'src/**/*.abc' build/ | ||
`) | ||
`); | ||
if (cli.input.length !== 3) { | ||
cli.showHelp(2) | ||
cli.showHelp(2); | ||
} | ||
const [dataFile, sourceGlob, destination] = cli.input | ||
const data = require(path.resolve(dataFile)) | ||
const [dataFile, sourceGlob, destination] = cli.input; | ||
const data = require(path.resolve(dataFile)); | ||
glob(sourceGlob) | ||
.then(files => files.map(file => ({ | ||
data, | ||
file, | ||
destination: path.join(destination, path.basename(file)) | ||
}))) | ||
.then(files => | ||
files.map(file => ({ | ||
data, | ||
file, | ||
destination: path.join(destination, path.basename(file)) | ||
})) | ||
) | ||
.then(fileList => fileList.map(renderToFile)) | ||
.then(fileWriteOperations => Promise.all(fileWriteOperations)) | ||
.then(fileWriteOperations => Promise.all(fileWriteOperations)); | ||
function renderToFile({ data, file, destination }) { | ||
return pLimit(() => | ||
renderTemplateFile(file, data) | ||
.then(renderedString => writeFile(destination, renderedString)) | ||
) | ||
renderTemplateFile(file, data).then(renderedString => | ||
writeFile(destination, renderedString) | ||
) | ||
); | ||
} |
23
index.js
@@ -1,12 +0,12 @@ | ||
const pify = require('pify') | ||
const readFile = pify(require('fs').readFile) | ||
const valueForProperty = require('dot-prop').get | ||
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 = valueForProperty(data, captured.trim()) | ||
const replacement = valueForProperty(data, captured.trim()); | ||
// If a template variable is found but nothing is supplied to fill it, remove it | ||
if (replacement == null) { | ||
return '' | ||
return ''; | ||
} | ||
@@ -16,12 +16,13 @@ | ||
if (typeof replacement === 'function') { | ||
return replacement() | ||
return replacement(); | ||
} | ||
return replacement | ||
}) | ||
return replacement; | ||
}); | ||
} | ||
function renderTemplateFile(filepath, data) { | ||
return readFile(filepath, 'utf8') | ||
.then(templateString => renderString(templateString, data)) | ||
return readFile(filepath, 'utf8').then(templateString => | ||
renderString(templateString, data) | ||
); | ||
} | ||
@@ -32,2 +33,2 @@ | ||
renderTemplateFile | ||
} | ||
}; |
{ | ||
"name": "template-file", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "🔀 Replace {{ variables }} in all your files", | ||
@@ -21,2 +21,5 @@ "main": "index.js", | ||
}, | ||
"eslintConfig": { | ||
"extends": "gsandf" | ||
}, | ||
"scripts": { | ||
@@ -26,17 +29,13 @@ "test": "ava" | ||
"dependencies": { | ||
"dot-prop": "^4.2.0", | ||
"dot-prop": "^5.1.0", | ||
"glob": "^7.1.1", | ||
"meow": "^3.7.0", | ||
"p-limit": "^1.1.0", | ||
"pify": "^3.0.0" | ||
"meow": "^5.0.0", | ||
"p-limit": "^2.2.0", | ||
"pify": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.22.0", | ||
"eslint": "^4.1.0", | ||
"eslint-config-standard": "^10.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^5.0.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1" | ||
"ava": "^2.2.0", | ||
"eslint": "^6.0.1", | ||
"eslint-config-gsandf": "^1.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
9436
9
140
1
+ Addedarrify@1.0.1(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedcamelcase-keys@4.2.0(transitive)
+ Addeddecamelize-keys@1.1.1(transitive)
+ Addeddot-prop@5.3.0(transitive)
+ Addedfind-up@2.1.0(transitive)
+ Addedindent-string@3.2.0(transitive)
+ Addedis-obj@2.0.0(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedjson-parse-better-errors@1.0.2(transitive)
+ Addedload-json-file@4.0.0(transitive)
+ Addedlocate-path@2.0.0(transitive)
+ Addedmap-obj@2.0.0(transitive)
+ Addedmeow@5.0.0(transitive)
+ Addedminimist-options@3.0.2(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@2.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedparse-json@4.0.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedpath-type@3.0.0(transitive)
+ Addedpify@4.0.1(transitive)
+ Addedquick-lru@1.1.0(transitive)
+ Addedread-pkg@3.0.0(transitive)
+ Addedread-pkg-up@3.0.0(transitive)
+ Addedredent@2.0.0(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedstrip-indent@2.0.0(transitive)
+ Addedtrim-newlines@2.0.0(transitive)
+ Addedyargs-parser@10.1.0(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcamelcase-keys@2.1.0(transitive)
- Removeddot-prop@4.2.1(transitive)
- Removedfind-up@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedindent-string@2.1.0(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedis-obj@1.0.1(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedload-json-file@1.1.0(transitive)
- Removedmeow@3.7.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpath-exists@2.1.0(transitive)
- Removedpath-type@1.1.0(transitive)
- Removedpify@2.3.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedread-pkg@1.1.0(transitive)
- Removedread-pkg-up@1.0.1(transitive)
- Removedredent@1.0.0(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-indent@1.0.1(transitive)
- Removedtrim-newlines@1.0.0(transitive)
Updateddot-prop@^5.1.0
Updatedmeow@^5.0.0
Updatedp-limit@^2.2.0
Updatedpify@^4.0.1