디렉토리 내에서 파일을 읽고 파일로 무언가를 수행하는 방법을 알고 싶다면 여기로 가십시오. 또한를 통해 명령을 실행하는 방법을 보여줍니다 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);
path.join(__dirname, 'start.html')