Browse Source

Merge pull request #116 from csbun/feature/cli-with-editor

open browser after cli editor started
master
Yang Pei 4 years ago
committed by GitHub
parent
commit
9a5c40e3be
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .eslintrc.js
  2. 1
      .gitignore
  3. 13
      package.json
  4. 7
      packages/cli/package.json
  5. 9
      packages/cli/src/cmd/dev/index.js
  6. 11
      packages/cli/src/cmd/editor/build.js
  7. 4
      packages/cli/src/cmd/editor/index.js
  8. 1
      packages/cli/src/cmd/init/index.js
  9. 17
      packages/cli/src/utils/server/index.js

1
.eslintrc.js

@ -15,7 +15,6 @@ module.exports = {
} }
}, },
rules: { rules: {
'import/no-extraneous-dependencies': ['error', { peerDependencies: true }],
'prettier/prettier': 'error', 'prettier/prettier': 'error',
'react/jsx-filename-extension': ['warn', { extensions: ['.ts', '.tsx'] }], // 修复 tsx 文件引用 tsx 文件报错的问题 'react/jsx-filename-extension': ['warn', { extensions: ['.ts', '.tsx'] }], // 修复 tsx 文件引用 tsx 文件报错的问题
"no-var": 0,//禁用var,用let和const代替 "no-var": 0,//禁用var,用let和const代替

1
.gitignore

@ -2,6 +2,7 @@
.vscode .vscode
.eslintcache .eslintcache
.size-snapshot.json .size-snapshot.json
.husky
npm-debug.log* npm-debug.log*
yarn-error.log* yarn-error.log*
lerna-debug.log lerna-debug.log

13
package.json

@ -2,17 +2,14 @@
"name": "root", "name": "root",
"private": true, "private": true,
"scripts": { "scripts": {
"lint": "eslint --cache --fix --ext .ts,tsx,js,jsx ./packages/**/src > ./.eslint-error.log; exit 0",
"test": "jest --coverage", "test": "jest --coverage",
"e2e": "cypress open", "e2e": "cypress open",
"doc": "dumi dev", "doc": "dumi dev",
"start": "concurrently \"lerna run watch --parallel\" \"cross-env APP_ROOT=example umi dev\"", "start": "concurrently \"lerna run watch --parallel\" \"cross-env APP_ROOT=example umi dev\"",
"example": "concurrently \"lerna run watch --parallel\" \"cross-env APP_ROOT=example umi dev\" \"imove -d\"", "example": "concurrently \"lerna run watch --parallel\" \"cross-env APP_ROOT=example umi dev\" \"imove -d\"",
"postinstall": "lerna init && lerna bootstrap && npm link packages/cli" "postinstall": "lerna init && lerna bootstrap && npm link packages/cli",
"prepare": "husky install"
}, },
"pre-commit": [
"lint"
],
"jest": { "jest": {
"projects": [ "projects": [
"config/cli.jest.config.js", "config/cli.jest.config.js",
@ -37,12 +34,13 @@
"eslint-config-prettier": "^7.2.0", "eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1", "eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0", "eslint-plugin-react": "^7.22.0",
"husky": "^7.0.1",
"lerna": "^3.22.1", "lerna": "^3.22.1",
"less": "^3.12.2", "less": "^3.12.2",
"lint-staged": "^11.0.1",
"lowdb": "^1.0.0", "lowdb": "^1.0.0",
"ora": "^4.1.1", "ora": "^4.1.1",
"postcss": "^8.2.1", "postcss": "^8.2.1",
"pre-commit": "^1.2.2",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"rollup": "^2.6.1", "rollup": "^2.6.1",
"rollup-plugin-postcss": "^4.0.0", "rollup-plugin-postcss": "^4.0.0",
@ -53,5 +51,8 @@
"typescript": "^4.1.3", "typescript": "^4.1.3",
"umi": "^3.3.3", "umi": "^3.3.3",
"watch": "^1.0.2" "watch": "^1.0.2"
},
"lint-staged": {
"packages/**/*.{ts,tsx,js,jsx}": "eslint --cache --fix"
} }
} }

7
packages/cli/package.json

@ -12,7 +12,7 @@
], ],
"scripts": { "scripts": {
"build:editor": "node src/cmd/editor/build.js", "build:editor": "node src/cmd/editor/build.js",
"postinstall": "npm run build:editor" "prepare": "npm run build:editor"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -22,7 +22,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@imove/compile-code": "^0.1.0", "@imove/compile-code": "^0.1.0",
"@imove/core": "^0.3.0", "@imove/core": "^0.3.9",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"commander": "^6.0.0", "commander": "^6.0.0",
"cors": "^2.8.5", "cors": "^2.8.5",
@ -31,6 +31,7 @@
"eventemitter3": "^4.0.7", "eventemitter3": "^4.0.7",
"express": "^4.17.1", "express": "^4.17.1",
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"lowdb": "^1.0.0" "lowdb": "^1.0.0",
"open": "^8.2.1"
} }
} }

9
packages/cli/src/cmd/dev/index.js

@ -1,11 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const Base = require('../base'); const Base = require('../base');
const mergePkg = require('./mergePkg'); const mergePkg = require('./mergePkg');
const { compileForProject } = require('@imove/compile-code'); 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_PATH = path.join(process.cwd(), './.cache');
const CACHE_DSL_FILE = path.join(CACHE_PATH, 'imove.dsl.json'); const CACHE_DSL_FILE = path.join(CACHE_PATH, 'imove.dsl.json');
@ -56,7 +59,7 @@ class Dev extends Base {
} }
run() { run() {
const app = createServer(); const app = createServer(DEFAULT_PORT);
app.post('/api/save', this.save.bind(this)); app.post('/api/save', this.save.bind(this));
app.get('/api/connect', this.connect.bind(this)); app.get('/api/connect', this.connect.bind(this));
} }

11
packages/cli/src/cmd/editor/build.js

@ -1,12 +1,21 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path'); const path = require('path');
const fs = require('fs-extra');
const { lessLoader } = require('esbuild-plugin-less'); const { lessLoader } = require('esbuild-plugin-less');
const outfile = path.join(__dirname, 'template/dist/app.bundle.js');
// if builded and NOT force re-build, exit
if (fs.existsSync(outfile) && process.argv.indexOf('--force') < 0) {
console.log('imove editor alredy builded');
return;
}
require('esbuild') require('esbuild')
.build({ .build({
bundle: true, bundle: true,
entryPoints: [path.join(__dirname, 'template/app.jsx')], entryPoints: [path.join(__dirname, 'template/app.jsx')],
outfile: path.join(__dirname, 'template/dist/app.bundle.js'), outfile,
plugins: [ plugins: [
// fix import('antd/dist/antd.less') // fix import('antd/dist/antd.less')
{ {

4
packages/cli/src/cmd/editor/index.js

@ -5,7 +5,7 @@ const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync'); const FileSync = require('lowdb/adapters/FileSync');
const express = require('express'); const express = require('express');
const Base = require('../base'); const Base = require('../base');
const { createServer } = require('../../utils/server'); const { DEFAULT_PORT, createServer } = require('../../utils/server');
class Editor extends Base { class Editor extends Base {
constructor(...args) { constructor(...args) {
@ -69,7 +69,7 @@ class Editor extends Base {
} }
run() { run() {
const server = createServer(); const server = createServer(DEFAULT_PORT, true);
server.use(express.static(path.join(__dirname, './template'))); server.use(express.static(path.join(__dirname, './template')));
server.post('/api/queryGraph', this.queryGraph.bind(this)); server.post('/api/queryGraph', this.queryGraph.bind(this));
server.post('/api/modifyGraph', this.modifyGraph.bind(this)); server.post('/api/modifyGraph', this.modifyGraph.bind(this));

1
packages/cli/src/cmd/init/index.js

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const Base = require('../base'); const Base = require('../base');

17
packages/cli/src/utils/server/index.js

@ -1,10 +1,21 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const open = require('open');
const DEFAULT_PORT = 3500;
const cachedServer = {}; 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]) { if (cachedServer[port]) {
openBrowserIfNeeded(url, needOpen);
return cachedServer[port]; return cachedServer[port];
} }
const app = express(); const app = express();
@ -21,11 +32,13 @@ const createServer = (port = 3500) => {
next(); next();
}); });
app.listen(port, () => { app.listen(port, () => {
console.log(`server starts successfully at ${port}!`); console.log(`server starts at ${url}`);
openBrowserIfNeeded(url, needOpen);
}); });
return app; return app;
}; };
module.exports = { module.exports = {
DEFAULT_PORT,
createServer, createServer,
}; };

Loading…
Cancel
Save