diff --git a/frontend/.gitignore b/frontend/.gitignore index 2c591145e9941032e5652fa1a4a38efbd97220f3..ea4fafcc97822bbe0e4b29dc56d05b0d72638bbe 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -10,7 +10,7 @@ # next.js /.next/ -/out/ +/serverless/ # production /build diff --git a/frontend/.prettierignore b/frontend/.prettierignore index ca1a85b94250608f285557d997330c42296db7f9..809f1079114cc9ebd43616d790ac221ce54ed11d 100644 --- a/frontend/.prettierignore +++ b/frontend/.prettierignore @@ -1,4 +1,4 @@ dist -out +serverless wasm output diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 332978700b267390c1e17e4199387d4ba67e7434..628ddfc096d29208c2a1503962494286bc47430b 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -4,16 +4,17 @@ from node:12 WORKDIR /usr/src/app ENV NODE_ENV production +ENV PORT 8999 +ENV HOST 0.0.0.0 COPY package.json . COPY yarn.lock . -RUN npm i -g pm2 - -RUN yarn +RUN yarn install --frozen-lockfile COPY dist dist COPY public public +COPY ecosystem.config.js ecosystem.config.js EXPOSE 8999 -CMD ["pm2-runtime", "dist/server/index.js"] +CMD ["yarn", "start"] diff --git a/frontend/cli.js b/frontend/cli.js index 9c72723393ac99a9a72b752400c266b95b305665..a4e79912a94da0a55e80a09a0624d59236467c32 100755 --- a/frontend/cli.js +++ b/frontend/cli.js @@ -65,8 +65,7 @@ pm2.connect(err => { pm2.list((err, list) => { exitIfError(err, 2); - const appRoot = require('app-root-path'); - const ecosystem = require(appRoot + '/ecosystem.config'); + const ecosystem = require('./ecosystem.config'); const app = ecosystem.apps[0]; const host = argv.host || 'localhost'; diff --git a/frontend/ecosystem.config.js b/frontend/ecosystem.config.js index 274929e58c7e1ffc05523ff4ab7683dff490dae6..ae5d54edaaf6b09765b17cbcee01dc4cbe98c0e5 100644 --- a/frontend/ecosystem.config.js +++ b/frontend/ecosystem.config.js @@ -1,13 +1,11 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -const {resolve} = require('app-root-path'); - module.exports = { apps: [ { name: 'visualdl', script: 'dist/server/index.js', - cwd: resolve('/'), + cwd: __dirname, args: '', instances: 'max', autorestart: true, diff --git a/frontend/index.js b/frontend/index.js deleted file mode 100644 index 42b8fd527c16ca1a6cf55cf0dbe79790209cbcf5..0000000000000000000000000000000000000000 --- a/frontend/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require(__dirname + '/dist/server/index.js'); diff --git a/frontend/package.json b/frontend/package.json index 93f730f42e9e89e1f90a0d2fb08d65ff8ffad891..83e4e572b849c39afc4539d0623c3b0f0f745149 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "visualdl", - "version": "2.0.0-beta.11", + "version": "2.0.0-beta.14", "title": "VisualDL", "description": "A platform to visualize the deep learning process and result.", "keywords": [ @@ -30,20 +30,19 @@ "build:server": "tsc --project server/tsconfig.json", "build:wasm": "wasm-pack build --release --out-dir dist --out-name index wasm", "build": "./scripts/build.sh", - "export": "next export", - "start": "NODE_ENV=production node dist/server/index.js", + "export": "next export -o serverless", + "start": "pm2-runtime ecosystem.config.js", "lint": "tsc -p tsconfig.json --noEmit && tsc -p server/tsconfig.json --noEmit && eslint --ext .tsx,.jsx.ts,.js --ignore-path .gitignore .", "format": "prettier --write \"**/*.ts\" \"**/*.tsx\" \"**/*.js\" \"**/*.jsx\"", "test": "echo \"Error: no test specified\" && exit 0", - "prepublishOnly": "yarn lint && yarn test && ./scripts/prepare.sh", + "prepublishOnly": "yarn lint && yarn test && ./scripts/build.sh", "preversion": "yarn lint", - "version": "yarn format" + "version": "yarn format && git add -A" }, - "main": "index.js", "bin": "cli.js", "files": [ "dist", - "out", + "serverless", "public", "wasm/dist", "types", @@ -52,7 +51,6 @@ "ecosystem.config.js" ], "dependencies": { - "app-root-path": "3.0.0", "dagre-d3": "0.6.4", "detect-node": "2.0.4", "echarts": "4.6.0", diff --git a/frontend/scripts/build.sh b/frontend/scripts/build.sh index a5ef90ad68649d68cc2e6d9fbb31269ab3c18b7f..1628b73a5702095cfb8bef31569f4410137c4d0c 100755 --- a/frontend/scripts/build.sh +++ b/frontend/scripts/build.sh @@ -9,41 +9,47 @@ fi WORKING_PATH=`pwd` SERVER_DIR="dist" SERVER_DIR_PATH="$WORKING_PATH/$SERVER_DIR" -CLIENT_DIR="out" -CLIENT_DIR_PATH="$WORKING_PATH/$CLIENT_DIR" +SERVERLESS_DIR="serverless" +SERVERLESS_DIR_PATH="$WORKING_PATH/$SERVERLESS_DIR" OUTPUT="output" OUTPUT_PATH="$WORKING_PATH/$OUTPUT" -# generate dist -rm -rf $SERVER_DIR_PATH -mkdir -p $SERVER_DIR_PATH -rm -rf $CLIENT_DIR_PATH -mkdir -p $CLIENT_DIR_PATH +build_server() { + # generate dist + rm -rf $SERVER_DIR_PATH + mkdir -p $SERVER_DIR_PATH -# generate output -rm -rf $OUTPUT_PATH -mkdir -p $OUTPUT_PATH + # next build + yarn build:next -# next build -yarn build:next + # server build + yarn build:server -# server build -yarn build:server + # move static files + cp next.config.js $SERVER_DIR_PATH + cp package.json $SERVER_DIR_PATH + cp -r public $SERVER_DIR_PATH +} -# move static files -cp next.config.js $SERVER_DIR_PATH -cp package.json $SERVER_DIR_PATH +build_serverless() { + build_server -# package server files -(cd $SERVER_DIR_PATH && tar zcf $OUTPUT_PATH/server.tar.gz .) + # generate dist + rm -rf $SERVERLESS_DIR_PATH + mkdir -p $SERVERLESS_DIR_PATH -# export -# WARNING: export LAST!!! dist files will be deleted by next after export -yarn export + # next export + yarn export +} -# package client files -(cd $CLIENT_DIR_PATH && tar zcf $OUTPUT_PATH/client.tar.gz .) +# generate output +rm -rf $OUTPUT_PATH +mkdir -p $OUTPUT_PATH + +# package serverless files +PUBLIC_PATH="/app" API_URL="/api" build_serverless +(cd $SERVERLESS_DIR_PATH && tar zcf $OUTPUT_PATH/serverless.tar.gz .) -# clean -rm -rf $SERVER_DIR_PATH -rm -rf $CLIENT_DIR_PATH +# package server files +PUBLIC_PATH="" API_URL="/api" build_server +(cd $SERVER_DIR_PATH && tar zcf $OUTPUT_PATH/server.tar.gz .) diff --git a/frontend/scripts/docker.sh b/frontend/scripts/docker.sh index 31639d63ada78a9dea8e2a6bae287c7c66209bd4..471852231622972bf6fdbbf12efc2619f4ea1565 100755 --- a/frontend/scripts/docker.sh +++ b/frontend/scripts/docker.sh @@ -2,9 +2,6 @@ set -e -if [ ! -d dist ]; then - echo "Please build first!" - exit 1 -fi +./scripts/build.sh docker build -t paddlepaddle/visualdl . diff --git a/frontend/scripts/prepare.sh b/frontend/scripts/prepare.sh deleted file mode 100755 index 964a3b47cde6f2f2d1fc07242ed9dfff98813862..0000000000000000000000000000000000000000 --- a/frontend/scripts/prepare.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -set -e - -if [ -f "$HOME/.cargo/env" ]; then - source $HOME/.cargo/env -fi - -WORKING_PATH=`pwd` -SERVER_DIR="dist" -SERVER_DIR_PATH="$WORKING_PATH/$SERVER_DIR" - -# generate dist -rm -rf $SERVER_DIR_PATH -mkdir -p $SERVER_DIR_PATH - -# next build -yarn build:next - -# server build -yarn build:server - -# move static files -cp next.config.js $SERVER_DIR_PATH -cp package.json $SERVER_DIR_PATH diff --git a/frontend/server/index.ts b/frontend/server/index.ts index 785bcc084f242c79273e186f116fa503a767505a..fba0f154ab15c0011cba9547bcef141ed3a37188 100644 --- a/frontend/server/index.ts +++ b/frontend/server/index.ts @@ -15,7 +15,7 @@ const isDev = process.env.NODE_ENV !== 'production'; setConfig(config); const host = process.env.HOST || 'localhost'; -const port: string | number = Number.parseInt(process.env.PORT, 10) || 8999; +const port = Number.parseInt(process.env.PORT, 10) || 8999; const proxy = process.env.PROXY; const delay = Number.parseInt(process.env.DELAY, 10); @@ -23,7 +23,7 @@ const server = express(); const app = next({dev: isDev, conf: config}); const handle = app.getRequestHandler(); -(async () => { +async function start() { await app.prepare(); if (proxy) { @@ -59,6 +59,12 @@ const handle = app.getRequestHandler(); }); }); }); -})(); +} + +if (require.main === module) { + start(); +} + +export default start; export {server, app}; diff --git a/frontend/utils/i18next/config/create-config.ts b/frontend/utils/i18next/config/create-config.ts index b241c4a5d18b9ed7736eb23e98af4287aa48ab7b..610b47ebeff80e6f9634855a8a5be0f0274c64ac 100644 --- a/frontend/utils/i18next/config/create-config.ts +++ b/frontend/utils/i18next/config/create-config.ts @@ -32,6 +32,7 @@ export const createConfig = (userConfig: Config): Config => { if (isServer()) { const fs = eval("require('fs')"); const path = require('path'); // eslint-disable-line @typescript-eslint/no-var-requires + const projectRoot = process.cwd(); let serverLocalePath = localePath; /* @@ -40,14 +41,14 @@ export const createConfig = (userConfig: Config): Config => { */ if (typeof combinedConfig.defaultNS === 'string') { const defaultFile = `/${defaultLanguage}/${combinedConfig.defaultNS}.${localeExtension}`; - const defaultNSPath = path.join(process.cwd(), localePath, defaultFile); + const defaultNSPath = path.join(projectRoot, localePath, defaultFile); const defaultNSExists = fs.existsSync(defaultNSPath); if (!defaultNSExists) { /* If defaultNS doesn't exist, try to fall back to the deprecated static folder https://github.com/isaachinman/next-i18next/issues/523 */ - const staticDirPath = path.join(process.cwd(), STATIC_LOCALE_PATH, defaultFile); + const staticDirPath = path.join(projectRoot, STATIC_LOCALE_PATH, defaultFile); const staticDirExists = fs.existsSync(staticDirPath); if (staticDirExists) { @@ -67,8 +68,8 @@ export const createConfig = (userConfig: Config): Config => { Set server side backend */ combinedConfig.backend = { - loadPath: path.join(process.cwd(), `${serverLocalePath}/${localeStructure}.${localeExtension}`), - addPath: path.join(process.cwd(), `${serverLocalePath}/${localeStructure}.missing.${localeExtension}`) + loadPath: path.join(projectRoot, `${serverLocalePath}/${localeStructure}.${localeExtension}`), + addPath: path.join(projectRoot, `${serverLocalePath}/${localeStructure}.missing.${localeExtension}`) }; /* @@ -78,7 +79,7 @@ export const createConfig = (userConfig: Config): Config => { if (!combinedConfig.ns) { const getAllNamespaces = (p: string) => fs.readdirSync(p).map((file: string) => file.replace(`.${localeExtension}`, '')); - combinedConfig.ns = getAllNamespaces(path.join(process.cwd(), `${serverLocalePath}/${defaultLanguage}`)); + combinedConfig.ns = getAllNamespaces(path.join(projectRoot, `${serverLocalePath}/${defaultLanguage}`)); } } else { let clientLocalePath = localePath; diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 56f757d951d19f2d5dd0e3c405832e2f3306904f..5a1aa08374c508fa1431845441077f81985dba1c 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -1956,11 +1956,6 @@ anymatch@~3.1.1: normalize-path "^3.0.0" picomatch "^2.0.4" -app-root-path@3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-3.0.0.tgz#210b6f43873227e18a4b810a032283311555d5ad" - integrity sha512-qMcx+Gy2UZynHjOHOIXPNvpf+9cjvk3cWrBBK7zg4gH9+clobJRb9NGzcT7mQTcV/6Gm/1WelUtqxVXnNlrwcw== - aproba@^1.1.1: version "1.2.0" resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a" diff --git a/build.ps1 b/scripts/build.ps1 similarity index 100% rename from build.ps1 rename to scripts/build.ps1 diff --git a/scripts/build.sh b/scripts/build.sh index 7387f7e333e284d68a8395dd5418aedef2e6bf03..9192f5de1731e8fdbd5d81947dfd05a51a2b58f0 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -8,14 +8,20 @@ BUILD_DIR=$TOP_DIR/build mkdir -p $BUILD_DIR -build_frontend() { +build_frontend_from_source() { cd $FRONTEND_DIR PUBLIC_PATH="/app" API_URL="/api" ./scripts/build.sh } +build_frontend() { + local PACKAGE_NAME="visualdl" + local SRC=`npm view ${PACKAGE_NAME} dist.tarball` + wget $SRC -O "$BUILD_DIR/$PACKAGE_NAME.tar.gz" + tar zxf "$BUILD_DIR/$PACKAGE_NAME.tar.gz" -C "$BUILD_DIR" +} + build_frontend_fake() { - cd $FRONTEND_DIR - mkdir -p out + mkdir -p "$BUILD_DIR/package/serverless" } build_backend() { @@ -46,7 +52,7 @@ clean_env() { package() { mkdir -p $TOP_DIR/visualdl/server/dist - cp -rf $FRONTEND_DIR/out/* $TOP_DIR/visualdl/server/dist + cp -rf $BUILD_DIR/package/serverless/* $TOP_DIR/visualdl/server/dist cp $BUILD_DIR/visualdl/logic/core.so $TOP_DIR/visualdl cp $BUILD_DIR/visualdl/logic/core.so $TOP_DIR/visualdl/python/ } diff --git a/scripts/setup_dev_env.sh b/scripts/setup_dev_env.sh index 68d2e34f320c9c3cdb1f7539c8cf33b567198679..ace69f3c5247d7c5c1263a2110b4879f1440353b 100755 --- a/scripts/setup_dev_env.sh +++ b/scripts/setup_dev_env.sh @@ -3,7 +3,9 @@ set -e SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $SCRIPT_DIR/../frontend +cd $SCRIPT_DIR/.. +mkdir -p build +cd build which node >/dev/null 2>&1 if [ $? -ne 0 ]; then @@ -12,7 +14,7 @@ if [ $? -ne 0 ]; then fi echo "Setting up nodejs dependencies" -npm install --no-package-lock +npm install visualdl@latest --no-package-lock processors=1 if [ "$(uname)" == "Darwin" ]; then @@ -22,9 +24,6 @@ elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then fi echo "Building VisualDL SDK" -cd $SCRIPT_DIR/.. -mkdir -p build -cd build cmake .. make -j $processors diff --git a/scripts/start_dev_server.sh b/scripts/start_dev_server.sh index ae1c6e1765c551c7b3610537583ff1390014889d..01ca2ca6c7e2d090dacbd6237c615bcf4c9e76b3 100755 --- a/scripts/start_dev_server.sh +++ b/scripts/start_dev_server.sh @@ -41,16 +41,14 @@ CURRENT_DIR=`pwd` SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd $SCRIPT_DIR/../frontend export PYTHONPATH=$PYTHONPATH:"$SCRIPT_DIR/.." FRONTEND_PORT=8999 -PROXY="http://$HOST:$PORT" PUBLIC_PATH="/app" API_URL="/api" PORT=$FRONTEND_PORT yarn dev & -# Track pid -FRONTEND_PID=$! +VDL_BIN="./build/node_modules/.bin/visualdl" +$VDL_BIN start --port=$FRONTEND_PORT --host=$HOST --proxy="http://$HOST:$PORT" function finish { - kill -9 $FRONTEND_PID + $VDL_BIN stop } trap finish EXIT HUP INT QUIT PIPE TERM diff --git a/setup.py b/setup.py index b20ce455064bff127a39690b75868f1ff49ad444..b40dcec6b6c9adf1b3dca044873513a503bbfe71 100644 --- a/setup.py +++ b/setup.py @@ -75,9 +75,9 @@ class BaseCommand(setuptools.Command): class build_py(setuptools.command.build_py.build_py): def run(self): - cmd = ['bash', 'build.sh'] + cmd = ['bash', 'scripts/build.sh'] if platform == "win32": - cmd = ['powershell', '-NoProfile', './build.ps1'] + cmd = ['powershell', '-NoProfile', './scripts/build.ps1'] env = dict(os.environ) if MODE == "travis-CI": cmd.append('travis-CI')