Comparing version 3.0.3 to 4.0.0
@@ -12,3 +12,3 @@ #!/usr/bin/env node | ||
const os = require('os') | ||
const isBinaryFile = require('isbinaryfile') | ||
const { isBinaryFile } = require('isbinaryfile') | ||
@@ -26,2 +26,8 @@ module.exports = generify | ||
} | ||
var transforms = data.transforms | ||
if (transforms) { | ||
delete data.transforms | ||
} else { | ||
transforms = {} | ||
} | ||
@@ -61,5 +67,3 @@ // needed for the path replacing to work | ||
function copyAndReplace (source, dest) { | ||
isBinaryFile(source, function (err, isBinary) { | ||
if (err) return complete(err) | ||
isBinaryFile(source).then(function (isBinary) { | ||
if (isBinary) { | ||
@@ -73,12 +77,27 @@ pump( | ||
fs.createReadStream(source), | ||
split(replaceLine), | ||
split(replaceLine.bind({ source, dest })), | ||
fs.createWriteStream(dest), | ||
complete) | ||
} | ||
}) | ||
}, complete) | ||
} | ||
function replacer () { | ||
if (transforms[this.key]) { | ||
return transforms[this.key](data[this.key], { | ||
souce: this.source, | ||
dest: this.dest, | ||
key: this.key | ||
}) | ||
} | ||
return data[this.key] | ||
} | ||
function replaceLine (line) { | ||
const ctx = { source: this.source, dest: this.dest } | ||
keys.forEach(function (key) { | ||
line = line.replace(new RegExp('__' + key + '__', 'g'), data[key]) | ||
line = line.replace( | ||
new RegExp('__' + key + '__', 'g'), | ||
replacer.bind({ ...ctx, key }) | ||
) | ||
}) | ||
@@ -85,0 +104,0 @@ return line + os.EOL |
{ | ||
"name": "generify", | ||
"version": "3.0.3", | ||
"version": "4.0.0", | ||
"description": "A reusable project generator", | ||
"main": "generify.js", | ||
"scripts": { | ||
"test": "standard | snazzy && tap test/*.js" | ||
"test": "standard | snazzy && tap --no-esm --no-ts --no-jsx test/*.js" | ||
}, | ||
@@ -28,16 +28,15 @@ "bin": { | ||
"devDependencies": { | ||
"osenv": "^0.1.0", | ||
"pre-commit": "^1.2.2", | ||
"rimraf": "^2.6.3", | ||
"rimraf": "^3.0.0", | ||
"snazzy": "^8.0.0", | ||
"standard": "^12.0.1", | ||
"tap": "^12.5.3" | ||
"standard": "^14.0.0", | ||
"tap": "^14.0.0" | ||
}, | ||
"dependencies": { | ||
"isbinaryfile": "^3.0.2", | ||
"isbinaryfile": "^4.0.2", | ||
"mkdirp": "^0.5.0", | ||
"pump": "^3.0.0", | ||
"split2": "^2.0.0", | ||
"split2": "^3.0.0", | ||
"walker": "^1.0.6" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# generify [![Build Status](https://travis-ci.org/mcollina/generify.svg?branch=master)](https://travis-ci.org/mcollina/generify) | ||
# generify ![Build Status](https://github.com/mcollina/generify/workflows/ci/badge.svg) | ||
@@ -44,2 +44,19 @@ A reusable project generator that _copies file recursively_, while | ||
If the supplied `data` has a key `transforms`, with a hash of tranformation | ||
functions, then the found tokens will be used to run a corresponding | ||
transform on the supplied data. Example: | ||
```js | ||
const data = { | ||
transforms: { foo: (data, context) => data.toUpperCase() }, | ||
foo: 'foo' | ||
} | ||
// __foo__ => foo_transform(data = 'foo') => 'FOO' | ||
``` | ||
The `context` object contains properties: `source` (input file path), | ||
`dest` (output file path), `key` (the matched key). | ||
### `__` handling | ||
@@ -46,0 +63,0 @@ |
@@ -9,6 +9,6 @@ 'use strict' | ||
const path = require('path') | ||
const osenv = require('osenv') | ||
const rimraf = require('rimraf') | ||
const base = path.join(__dirname, 'fixtures') | ||
const testRun = 'fixture-notification-' + new Date().getTime() | ||
const fixtures = fs.readdirSync(base).filter((d) => !d.match(/expected$/)) | ||
@@ -63,3 +63,3 @@ | ||
var dest = path.join(osenv.tmpdir(), 'generify', fixture) | ||
var dest = path.join(process.cwd(), 'test-runs', testRun, fixture) | ||
var data = { hello: 'hello world' } | ||
@@ -70,2 +70,11 @@ | ||
} | ||
if (fixture === 'transforms') { | ||
data.transforms = { | ||
foo: (d, ctx) => { | ||
if (ctx.dest.endsWith('b')) return d | ||
return d.toUpperCase() | ||
} | ||
} | ||
data.foo = 'foo' | ||
} | ||
@@ -72,0 +81,0 @@ var expectedSet = new Set(Object.keys(expected).map(f => f.replace(/^[/\\]+/, ''))) |
@@ -9,6 +9,6 @@ 'use strict' | ||
const path = require('path') | ||
const osenv = require('osenv') | ||
const rimraf = require('rimraf') | ||
const base = path.join(__dirname, 'fixtures') | ||
const testRun = 'fixtures-' + new Date().getTime() | ||
const fixtures = fs.readdirSync(base).filter((d) => !d.match(/expected$/)) | ||
@@ -63,3 +63,3 @@ | ||
var dest = path.join(osenv.tmpdir(), 'generify', fixture) | ||
var dest = path.join(process.cwd(), 'test-runs', testRun, fixture) | ||
var data = { hello: 'hello world' } | ||
@@ -70,2 +70,11 @@ | ||
} | ||
if (fixture === 'transforms') { | ||
data.transforms = { | ||
foo: (d, ctx) => { | ||
if (ctx.dest.endsWith('b')) return d | ||
return d.toUpperCase() | ||
} | ||
} | ||
data.foo = 'foo' | ||
} | ||
@@ -72,0 +81,0 @@ generify(path.join(base, fixture), dest, data, function (err) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18235
5
24
298
86
+ Addedisbinaryfile@4.0.10(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsplit2@3.2.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisbinaryfile@3.0.3(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsplit2@2.2.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedxtend@4.0.2(transitive)
Updatedisbinaryfile@^4.0.2
Updatedsplit2@^3.0.0