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

🚨 move to using GS&F code style #17

Merged
merged 1 commit into from
Sep 14, 2017
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
17 changes: 0 additions & 17 deletions .eslintrc.js

This file was deleted.

39 changes: 20 additions & 19 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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);

test('Data is replaced when given string', t => {
// Should return the same without regard of consistent spacing
const templateString =
'The {{ adjective1 }}, {{adjective2 }} {{ person.title}} jumped over the {{adjective3}} {{ noun }}.'
'The {{ adjective1 }}, {{adjective2 }} {{ person.title}} jumped over the {{adjective3}} {{ noun }}.';
const templateData = {
adjective1: 'cool',
adjective2: 'pizza-loving',
Expand All @@ -18,17 +18,18 @@ test('Data is replaced when given string', t => {
person: {
title: 'developer'
}
}
};

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');

const mimeTypes = [
'application/atom+xml',
Expand All @@ -55,17 +56,17 @@ test('Data is replaced when given file path', async t => {
'text/plain',
'text/x-component',
'text/xml'
]
];

const actual = await renderTemplateFile(inputFile, {
aPath: '/this-is-a-test',
domain: 'reallycooldomain.com',
gzip: {
mimeTypes: () => mimeTypes.join(' ')
}
})
});

const expected = await readFile(expectedFile, 'utf8')
const expected = await readFile(expectedFile, 'utf8');

t.is(actual, expected)
})
t.is(actual, expected);
});
45 changes: 24 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/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);

const cli = meow(`
Usage
Expand All @@ -24,27 +24,30 @@ const cli = meow(`

Compile all .abc files in src/ to build/
$ 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 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
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 '';
}

// If the replacement is a function, replace the variable with the result of the function
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)
);
}

module.exports = {
renderString,
renderTemplateFile
}
};
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"engines": {
"node": ">=4.0.0"
},
"eslintConfig": {
"extends": "gsandf"
},
"scripts": {
"test": "ava"
},
Expand All @@ -31,10 +34,6 @@
"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"
"eslint-config-gsandf": "^0.3.0"
}
}
Loading