Skip to content

Commit

Permalink
Merge pull request #142 from tanliyuan/feature/test_tag
Browse files Browse the repository at this point in the history
后端js改写成ts;优化简书发布;
  • Loading branch information
tanliyuan authored Mar 4, 2021
2 parents b63493c + 547455a commit 78257af
Show file tree
Hide file tree
Showing 59 changed files with 400 additions and 316 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/node_modules
./extensions
**/dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ yarn.lock
package-lock.json
.umi/
dist/
.history
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM jelastic/nodejs:15.9.0-npm AS frontend

WORKDIR /app
ADD ./frontend /app
RUN npm install
RUN npm install --registry=https://registry.npm.taobao.org
RUN npm run build

FROM jelastic/nodejs:15.9.0-npm
Expand All @@ -11,7 +11,8 @@ COPY --from=frontend /app/dist /frontend
WORKDIR /app
ADD ./backend ./docker_init.sh ./nginx /app/
RUN cp /app/artipub.conf /etc/nginx/conf.d/artipub.conf
RUN npm install
RUN npm install --registry=https://registry.npm.taobao.org
RUN npm run build

EXPOSE 3000 8000
CMD /app/docker_init.sh
Expand Down
16 changes: 0 additions & 16 deletions Dockerfile-local

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ArtiPub 提供 3 种安装方式如下。
version: "3.3"
services:
app:
image: "tanliyuan123/artipub:1.1.0"
image: "tanliyuan123/artipub:1.2.0"
environment:
MONGO_HOST: "mongo"
# MONGO_USERNAME: root
Expand Down Expand Up @@ -122,7 +122,7 @@ docker-compose up
如果你本地已有启动的mongodb容器,不想用上面方式再起一个。其中 `goofy_ganguly` 为本地已启动的 mongodb 容器名, 替换成你本地的即可。

```bash
docker run --rm -it --link goofy_ganguly -p 3000:3000/tcp -p 8000:8000/tcp tanliyuan123/artipub:1.0
docker run --rm -it --link goofy_ganguly -p 3000:3000/tcp -p 8000:8000/tcp tanliyuan123/artipub:1.2.0
```

### 通过 npm 包安装
Expand Down
1 change: 1 addition & 0 deletions backend/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
8 changes: 4 additions & 4 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

```bash
//全局安装
npm i -g artipub-backend
npm i -g artipub-backend --production

//默认启动 http://localhost:3000, 确保本地 mongodb 已经启动在 27017 端口
artipub-be start

//查看其他配置参数
artipub-be --help
artipub-be start --help
```

```bash
//非全局安装
npm i artipub-backend
npm i artipub-backend --production

//安装目录下执行, 确保npm 5+, 有npx命令
npx artipub-be start
Expand All @@ -25,5 +25,5 @@ npx artipub-be start
./node_modules/.bin/artipub-be start

//查看其他配置参数
npx artipub-be --help
npx artipub-be start --help
```
4 changes: 2 additions & 2 deletions backend/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ program
process.env.MONGO_USERNAME = username
process.env.MONGO_PASSWORD = password

// 开启后段服务
require('./server')
// 开启后端服务
require('./dist/server')
})

program.parse(process.argv)
2 changes: 1 addition & 1 deletion backend/config.js → backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const addr = mongodbContainerAddr ? mongodbContainerAddr[1] : '127.0.0.1';
const mongodbContainerPort = Object.entries(process.env).find((entry) => entry[0].endsWith('_TCP_PORT'));
const port = mongodbContainerPort ? mongodbContainerPort[1] : '27017';

module.exports = {
export default {
HOST: '0.0.0.0',
PORT: 3000,
MONGO_HOST: process.env.MONGO_HOST ? process.env.MONGO_HOST : addr,
Expand Down
2 changes: 1 addition & 1 deletion backend/constants.js → backend/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
platform: {
JUEJIN: 'juejin',
SEGMENTFAULT: 'segmentfault',
Expand Down
4 changes: 2 additions & 2 deletions backend/data.js → backend/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const constants = require('./constants')
module.exports = {
import constants from './constants';
export default {
// 平台
platforms: [
{
Expand Down
11 changes: 6 additions & 5 deletions backend/exec.js → backend/exec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import config from './config'
const mongoose = require('mongoose')
const CronJob = require('cron').CronJob
const AsyncLock = require('async-lock')
const constants = require('./constants')
const models = require('./models')
const config = require('./config')
const logger = require('./logger')
import constants from './constants'
import models from './models'
import logger from './logger'
const ArticlePublisher = require('./lib/ArticlePublisher')
const StatsFetcher = require('./lib/StatsFetcher')

Expand Down Expand Up @@ -79,6 +80,6 @@ class Runner {
}
}

module.exports = {
export default {
Runner,
}
23 changes: 23 additions & 0 deletions backend/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
declare const UE: any;

declare module 'puppeteer-chromium-resolver' {
import * as Puppeteer from 'puppeteer-core';

interface RevisionInfo {
puppeteer: typeof Puppeteer
executablePath: string
revision: string
folderPath: string
url: string
local: string
product: any
}

function PCR(option: object): RevisionInfo
namespace PCR {
function getStats(): any
}

export = PCR;
}

6 changes: 3 additions & 3 deletions backend/init.js → backend/init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const models = require('./models')
const data = require('./data')
import models from './models'
import data from './data'

// 数据库初始化
const init = async () => {
Expand Down Expand Up @@ -45,4 +45,4 @@ const init = async () => {
await models.Cookie.deleteMany({ domain: { $regex: '.juejin.im' } });
}

module.exports = init
export default init
7 changes: 0 additions & 7 deletions backend/jsconfig.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const models = require('../models')
const logger = require('../logger')
const constants = require('../constants')
const BaseExecutor = require('./BaseExecutor')
import models from '../models'
import logger from '../logger'
import constants from '../constants'
import BaseExecutor = require ( './BaseExecutor' )

class ArticlePublisher extends BaseExecutor {
async run() {
Expand Down Expand Up @@ -45,7 +45,7 @@ class ArticlePublisher extends BaseExecutor {
task.error = e.toString()
task.updateTs = new Date()
await task.save()
console.error(e)
logger.error(e)
}finally{
if(this.spider.browser){
const pages = await this.spider.browser.pages();
Expand All @@ -57,4 +57,4 @@ class ArticlePublisher extends BaseExecutor {
}
}

module.exports = ArticlePublisher
export = ArticlePublisher
12 changes: 7 additions & 5 deletions backend/lib/BaseExecutor.js → backend/lib/BaseExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const spiders = require('../spiders')
const models = require('../models')
const constants = require('../constants')
import models from '../models'
import logger from '../logger'

class BaseExecutor {
task: any
platform: any
spider: any
constructor(task) {
this.task = task
this.platform = undefined
Expand All @@ -17,7 +19,7 @@ class BaseExecutor {
const spiderName = this.platform.name

const Spider = require(`../spiders/${spiderName}`)
console.log(Spider)
logger.info(Spider)
this.spider = new Spider(task._id)
}

Expand All @@ -31,4 +33,4 @@ class BaseExecutor {
}
}

module.exports = BaseExecutor
export = BaseExecutor
11 changes: 4 additions & 7 deletions backend/lib/StatsFetcher.js → backend/lib/StatsFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const models = require('../models')
const logger = require('../logger')
const spiders = require('../spiders')
const constants = require('../constants')
const BaseExecutor = require('./BaseExecutor')
import BaseExecutor = require('./BaseExecutor')
import logger from '../logger'

class StatsFetcher extends BaseExecutor {
async run() {
if (this.spider) {
try{
await this.spider.runFetchStats()
}catch(e){
console.error(e)
logger.error(e)
}finally{
if(this.spider.browser){
const pages = await this.spider.browser.pages();
Expand All @@ -22,4 +19,4 @@ class StatsFetcher extends BaseExecutor {
}
}

module.exports = StatsFetcher
export = StatsFetcher
4 changes: 2 additions & 2 deletions backend/logger.js → backend/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const log4js = require('log4js')
import log4js = require ('log4js')

// 日志配置
const logger = log4js.getLogger()
Expand All @@ -14,4 +14,4 @@ log4js.configure({
});
logger.level = 'debug'

module.exports = logger
export default logger
2 changes: 1 addition & 1 deletion backend/models/article.js → backend/models/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ const articleSchema = new mongoose.Schema({

const Article = mongoose.model('articles', articleSchema)

module.exports = Article
export = Article
2 changes: 1 addition & 1 deletion backend/models/cookie.js → backend/models/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ const cookieSchema = new mongoose.Schema({

const Cookie = mongoose.model('cookies', cookieSchema)

module.exports = Cookie
export = Cookie
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const environmentSchema = new mongoose.Schema({

const Environment = mongoose.model('environments', environmentSchema)

module.exports = Environment
export = Environment
2 changes: 1 addition & 1 deletion backend/models/index.js → backend/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
Article: require('./article'),
Task: require('./task'),
Platform: require('./platform'),
Expand Down
2 changes: 1 addition & 1 deletion backend/models/platform.js → backend/models/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const platformSchema = new mongoose.Schema({

const Platform = mongoose.model('platforms', platformSchema)

module.exports = Platform
export = Platform
2 changes: 1 addition & 1 deletion backend/models/task.js → backend/models/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ const taskSchema = new mongoose.Schema({

const Task = mongoose.model('tasks', taskSchema)

module.exports = Task
export = Task
3 changes: 1 addition & 2 deletions backend/models/token.js → backend/models/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const mongoose = require('mongoose')
const ObjectId = require('bson').ObjectId

const tokenSchema = new mongoose.Schema({
accessToken: String,
Expand All @@ -11,4 +10,4 @@ const tokenSchema = new mongoose.Schema({

const Token = mongoose.model('tokens', tokenSchema)

module.exports = Token
export = Token
21 changes: 17 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "artipub-backend",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "ts-node server.ts",
"build": "tsc",
"prod": "node ./dist/server.js",
"prerelease": "npm version prerelease --preid=alpha"
},
"author": "Marvin Zhang",
Expand All @@ -18,22 +20,33 @@
"bin": {
"artipub-be": "cli.js"
},
"files": [
"dist/",
"cli.js"
],
"license": "ISC",
"dependencies": {
"async-lock": "^1.2.2",
"axios": "^0.20.0",
"axios": "^0.21.1",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"clipboardy": "^2.1.0",
"commander": "^7.1.0",
"cron": "^1.7.1",
"express": "^4.17.1",
"log4js": "^5.1.0",
"mongoose": "^5.6.12",
"mongoose": "^5.11.18",
"morgan": "^1.9.1",
"puppeteer-chromium-resolver": "^5.2.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"showdown": "^1.9.0"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^14.14.31",
"@types/puppeteer-core": "^5.4.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.2"
}
}
Loading

0 comments on commit 78257af

Please sign in to comment.