-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblogsearch-crawler.ts
67 lines (61 loc) · 1.76 KB
/
blogsearch-crawler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
import * as process from 'process';
import template from './blogsearch.config.js.template';
import { UncheckedConfig } from './configTypes';
import checkConfig from './checkers';
import crawl from './crawler';
const arg = process.argv[2];
const configFile = arg || 'blogsearch.config.js';
const configPath = path.resolve(process.cwd(), configFile);
if (!fs.existsSync(configPath)) {
if (arg) {
console.error(`${configFile} doesn't exist.`);
} else {
fs.writeFileSync(configPath, template);
console.error(`
blogsearch.config.js doesn't exist.
An example blogsearch.config.js is created.
Visit https://github.com/kbumsik/blogsearch/tree/master/blogsearch-crawler
to learn how to configure blogsearch.config.js.
`);
}
process.exit(1);
}
crawlBlog(tryImport(configPath))
.then(_value => {
console.log('Parsing complete.');
process.exit(0);
})
.catch(error => {
console.error(error);
console.log('Parsing failed.');
process.exit(1);
});
// Load configuration
async function crawlBlog (uncheckedConfig: UncheckedConfig) {
try {
const config = checkConfig(uncheckedConfig);
// Override DB file
if (!fs.existsSync(path.dirname(config.output))) {
fs.mkdirSync(path.dirname(config.output));
}
if (fs.existsSync(config.output)) {
fs.unlinkSync(config.output);
}
await crawl(config);
} catch (error) {
throw new Error(`${error}.
See: https://github.com/kbumsik/blogsearch/tree/master/blogsearch`);
}
}
function tryImport (configPath: string) {
try {
return require(configPath);
} catch (error) {
console.error(error);
console.error(`Parsing ${configPath} failed. Is it a correct .js file?`);
process.exit(1);
}
}