cptmpl
Advanced tools
Comparing version 0.0.1 to 0.0.2
32
index.js
@@ -8,4 +8,5 @@ 'use strict' | ||
const chalk = require('chalk') | ||
const readdir = require('recursive-readdir') | ||
module.exports = async function cptmpl (_src, _dest, data = {}, opts = {}) { | ||
const cptmpl = module.exports = async function cptmpl (_src, _dest, data = {}, opts = {}) { | ||
const { mode, force } = opts | ||
@@ -32,2 +33,27 @@ const handleConflicts = opts.handleConflicts || defaultHandleConflicts | ||
module.exports.recursive = async function cptmplr (_src, _dest, data = {}, opts = {}) { | ||
const filesWithStats = {} | ||
await readdir(_src, [(file, stats) => { | ||
filesWithStats[file] = stats | ||
}]) | ||
for (let file of Object.keys(filesWithStats)) { | ||
const dest = path.join(_dest, path.relative(_src, file)) | ||
// Make directories | ||
const stats = filesWithStats[file] | ||
if (stats.isDirectory()) { | ||
await fs.ensureDir(dest, opts.mode || getFileMode(stats.mode)) | ||
continue | ||
} | ||
// Copy template files | ||
await cptmpl(file, path.join(_dest, path.relative(_src, file)), data, { | ||
mode: opts.mode || getFileMode(stats.mode), | ||
force: opts.force, | ||
handleConflicts: opts.handleConflicts | ||
}) | ||
} | ||
} | ||
// Detect file conflict | ||
@@ -99,1 +125,5 @@ async function defaultHandleConflicts (dest, contents) { | ||
} | ||
function getFileMode (mode) { | ||
return '0' + (mode & parseInt('777', 8)).toString(8) | ||
} |
{ | ||
"name": "cptmpl", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Copy and process a template file", | ||
@@ -34,3 +34,4 @@ "author": "Wes Todd <wes@wesleytodd.com>", | ||
"inquirer": "^6.2.0", | ||
"minimist": "^1.2.0" | ||
"minimist": "^1.2.0", | ||
"recursive-readdir": "^2.2.2" | ||
}, | ||
@@ -37,0 +38,0 @@ "devDependencies": { |
@@ -1,2 +0,2 @@ | ||
# Copy and process a template file | ||
# Copy and process a template file or files | ||
@@ -14,3 +14,3 @@ [![NPM Version](https://img.shields.io/npm/v/cptmpl//npmjs.org/package/cptmpl) | ||
``` | ||
$ npm i --save [--global] cptmpl | ||
$ npm i --save cptmpl | ||
$ cptmpl --help | ||
@@ -27,6 +27,6 @@ | ||
-D, --data a JSON string of data for the template | ||
-r, --recursive copy a directory of templates recursivly | ||
-f, --force force overwite file | ||
--mode the file permissions mode | ||
-V, --version output the version number | ||
-V, --version output the version number | ||
--help display this help | ||
@@ -55,2 +55,7 @@ | ||
}) | ||
// Recursivly copy src directory to dest | ||
await cptmpl.recursive('src', 'dest', { | ||
name: 'world' | ||
}) | ||
})() | ||
@@ -57,0 +62,0 @@ ``` |
@@ -19,5 +19,31 @@ 'use strict' | ||
// Ensure the right files are created | ||
assert(fs.pathExists(path.join(TMP_DIR, 'bar.md'))) | ||
assert(fs.readFileSync(path.join(TMP_DIR, 'bar.md')).includes('Hello world!')) | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'bar.md'))) | ||
assert(await fs.readFileSync(path.join(TMP_DIR, 'bar.md')).includes('Hello world!')) | ||
}) | ||
it('should copy a directory of templates recursivly', async function () { | ||
await cptmpl.recursive(path.join(FIX_DIR, 'dir'), TMP_DIR, { | ||
name: 'world' | ||
}) | ||
// Ensure the right files are created | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'empty'))) | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'bin/foo'))) | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'foo.md'))) | ||
assert(await fs.readFileSync(path.join(TMP_DIR, 'foo.md')).includes('Hello world!')) | ||
assert(await fs.pathExists(path.join(TMP_DIR, 'bar/bar.md'))) | ||
assert(await fs.readFileSync(path.join(TMP_DIR, 'bar/bar.md')).includes('Hello world!')) | ||
}) | ||
it('should refuse to copy recursivly on a file', function (done) { | ||
cptmpl.recursive(path.join(FIX_DIR, 'foo.md'), TMP_DIR, { | ||
name: 'world' | ||
}).catch((err) => { | ||
assert.strictEqual(err.code, 'ENOTDIR') | ||
done() | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
10855
11
147
80
8
+ Addedrecursive-readdir@^2.2.2
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedrecursive-readdir@2.2.3(transitive)