Last active
January 14, 2019 22:22
-
-
Save foxleigh81/fe4f64ca55ab4c0ee4aff96f16575837 to your computer and use it in GitHub Desktop.
Wrap - takes a filename and wraps a directory with the same name (minus extension) around it and renames the file to index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// takes a filename and wraps a directory with the same name (minus extension) around it and renames the file to whatever you like | |
// all output is placed in a directory called 'output' in the directory the script is run in. | |
var fs = require('fs') | |
var path = require('path') | |
const searchRecursive = (dir, pattern) => { | |
// This is where we store pattern matches of all files inside the directory | |
let results = [] | |
// Read contents of directory | |
fs.readdirSync(dir).forEach((dirInner) => { | |
// Obtain absolute path | |
dirInner = path.resolve(dir, dirInner) | |
// Get stats to determine if path is a directory or a file | |
const stat = fs.statSync(dirInner) | |
// If path is a directory, scan it and combine results | |
if (stat.isDirectory()) results = results.concat(searchRecursive(dirInner, pattern)) | |
// If path is a file and ends with pattern then push it onto results | |
if (stat.isFile() && dirInner.endsWith(pattern)) results.push(dirInner) | |
}) | |
return results | |
} | |
const library = path.resolve(__dirname, './') | |
const src = '/path/to/source/files' | |
const ext = '.extension' | |
const filename = 'index.md' | |
searchRecursive(library, '.md').map(dir => { | |
const cleaned = dir.replace(src, '').replace(ext, '') | |
const array = cleaned.split('/') | |
// Creates a sub directory in the current directory so no original files are altered. | |
const newdir = `./output/${array[0]}/${array[1]}` | |
fs.mkdir(newdir, { recursive: true }, (err) => { | |
fs.copyFile(dir, `${newdir}/${filename}`, (err) => { | |
if (err) throw err; | |
console.log('Wrapping', dir); | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment