From a0393e843ebe362cd7fa84ee5cdfad44d375459f Mon Sep 17 00:00:00 2001 From: _asdx_ Date: Sun, 25 Feb 2018 13:50:03 +0000 Subject: [PATCH] Fix argument passing when invoking globally --- README.md | 22 ++++++++++++++++++++-- bin/structr.js | 2 +- lib/index.js | 13 ++++++++++--- package.json | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7623d47..4943492 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,25 @@ ## Use it -The script consumes a text based hierarchy descriptor as its first argument, which contains the directories and files the user wishes to create. +The script consumes a text-based hierarchy descriptor as its first argument, which contains the directories and files the user wishes to create. The second, optional argument might specify the target directory. -Nesting can be expressed by space characters. See the example input below: +So the following command: + + structr "$(< desc)" "$(pwd)" + +will create the desired hierarchy at `pwd`: + +``` +Creating directory .../dir +Creating file .../dir/file.txt +Creating directory .../dir/dir +Creating file .../dir/dir/file.h +Creating directory .../another_dir +Creating directory .../another_dir/favDir +Creating file .../file.s +``` + +with `desc` being a descriptor file, containing the following text: ``` dir @@ -23,3 +39,5 @@ another_dir favDir file.s ``` + +Note: nesting of files and folders is expressed by single space characters. diff --git a/bin/structr.js b/bin/structr.js index e028f59..c77a147 100644 --- a/bin/structr.js +++ b/bin/structr.js @@ -1,3 +1,3 @@ #!/usr/bin/env node -require("../lib") +require("../lib").generate(process.argv[2], { pathPrefix: process.argv[3] }) diff --git a/lib/index.js b/lib/index.js index 3bc7a93..3ff6fe2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ module.exports = { generate } function generate(s, opts) { let pathPrefix = opts && opts.pathPrefix || __dirname + console.log(`Creating file structure at ${pathPrefix}\n`) if (typeof s !== "string") { throw new Error("Input must be a string") } if (s.length < 1) { return } let entries = parseDescriptor(s) @@ -39,10 +40,16 @@ function createEntry(path) { else { createDirectory(path) } } -function createDirectory(path) { fs.mkdirSync(path) } +function createDirectory(path) { + console.log(`Creating directory ${path}`) + fs.mkdirSync(path) +} -function createFile(path) { fs.writeFileSync(path, "") } +function createFile(path) { + console.log(`Creating file ${path}`) + fs.writeFileSync(path, "") +} function parseDescriptor(s) { return s.split("\n") } -if (!module.parent) { generate(process.argv[2], process.argv[3]) } +if (!module.parent) { generate(process.argv[2], { pathPrefix: process.argv[3] }) } diff --git a/package.json b/package.json index 3ec1b2f..a8e7b3a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-structr", - "version": "1.0.2", + "version": "1.0.3", "description": "Utility to generate custom file and directory hierarchies", "main": "lib/index.js", "bin": {