后台相关的命令以及web编译过程中对oak:开头的jsx的处理
This commit is contained in:
parent
298cdf875b
commit
bf98e82da0
|
|
@ -0,0 +1,114 @@
|
||||||
|
const t = require('@babel/types');
|
||||||
|
const pull = require('lodash/pull');
|
||||||
|
const { assert } = require('console');
|
||||||
|
|
||||||
|
function isOakNamespaceIdentifier(node, name) {
|
||||||
|
if (t.isJSXNamespacedName(node) && t.isJSXIdentifier(node.namespace) && node.namespace.name === 'oak'
|
||||||
|
&& t.isJSXIdentifier(node.name) && node.name.name === name) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = (babel) => {
|
||||||
|
return {
|
||||||
|
visitor: {
|
||||||
|
JSXAttribute(path, state) {
|
||||||
|
const node = path.node;
|
||||||
|
|
||||||
|
if (isOakNamespaceIdentifier(node.name, 'path')) {
|
||||||
|
// 若存在oak:path,则注入oakParent={this.state.oakFullpath}和oakPath={oak:path}
|
||||||
|
assert(t.isJSXOpeningElement(path.parent));
|
||||||
|
const { attributes } = path.parent;
|
||||||
|
|
||||||
|
const parentAttr = attributes.find(
|
||||||
|
(ele) => t.isJSXIdentifier(ele.name) && ele.name.name === 'oakParent'
|
||||||
|
);
|
||||||
|
if (parentAttr) {
|
||||||
|
console.warn(`「${state.filename}」有JSX元素同时定义了oak:path和oakParent,请确保oakParent等于{this.state.oakFullpath}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.push(
|
||||||
|
t.jsxAttribute(
|
||||||
|
t.jsxIdentifier("oakParent"),
|
||||||
|
t.jsxExpressionContainer(
|
||||||
|
t.memberExpression(
|
||||||
|
t.memberExpression(
|
||||||
|
t.thisExpression(),
|
||||||
|
t.identifier("state")
|
||||||
|
),
|
||||||
|
t.identifier("oakFullpath")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathAttr = attributes.find(
|
||||||
|
(ele) => t.isJSXIdentifier(ele.name) && ele.name.name === 'oakPath'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (pathAttr) {
|
||||||
|
console.warn(`「${state.filename}」有JSX元素同时定义了oak:path和oakPath,请确保两者相等`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.push(
|
||||||
|
t.jsxAttribute(
|
||||||
|
t.jsxIdentifier("oakPath"),
|
||||||
|
node.value
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
path.remove();
|
||||||
|
}
|
||||||
|
else if (isOakNamespaceIdentifier(node.name, 'value')) {
|
||||||
|
// 如果是oak:value,增加value和data-attr属性
|
||||||
|
assert(t.isJSXOpeningElement(path.parent));
|
||||||
|
assert(t.isStringLiteral(node.value));
|
||||||
|
const { attributes } = path.parent;
|
||||||
|
|
||||||
|
const valueAttr = attributes.find(
|
||||||
|
(ele) => t.isJSXIdentifier(ele.name) && ele.name.name === 'value'
|
||||||
|
);
|
||||||
|
if (valueAttr) {
|
||||||
|
console.warn(`「${state.filename}」有JSX元素同时定义了oak:value和value,请确保value等于{this.state["oak:value"]}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.push(
|
||||||
|
t.jsxAttribute(
|
||||||
|
t.jsxIdentifier("value"),
|
||||||
|
t.jsxExpressionContainer(
|
||||||
|
t.memberExpression(
|
||||||
|
t.memberExpression(
|
||||||
|
t.thisExpression(),
|
||||||
|
t.identifier("state")
|
||||||
|
),
|
||||||
|
t.identifier(node.value.value)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataAttrAttr = attributes.find(
|
||||||
|
(ele) => t.isJSXIdentifier(ele.name) && ele.name.name === 'data-attr'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (dataAttrAttr) {
|
||||||
|
assert(t.isStringLiteral(dataAttrAttr.value) && dataAttrAttr.value.value === node.value.value, `「${state.filename}」中有JSX元素同时定义了oak:value和data-attr,且两者的值不相等`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.push(
|
||||||
|
t.jsxAttribute(
|
||||||
|
t.jsxIdentifier("data-attr"),
|
||||||
|
node.value
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
path.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const { resolve, relative } = require('path');
|
const { resolve, relative } = require('path');
|
||||||
|
|
||||||
module.exports = function (content) {
|
module.exports = function (content) {
|
||||||
const options = this.getOptions() || {}; //获取配置参数
|
/* const options = this.getOptions() || {}; //获取配置参数
|
||||||
const { context: projectContext } = options; // context 本项目路径
|
const { context: projectContext } = options; // context 本项目路径
|
||||||
const {
|
const {
|
||||||
options: webpackLegacyOptions,
|
options: webpackLegacyOptions,
|
||||||
|
|
@ -17,7 +17,7 @@ module.exports = function (content) {
|
||||||
if (/.tsx|.jsx/.test(resourcePath)) {
|
if (/.tsx|.jsx/.test(resourcePath)) {
|
||||||
// console.log(content);
|
// console.log(content);
|
||||||
|
|
||||||
}
|
} */
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,4 +1,5 @@
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
|
/// <reference types="node" />
|
||||||
import { PathLike } from 'fs';
|
import { PathLike } from 'fs';
|
||||||
import { checkFileExistsAndCreateType } from './enum';
|
import { checkFileExistsAndCreateType } from './enum';
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ const commander_1 = __importDefault(require("commander"));
|
||||||
const create_1 = require("./create");
|
const create_1 = require("./create");
|
||||||
const build_1 = __importDefault(require("./build"));
|
const build_1 = __importDefault(require("./build"));
|
||||||
const make_1 = __importDefault(require("./make"));
|
const make_1 = __importDefault(require("./make"));
|
||||||
|
const run_1 = __importDefault(require("./run"));
|
||||||
const config_1 = require("./config");
|
const config_1 = require("./config");
|
||||||
const tip_style_1 = require("./tip-style");
|
const tip_style_1 = require("./tip-style");
|
||||||
/**
|
/**
|
||||||
|
|
@ -69,6 +70,11 @@ commander_1.default
|
||||||
.option('-d, --dev', 'dev')
|
.option('-d, --dev', 'dev')
|
||||||
.description(`update project's template powered by ${config_1.CLI_NAME}`)
|
.description(`update project's template powered by ${config_1.CLI_NAME}`)
|
||||||
.action(create_1.update);
|
.action(create_1.update);
|
||||||
|
commander_1.default
|
||||||
|
.command('run')
|
||||||
|
.option('-i, --initialize', 'true')
|
||||||
|
.description(`run backend server by ${config_1.CLI_NAME}`)
|
||||||
|
.action(run_1.default);
|
||||||
// output help information on unknown commands
|
// output help information on unknown commands
|
||||||
commander_1.default.arguments('<command>').action((cmd) => {
|
commander_1.default.arguments('<command>').action((cmd) => {
|
||||||
commander_1.default.outputHelp();
|
commander_1.default.outputHelp();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export default function run(options: any): Promise<void>;
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const tip_style_1 = require("./tip-style");
|
||||||
|
const cross_spawn_1 = __importDefault(require("cross-spawn"));
|
||||||
|
async function run(options) {
|
||||||
|
if (options.initialize) {
|
||||||
|
(0, tip_style_1.Success)(`${(0, tip_style_1.success)('初始化数据库中……')}`);
|
||||||
|
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||||
|
const drop = options.args.includes('drop') || false;
|
||||||
|
const result = cross_spawn_1.default.sync('ts-node', [require.resolve('../scripts/' + 'initialize-database.js'), `${drop}`], {
|
||||||
|
stdio: 'inherit',
|
||||||
|
shell: true,
|
||||||
|
});
|
||||||
|
if (result.status === 0) {
|
||||||
|
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`初始化数据库完成`)}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Error(`${(0, tip_style_1.error)(`初始化数据库失败`)}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
(0, tip_style_1.Success)(`${(0, tip_style_1.success)('启动服务器……')}`);
|
||||||
|
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||||
|
const result = cross_spawn_1.default.sync('ts-node', [require.resolve('../scripts/' + 'start-server.js')], {
|
||||||
|
stdio: 'inherit',
|
||||||
|
shell: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.default = run;
|
||||||
|
|
@ -45,6 +45,7 @@
|
||||||
"loader-utils": "^3.2.0",
|
"loader-utils": "^3.2.0",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"mini-css-extract-plugin": "^2.5.3",
|
"mini-css-extract-plugin": "^2.5.3",
|
||||||
|
"oak-backend-base": "file:../oak-backend-base",
|
||||||
"postcss-less": "^6.0.0",
|
"postcss-less": "^6.0.0",
|
||||||
"progress-bar-webpack-plugin": "^2.1.0",
|
"progress-bar-webpack-plugin": "^2.1.0",
|
||||||
"required-path": "^1.0.1",
|
"required-path": "^1.0.1",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
const { initialize } = require('oak-backend-base');
|
||||||
|
const pwd = process.cwd();
|
||||||
|
|
||||||
|
const dropIfExists = process.argv[2];
|
||||||
|
|
||||||
|
initialize(pwd, dropIfExists)
|
||||||
|
.then(
|
||||||
|
() => process.exit(0)
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
const { startup } = require('oak-backend-base');
|
||||||
|
const pwd = process.cwd();
|
||||||
|
|
||||||
|
startup(pwd);
|
||||||
|
|
@ -3,6 +3,7 @@ import program from 'commander';
|
||||||
import { create, update } from './create';
|
import { create, update } from './create';
|
||||||
import build from './build';
|
import build from './build';
|
||||||
import make from './make';
|
import make from './make';
|
||||||
|
import run from './run';
|
||||||
import { CLI_VERSION, CLI_NAME } from './config';
|
import { CLI_VERSION, CLI_NAME } from './config';
|
||||||
import { error, warn } from './tip-style';
|
import { error, warn } from './tip-style';
|
||||||
|
|
||||||
|
|
@ -71,6 +72,11 @@ program
|
||||||
.option('-d, --dev', 'dev')
|
.option('-d, --dev', 'dev')
|
||||||
.description(`update project's template powered by ${CLI_NAME}`)
|
.description(`update project's template powered by ${CLI_NAME}`)
|
||||||
.action(update);
|
.action(update);
|
||||||
|
program
|
||||||
|
.command('run')
|
||||||
|
.option('-i, --initialize', 'true')
|
||||||
|
.description(`run backend server by ${CLI_NAME}`)
|
||||||
|
.action(run);
|
||||||
// output help information on unknown commands
|
// output help information on unknown commands
|
||||||
program.arguments('<command>').action((cmd) => {
|
program.arguments('<command>').action((cmd) => {
|
||||||
program.outputHelp();
|
program.outputHelp();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
import {
|
||||||
|
Success,
|
||||||
|
error,
|
||||||
|
primary,
|
||||||
|
success,
|
||||||
|
warn,
|
||||||
|
Warn,
|
||||||
|
} from './tip-style';
|
||||||
|
import spawn from 'cross-spawn';
|
||||||
|
|
||||||
|
export default async function run(options: any): Promise<void> {
|
||||||
|
if (options.initialize) {
|
||||||
|
Success(`${success('初始化数据库中……')}`);
|
||||||
|
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||||
|
const drop = options.args.includes('drop') || false;
|
||||||
|
const result = spawn.sync(
|
||||||
|
'ts-node',
|
||||||
|
[require.resolve('../scripts/' + 'initialize-database.js'), `${drop}`],
|
||||||
|
{
|
||||||
|
stdio: 'inherit',
|
||||||
|
shell: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.status === 0) {
|
||||||
|
Success(`${success(`初始化数据库完成`)}`);
|
||||||
|
} else {
|
||||||
|
Error(`${error(`初始化数据库失败`)}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Success(`${success('启动服务器……')}`);
|
||||||
|
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||||
|
const result = spawn.sync(
|
||||||
|
'ts-node',
|
||||||
|
[require.resolve('../scripts/' + 'start-server.js')],
|
||||||
|
{
|
||||||
|
stdio: 'inherit',
|
||||||
|
shell: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue