create-antd-skin 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/layout.js +30 -16
- package/package.json +1 -1
package/bin/layout.js
CHANGED
|
@@ -43,7 +43,8 @@ const generator_1 = __importDefault(require("@babel/generator"));
|
|
|
43
43
|
const t = __importStar(require("@babel/types"));
|
|
44
44
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
45
45
|
const path_1 = __importDefault(require("path"));
|
|
46
|
-
const
|
|
46
|
+
const LAYOUT_PKG = "@adminui-dev/antd-layout";
|
|
47
|
+
const WARDEN_SKIN_PKG = "../components/warden-skin";
|
|
47
48
|
async function patchLayoutFile(filePath) {
|
|
48
49
|
const code = await fs_extra_1.default.readFile(filePath, "utf8");
|
|
49
50
|
if (!code.includes("<AntdLayout"))
|
|
@@ -53,19 +54,24 @@ async function patchLayoutFile(filePath) {
|
|
|
53
54
|
plugins: ["typescript", "jsx"],
|
|
54
55
|
});
|
|
55
56
|
const layoutNames = new Set();
|
|
56
|
-
let
|
|
57
|
+
let hasWardenSkinImport = false;
|
|
57
58
|
let changed = false;
|
|
58
59
|
// 1. 收集 import
|
|
59
60
|
(0, traverse_1.default)(ast, {
|
|
60
61
|
ImportDeclaration(path) {
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
const source = path.node.source.value;
|
|
63
|
+
// layout package
|
|
64
|
+
if (source === LAYOUT_PKG) {
|
|
63
65
|
for (const spec of path.node.specifiers) {
|
|
64
66
|
if (t.isImportSpecifier(spec)) {
|
|
65
67
|
layoutNames.add(spec.local.name);
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
70
|
}
|
|
71
|
+
// wardenSkin 是否已经存在
|
|
72
|
+
if (source === WARDEN_SKIN_PKG) {
|
|
73
|
+
hasWardenSkinImport = true;
|
|
74
|
+
}
|
|
69
75
|
},
|
|
70
76
|
});
|
|
71
77
|
if (layoutNames.size === 0)
|
|
@@ -73,33 +79,41 @@ async function patchLayoutFile(filePath) {
|
|
|
73
79
|
// 2. 修改 JSX
|
|
74
80
|
(0, traverse_1.default)(ast, {
|
|
75
81
|
JSXOpeningElement(path) {
|
|
76
|
-
|
|
82
|
+
const node = path.node;
|
|
83
|
+
if (!t.isJSXIdentifier(node.name))
|
|
77
84
|
return;
|
|
78
|
-
const name =
|
|
79
|
-
//
|
|
85
|
+
const name = node.name.name;
|
|
86
|
+
// 只处理 AntdLayout
|
|
80
87
|
if (!layoutNames.has(name))
|
|
81
88
|
return;
|
|
82
|
-
|
|
89
|
+
// 是否已有 themeSkins
|
|
90
|
+
const hasThemeSkins = node.attributes.some(attr => t.isJSXAttribute(attr) &&
|
|
83
91
|
t.isJSXIdentifier(attr.name) &&
|
|
84
92
|
attr.name.name === "themeSkins");
|
|
85
93
|
if (hasThemeSkins)
|
|
86
94
|
return;
|
|
87
|
-
|
|
95
|
+
node.attributes.push(t.jsxAttribute(t.jsxIdentifier("themeSkins"), t.jsxExpressionContainer(t.identifier("wardenSkin"))));
|
|
88
96
|
changed = true;
|
|
89
97
|
},
|
|
90
98
|
});
|
|
91
|
-
if (!
|
|
92
|
-
return;
|
|
93
|
-
// 3. 注入 import(只在 layouts 文件里)
|
|
94
|
-
if (!hasImport) {
|
|
99
|
+
if (changed && !hasWardenSkinImport) {
|
|
95
100
|
ast.program.body.unshift(t.importDeclaration([
|
|
96
101
|
t.importDefaultSpecifier(t.identifier("wardenSkin")),
|
|
97
|
-
], t.stringLiteral(
|
|
102
|
+
], t.stringLiteral(WARDEN_SKIN_PKG)));
|
|
98
103
|
}
|
|
99
|
-
|
|
104
|
+
if (!changed && hasWardenSkinImport)
|
|
105
|
+
return;
|
|
106
|
+
const output = (0, generator_1.default)(ast, {
|
|
107
|
+
retainLines: true,
|
|
108
|
+
});
|
|
100
109
|
await fs_extra_1.default.writeFile(filePath, output.code);
|
|
101
110
|
console.log("✔ updated:", path_1.default.relative(process.cwd(), filePath));
|
|
102
111
|
}
|
|
112
|
+
function hasThemeSkinsProp(node) {
|
|
113
|
+
return node.attributes.some(attr => t.isJSXAttribute(attr) &&
|
|
114
|
+
t.isJSXIdentifier(attr.name) &&
|
|
115
|
+
attr.name.name === "themeSkins");
|
|
116
|
+
}
|
|
103
117
|
async function getLayoutFiles(dir) {
|
|
104
118
|
const result = [];
|
|
105
119
|
const items = await fs_extra_1.default.readdir(dir);
|
|
@@ -110,7 +124,7 @@ async function getLayoutFiles(dir) {
|
|
|
110
124
|
result.push(...await getLayoutFiles(full));
|
|
111
125
|
continue;
|
|
112
126
|
}
|
|
113
|
-
if (/\.(tsx
|
|
127
|
+
if (/\.(ts|tsx)$/.test(item)) {
|
|
114
128
|
result.push(full);
|
|
115
129
|
}
|
|
116
130
|
}
|