dir-generate
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib/dir-generate.js'); | ||
module.exports = require('./lib/dir-generate.js').run(); |
@@ -20,56 +20,58 @@ #!/usr/bin/env node | ||
commander | ||
.option('-d, --dir [source_dir] ', | ||
'set target dir path. defaults to ./', | ||
'./') | ||
.option('-f, --file [source_file]', | ||
'set config file path. defaults to ./index.md', | ||
'./index.md'); | ||
exports.run = function () { | ||
commander | ||
.option('-d, --dir [source_dir] ', | ||
'set target dir path. defaults to ./', | ||
'./') | ||
.option('-f, --file [source_file]', | ||
'set config file path. defaults to ./index.md', | ||
'./index.md'); | ||
commander.parse(process.argv); | ||
commander.parse(process.argv); | ||
var filePath = commander.file; | ||
var dirPath = commander.dir; | ||
var filePath = commander.file; | ||
var dirPath = commander.dir; | ||
fse.readFile(filePath, 'utf8', function (err, data) { | ||
if (err) { | ||
throw new Error('Failed to open file.'); | ||
} | ||
fse.readFile(filePath, 'utf8', function (err, data) { | ||
if (err) { | ||
throw new Error('Failed to open file.'); | ||
} | ||
var json = md.parse(data); | ||
if (json.shift() !== 'markdown') { | ||
throw new Error('Markdown init wrong.'); | ||
} | ||
initDir(json.shift(), dirPath); | ||
console.log('dir-generate finish.'); | ||
}); | ||
var json = md.parse(data); | ||
if (json.shift() !== 'markdown') { | ||
throw new Error('Markdown init wrong.'); | ||
} | ||
initDir(json.shift(), dirPath); | ||
console.log('dir-generate finish.'); | ||
}); | ||
function initDir (json, dirPath) { | ||
if(json.length === 0) { | ||
return; | ||
} | ||
var type = json.shift(); | ||
if (type === 'bulletlist') { | ||
json.forEach(function (item) { | ||
initDir(item, dirPath); | ||
}); | ||
} else if(type === 'listitem') { | ||
var data = json.shift(); | ||
if(!data) { | ||
throw new Error('Wrong parse.'); | ||
function initDir (json, dirPath) { | ||
if(json.length === 0) { | ||
return; | ||
} | ||
var outputPath = path.join(dirPath, data); | ||
if (/\b\w*\.\w+$/.test(data)) { | ||
fse.outputFile(outputPath, ''); | ||
var type = json.shift(); | ||
if (type === 'bulletlist') { | ||
json.forEach(function (item) { | ||
initDir(item, dirPath); | ||
}); | ||
} else if(type === 'listitem') { | ||
var data = json.shift(); | ||
if(!data) { | ||
throw new Error('Wrong parse.'); | ||
} | ||
var outputPath = path.join(dirPath, data); | ||
if (/\b\w*\.\w+$/.test(data)) { | ||
fse.outputFile(outputPath, ''); | ||
} else { | ||
fse.ensureDir(outputPath); | ||
var next = json.shift(); | ||
if(next) { | ||
initDir(next, outputPath); | ||
} | ||
} | ||
} else { | ||
fse.ensureDir(outputPath); | ||
var next = json.shift(); | ||
if(next) { | ||
initDir(next, outputPath); | ||
} | ||
throw new Error('Wrong input.'); | ||
} | ||
} else { | ||
throw new Error('Wrong input.'); | ||
} | ||
} | ||
}; |
{ | ||
"name": "dir-generate", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "a easy tool to make dir with markdown", | ||
"bin": { | ||
"dir-generate": "./lib/dir-generate" | ||
"dir-generate": "./bin/dir-generate" | ||
}, | ||
@@ -8,0 +8,0 @@ "keywords": [ |
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
10843
11
65