create-antd-skin 1.0.8 → 1.1.0
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/index.js +1 -1
- package/bin/layout.js +43 -41
- package/package.json +1 -1
package/bin/index.js
CHANGED
package/bin/layout.js
CHANGED
|
@@ -36,76 +36,69 @@ 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
|
});
|
|
55
|
+
const layoutNames = new Set();
|
|
60
56
|
let hasImport = false;
|
|
61
57
|
let changed = false;
|
|
58
|
+
// 1. 收集 import
|
|
62
59
|
(0, traverse_1.default)(ast, {
|
|
63
60
|
ImportDeclaration(path) {
|
|
64
|
-
if (path.node.source.value ===
|
|
65
|
-
"../components/warden-skin") {
|
|
61
|
+
if (path.node.source.value === ANTD_LAYOUT_PKG) {
|
|
66
62
|
hasImport = true;
|
|
63
|
+
for (const spec of path.node.specifiers) {
|
|
64
|
+
if (t.isImportSpecifier(spec)) {
|
|
65
|
+
layoutNames.add(spec.local.name);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
67
68
|
}
|
|
68
69
|
},
|
|
70
|
+
});
|
|
71
|
+
if (layoutNames.size === 0)
|
|
72
|
+
return;
|
|
73
|
+
// 2. 修改 JSX
|
|
74
|
+
(0, traverse_1.default)(ast, {
|
|
69
75
|
JSXOpeningElement(path) {
|
|
70
|
-
if (!t.isJSXIdentifier(path.node.name)
|
|
71
|
-
path.node.name.name !== "AntdLayout") {
|
|
76
|
+
if (!t.isJSXIdentifier(path.node.name))
|
|
72
77
|
return;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const name = path.node.name.name;
|
|
79
|
+
// 关键:只处理来自 layout package 的组件
|
|
80
|
+
if (!layoutNames.has(name))
|
|
81
|
+
return;
|
|
82
|
+
const hasThemeSkins = path.node.attributes.some(attr => t.isJSXAttribute(attr) &&
|
|
83
|
+
t.isJSXIdentifier(attr.name) &&
|
|
84
|
+
attr.name.name === "themeSkins");
|
|
85
|
+
if (hasThemeSkins)
|
|
80
86
|
return;
|
|
81
|
-
}
|
|
82
87
|
path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("themeSkins"), t.jsxExpressionContainer(t.identifier("wardenSkin"))));
|
|
83
88
|
changed = true;
|
|
84
|
-
}
|
|
89
|
+
},
|
|
85
90
|
});
|
|
86
|
-
if (!changed)
|
|
91
|
+
if (!changed)
|
|
87
92
|
return;
|
|
88
|
-
|
|
93
|
+
// 3. 注入 import(只在 layouts 文件里)
|
|
89
94
|
if (!hasImport) {
|
|
90
95
|
ast.program.body.unshift(t.importDeclaration([
|
|
91
|
-
t.importDefaultSpecifier(t.identifier("wardenSkin"))
|
|
92
|
-
], t.stringLiteral("
|
|
96
|
+
t.importDefaultSpecifier(t.identifier("wardenSkin")),
|
|
97
|
+
], t.stringLiteral("@/components/warden-skin")));
|
|
93
98
|
}
|
|
94
|
-
const output = (0, generator_1.default)(ast, {
|
|
95
|
-
retainLines: true
|
|
96
|
-
}, code);
|
|
99
|
+
const output = (0, generator_1.default)(ast, {}, code);
|
|
97
100
|
await fs_extra_1.default.writeFile(filePath, output.code);
|
|
98
|
-
console.log(
|
|
99
|
-
}
|
|
100
|
-
async function updateLayouts(projectRoot) {
|
|
101
|
-
const layoutsDir = path_1.default.join(projectRoot, "src/layouts");
|
|
102
|
-
if (!(await fs_extra_1.default.pathExists(layoutsDir))) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
const files = await getLayoutFiles(layoutsDir);
|
|
106
|
-
for (const file of files) {
|
|
107
|
-
await patchLayoutFile(file);
|
|
108
|
-
}
|
|
101
|
+
console.log("✔ updated:", path_1.default.relative(process.cwd(), filePath));
|
|
109
102
|
}
|
|
110
103
|
async function getLayoutFiles(dir) {
|
|
111
104
|
const result = [];
|
|
@@ -123,3 +116,12 @@ async function getLayoutFiles(dir) {
|
|
|
123
116
|
}
|
|
124
117
|
return result;
|
|
125
118
|
}
|
|
119
|
+
async function updateLayouts(projectRoot) {
|
|
120
|
+
const layoutsDir = path_1.default.join(projectRoot, "src/layouts");
|
|
121
|
+
if (!(await fs_extra_1.default.pathExists(layoutsDir)))
|
|
122
|
+
return;
|
|
123
|
+
const files = await getLayoutFiles(layoutsDir);
|
|
124
|
+
for (const file of files) {
|
|
125
|
+
await patchLayoutFile(file);
|
|
126
|
+
}
|
|
127
|
+
}
|