create-antd-skin 1.0.9 → 1.1.1
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 +51 -41
- package/package.json +1 -1
package/bin/layout.js
CHANGED
|
@@ -36,76 +36,77 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getLayoutFiles = getLayoutFiles;
|
|
40
39
|
exports.updateLayouts = updateLayouts;
|
|
41
|
-
exports.patchLayoutFile = patchLayoutFile;
|
|
42
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
43
|
-
const path_1 = __importDefault(require("path"));
|
|
44
40
|
const parser_1 = require("@babel/parser");
|
|
45
41
|
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
46
42
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
47
43
|
const t = __importStar(require("@babel/types"));
|
|
44
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
45
|
+
const path_1 = __importDefault(require("path"));
|
|
46
|
+
const ANTD_LAYOUT_PKG = "@adminui-dev/antd-layout";
|
|
48
47
|
async function patchLayoutFile(filePath) {
|
|
49
48
|
const code = await fs_extra_1.default.readFile(filePath, "utf8");
|
|
50
|
-
if (!code.includes("AntdLayout"))
|
|
49
|
+
if (!code.includes("<AntdLayout"))
|
|
51
50
|
return;
|
|
52
|
-
}
|
|
53
51
|
const ast = (0, parser_1.parse)(code, {
|
|
54
52
|
sourceType: "module",
|
|
55
|
-
plugins: [
|
|
56
|
-
"typescript",
|
|
57
|
-
"jsx"
|
|
58
|
-
]
|
|
53
|
+
plugins: ["typescript", "jsx"],
|
|
59
54
|
});
|
|
60
|
-
let
|
|
55
|
+
let layoutNames = new Set();
|
|
56
|
+
let hasLayoutImport = false;
|
|
57
|
+
let hasWardenSkinImport = false;
|
|
61
58
|
let changed = false;
|
|
59
|
+
// 1. 收集 import
|
|
62
60
|
(0, traverse_1.default)(ast, {
|
|
63
61
|
ImportDeclaration(path) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
const source = path.node.source.value;
|
|
63
|
+
// layout package
|
|
64
|
+
if (source === "@adminui-dev/antd-layout") {
|
|
65
|
+
hasLayoutImport = true;
|
|
66
|
+
for (const spec of path.node.specifiers) {
|
|
67
|
+
if (t.isImportSpecifier(spec)) {
|
|
68
|
+
layoutNames.add(spec.local.name);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// ❗关键:wardenSkin 是否已存在
|
|
73
|
+
if (source === "../components/warden-skin") {
|
|
74
|
+
hasWardenSkinImport = true;
|
|
67
75
|
}
|
|
68
76
|
},
|
|
77
|
+
});
|
|
78
|
+
if (layoutNames.size === 0)
|
|
79
|
+
return;
|
|
80
|
+
// 2. 修改 JSX
|
|
81
|
+
(0, traverse_1.default)(ast, {
|
|
69
82
|
JSXOpeningElement(path) {
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
const name = path.node.name;
|
|
84
|
+
if (!t.isJSXIdentifier(name))
|
|
72
85
|
return;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
t.isJSXIdentifier(attr.name) &&
|
|
77
|
-
attr.name.name === "themeSkins");
|
|
78
|
-
});
|
|
79
|
-
if (hasThemeSkins) {
|
|
86
|
+
if (!layoutNames.has(name.name))
|
|
87
|
+
return;
|
|
88
|
+
if (hasThemeSkinsProp(path.node))
|
|
80
89
|
return;
|
|
81
|
-
}
|
|
82
90
|
path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("themeSkins"), t.jsxExpressionContainer(t.identifier("wardenSkin"))));
|
|
83
91
|
changed = true;
|
|
84
92
|
}
|
|
85
93
|
});
|
|
86
|
-
if (!changed)
|
|
94
|
+
if (!changed)
|
|
87
95
|
return;
|
|
88
|
-
|
|
89
|
-
if (!
|
|
96
|
+
// 3. 注入 import(只在 layouts 文件里)
|
|
97
|
+
if (!hasWardenSkinImport) {
|
|
90
98
|
ast.program.body.unshift(t.importDeclaration([
|
|
91
99
|
t.importDefaultSpecifier(t.identifier("wardenSkin"))
|
|
92
|
-
], t.stringLiteral("
|
|
100
|
+
], t.stringLiteral("@/components/warden-skin")));
|
|
93
101
|
}
|
|
94
|
-
const output = (0, generator_1.default)(ast, {
|
|
95
|
-
retainLines: true
|
|
96
|
-
}, code);
|
|
102
|
+
const output = (0, generator_1.default)(ast, {}, code);
|
|
97
103
|
await fs_extra_1.default.writeFile(filePath, output.code);
|
|
98
|
-
console.log(
|
|
104
|
+
console.log("✔ updated:", path_1.default.relative(process.cwd(), filePath));
|
|
99
105
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
const files = await getLayoutFiles(layoutsDir);
|
|
106
|
-
for (const file of files) {
|
|
107
|
-
await patchLayoutFile(file);
|
|
108
|
-
}
|
|
106
|
+
function hasThemeSkinsProp(node) {
|
|
107
|
+
return node.attributes.some(attr => t.isJSXAttribute(attr) &&
|
|
108
|
+
t.isJSXIdentifier(attr.name) &&
|
|
109
|
+
attr.name.name === "themeSkins");
|
|
109
110
|
}
|
|
110
111
|
async function getLayoutFiles(dir) {
|
|
111
112
|
const result = [];
|
|
@@ -123,3 +124,12 @@ async function getLayoutFiles(dir) {
|
|
|
123
124
|
}
|
|
124
125
|
return result;
|
|
125
126
|
}
|
|
127
|
+
async function updateLayouts(projectRoot) {
|
|
128
|
+
const layoutsDir = path_1.default.join(projectRoot, "src/layouts");
|
|
129
|
+
if (!(await fs_extra_1.default.pathExists(layoutsDir)))
|
|
130
|
+
return;
|
|
131
|
+
const files = await getLayoutFiles(layoutsDir);
|
|
132
|
+
for (const file of files) {
|
|
133
|
+
await patchLayoutFile(file);
|
|
134
|
+
}
|
|
135
|
+
}
|