See you big guy in the nuggets post articles, see some big guy’s warehouse directory, want to do one of their own, however, lazy attack, have to find a reason not to do this thing, programmers don’t have to lazy yao (seriously) :
Disadvantages of working with directories manually:
-
Add article, modify article title, delete article, need to find the corresponding title operation, including content and markdown syntax, many very tedious
-
When adding articles and modifying articles, I hope to record the update time of articles. This manual time to fill in, too much is very tedious
I convinced myself, and then I started
Train of thought
-
Iterate over all file recursive implementations
-
When files and folders are encountered, write to readme.md using markdown syntax
Problems encountered:
-
File folder error, such as not CSS in the CSS directory, here need to use synchronous reading to deal with the problem
-
This is partly because the Fs. readdirSync API of FS reads the list of files in a sort of order, rather than the one we see in resource management, that we need to manually control when reading the subdirectory root, Move the unfinished (n) file name to the end of the file list
// Check if n(incomplete) exists in the file name of the folder, if so, move the n to the end of the array
let nIndex = files.findIndex(f= > f === 'n')
~nIndex && (files.splice(nIndex, 1), files.push('n'))
Copy the code
File structure
Currently only subdirectory support and n(unfinished) are being considered, not so much for extension
Example:
let fpath = 'front end'
const repPath = 'https://github.com/869288142/blog/tree/master'
const outputFile = 'README.md'
let g = require('./generateDir')
new g(repPath, outputFile, fpath).run()
Copy the code
The complete code
let fs = require('fs') // To read and write files, we need to introduce the fs-- file system module
let path = require('path')
let { sep } = path
class generateDir {
constructor(repPath, outputFile, path) {
this.repPath = repPath
this.outputFile = outputFile
this.path = path
}
run() {
this.unLinkOutput(this.outputFile)
this.readDir(this.path)
}
readDir(path) {
let exists = fs.existsSync(path), // Check whether the directory exists synchronously
stat = fs.statSync(path) // File information
if (exists && stat) {
// If the directory exists
if (stat.isFile()) {
// If it is a file
let fpath = path.split(sep) // Split the path into arrays with the path separator
// Get the file name
let fileName = fpath[fpath.length - 1]
// File update time
let updateTime = this.formatDate(stat.mtime)
// Append the content to readme.md
let fileNameWithoutSuffix = fileName.replace(/(.*)\.md/.'$1')
// File output content
let content =
/ / file name
` * [${fileNameWithoutSuffix}] ` +
// URL
` (The ${this.repPath}/${fpath.join('/')}) ` +
// Update time
` update in${updateTime}` +
/ / a blank line
`\r\n \r\n`
fs.appendFileSync(this.outputFile, content)
} else if (stat.isDirectory()) {
// If it is a folder
let fpath = path.split(sep) // Split the path into arrays with the path separator
// Get the file name
let fileName = fpath[fpath.length - 1]
let files = fs.readdirSync(path) // Returns the names of all files in the specified directory
// Filter out only n subordinate folders and empty n folders
let normalContent = ` * *${fileName}* * `
let nContent = '\r\n \r\n** Writing **'
let totalContent = `${
fileName === 'n' ? nContent : normalContent
} \r\n \r\n`
if(files.length ! = =1&& files.length ! = =0) {
// n Folder name is replaced with writing
fs.appendFileSync(this.outputFile, totalContent)
// Walk through the folder subordinate files
if (files && files.length > 0) {
// Make sure that the subordinate files of folder n are printed after the first level folder
let nIndex = files.findIndex(f= > f === 'n')
~nIndex && (files.splice(nIndex, 1), files.push('n'))
// Perform recursive operations on each file
files.forEach(file= > {
this.readDir(path + sep + file)
})
}
}
}
} else {
console.info('Root directory does not exist.')
}
}
formatDate(date) {
return `${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}`
}
unLinkOutput(outputFile) {
let unlinkPath = `${path.resolve('. ')}/${outputFile}`
if (fs.existsSync(unlinkPath)) {
fs.unlinkSync(unlinkPath)
}
}
}
module.exports = exports = generateDir
Copy the code
Summary:
Actually, this should be quite many people think of it, hee hee, but as of a new, very happy can use technology to simplify their daily operation, before has also tried to write a script to automatically delete library and then populate the data, code logic and the quality is not good, if bosses feel ok, hope to get the first star in life, The newbie is still practicing writing.