create-antd-skin 1.1.1 → 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.
Files changed (2) hide show
  1. package/bin/layout.js +27 -21
  2. 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 ANTD_LAYOUT_PKG = "@adminui-dev/antd-layout";
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"))
@@ -52,8 +53,7 @@ async function patchLayoutFile(filePath) {
52
53
  sourceType: "module",
53
54
  plugins: ["typescript", "jsx"],
54
55
  });
55
- let layoutNames = new Set();
56
- let hasLayoutImport = false;
56
+ const layoutNames = new Set();
57
57
  let hasWardenSkinImport = false;
58
58
  let changed = false;
59
59
  // 1. 收集 import
@@ -61,16 +61,15 @@ async function patchLayoutFile(filePath) {
61
61
  ImportDeclaration(path) {
62
62
  const source = path.node.source.value;
63
63
  // layout package
64
- if (source === "@adminui-dev/antd-layout") {
65
- hasLayoutImport = true;
64
+ if (source === LAYOUT_PKG) {
66
65
  for (const spec of path.node.specifiers) {
67
66
  if (t.isImportSpecifier(spec)) {
68
67
  layoutNames.add(spec.local.name);
69
68
  }
70
69
  }
71
70
  }
72
- // ❗关键:wardenSkin 是否已存在
73
- if (source === "../components/warden-skin") {
71
+ // wardenSkin 是否已经存在
72
+ if (source === WARDEN_SKIN_PKG) {
74
73
  hasWardenSkinImport = true;
75
74
  }
76
75
  },
@@ -80,26 +79,33 @@ async function patchLayoutFile(filePath) {
80
79
  // 2. 修改 JSX
81
80
  (0, traverse_1.default)(ast, {
82
81
  JSXOpeningElement(path) {
83
- const name = path.node.name;
84
- if (!t.isJSXIdentifier(name))
82
+ const node = path.node;
83
+ if (!t.isJSXIdentifier(node.name))
85
84
  return;
86
- if (!layoutNames.has(name.name))
85
+ const name = node.name.name;
86
+ // 只处理 AntdLayout
87
+ if (!layoutNames.has(name))
87
88
  return;
88
- if (hasThemeSkinsProp(path.node))
89
+ // 是否已有 themeSkins
90
+ const hasThemeSkins = node.attributes.some(attr => t.isJSXAttribute(attr) &&
91
+ t.isJSXIdentifier(attr.name) &&
92
+ attr.name.name === "themeSkins");
93
+ if (hasThemeSkins)
89
94
  return;
90
- path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("themeSkins"), t.jsxExpressionContainer(t.identifier("wardenSkin"))));
95
+ node.attributes.push(t.jsxAttribute(t.jsxIdentifier("themeSkins"), t.jsxExpressionContainer(t.identifier("wardenSkin"))));
91
96
  changed = true;
92
- }
97
+ },
93
98
  });
94
- if (!changed)
95
- return;
96
- // 3. 注入 import(只在 layouts 文件里)
97
- if (!hasWardenSkinImport) {
99
+ if (changed && !hasWardenSkinImport) {
98
100
  ast.program.body.unshift(t.importDeclaration([
99
- t.importDefaultSpecifier(t.identifier("wardenSkin"))
100
- ], t.stringLiteral("@/components/warden-skin")));
101
+ t.importDefaultSpecifier(t.identifier("wardenSkin")),
102
+ ], t.stringLiteral(WARDEN_SKIN_PKG)));
101
103
  }
102
- const output = (0, generator_1.default)(ast, {}, code);
104
+ if (!changed && hasWardenSkinImport)
105
+ return;
106
+ const output = (0, generator_1.default)(ast, {
107
+ retainLines: true,
108
+ });
103
109
  await fs_extra_1.default.writeFile(filePath, output.code);
104
110
  console.log("✔ updated:", path_1.default.relative(process.cwd(), filePath));
105
111
  }
@@ -118,7 +124,7 @@ async function getLayoutFiles(dir) {
118
124
  result.push(...await getLayoutFiles(full));
119
125
  continue;
120
126
  }
121
- if (/\.(tsx|ts)$/.test(item)) {
127
+ if (/\.(ts|tsx)$/.test(item)) {
122
128
  result.push(full);
123
129
  }
124
130
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-antd-skin",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "this is skin-cli for @adminui-dev/antd-layout",
5
5
  "main": "index.js",
6
6
  "author": "zhouwenqi",