后台相关的命令以及web编译过程中对oak:开头的jsx的处理

This commit is contained in:
Xu Chang 2022-06-24 18:41:58 +08:00
parent 298cdf875b
commit bf98e82da0
12 changed files with 927 additions and 707 deletions

View File

@ -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();
}
}
}
}
};

View File

@ -1,7 +1,7 @@
const { resolve, relative } = require('path');
module.exports = function (content) {
const options = this.getOptions() || {}; //获取配置参数
/* const options = this.getOptions() || {}; //获取配置参数
const { context: projectContext } = options; // context 本项目路径
const {
options: webpackLegacyOptions,
@ -17,7 +17,7 @@ module.exports = function (content) {
if (/.tsx|.jsx/.test(resourcePath)) {
// console.log(content);
}
} */
return content;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
/// <reference types="node" />
/// <reference types="node" />
import { PathLike } from 'fs';
import { checkFileExistsAndCreateType } from './enum';
/**

View File

@ -8,6 +8,7 @@ const commander_1 = __importDefault(require("commander"));
const create_1 = require("./create");
const build_1 = __importDefault(require("./build"));
const make_1 = __importDefault(require("./make"));
const run_1 = __importDefault(require("./run"));
const config_1 = require("./config");
const tip_style_1 = require("./tip-style");
/**
@ -69,6 +70,11 @@ commander_1.default
.option('-d, --dev', 'dev')
.description(`update project's template powered by ${config_1.CLI_NAME}`)
.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
commander_1.default.arguments('<command>').action((cmd) => {
commander_1.default.outputHelp();

1
lib/run.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export default function run(options: any): Promise<void>;

34
lib/run.js Normal file
View File

@ -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;

View File

@ -45,6 +45,7 @@
"loader-utils": "^3.2.0",
"lodash": "^4.17.21",
"mini-css-extract-plugin": "^2.5.3",
"oak-backend-base": "file:../oak-backend-base",
"postcss-less": "^6.0.0",
"progress-bar-webpack-plugin": "^2.1.0",
"required-path": "^1.0.1",

View File

@ -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)
);

4
scripts/start-server.js Normal file
View File

@ -0,0 +1,4 @@
const { startup } = require('oak-backend-base');
const pwd = process.cwd();
startup(pwd);

View File

@ -3,6 +3,7 @@ import program from 'commander';
import { create, update } from './create';
import build from './build';
import make from './make';
import run from './run';
import { CLI_VERSION, CLI_NAME } from './config';
import { error, warn } from './tip-style';
@ -71,6 +72,11 @@ program
.option('-d, --dev', 'dev')
.description(`update project's template powered by ${CLI_NAME}`)
.action(update);
program
.command('run')
.option('-i, --initialize', 'true')
.description(`run backend server by ${CLI_NAME}`)
.action(run);
// output help information on unknown commands
program.arguments('<command>').action((cmd) => {
program.outputHelp();

44
src/run.ts Normal file
View File

@ -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,
}
);
}
}