Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cptmpl

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cptmpl - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

.github/CODE_OF_CONDUCT.md

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!'))
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc