Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

template-file

Package Overview
Dependencies
Maintainers
15
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

template-file - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

39

__tests__/index.js

@@ -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);
});
#!/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)
)
);
}

@@ -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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc