如果您想知道如何在目录中读取文件并对其进行处理,那么就可以了。这也向您展示了如何通过power shell. 这是在TypeScript!我遇到了麻烦,所以我希望有一天能对某人有所帮助。如果您认为这没有帮助,请随时对我投反对票。这对我所做的是webpack我.ts在某个文件夹中的每个目录中的所有文件,以便为部署做好准备。希望你能用起来!
import * as fs from 'fs';
let path = require('path');
let pathDir = '/path/to/myFolder';
const execSync = require('child_process').execSync;
let readInsideSrc = (error: any, files: any, fromPath: any) => {
    if (error) {
        console.error('Could not list the directory.', error);
        process.exit(1);
    }
    files.forEach((file: any, index: any) => {
        if (file.endsWith('.ts')) {
            //set the path and read the webpack.config.js file as text, replace path
            let config = fs.readFileSync('myFile.js', 'utf8');
            let fileName = file.replace('.ts', '');
            let replacedConfig = config.replace(/__placeholder/g, fileName);
            //write the changes to the file
            fs.writeFileSync('myFile.js', replacedConfig);
            //run the commands wanted
            const output = execSync('npm run scriptName', { encoding: 'utf-8' });
            console.log('OUTPUT:\n', output);
            //rewrite the original file back
            fs.writeFileSync('myFile.js', config);
        }
    });
};
// loop through all files in 'path'
let passToTest = (error: any, files: any) => {
    if (error) {
        console.error('Could not list the directory.', error);
        process.exit(1);
    }
    files.forEach(function (file: any, index: any) {
        let fromPath = path.join(pathDir, file);
        fs.stat(fromPath, function (error2: any, stat: any) {
            if (error2) {
                console.error('Error stating file.', error2);
                return;
            }
            if (stat.isDirectory()) {
                fs.readdir(fromPath, (error3: any, files1: any) => {
                    readInsideSrc(error3, files1, fromPath);
                });
            } else if (stat.isFile()) {
                //do nothing yet
            }
        });
    });
};
//run the bootstrap
fs.readdir(pathDir, passToTest);