Merges the contents of all files in the specified directory

This is the second day of my participation in Gwen Challenge

background

Near graduation, liver finished the thesis, the instructor called all, all, all the source code put appendix

Feeling speechless (it was unaware of how much front-end code there was), and unable to refuse

So many files, manual CV is impossible to manual CV, we give it the whole script, merge the content into a file, directly insert Word

The preparatory work

Install a Node environment

  • Node

The target

Execute the following command to merge files in the target directory

node index.js <target_directory>
Copy the code

starts

Gets the instruction pass parameter

Using the Process module, all parameters are indicated on the argv property

const process = require('process')

// The directory passed in
const targetDir = process.argv[2]

console.log(process.argv)
Copy the code

The test results

node index.js /home/sugar/Documents/VueProject

Print the result
[
  '/ home/sugar /. NVM/versions/node/v16.0.0 / bin/node'.'/home/sugar/Documents/VueProject/my-blog-vuepress/docs/technology/works/index.js'.'/home/sugar/Documents/VueProject'
]
Copy the code

Gets all files in the specified directory

Train of thought

We use the path and FS modules in combination with recursion:

  1. readdirSyncMethod to get all files (including directories)
  2. throughisFileCheck whether it is a file
  3. usepath.joinStitching path

The concrete implementation is as follows

const path = require('path')
const fs = require('fs')
/** * Recursively retrieves the absolute path of all files in the specified directory *@param {string} Dir Target directory *@param {string[]} 
 * @returns {string[]} File absolute path array */
function getDirFiles(dir) {
    let result = []
    let files = fs.readdirSync(dir, { withFileTypes: true })
    files.forEach(file= > {
        const filepath = path.join(dir, file.name)
        if (file.isFile()) {
            result.push(filepath)
        } else if(file.isDirectory()) { result.push(... getDirFiles(filepath)) } })return result;
}
Copy the code

test

console.log(getDirFiles('/home/sugar/Documents/VueProject/my-blog-vuepress/docs'));
Copy the code

Print the result and get the absolute path of all the files

[
  '/home/sugar/Documents/VueProject/my-blog-vuepress/docs/.vuepress/comment.js'.'/home/sugar/Documents/VueProject/my-blog-vuepress/docs/.vuepress/components/LeetCode.vue'. 237 more items ]Copy the code

Git, node_modules, add a filter:

  • Use arrays to store the relative paths of directories or files that need to be expelled
  • useendsWithMethod to match
  • Array.someMethod traversal, meet the conditions to exclude
/** * Recursively retrieves the absolute path of all files in the specified directory *@param {string} Dir Target directory *@param {string[]} 
 * @returns {string[]} File absolute path array */
function getDirFiles(dir, exclude = []) {
    let result = []
    let files = fs.readdirSync(dir, { withFileTypes: true })
    files.forEach(file= > {
        const filepath = path.join(dir, file.name)
        const isExclude = exclude.some(v= > {
            return filepath.endsWith(v)
        })
        if(! isExclude) {if (file.isFile()) {
                result.push(filepath)
            } else if(file.isDirectory()) { result.push(... getDirFiles(filepath, exclude)) } } })return result;
}
Copy the code

Merge file The contents of the target file

Ideas:

  • throughreadFileSyncReads the contents of the specified file
  • useappendFileSyncMethod to append content to the target file
  • useDate.nowGenerates a timestamp as the target file name

The concrete implementation is as follows

/** * the content is merged into a file *@param {string[]} files 
 */
function mergeFile(files) {
    // Write to the target file (timestamp name)
    const writeFilepath = path.join(__dirname, `The ${Date.now()}.txt`)
    files.forEach(f= > {
        // The contents of the file
        const txt = fs.readFileSync(f, { encoding: 'utf-8' })
        // The relative path of the files (note that this targetDir is the public directory of the files represented by the external variables, this line of code is mainly to get the relative path of the files)
        const dir = f.slice(targetDir.length + 1)

        // How to append content
        fs.appendFileSync(writeFilepath, `${dir}\n`)
        fs.appendFileSync(writeFilepath, `${txt}\n\n`)})console.log('ok', files.length, 'File');
    console.log(files);
}
Copy the code

test

Take my current project as an example

node index.js /home/sugar/Documents/VueProject/my-blog-vuepress
Copy the code

The results of

Ok 309 filesCopy the code

Oh, boy, that’s over $30, 000

The complete code

const process = require('process')
const path = require('path')
const fs = require('fs')

// The directory passed in
const targetDir = process.argv[2]
// Ignore the content
const ignore = ['node_modules'.'.git'.'dist'.'ignore'.'README.md'.'.lock'.'.png'.'docs'.'.eslintrc.js'.'.env'.'LICENSE'.'tsconfig.json'.'.github'.'_tests_']

const files = getDirFiles(targetDir, ignore)

mergeFile(files)

/** * the content is merged into a file *@param {string[]} files 
 */
function mergeFile(files) {
    // Write to the target file (timestamp name)
    const writeFilepath = path.join(__dirname, `The ${Date.now()}.txt`)
    files.forEach(f= > {
        // The contents of the file
        const txt = fs.readFileSync(f, { encoding: 'utf-8' })
        // The relative path of the file
        const dir = f.slice(targetDir.length + 1)

        // How to append content
        fs.appendFileSync(writeFilepath, `${dir}\n`)
        fs.appendFileSync(writeFilepath, `${txt}\n\n`)})console.log('ok', files.length, 'File');
    console.log(files);
}

/** * Recursively retrieves the absolute path of all files in the specified directory *@param {string} Dir Target directory *@param {string[]} 
 * @returns {string[]} File absolute path array */
function getDirFiles(dir, exclude = []) {
    let result = []
    let files = fs.readdirSync(dir, { withFileTypes: true })
    files.forEach(file= > {
        const filepath = path.join(dir, file.name)
        const isExclude = exclude.some(v= > {
            return filepath.endsWith(v)
        })
        if(! isExclude) {if (file.isFile()) {
                result.push(filepath)
            } else if(file.isDirectory()) { result.push(... getDirFiles(filepath, exclude)) } } })return result;
}
Copy the code