|
|
@@ -21,74 +21,79 @@ const APP_TYPE = process.argv[2];
|
|
|
|
|
|
const getFishes = () => {
|
|
|
console.info('scraping fishes...')
|
|
|
- scrapeCritters({url: AC_FISH, type: CONFIG.Fish})
|
|
|
+ return scrapeCritters({url: AC_FISH, type: CONFIG.Fish})
|
|
|
.then(saveImage(`${PUBLIC_PATH}/images`, 'fishes'))
|
|
|
.then(storeData(`${PUBLIC_PATH}/data/fishes.json`));
|
|
|
}
|
|
|
|
|
|
const getInsects = () => {
|
|
|
console.info('scraping insects...')
|
|
|
- scrapeCritters({url: AC_INSECT, type: CONFIG.Insect})
|
|
|
+ return scrapeCritters({url: AC_INSECT, type: CONFIG.Insect})
|
|
|
.then(saveImage(`${PUBLIC_PATH}/images`, 'insects'))
|
|
|
.then(storeData(`${PUBLIC_PATH}/data/insects.json`))
|
|
|
}
|
|
|
|
|
|
-const startApp = () => {
|
|
|
+const createPaths = () => {
|
|
|
try {
|
|
|
console.info('Creating required paths:');
|
|
|
['data', `images/insects`, `images/fishes`].map(path => {
|
|
|
console.info(`-- ${PUBLIC_PATH}/${path}`)
|
|
|
fs.mkdirSync(`${PUBLIC_PATH}/${path}`, {recursive: true})
|
|
|
- })
|
|
|
+ });
|
|
|
+ return Promise.resolve();
|
|
|
} catch (e) {
|
|
|
- console.error("Could not create required path folders: ", e);
|
|
|
- process.exit();
|
|
|
+ return Promise.reject(e)
|
|
|
}
|
|
|
- getFishes();
|
|
|
- getInsects();
|
|
|
}
|
|
|
|
|
|
-if (APP_TYPE === 'no-server') {
|
|
|
- try {
|
|
|
- startApp();
|
|
|
- } catch (e) {
|
|
|
- console.error("Scraper failed: ", e);
|
|
|
- process.exit();
|
|
|
- }
|
|
|
- console.info("All data downloaded");
|
|
|
- process.exit();
|
|
|
-}
|
|
|
-
|
|
|
-const app: express.Application = express();
|
|
|
-
|
|
|
-const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
|
|
|
- console.error(err);
|
|
|
- res.status(500);
|
|
|
- res.send({ error: err });
|
|
|
+const startApp = () => {
|
|
|
+ return createPaths().then(() => {
|
|
|
+ return Promise.all([getFishes(), getInsects()]).then(() => {
|
|
|
+ console.log("All data downloaded");
|
|
|
+ });
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-app.use(express.static(PUBLIC_PATH, {
|
|
|
- setHeaders: function setHeaders(res, path, stat) {
|
|
|
- res.header('Access-Control-Allow-Origin', '*');
|
|
|
- res.header('Access-Control-Allow-Methods', 'GET');
|
|
|
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
- }}))
|
|
|
-
|
|
|
-app.use((req, res, next)=> {
|
|
|
- res.header("Access-Control-Allow-Origin", "*");
|
|
|
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
-
|
|
|
- if ('OPTIONS' == req.method) {
|
|
|
- res.sendStatus(200);
|
|
|
- } else {
|
|
|
- next();
|
|
|
+if (APP_TYPE === '--batch') {
|
|
|
+ startApp().then(result => {
|
|
|
+ process.exit();
|
|
|
+ }).catch(error => {
|
|
|
+ console.error("Scraper failed: ", error);
|
|
|
+ process.exit();
|
|
|
+ })
|
|
|
+} else {
|
|
|
+ const app: express.Application = express();
|
|
|
+
|
|
|
+ const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
|
|
|
+ console.error(err);
|
|
|
+ res.status(500);
|
|
|
+ res.send({ error: err });
|
|
|
}
|
|
|
-})
|
|
|
-
|
|
|
-app.use(errorHandler);
|
|
|
-app.get('/', (req, res) => { res.send('Hello World!') });
|
|
|
-
|
|
|
-app.listen(PORT, () => {
|
|
|
- startApp();
|
|
|
- console.log(`All done, app listening at http://localhost:${PORT}`);
|
|
|
-});
|
|
|
+
|
|
|
+ app.use(express.static(PUBLIC_PATH, {
|
|
|
+ setHeaders: function setHeaders(res, path, stat) {
|
|
|
+ res.header('Access-Control-Allow-Origin', '*');
|
|
|
+ res.header('Access-Control-Allow-Methods', 'GET');
|
|
|
+ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
+ }}))
|
|
|
+
|
|
|
+ app.use((req, res, next)=> {
|
|
|
+ res.header("Access-Control-Allow-Origin", "*");
|
|
|
+ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
+
|
|
|
+ if ('OPTIONS' == req.method) {
|
|
|
+ res.sendStatus(200);
|
|
|
+ } else {
|
|
|
+ next();
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ app.use(errorHandler);
|
|
|
+ app.get('/', (req, res) => { res.send('Hello World!') });
|
|
|
+
|
|
|
+ app.listen(PORT, () => {
|
|
|
+ startApp().then(() => {
|
|
|
+ console.log(`All done, app listening at http://localhost:${PORT}`);
|
|
|
+ })
|
|
|
+ });
|
|
|
+}
|