diff --git a/.eslintrc.js b/.eslintrc.js index f4e5ed0..b2333d1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,7 +15,6 @@ module.exports = { } }, rules: { - 'import/no-extraneous-dependencies': ['error', { peerDependencies: true }], 'prettier/prettier': 'error', 'react/jsx-filename-extension': ['warn', { extensions: ['.ts', '.tsx'] }], // 修复 tsx 文件引用 tsx 文件报错的问题 "no-var": 0,//禁用var,用let和const代替 diff --git a/packages/cli/package.json b/packages/cli/package.json index 0137d24..4a5e028 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -11,8 +11,7 @@ "compile" ], "scripts": { - "build:editor": "node src/cmd/editor/build.js", - "postinstall": "npm run build:editor" + "build:editor": "node src/cmd/editor/build.js" }, "repository": { "type": "git", @@ -22,7 +21,7 @@ "license": "MIT", "dependencies": { "@imove/compile-code": "^0.1.0", - "@imove/core": "^0.3.0", + "@imove/core": "^0.3.9", "body-parser": "^1.19.0", "commander": "^6.0.0", "cors": "^2.8.5", @@ -31,6 +30,7 @@ "eventemitter3": "^4.0.7", "express": "^4.17.1", "fs-extra": "^9.0.1", - "lowdb": "^1.0.0" + "lowdb": "^1.0.0", + "open": "^8.2.1" } } diff --git a/packages/cli/src/cmd/dev/index.js b/packages/cli/src/cmd/dev/index.js index 6d1ea81..1ced60c 100644 --- a/packages/cli/src/cmd/dev/index.js +++ b/packages/cli/src/cmd/dev/index.js @@ -1,11 +1,14 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ const path = require('path'); const fs = require('fs-extra'); const Base = require('../base'); const mergePkg = require('./mergePkg'); const { compileForProject } = require('@imove/compile-code'); -const { createServer } = require('../../utils/server'); +const { DEFAULT_PORT, createServer } = require('../../utils/server'); -const noop = () => {}; +const noop = function () { + /* noop function */ +}; const CACHE_PATH = path.join(process.cwd(), './.cache'); const CACHE_DSL_FILE = path.join(CACHE_PATH, 'imove.dsl.json'); @@ -56,7 +59,7 @@ class Dev extends Base { } run() { - const app = createServer(); + const app = createServer(DEFAULT_PORT); app.post('/api/save', this.save.bind(this)); app.get('/api/connect', this.connect.bind(this)); } diff --git a/packages/cli/src/cmd/editor/index.js b/packages/cli/src/cmd/editor/index.js index 4101635..8b088d9 100644 --- a/packages/cli/src/cmd/editor/index.js +++ b/packages/cli/src/cmd/editor/index.js @@ -5,7 +5,7 @@ const low = require('lowdb'); const FileSync = require('lowdb/adapters/FileSync'); const express = require('express'); const Base = require('../base'); -const { createServer } = require('../../utils/server'); +const { DEFAULT_PORT, createServer } = require('../../utils/server'); class Editor extends Base { constructor(...args) { @@ -69,7 +69,7 @@ class Editor extends Base { } run() { - const server = createServer(); + const server = createServer(DEFAULT_PORT, true); server.use(express.static(path.join(__dirname, './template'))); server.post('/api/queryGraph', this.queryGraph.bind(this)); server.post('/api/modifyGraph', this.modifyGraph.bind(this)); diff --git a/packages/cli/src/cmd/init/index.js b/packages/cli/src/cmd/init/index.js index 58fa05c..3708bbc 100644 --- a/packages/cli/src/cmd/init/index.js +++ b/packages/cli/src/cmd/init/index.js @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ const path = require('path'); const fs = require('fs-extra'); const Base = require('../base'); diff --git a/packages/cli/src/utils/server/index.js b/packages/cli/src/utils/server/index.js index 3cd7a1e..4ba2efc 100644 --- a/packages/cli/src/utils/server/index.js +++ b/packages/cli/src/utils/server/index.js @@ -1,10 +1,21 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ const express = require('express'); const bodyParser = require('body-parser'); +const open = require('open'); +const DEFAULT_PORT = 3500; const cachedServer = {}; -const createServer = (port = 3500) => { +function openBrowserIfNeeded(url, needOpen = false) { + if (needOpen) { + open(url); + } +} + +const createServer = (port = DEFAULT_PORT, needOpen = false) => { + const url = `http://127.0.0.1:${port}`; if (cachedServer[port]) { + openBrowserIfNeeded(url, needOpen); return cachedServer[port]; } const app = express(); @@ -21,11 +32,13 @@ const createServer = (port = 3500) => { next(); }); app.listen(port, () => { - console.log(`server starts successfully at ${port}!`); + console.log(`server starts at ${url}`); + openBrowserIfNeeded(url, needOpen); }); return app; }; module.exports = { + DEFAULT_PORT, createServer, };