Comparing version 0.0.5 to 0.1.0
30
index.js
@@ -10,5 +10,7 @@ 'use strict' | ||
const cptmpl = module.exports = async function cptmpl (_src, _dest, data = {}, opts = {}) { | ||
const cptmpl = async function cptmpl (_src, _dest, data = {}, opts = {}) { | ||
const { mode, force } = opts | ||
const handleConflicts = opts.handleConflicts || defaultHandleConflicts | ||
const promptModule = opts.promptModule || ((opts) => inquirer.prompt(opts)) | ||
const displayDiff = opts.displayDiff || defaultDisplayDiff | ||
@@ -31,3 +33,3 @@ const src = path.resolve(_src) | ||
if (force !== true) { | ||
const shouldContinue = await handleConflicts(dest, rendered) | ||
const shouldContinue = await handleConflicts(dest, rendered, { promptModule, displayDiff }) | ||
if (!shouldContinue) { | ||
@@ -42,3 +44,3 @@ return | ||
module.exports.recursive = async function cptmplr (_src, _dest, data = {}, opts = {}) { | ||
async function cptmplr (_src, _dest, data = {}, opts = {}) { | ||
const filesWithStats = {} | ||
@@ -67,3 +69,5 @@ await readdir(_src, [(file, stats) => { | ||
force: opts.force, | ||
handleConflicts: opts.handleConflicts | ||
handleConflicts: opts.handleConflicts, | ||
promptModule: opts.promptModule, | ||
displayDiff: opts.displayDiff | ||
}) | ||
@@ -74,3 +78,3 @@ } | ||
// Detect file conflict | ||
async function defaultHandleConflicts (dest, contents) { | ||
async function defaultHandleConflicts (dest, contents, opts) { | ||
if (!await fs.exists(dest)) return true | ||
@@ -90,7 +94,8 @@ if ((await fs.stat(dest)).isDirectory()) return true | ||
// How to resolve? | ||
return promptConflict(dest, existing, contents) | ||
return promptConflict(dest, existing, contents, opts) | ||
} | ||
async function promptConflict (dest, existing, contents) { | ||
const { whatToDo } = await inquirer.prompt({ | ||
async function promptConflict (dest, existing, contents, opts = {}) { | ||
const { promptModule, displayDiff } = opts | ||
const { whatToDo } = await promptModule({ | ||
type: 'expand', | ||
@@ -118,7 +123,7 @@ name: 'whatToDo', | ||
displayDiff(existing, contents) | ||
return promptConflict(dest, existing, contents) | ||
return promptConflict(dest, existing, contents, opts) | ||
} | ||
} | ||
function displayDiff (existing, contents) { | ||
function defaultDisplayDiff (existing, contents) { | ||
process.stderr.write('\n') | ||
@@ -146,1 +151,6 @@ diff.diffLines(existing.toString('utf8'), contents.toString('utf8')) | ||
} | ||
module.exports = cptmpl | ||
module.exports.recursive = cptmplr | ||
module.exports.defaultHandleConflicts = defaultHandleConflicts | ||
module.exports.defaultDisplayDiff = defaultDisplayDiff |
{ | ||
"name": "cptmpl", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Copy and process a template file", | ||
@@ -23,13 +23,13 @@ "author": "Wes Todd <wes@wesleytodd.com>", | ||
"scripts": { | ||
"test": "standard && mocha", | ||
"prepublushOnly": "npm t", | ||
"postpublish": "git push && git push --tags" | ||
"test": "mocha", | ||
"lint": "standard", | ||
"prepublushOnly": "npm run lint && npm run test" | ||
}, | ||
"dependencies": { | ||
"chalk": "^2.4.1", | ||
"chalk": "^4.1.2", | ||
"detect-conflict": "^1.0.1", | ||
"diff": "^3.5.0", | ||
"ejs": "^2.6.1", | ||
"fs-extra": "^7.0.0", | ||
"inquirer": "^7.0.0", | ||
"diff": "^5.1.0", | ||
"ejs": "^3.1.9", | ||
"fs-extra": "^11.2.0", | ||
"inquirer": "^8.2.6", | ||
"minimist": "^1.2.0", | ||
@@ -39,5 +39,5 @@ "recursive-readdir": "^2.2.2" | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"standard": "^14.1.0" | ||
"mocha": "^10.2.0", | ||
"standard": "^17.1.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # Copy and process a template file or files | ||
[![NPM Downloads](https://img.shields.io/npm/dm/cptmpl.svg)](https://npmjs.org/package/cptmpl) | ||
[![Build Status](https://travis-ci.org/wesleytodd/cptmpl.svg?branch=master)](https://travis-ci.org/wesleytodd/cptmpl) | ||
[![test](https://github.com/wesleytodd/cptmpl/workflows/Test/badge.svg)](https://github.com/wesleytodd/cptmpl/actions?query=workflow%3ATest) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/standard/standard) | ||
@@ -8,0 +8,0 @@ |
@@ -23,3 +23,3 @@ 'use strict' | ||
it('should copy a directory of templates recursivly', async function () { | ||
it('should copy a directory of templates recursively', async function () { | ||
// Create empty dir since git wont let us check it in | ||
@@ -85,2 +85,32 @@ await fs.ensureDir(path.join(FIX_DIR, 'dir', 'empty')) | ||
}) | ||
it('should support overrides for prompt and diff', async function () { | ||
let called = 0 | ||
await cptmpl(path.join(FIX_DIR, 'foo.md'), path.join(TMP_DIR, 'foo.md'), { | ||
name: 'foo' | ||
}, { | ||
promptModule: () => { | ||
throw new Error('should not have been called') | ||
} | ||
}) | ||
await cptmpl(path.join(FIX_DIR, 'foo.md'), path.join(TMP_DIR, 'foo.md'), { | ||
name: 'bar' | ||
}, { | ||
promptModule: (prompts) => { | ||
called++ | ||
assert.strictEqual(prompts.name, 'whatToDo') | ||
return (called === 1) | ||
? { whatToDo: 'Diff' } | ||
: { whatToDo: 'Yes' } | ||
}, | ||
displayDiff: (existing, content) => { | ||
called++ | ||
assert(existing.includes('foo')) | ||
assert(content.includes('bar')) | ||
} | ||
}) | ||
assert.strictEqual(called, 3) | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'foo.md'))) | ||
assert(await fs.readFileSync(path.join(TMP_DIR, 'foo.md')).includes('Hello bar!')) | ||
}) | ||
}) |
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
18414
20
228
+ Addedasync@3.2.6(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedclone@1.0.4(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addeddiff@5.2.0(transitive)
+ Addedejs@3.1.10(transitive)
+ Addedfilelist@1.0.4(transitive)
+ Addedfs-extra@11.2.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinquirer@8.2.6(transitive)
+ Addedis-interactive@1.0.0(transitive)
+ Addedis-unicode-supported@0.1.0(transitive)
+ Addedjake@10.9.2(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedlog-symbols@4.1.0(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedora@5.4.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrxjs@7.8.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addeduniversalify@2.0.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwcwidth@1.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removeddiff@3.5.0(transitive)
- Removedejs@2.7.4(transitive)
- Removedfs-extra@7.0.1(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedinquirer@7.3.3(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedrxjs@6.6.7(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedtslib@1.14.1(transitive)
- Removeduniversalify@0.1.2(transitive)
Updatedchalk@^4.1.2
Updateddiff@^5.1.0
Updatedejs@^3.1.9
Updatedfs-extra@^11.2.0
Updatedinquirer@^8.2.6