Skip to content

Commit

Permalink
Remove babel (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Jan 12, 2018
1 parent 0fed415 commit f4a1126
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 186 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ package-lock.json
# Test build results
test/fixtures/*/build

# Babel build results
dist

# Coverage reports
/coverage
4 changes: 2 additions & 2 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"{lib,test}/**/*.js": [
"prettier --print-width 100 --tab-width 2 --no-semi --single-quote --write",
"**/*.js": [
"prettier --write",
"git add",
"eslint"
]
Expand Down
5 changes: 1 addition & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Dotfiles
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.lintstagedrc
.prettierrc
.travis.yml

# ES6 code
/lib

# Tests
/test
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 100,
"useTabs": false,
"singleQuote": true,
"tabWidth": 2,
"semi": true
}
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ jobs:
- stage: lint
node_js: "8"
script:
- npm run lint:whitespace
- npm run lint:prettier
- npm run lint:js
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
### 4.0.0 - January 4, 2018

So this library went through a bit of churny phase, my apologies for that. It was caused by a couple
of factors; moving the library to a new home, a new rendering engine and me trying to abstract said
rendering engine for reuse in metalsmith-layouts.
Expand Down
15 changes: 0 additions & 15 deletions lib/get-transformer.js

This file was deleted.

99 changes: 63 additions & 36 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,120 @@
const match = require('multimatch')
const isUtf8 = require('is-utf8')
const getTransformer = require('./get-transformer')
const match = require('multimatch');
const isUtf8 = require('is-utf8');
const jstransformer = require('jstransformer');
const toTransformer = require('inputformat-to-jstransformer');

/**
* Engine
* Gets jstransformer for an extension, and caches them
*/

const cache = {};

function getTransformer(ext) {
if (ext in cache) {
return cache[ext];
}

const transformer = toTransformer(ext);
cache[ext] = transformer ? jstransformer(transformer) : false;

return cache[ext];
}

/**
* Engine, renders file contents with all available transformers
*/

function render({ filename, files, metadata, engineOptions }) {
return new Promise(resolve => {
const [base, ...extensions] = filename.split('.')
const file = files[filename]
const [base, ...extensions] = filename.split('.');
const file = files[filename];

// Stringify file contents
file.contents = file.contents.toString()
file.contents = file.contents.toString();

// Go through all extensions
const extLength = extensions.length
const extLength = extensions.length;
for (let i = 0; i < extLength; i += 1) {
const ext = extensions.pop()
const transform = getTransformer(ext)
const locals = Object.assign({}, metadata, file)
const ext = extensions.pop();
const transform = getTransformer(ext);
const locals = Object.assign({}, metadata, file);

// Stop if the current extension can't be transformed
if (!transform) {
extensions.push(ext)
break
extensions.push(ext);
break;
}

// If this is the last extension, replace it with a new one
if (extensions.length === 0) {
extensions.push(transform.outputFormat)
extensions.push(transform.outputFormat);
}

// Transform the contents
file.contents = transform.render(file.contents, engineOptions, locals).body
file.contents = transform.render(file.contents, engineOptions, locals).body;
}

// Store results and delete old file
file.contents = Buffer.from(file.contents)
files[[base, ...extensions].join('.')] = file // eslint-disable-line no-param-reassign
delete files[filename] // eslint-disable-line no-param-reassign
return resolve()
})
file.contents = Buffer.from(file.contents);
files[[base, ...extensions].join('.')] = file; // eslint-disable-line no-param-reassign
delete files[filename]; // eslint-disable-line no-param-reassign
return resolve();
});
}

/**
* Validate
* Validate, checks whether a file should be processed
*/

function validate({ filename, files }) {
// Files without an extension cannot be processed
if (!filename.includes('.')) {
return false
return false;
}

// Files that are not utf8 are ignored
if (!isUtf8(files[filename].contents)) {
return false
return false;
}

// Files without an applicable jstransformer are ignored
const extension = filename.split('.').pop()
return getTransformer(extension)
const extension = filename.split('.').pop();
return getTransformer(extension);
}

/**
* Plugin
* Plugin, the main plugin used by metalsmith
*/

module.exports = ({ pattern = '**', engineOptions = {} } = {}) => (files, metalsmith, done) => {
const metadata = metalsmith.metadata()
module.exports = options => (files, metalsmith, done) => {
const defaults = {
pattern: '**',
engineOptions: {}
};
const settings = Object.assign({}, defaults, options);
const metadata = metalsmith.metadata();

// Check whether the pattern option is valid
if (!(typeof pattern === 'string' || Array.isArray(pattern))) {
done(new Error('invalid pattern, the pattern option should be a string or array.'))
if (!(typeof settings.pattern === 'string' || Array.isArray(settings.pattern))) {
done(new Error('invalid pattern, the pattern option should be a string or array.'));
}

const matchedFiles = match(Object.keys(files), pattern)
const matchedFiles = match(Object.keys(files), settings.pattern);

// Filter files by validity
const validFiles = matchedFiles.filter(filename => validate({ filename, files }))
const validFiles = matchedFiles.filter(filename => validate({ filename, files }));

// Let the user know when there are no files to process, usually caused by missing jstransformer
if (validFiles.length === 0) {
done(new Error('no files to process, check whether you have a jstransformer installed.'))
done(new Error('no files to process, check whether you have a jstransformer installed.'));
}

// Map all files that should be processed to an array of promises and call done when finished
Promise.all(validFiles.map(filename => render({ filename, files, metadata, engineOptions })))
Promise.all(
validFiles.map(filename =>
render({ filename, files, metadata, engineOptions: settings.engineOptions })
)
)
.then(() => done())
.catch(/* istanbul ignore next */ error => done(error))
}
.catch(/* istanbul ignore next */ error => done(error));
};
Loading

0 comments on commit f4a1126

Please sign in to comment.