Skip to content

Commit

Permalink
Fixbug no file exist check (#12)
Browse files Browse the repository at this point in the history
* feat: when network is weak, try run toMarkdown()

* feat: file exist check

* code clear

* add console log

* add ;
  • Loading branch information
luojiyin1987 authored May 8, 2022
1 parent f938354 commit f3df3d0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48996,14 +48996,14 @@ const {
const {
gatherInputs,
inputExistCheck,
fileExistCheck,
getRouteAddr,
haveRouterAddrmd,
HTMLtoMarkdown
} = __nccwpck_require__(7677);

// cd ./news-translation
// You can run `node script\toMarkdown\index.js URL<String>`(URL is the URL of the article).

(async function toMarkdown() {
try {
const input = gatherInputs();
Expand All @@ -49019,6 +49019,10 @@ const {
const articleFileName = await haveRouterAddrmd(articleChildRouter);
const htmlString = await (await nodeFetch(URL, options)).text();
const articleText = await HTMLtoMarkdown(htmlString);

if (await fileExistCheck(input.markDownFilePath + articleFileName)) {
return Promise.reject("file has exist");
}

await fs.writeFile(
input.markDownFilePath + articleFileName,
Expand All @@ -49028,8 +49032,8 @@ const {
}
);
} catch (error) {
console.log('ERR:', error);
process.exitCode = 1;
console.log('ERR:', error);
process.exitCode = 1;
}
})();

Expand Down Expand Up @@ -49120,6 +49124,13 @@ exports.inputExistCheck = (input) =>
input.newsLink ? resolve(input.newsLink) : reject(Err_DontGetNewsLink);
});


//fileExitCheck in the path.
exports.fileExistCheck = (path) =>
new Promise((resolve, reject) => {
fs.existsSync(path)? resolve(true) : reject(false);
});

// Check the input parameters, and get the routing address of the article.
// - 原文网址:[原文标题](https://www.freecodecamp.org/news/xxxxxxx/
exports.getRouteAddr = (URL) =>
Expand Down
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const {
const {
gatherInputs,
inputExistCheck,
fileExistCheck,
getRouteAddr,
haveRouterAddrmd,
HTMLtoMarkdown
} = require('./utilities.js');

// cd ./news-translation
// You can run `node script\toMarkdown\index.js URL<String>`(URL is the URL of the article).

(async function toMarkdown() {
try {
const input = gatherInputs();
Expand All @@ -31,6 +31,10 @@ const {
const articleFileName = await haveRouterAddrmd(articleChildRouter);
const htmlString = await (await nodeFetch(URL, options)).text();
const articleText = await HTMLtoMarkdown(htmlString);

if (await fileExistCheck(input.markDownFilePath + articleFileName)) {
return Promise.reject("file has exist");
}

await fs.writeFile(
input.markDownFilePath + articleFileName,
Expand All @@ -40,7 +44,7 @@ const {
}
);
} catch (error) {
console.log('ERR:', error);
process.exitCode = 1;
console.log('ERR:', error);
process.exitCode = 1;
}
})();
7 changes: 7 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ exports.inputExistCheck = (input) =>
input.newsLink ? resolve(input.newsLink) : reject(Err_DontGetNewsLink);
});


//fileExitCheck in the path.
exports.fileExistCheck = (path) =>
new Promise((resolve, reject) => {
fs.existsSync(path)? resolve(true) : reject(false);
});

// Check the input parameters, and get the routing address of the article.
// - 原文网址:[原文标题](https://www.freecodecamp.org/news/xxxxxxx/
exports.getRouteAddr = (URL) =>
Expand Down

0 comments on commit f3df3d0

Please sign in to comment.