Skip to content

Commit

Permalink
Fix argument passing when invoking globally
Browse files Browse the repository at this point in the history
  • Loading branch information
xasdx committed Feb 25, 2018
1 parent dba760b commit a0393e8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,3 +39,5 @@ another_dir
favDir
file.s
```

Note: nesting of files and folders is expressed by single space characters.
2 changes: 1 addition & 1 deletion bin/structr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("../lib")
require("../lib").generate(process.argv[2], { pathPrefix: process.argv[3] })
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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] }) }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit a0393e8

Please sign in to comment.