ソースを参照

Add build task. Create build.json for production

Tatiana Inama 6 年 前
コミット
bc78c291c0
4 ファイル変更27 行追加4 行削除
  1. 10 0
      build.json
  2. 1 1
      scraper/nodemon.json
  3. 1 1
      scraper/package.json
  4. 15 2
      scraper/src/server.ts

+ 10 - 0
build.json

@@ -0,0 +1,10 @@
+{
+  "CritterCrossing": {
+          "dir": "app",
+          "artifacts": ["build/"]
+  },
+  "CritterScraper": {
+          "dir": "scraper",
+          "artifacts": ["build/"]
+  }
+}

+ 1 - 1
scraper/nodemon.json

@@ -2,5 +2,5 @@
   "watch": ["src"],
   "ext": "ts",
   "ignore": ["src/**/*.spec.ts"],
-  "exec": "ts-node ./server.ts"
+  "exec": "ts-node ./src/server.ts"
 }

+ 1 - 1
scraper/package.json

@@ -6,7 +6,7 @@
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "nodemon",
-    "build": "rm -rf ./build & tsc"
+    "build": "rm -rf ./build & tsc --p ./tsconfig.json"
   },
   "author": "Tatiana Inama",
   "license": "ISC",

+ 15 - 2
scraper/src/server.ts

@@ -1,8 +1,11 @@
 import express from 'express';
 import dotenv from 'dotenv'
+import fs from 'fs';
 import { saveImage, scrapeCritters, CONFIG, storeData } from './scraper';
 
 const app: express.Application = express();
+const publicDataPath = `${__dirname}/../public/data`;
+const publicImagesPath = `${__dirname}/../public/images`;
 
 dotenv.config();
 
@@ -11,6 +14,12 @@ if (process.env.AC_FISH === undefined || process.env.AC_INSECT === undefined) {
   process.exit();
 }
 
+const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
+  console.error(err);
+  res.status(500);
+  res.send({ error: err });
+}
+
 app.use(express.static('public', {
   setHeaders: function setHeaders(res, path, stat) {
     res.header('Access-Control-Allow-Origin', '*');
@@ -29,18 +38,22 @@ app.use((req, res, next)=> {
   }
 })
 
+app.use(errorHandler);
 app.get('/', (req, res) => { res.send('Hello World!') });
 
 
 const getFishes = () => {
-  scrapeCritters({url: process.env.AC_FISH!, type: CONFIG.Fish}).then(saveImage('images/fishes/')).then(storeData(`${__dirname}/public/data/fishes.json`));
+  scrapeCritters({url: process.env.AC_FISH!, type: CONFIG.Fish}).then(saveImage('images/fishes/')).then(storeData(`${publicDataPath}/fishes.json`));
 }
 
 const getInsects = () => {
-  scrapeCritters({url:process.env.AC_INSECT!, type: CONFIG.Insect}).then(saveImage('images/insects/')).then(storeData(`${__dirname}/public/data/insects.json`))
+  scrapeCritters({url:process.env.AC_INSECT!, type: CONFIG.Insect}).then(saveImage('images/insects/')).then(storeData(`${publicDataPath}/insects.json`))
 }
 
 app.listen(process.env.PORT, () => {
+  [publicDataPath, publicImagesPath].map(path => {
+    fs.mkdirSync(path, {recursive: true})
+  })
   getFishes();
   getInsects();
   console.log(`Scrapper app listening at http://localhost:${process.env.PORT}`);