Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing ERC data #5

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class EIPsFilters {
export class AppController {
private readonly logger = new Logger('App');

constructor(private readonly appService: AppService) {}
constructor(private readonly appService: AppService) { }

@Get('/eips/search')
@ApiOperation({ description: 'Search EIPs.' })
Expand Down Expand Up @@ -90,12 +90,12 @@ export class AppController {
};
}

// @Get('/eips/update')
// @ApiOperation({ description: 'Updata Eips.' })
// async updateAllEips() {
// const result = await this.appService.updateEips();
// return { data: result };
// }
@Get('/eips/update')
@ApiOperation({ description: 'Updata Eips.' })
async updateAllEips() {
const result = await this.appService.updateEips();
return { data: result };
}

@Get('/ercs/download')
@ApiOperation({ description: 'download Ecs.' })
Expand Down
142 changes: 86 additions & 56 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ export class AppService {
return result;
}

async downloadEips() {
const paths = './ETH-EIPS/';
deleteFolder(paths);
return new Promise(function (res, rej) {
download(
'direct:https://github.com/ethereum/EIPs.git',
'ETH-EIPs',
{ clone: true },
function (err) {
if (err) {
rej(err);
} else {
res('download EIP success!');
console.log('download EIP success!');
}
},
);
});
}

async downloadErcs() {
const paths = './ETH-ERCS/';
deleteFolder(paths);
Expand All @@ -261,7 +281,7 @@ export class AppService {
rej(err);
} else {
res('download ERC success!');
console.log('下载完成');
console.log('download ERC success!');
}
},
);
Expand All @@ -271,67 +291,77 @@ export class AppService {
async updateEips() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const paths = './ETH-EIPs/';
console.log('开始清理文件夹');
deleteFolder(paths);
console.log('清理文件夹成功');
await this.downloadErcs();
download(
'direct:https://github.com/ethereum/EIPs.git',
'ETH-EIPs',
{ clone: true },
(err) => {
console.log(err ? '拉取Error' : '拉取Success');
if (!err) {
const writeData = [];
const directory = './ETH-EIPs/EIPS/';
fs.readdir(directory, (err, files) => {
await this.downloadEips();

const writeData = [];
const directory = './ETH-EIPs/EIPS/';
// erc
const getArr = async (path: string, type: 'eip' | 'erc') => {
let files;
try {
files = await fs.promises.readdir(path);
} catch (err) {
console.error(err);
return [];
}
return files.map((f) => f.replace(type + '-', ''));
};

const eipArr = await getArr('./ETH-EIPs/EIPS/', 'eip');
const ercArr = await getArr('./ETH-ERCS/ERCS/', 'erc');
const ercNew = ercArr.filter((item) => !eipArr.includes(item));
ercNew.shift();
ercNew.forEach(async (item) => {
let fileInfo;
try {
fileInfo = await fs.promises.readFile(
path.join('./ETH-ERCS/ERCS/', `erc-${item}`),
'utf8',
);
const ercData = this.formateData(fileInfo);
writeData.push(ercData);
} catch (err) {
console.error(err);
return;
}
});

fs.readdir(directory, async (err, files) => {
if (err) {
console.error(err);
return;
}
(async function getFileMeta(i) {
if (i === files.length) {
console.log('解析EIPS文件完成');
await that.saveData(writeData);
console.log('写入DB完成');
} else {
fs.readFile(path.join(directory, files[i]), 'utf8', (err, data) => {
if (err) {
console.error(err);
console.log('读取文件失败', err);
return;
}
(async function getFileMeta(i) {
console.log(i, files.length);

if (i === files.length) {
console.log('解析EIPS文件完成');
await that.saveData(writeData);
console.log('写入DB完成');
} else {
fs.readFile(
path.join(directory, files[i]),
'utf8',
(err, data) => {
if (err) {
console.log('读取文件失败', err);
return;
}
const result = that.formateData(data);
if (result.status !== 'Moved') {
writeData.push(result);
} else {
const ercDirectory = './ETH-ERCS/ERCS/';
const id = files[i].split('-')[1];
const fsInfo = fs.readFileSync(
path.join(ercDirectory, `erc-${id}`),
'utf8',
);
const ercData = that.formateData(fsInfo);
writeData.push(ercData);
}

getFileMeta(i + 1);
},
);
}
})(0);
const result = that.formateData(data);
if (result.status !== 'Moved') {
writeData.push(result);
} else {
const ercDirectory = './ETH-ERCS/ERCS/';
const id = files[i].split('-')[1];
const fsInfo = fs.readFileSync(
path.join(ercDirectory, `erc-${id}`),
'utf8',
);
const ercData = that.formateData(fsInfo);
writeData.push(ercData);
}

getFileMeta(i + 1);
});
} else {
// console.log(err);
deleteFolder(paths);
}
},
);
})(0);
});
}

async sendEmail() {
Expand Down
Loading