вторник, 10 сентября 2024 г.

Detect file encoding UTF-8 or Windows-1251

Install or copy: https://www.npmjs.com/package/detect-character-encoding

Code:

const fs = require('fs');

const path = require('path');

const languageEncoding = require('./Detect-File-Encoding-and-Language-main');


const folderPath = path.resolve('C:/path/to/folder/src');


function walkDirAndGetFileEncoding (folderPath) {

    fs.readdirSync(folderPath).forEach(function (filesOrFolder) {

        const fileOrFolderPath = path.join(folderPath, filesOrFolder);

        const stats = fs.statSync(fileOrFolderPath);

        if (stats.isDirectory()) {

            walkDirAndGetFileEncoding(fileOrFolderPath);

        } else if (stats.isFile()) {

            languageEncoding(fileOrFolderPath).then(function (fileInfo) {

                if (fileInfo.encoding !== 'UTF-8') {

                    console.log(fileInfo.encoding + ' ' + fileOrFolderPath);

                }

            });

        }

    });

}


walkDirAndGetFileEncoding(folderPath);