Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
Version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RedKenrok committed Dec 24, 2018
1 parent e7cc43e commit 08654c2
Show file tree
Hide file tree
Showing 7 changed files with 1,016 additions and 1,264 deletions.
28 changes: 19 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
# Changelog

## 2.0.0 (2018-12-24)
For hoast `v1.1.5`.
### Changed
+ Instead of only being able to only convert utf-8 content, the file's entire data is made available using the first parameter of the engine function.
+ Rewrote existing test into multiple more specific tests.
### Removed
+ the `extension` option is removed and can be manually done by accessing the `path` property of the file. To replace an extension use the following code `file.path.substring(0, file.lastIndexOf('.')).concat('.extension')`, whereby `.extension` is the desired new extension.

> Do note options have changed, and are not backwards compatible.
## 1.1.1 (2018-10-24)
For hoast `v1.1.1`
For hoast `v1.1.1`.
### Changed
- Updated dependencies.
- Updated to reflect changes to `hoast`'s helpers.
+ Updated dependencies.
+ Updated to reflect changes to `hoast`'s helpers.

## 1.1.0 (2018-10-18)
For hoast `v1.1.0`.
### Changed
- Reduced module complexity by using new `hoast.helper.parse` and `hoast.helper.match` helper functions.
+ Reduced module complexity by using new `hoast.helper.parse` and `hoast.helper.match` helper functions.

## 1.0.1 (2018-09-28)
For hoast `v1.0.0`.
### Changed
- Updated `planckmatch` module from version `1.0.0` to `1.0.1`.
+ Updated `planckmatch` module from version `1.0.0` to `1.0.1`.

## 1.0.0 (2018-09-26)
For hoast `v1.0.0`.
### Added
- CodeCov coverage report added to CI workflow.
+ CodeCov coverage report added to CI workflow.
### Changed
- Restructured project files.
- Switched from using `nanomatch` to [`planckmatch`](https://github.com/redkenrok/node-planckmatch#readme) for filtering file paths.
+ Restructured project files.
+ Switched from using `nanomatch` to [`planckmatch`](https://github.com/redkenrok/node-planckmatch#readme) for filtering file paths.

> Do note option properties have changed, and are not backwards compatible.
> Do note options have changed, and are not backwards compatible.
## 0.1.0 (2018-08-21)
Initial release, for hoast `v0.1.0`.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ISC License

Copyright (c) 2018 Ron Dekker <[email protected]>
Copyright (c) 2018 Ron Dekker <rondekker.nl>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
76 changes: 63 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div align="center">

[![npm package @latest](https://img.shields.io/npm/v/hoast-convert.svg?label=npm@latest&style=flat-square&maxAge=3600)](https://npmjs.com/package/hoast-convert)
[![npm package @next](https://img.shields.io/npm/v/hoast-convert/next.svg?label=npm@next&style=flat-square&maxAge=3600)](https://npmjs.com/package/hoast-convert/v/next)
[![npm package version @latest](https://img.shields.io/npm/v/hoast-convert.svg?label=npm@latest&style=flat-square&maxAge=3600)](https://npmjs.com/package/hoast-convert)
[![npm package version @next](https://img.shields.io/npm/v/hoast-convert/next.svg?label=npm@next&style=flat-square&maxAge=3600)](https://npmjs.com/package/hoast-convert/v/next)

[![Travis-ci status](https://img.shields.io/travis-ci/hoast/hoast-convert.svg?branch=master&label=test%20status&style=flat-square&maxAge=3600)](https://travis-ci.org/hoast/hoast-convert)
[![CodeCov coverage](https://img.shields.io/codecov/c/github/hoast/hoast-convert/master.svg?label=test%20coverage&style=flat-square&maxAge=3600)](https://codecov.io/gh/hoast/hoast-convert)
[![Travis-ci test status](https://img.shields.io/travis-ci/hoast/hoast-convert.svg?branch=master&label=test%20status&style=flat-square&maxAge=3600)](https://travis-ci.org/hoast/hoast-convert)
[![CodeCov test coverage](https://img.shields.io/codecov/c/github/hoast/hoast-convert/master.svg?label=test%20coverage&style=flat-square&maxAge=3600)](https://codecov.io/gh/hoast/hoast-convert)

[![License agreement](https://img.shields.io/github/license/hoast/hoast-convert.svg?style=flat-square&maxAge=86400)](https://github.com/hoast/hoast-convert/blob/master/LICENSE)
[![Open issues on GitHub](https://img.shields.io/github/issues/hoast/hoast-convert.svg?style=flat-square&maxAge=86400)](https://github.com/hoast/hoast-convert/issues)
Expand All @@ -17,7 +17,7 @@ Convert the content of files using a specified function.

> As the name suggest this is a [hoast](https://github.com/hoast/hoast#readme) module.
> This is module is a little more advanced and is meant to be used for simple task that do not require a whole new module to be made. As a result a little more knowledge about [making modules](https://github.com/hoast/hoast#making) for hoast is recommended.
> This module is meant to be used for simple task that do not require a whole new module to be made. As a result a little more knowledge on [how to making modules](https://github.com/hoast/hoast#making) is recommended.
## Usage

Expand All @@ -29,12 +29,9 @@ $ npm install hoast-convert

### Parameters

* `engine`: A function that processes the data and returns the new content. The parameters are the file path, file content, file frontmatter, and global metadata. The frontmatter is only used if the [frontmatter module](https://github.com/hoast/hoast-frontmatter#readme) is used. Do note the function can be asynchronous or return a promise. The function needs the return the new content in the form of a string.
* `engine`: The file processing function which gets given two parameters, the file data and the hoast metadata. The return can be an object, which gets merged with the pre-existing file, or an array of objects, whereby each item in the array becomes a new file and gets merged with the pre-existing file.
* Type: `Function`
* Required: `yes`
* `extension`: The new extension name if it needs to change.
* Type: `String`
* Required: `no`
* `patterns`: Glob patterns to match file paths with. If the engine function is set it will only give the function any files matching the pattern.
* Type: `String` or `Array of strings`
* Required: `no`
Expand Down Expand Up @@ -62,13 +59,66 @@ const minifyHTML = require(`html-minifier`).minify;
Hoast(__dirname)
.use(read())
.use(convert({
engine: function(path, content, frontmatter, metadata) {
return minifyHTML(content);
engine: function(file, metadata) {
return {
content: {
data: minifyHTML(file.content.data)
}
};
},
extension: `min.html`,
patterns: `*.html`
}))
.process();
```

> In the example the HTML files are minified using [html-minifier](https://github.com/kangax/html-minifier#readme) and `min` is prepended to the extension.
> In the example above the HTML files are minified using [html-minifier](https://github.com/kangax/html-minifier#readme).
```javascript
const Hoast = require(`hoast`);
const read = Hoast.read,
convert = require(`hoast-convert`);
const babel = require(`@babel/core`);

Hoast(__dirname)
.use(read())
.use(convert({
engine: async function(file, metadata) {
const result = await babel.transformAsync(file.content.data, { code: true, map: true });
return [{
content: {
data: result.code
}
}, {
path: file.path.substring(0, file.lastIndexOf(`.`)).concat(`.map.js`);
content: {
data: result.map
}
}];
},
patterns: `*.js`
}))
.process();
```

> In the example above the JavaScript files are transformed using [Babel](https://github.com/babel/babel#readme), and an additional `.map.js` file is created. Do note Babel requires more setup than is shown in the example.
```javascript
const Hoast = require(`hoast`);
const read = Hoast.read,
convert = require(`hoast-convert`);

Hoast(__dirname, {
metadata: {
hello: `World!`
}
})
.use(read())
.use(convert({
engine: function(file, metadata) {
console.log(JSON.Stringify(metadata));
}
}))
.process();
```

> In the example above the metadata will be printed to the console as `{ hello: "World!" }`.
75 changes: 54 additions & 21 deletions library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,74 @@ module.exports = function(options) {

const mod = async function(hoast, files) {
debug(`Running module.`);
await Promise.all(
// Store results of asynchronous functions.
files = await Promise.all(
// Loop through files.
files.map(async function(file) {
debug(`Processing file '${file.path}'.`);

// Check if read module has been used.
if (file.content === null) {
debug(`File content not set, read module needs to be called before this.`);
return;
}

// Check if file content is text.
if (file.content.type !== `string`) {
debug(`File content not valid for processing.`);
return;
}
// Check against glob patterns.
if (!hoast.helpers.matchExpressions(file.path, this.expressions, options.patternOptions.all)) {
debug(`File path not valid for processing.`);
return;
}

debug(`File data is valid.`);

// Call engine function to get new content.
file.content.data = await options.engine(file.path, file.content.data, file.frontmatter, hoast.options.metadata);
debug(`File data converted.`);
// Call the engine function with the file data and metadata.
const results = await options.engine(file, hoast.options.metadata);
debug(`File data converted, result is of type ${Array.isArray(results) ? `array` : typeof(results)}.`);

// Replace file extension.
if (options.extension) {
file.path = file.path.substr(0, file.path.lastIndexOf(`.`)).concat(options.extension);
debug(`Replacing file extension to '${file.path}'.`);
// Check the type of the results and write.
if (Array.isArray(results)) {
file = results.map(function(result) {
return hoast.helpers.deepAssign({}, file, result);
});

debug(`Duplicated old data, merged new data on top. Created ${file.length - 1} new files.`);
} else if (typeof(results) === `object`) {
file = hoast.helpers.deepAssign(file, results);

debug(`Merged new onto old file data.`);
}
// If the file is a string only rewrite the content. (Scrapped)
/* else if (typeof(results) === `string`) {
if (!file.content || file.content.type !== `string`) {
debug(`File did not already have content of type string, therefore results are discarded.`);
return file;
}
file.content.data = results;
debug(`Content overwritten with result.`);
}*/

// Replace file extension(s). (Scrapped)
/* if (options.extension) {
if (Array.isArray(file)) {
file.forEach(function(file) {
file.path.substr(0, file.path.lastIndexOf(`.`)).concat(options.extension);
});
} else {
file.path = file.path.substr(0, file.path.lastIndexOf(`.`)).concat(options.extension);
}
debug(`Replaced file extension(s).`);
}*/

return file;
}, mod)
);

// Flatten files array and return it.
return files.reduce(function(previous, current) {
if (!current) {
return previous;
}
else if (Array.isArray(current)) {
return previous.concat(current);
} else {
previous.push(current);
return previous;
}
}, []);
};

mod.before = function(hoast) {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"url": "https://rondekker.nl"
},
"name": "hoast-convert",
"version": "1.1.1",
"version": "2.0.0",
"description": "Hoast module used to convert the content of files using a specified function.",
"license": "ISC",
"keywords": [
Expand All @@ -28,14 +28,14 @@
],
"scripts": {
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"pretest": "eslint library/**.js test/**.js",
"test": "nyc --reporter=text ava test/**.js --verbose"
"test": "nyc --reporter=text ava test/**.js --verbose",
"posttest": "eslint library/**.js test/**.js"
},
"devDependencies": {
"ava": "^0.25.0",
"ava": "^1.0.1",
"codecov": "^3.1.0",
"debug": "^4.1.0",
"eslint": "^5.10.0",
"debug": "^4.1.1",
"eslint": "^5.11.0",
"hoast": "^1.1.5",
"nyc": "^13.1.0"
}
Expand Down
Loading

0 comments on commit 08654c2

Please sign in to comment.