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

Fixbug no file exist check #12

Merged
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
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async 函数里可以用同步的思维写

throw new Error('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);
});
Comment on lines +49 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要什么东西都封装成 Promise 异步的……

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我只是看到里面代码大部分是这样的, 就想保持语法风格的统一。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我只是看到里面代码大部分是这样的, 就想保持语法风格的统一。

不能形式大于内容,过多异步也拖性能。


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