Skip to content

Instantly share code, notes, and snippets.

@timqian
Created October 28, 2020 07:25
Show Gist options
  • Save timqian/0b22f1eb530c8b2e44c91e6ff84dfa94 to your computer and use it in GitHub Desktop.
Save timqian/0b22f1eb530c8b2e44c91e6ff84dfa94 to your computer and use it in GitHub Desktop.
Find errors thrown by serverless
#!/usr/bin/env node
/**
* 1. Get all files
* 2. Find `new ServerlessError`
* 3. Copy error message and status code
*/
const fs = require('fs');
const path = require('path');
// 1. Get all files
const ignoredFolders = ['.git', 'node_modules']
const fileList = [];
const getPathsSync = function (dir){
const dirList = fs.readdirSync(dir);
dirList.forEach(function (item) {
const currentPath = path.join(dir, item);
const isFile = fs.statSync(currentPath).isFile();
const extname = path.extname(item);
if (isFile && extname === '.js') {
fileList.push(currentPath);
}
});
dirList.forEach(function(item){
const currentPath = path.join(dir, item);
const isDirectory = fs.statSync(currentPath).isDirectory();
if (isDirectory && !ignoredFolders.includes(item)) {
getPathsSync(currentPath);
}
});
return fileList;
};
const allFilePaths = getPathsSync('./platform-cn');
let resultStr = `| path | error |
| --- | --- |
`;
allFilePaths.forEach(path => {
const fileStr = fs.readFileSync(path, 'utf8');
const patt = /new\sServerlessError\((.*?)\)\;/gs;
const errors = [];
let result;
while ((result = patt.exec(fileStr)) !== null) {
// console.log(result[1]);
// errors.push(result[1]); //or use result[1]?
resultStr += `| ${path} | ${result[1].split('\n').join('').replace('\`', '\\`')} |\n`;
}
// console.log(errors)
})
console.log(resultStr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment