broccoli-replace
Advanced tools
Comparing version 0.12.0 to 2.0.0
38
index.js
@@ -1,26 +0,13 @@ | ||
/* | ||
* broccoli-replace | ||
* | ||
* Copyright (c) 2016 outaTiME | ||
* Licensed under the MIT license. | ||
* https://github.com/outaTiME/broccoli-replace/blob/master/LICENSE-MIT | ||
*/ | ||
'use strict'; | ||
// dependencies | ||
var Filter = require('broccoli-persistent-filter'); | ||
var minimatch = require('minimatch'); | ||
var Filter = require('broccoli-filter'); | ||
var Applause = require('applause'); | ||
function ReplaceFilter(inputTree, options) { | ||
if (!(this instanceof ReplaceFilter)) { | ||
if (!this) { | ||
return new ReplaceFilter(inputTree, options); | ||
} | ||
options = options || {}; | ||
Filter.call(this, inputTree, options); | ||
this.inputTree = inputTree; | ||
this.options = options || {}; | ||
this.files = this.options.files; | ||
this.applause = Applause.create(options); | ||
@@ -32,15 +19,3 @@ } | ||
ReplaceFilter.prototype.getDestFilePath = function(relativePath) { | ||
var files = this.options.files || []; | ||
for (var i = 0; i < files.length; i++) { | ||
var pattern = files[i]; | ||
var match = minimatch(relativePath, pattern); | ||
if (match === true) { | ||
return relativePath; | ||
} | ||
} | ||
return null; | ||
}; | ||
ReplaceFilter.prototype.processString = function(string) { | ||
ReplaceFilter.prototype.processString = function (string) { | ||
var res = this.applause.replace(string); | ||
@@ -50,5 +25,6 @@ var result = res.content; | ||
if (count === 0) { | ||
// no replacements | ||
// No replacements | ||
return string; | ||
} | ||
return result; | ||
@@ -55,0 +31,0 @@ }; |
{ | ||
"name": "broccoli-replace", | ||
"version": "2.0.0", | ||
"description": "Replace text patterns with applause.", | ||
"version": "0.12.0", | ||
"license": "MIT", | ||
"repository": "outaTiME/broccoli-replace", | ||
"author": { | ||
"name": "outaTiME", | ||
"url": "http://outa.im/" | ||
"email": "afalduto@gmail.com", | ||
"url": "https://outa.im" | ||
}, | ||
"repository": "outaTiME/broccoli-replace", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"release": "scripts/release.sh", | ||
"test": "broccoli build temp && mocha" | ||
"test": "eslint . && broccoli build temp && ava && del-cli temp", | ||
"release": "release-it" | ||
}, | ||
"dependencies": { | ||
"broccoli-persistent-filter": "^1.2.0", | ||
"minimatch": "^3.0.0", | ||
"applause": "1.2.2" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.4.0", | ||
"rimraf": "^2.5.0", | ||
"broccoli": "^0.16.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
@@ -42,27 +36,26 @@ "broccoli-plugin", | ||
"cson", | ||
"flatten" | ||
"flatten", | ||
"applause" | ||
], | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"applause": "^2.0.0", | ||
"broccoli-filter": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"broccoli": "^3.5.1", | ||
"broccoli-cli": "^1.0.0", | ||
"del-cli": "^3.0.1", | ||
"eslint": "^7.20.0", | ||
"eslint-config-xo": "^0.35.0", | ||
"eslint-config-xo-space": "^0.27.0", | ||
"release-it": "^14.4.1", | ||
"rimraf": "^2.5.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"eslint:recommended" | ||
], | ||
"env": { | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"rules": { | ||
"quotes": [ | ||
2, | ||
"single" | ||
], | ||
"indent": [ | ||
2, | ||
2 | ||
] | ||
} | ||
"xo", | ||
"xo-space" | ||
] | ||
} | ||
} |
416
README.md
@@ -1,3 +0,9 @@ | ||
# broccoli-replace [](https://travis-ci.org/outaTiME/broccoli-replace) [](https://npmjs.org/package/broccoli-replace) | ||
# broccoli-replace | ||
[](https://travis-ci.org/outaTiME/broccoli-replace) | ||
[](https://www.npmjs.com/package/broccoli-replace) | ||
 | ||
[](#) | ||
[](https://twitter.com/outa7iME) | ||
> Replace text patterns with [applause](https://github.com/outaTiME/applause). | ||
@@ -13,3 +19,3 @@ | ||
## Replace Filter | ||
## Usage | ||
@@ -19,6 +25,7 @@ Assuming installation via NPM, you can use `broccoli-replace` in your Brocfile.js like this: | ||
```javascript | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
var BroccoliReplace = require('broccoli-replace'); | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
'**/*.html' // replace only html files in src | ||
'**/*.html' | ||
], | ||
@@ -34,238 +41,10 @@ patterns: [ | ||
### Options | ||
## Options | ||
#### files | ||
Type: `Array` | ||
Default: `[]` | ||
Supports all the applause [options](https://github.com/outaTiME/applause#options). | ||
Define the source files that will be used for replacements, you can use globbing via [minimatch](https://github.com/isaacs/minimatch) library. | ||
## Examples | ||
> This is a mandatory value, an empty definition will ignore any kind of replacement. | ||
### Basic | ||
#### patterns | ||
Type: `Array` | ||
Define patterns that will be used to replace the contents of source files. | ||
#### patterns.match | ||
Type: `String|RegExp` | ||
Indicates the matching expression. | ||
If matching type is `String` we use a simple variable lookup mechanism `@@string` (in any other case we use the default regexp replace logic): | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
match: 'foo', | ||
replacement: 'bar' // replaces "@@foo" to "bar" | ||
} | ||
] | ||
} | ||
``` | ||
#### patterns.replacement or patterns.replace | ||
Type: `String|Function|Object` | ||
Indicates the replacement for match, for more information about replacement check out the [String.replace]. | ||
You can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
match: /foo/g, | ||
replacement: function () { | ||
return 'bar'; // replaces "foo" to "bar" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
Also supports object as replacement (we create string representation of object using [JSON.stringify]): | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
match: /foo/g, | ||
replacement: [1, 2, 3] // replaces "foo" with string representation of "array" object | ||
} | ||
] | ||
} | ||
``` | ||
> The replacement only resolve the [special replacement patterns] when using regexp for matching. | ||
[String.replace]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace | ||
[JSON.stringify]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | ||
[special replacement patterns]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter | ||
#### patterns.json | ||
Type: `Object` | ||
If an attribute `json` is found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support). | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
json: { | ||
"key": "value" // replaces "@@key" to "value" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
Also supports nested objects: | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
json: { | ||
"key": "value", // replaces "@@key" to "value" | ||
"inner": { // replaces "@@inner" with string representation of "inner" object | ||
"key": "value" // replaces "@@inner.key" to "value" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
For deferred invocations is possible to define functions: | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
json: function (done) { | ||
done({ | ||
key: 'value' | ||
}); | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
#### patterns.yaml | ||
Type: `String` | ||
If an attribute `yaml` found in pattern definition it will be converted and then processed like [json attribute](#patternsjson). | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
yaml: 'key: value' // replaces "@@key" to "value" | ||
} | ||
] | ||
} | ||
``` | ||
For deferred invocations is possible to define functions: | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
yaml: function (done) { | ||
done('key: value'); | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
#### patterns.cson | ||
Type: `String` | ||
If an attribute `cson` is found in pattern definition it will be converted and then processed like [json attribute](#patternsjson). | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
cson: 'key: \'value\'' | ||
} | ||
] | ||
} | ||
``` | ||
For deferred invocations is possible to define functions: | ||
```javascript | ||
{ | ||
patterns: [ | ||
{ | ||
cson: function (done) { | ||
done('key: \'value\''); | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
#### variables | ||
Type: `Object` | ||
This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support). You can still use this but for more control you should use the new `patterns` way. | ||
```javascript | ||
{ | ||
variables: { | ||
'key': 'value' // replaces "@@key" to "value" | ||
} | ||
} | ||
``` | ||
#### prefix | ||
Type: `String` | ||
Default: `@@` | ||
The prefix added for matching (prevent bad replacements / easy way). | ||
> This only applies for simple variable lookup mechanism. | ||
#### usePrefix | ||
Type: `Boolean` | ||
Default: `true` | ||
If set to `false`, we match the pattern without `prefix` concatenation (useful when you want to lookup a simple string). | ||
> This only applies for simple variable lookup mechanism. | ||
#### preservePrefix | ||
Type: `Boolean` | ||
Default: `false` | ||
If set to `true`, we preserve the `prefix` in target. | ||
> This only applies for simple variable lookup mechanism and when `patterns.replacement` is a string. | ||
#### delimiter | ||
Type: `String` | ||
Default: `.` | ||
The delimiter used to flatten when using object as replacement. | ||
#### preserveOrder | ||
Type: `Boolean` | ||
Default: `false` | ||
If set to `true`, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like `head` / `header` (typo regexps will be resolved at last). | ||
### Usage Examples | ||
#### Basic | ||
File `src/manifest.appcache`: | ||
@@ -288,5 +67,6 @@ | ||
```js | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
```javascript | ||
var BroccoliReplace = require('broccoli-replace'); | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
@@ -304,3 +84,3 @@ 'manifest.appcache' | ||
#### Multiple matching | ||
### Multiple matching | ||
@@ -331,3 +111,3 @@ File `src/manifest.appcache`: | ||
Web Developer / Graphic Designer: Ariel Oscar Falduto | ||
Site: http://www.outa.im | ||
Site: https://www.outa.im | ||
Twitter: @outa7iME | ||
@@ -340,5 +120,4 @@ Contact: afalduto at gmail dot com | ||
Standards: HTML5, CSS3, robotstxt.org, humanstxt.org | ||
Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt | ||
Software: Sublime Text 2, Photoshop, LiveReload | ||
Components: H5BP, Modernizr, jQuery, Bootstrap, LESS, Jade, Grunt | ||
Software: Sublime Text, Photoshop, LiveReload | ||
``` | ||
@@ -348,6 +127,7 @@ | ||
```js | ||
```javascript | ||
var BroccoliReplace = require('broccoli-replace'); | ||
var pkg = require('./package.json'); | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
@@ -370,3 +150,3 @@ 'manifest.appcache', | ||
#### Cache busting | ||
### Cache busting | ||
@@ -384,5 +164,6 @@ File `src/index.html`: | ||
```js | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
```javascript | ||
var BroccoliReplace = require('broccoli-replace'); | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
@@ -400,3 +181,3 @@ 'index.html' | ||
#### Include file | ||
### Include file | ||
@@ -413,6 +194,7 @@ File `src/index.html`: | ||
```js | ||
```javascript | ||
var BroccoliReplace = require('broccoli-replace'); | ||
var fs = require('fs'); | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
@@ -430,3 +212,3 @@ 'index.html' | ||
#### Regular expression | ||
### Regular expression | ||
@@ -441,5 +223,6 @@ File `src/username.txt`: | ||
```js | ||
var replace = require('broccoli-replace'); | ||
module.exports = replace('src', { | ||
```javascript | ||
var BroccoliReplace = require('broccoli-replace'); | ||
module.exports = new BroccoliReplace('src', { | ||
files: [ | ||
@@ -451,3 +234,3 @@ 'username.txt' | ||
match: /(\w+)\s(\w+)/, | ||
replacement: '$2, $1' // replaces "John Smith" to "Smith, John" | ||
replacement: '$2, $1' // Replaces "John Smith" to "Smith, John" | ||
} | ||
@@ -458,76 +241,57 @@ ] | ||
#### Lookup for `foo` instead of `@@foo` | ||
### Lookup for `foo` instead of `@@foo` | ||
Brocfile.js: | ||
```js | ||
var mergeTrees = require('broccoli-merge-trees'); | ||
var replace = require('broccoli-replace'); | ||
```javascript | ||
var BroccoliMergeTrees = require('broccoli-merge-trees'); | ||
var BroccoliReplace = require('broccoli-replace'); | ||
// option 1 (explicitly using an regexp) | ||
var replacer_op1 = replace('src_op1', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: /foo/g, | ||
replacement: 'bar' | ||
} | ||
] | ||
}); | ||
var inputNodes = [ | ||
new BroccoliReplace('src', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: /foo/g, // Explicitly using a regexp | ||
replacement: 'bar' | ||
} | ||
] | ||
}), | ||
new BroccoliReplace('src', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: 'foo', | ||
replacement: 'bar' | ||
} | ||
], | ||
usePrefix: false // Using the option provided | ||
}), | ||
new BroccoliReplace('src', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: 'foo', | ||
replacement: 'bar' | ||
} | ||
], | ||
prefix: '' // Removing the prefix manually | ||
}) | ||
]; | ||
// option 2 (easy way) | ||
var replacer_op2 = replace('src_op2', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: 'foo', | ||
replacement: 'bar' | ||
} | ||
], | ||
usePrefix: false | ||
}); | ||
// option 3 (old way) | ||
var replacer_op3 = replace('src_op3', { | ||
files: [ | ||
'foo.txt' | ||
], | ||
patterns: [ | ||
{ | ||
match: 'foo', | ||
replacement: 'bar' | ||
} | ||
], | ||
prefix: '' // remove prefix | ||
}); | ||
module.exports = mergeTrees([replacer_op1, replacer_op2, replacer_op3]); | ||
module.exports = new BroccoliMergeTrees(nodes); | ||
``` | ||
## Release History | ||
## Related | ||
* 2016-04-19 v0.12.0 Use broccoli-persistent-filter instead of broccoli-filter (thanks [@alexBaizeau](https://github.com/alexBaizeau)) | ||
* 2015-09-09 v0.11.0 Improvements in handling patterns. Fix plain object representation issue. More test cases. | ||
* 2015-08-19 v0.10.0 Last [applause](https://github.com/outaTiME/applause) integration and package.json update. | ||
* 2015-08-06 v0.3.3 Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON. | ||
* 2015-05-07 v0.3.1 Fix regression issue with empty string in replacement. | ||
* 2015-05-01 v0.3.0 Update to [applause](https://github.com/outaTiME/applause) v0.4.0. | ||
* 2014-10-10 v0.2.0 Escape regexp when matching type is `String`. | ||
* 2014-06-10 v0.1.7 Remove node v.8.0 support and third party dependencies updated. Update examples in new broccoli way. | ||
* 2014-04-20 v0.1.6 JSON / YAML / CSON as function supported. Readme updated (thanks [@milanlandaverde](https://github.com/milanlandaverde)). | ||
* 2014-03-23 v0.1.5 Readme updated. | ||
* 2014-03-22 v0.1.4 Modular core renamed to [applause](https://github.com/outaTiME/applause). Performance improvements. Expression flag removed. New pattern matching for CSON object. More test cases, readme updated and code cleanup. | ||
* 2014-03-21 v0.1.3 Test cases in Mocha, readme updated and code cleanup. | ||
* 2014-03-15 v0.1.2 New [pattern-replace](https://github.com/outaTiME/pattern-replace) modular core for replacements. | ||
* 2014-02-26 v0.0.4 Fix issue when no replacement found. | ||
* 2014-02-25 v0.0.3 Code normalization and documentation updated. | ||
* 2014-02-23 v0.0.2 Use Filter instead of Transformer. | ||
* 2014-02-22 v0.0.1 Initial version. | ||
- [applause](https://github.com/outaTiME/applause) - Human-friendly replacements | ||
--- | ||
## License | ||
Task submitted by [Ariel Falduto](http://outa.im/) | ||
MIT © [outaTiME](https://outa.im) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
5
1
8318
9
24
282
+ Addedbroccoli-filter@^1.3.0
+ Addedapplause@2.0.4(transitive)
+ Addedargparse@2.0.1(transitive)
+ Addedbroccoli-filter@1.3.0(transitive)
+ Addedcoffeescript@1.12.7(transitive)
+ Addedcopy-dereference@1.0.0(transitive)
+ Addedcson-parser@4.0.9(transitive)
+ Addedjs-yaml@4.1.0(transitive)
+ Addedoptional-require@1.1.8(transitive)
+ Addedrequire-at@1.0.6(transitive)
- Removedbroccoli-persistent-filter@^1.2.0
- Removedminimatch@^3.0.0
- Removedapplause@1.2.2(transitive)
- Removedargparse@1.0.10(transitive)
- Removedasync@2.6.4(transitive)
- Removedasync-disk-cache@1.3.5(transitive)
- Removedasync-promise-queue@1.0.5(transitive)
- Removedbinaryextensions@2.3.0(transitive)
- Removedbroccoli-persistent-filter@1.4.6(transitive)
- Removedcoffee-script@1.12.7(transitive)
- Removedcson-parser@1.3.5(transitive)
- Removededitions@1.3.4(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfs-tree-diff@0.5.9(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedhash-for-dep@1.5.1(transitive)
- Removedhasown@2.0.2(transitive)
- Removedheimdalljs@0.2.6(transitive)
- Removedheimdalljs-logger@0.1.10(transitive)
- Removedis-core-module@2.16.1(transitive)
- Removedistextorbinary@2.1.0(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedlodash@3.10.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpath-posix@1.0.0(transitive)
- Removedpath-root@0.1.1(transitive)
- Removedpath-root-regex@0.1.2(transitive)
- Removedresolve@1.22.10(transitive)
- Removedresolve-package-path@1.2.7(transitive)
- Removedrsvp@3.2.1(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtextextensions@2.6.0(transitive)
- Removedusername-sync@1.0.3(transitive)
Updatedapplause@^2.0.0