@mantou/gem-port 1.1.0 → 1.2.4

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/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Gem-port
2
+
3
+ Generate React/Vue/Svelte components for Gem elements
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ gem-port src/elements
9
+
10
+ # specify svelte element namespace
11
+ gem-port src/elements --svelte-ns=pat
12
+
13
+ # print help info
14
+ gem-port -h
15
+ ```
package/bin/index.js CHANGED
@@ -1849,29 +1849,29 @@ Expecting one of '${allowedValues.join("', '")}'`);
1849
1849
  });
1850
1850
 
1851
1851
  // src/index.ts
1852
- var import_path4 = __toESM(require("path"));
1852
+ var import_node_path4 = __toESM(require("node:path"));
1853
1853
  var import_commander = __toESM(require_commander());
1854
1854
 
1855
1855
  // package.json
1856
1856
  var name = "@mantou/gem-port";
1857
- var version = "1.1.0";
1857
+ var version = "1.2.4";
1858
1858
  var description = "Export React component";
1859
1859
 
1860
1860
  // src/common.ts
1861
- var import_path = __toESM(require("path"));
1862
- var import_fs = require("fs");
1861
+ var import_node_fs = require("node:fs");
1862
+ var import_node_path = __toESM(require("node:path"));
1863
1863
 
1864
1864
  // ../gem/lib/utils.js
1865
- var { assign, setPrototypeOf, fromEntries, entries, keys } = Object;
1865
+ var { assign, fromEntries, entries, keys } = Object;
1866
1866
  function camelToKebabCase(str) {
1867
- return str.replace(/[A-Z]/g, ($1) => "-" + $1.toLowerCase());
1867
+ return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
1868
1868
  }
1869
1869
 
1870
1870
  // ../gem-analyzer/lib/utils.js
1871
1871
  function getJsDoc(declaration) {
1872
1872
  if ("getJsDocs" in declaration) {
1873
1873
  const jsDocs = declaration.getJsDocs();
1874
- const deprecated = jsDocs.map((jsDoc) => jsDoc.getTags()).flat().some((e) => e.getTagName() === "deprecated");
1874
+ const deprecated = jsDocs.flatMap((jsDoc) => jsDoc.getTags()).some((e) => e.getTagName() === "deprecated");
1875
1875
  const comment = jsDocs.map((jsDoc) => jsDoc.getCommentText()).join("\n\n");
1876
1876
  return {
1877
1877
  deprecated,
@@ -1891,6 +1891,10 @@ function isGetter(declaration, kind = "get") {
1891
1891
  function isSetter(declaration) {
1892
1892
  return isGetter(declaration, "set");
1893
1893
  }
1894
+ function isPrivateId(name2) {
1895
+ if (name2.startsWith("#") || name2.startsWith("_"))
1896
+ return true;
1897
+ }
1894
1898
 
1895
1899
  // ../gem-analyzer/index.js
1896
1900
  var shadowDecoratorName = ["shadow"];
@@ -1954,23 +1958,25 @@ var parseElement = async (declaration, file, project2) => {
1954
1958
  };
1955
1959
  const className = declaration.getName();
1956
1960
  const constructorExtendsName = declaration.getExtends()?.getText();
1957
- const constructor = declaration.getConstructors()[0];
1958
- const appendElementDesc = (desc = "") => detail.description = (detail.description ? detail.description + "\n\n" : "") + desc;
1961
+ const cons = declaration.getConstructors()[0];
1962
+ const appendElementDesc = (desc = "") => detail.description = (detail.description ? `${detail.description}
1963
+
1964
+ ` : "") + desc;
1959
1965
  declaration.getJsDocs().forEach((jsDoc) => appendElementDesc(jsDoc.getCommentText()));
1960
1966
  if (className && constructorExtendsName) {
1961
1967
  detail.extend = await getExtendsClassDetail(constructorExtendsName, file, project2);
1962
1968
  detail.constructorName = className;
1963
1969
  detail.constructorExtendsName = constructorExtendsName;
1964
- if (constructor) {
1970
+ if (cons) {
1965
1971
  const params = {};
1966
- const jsDocs = constructor.getJsDocs();
1972
+ const jsDocs = cons.getJsDocs();
1967
1973
  jsDocs.forEach((jsDoc) => {
1968
1974
  appendElementDesc(jsDoc.getDescription());
1969
1975
  jsDoc.getTags().map((tag) => ({ tagName: tag.getTagName(), name: tag.getName(), comment: tag.getCommentText() })).filter(({ tagName, comment, name: name2 }) => tagName === "param" && comment && name2).forEach(({ name: name2, comment }) => {
1970
1976
  params[name2] = comment;
1971
1977
  });
1972
1978
  });
1973
- detail.constructorParams = constructor.getParameters().map((param) => ({
1979
+ detail.constructorParams = cons.getParameters().map((param) => ({
1974
1980
  name: param.getName(),
1975
1981
  type: getTypeText(param),
1976
1982
  description: params[param.getName()]
@@ -1980,7 +1986,7 @@ var parseElement = async (declaration, file, project2) => {
1980
1986
  const staticPropertiesDeclarations = declaration.getStaticProperties();
1981
1987
  for (const staticPropDeclaration of staticPropertiesDeclarations) {
1982
1988
  const staticPropName = staticPropDeclaration.getName();
1983
- if (staticPropName.startsWith("#"))
1989
+ if (isPrivateId(staticPropName))
1984
1990
  continue;
1985
1991
  const prop = {
1986
1992
  name: staticPropName,
@@ -2010,7 +2016,7 @@ var parseElement = async (declaration, file, project2) => {
2010
2016
  const staticMethodDeclarations = declaration.getStaticMethods();
2011
2017
  for (const staticMethodDeclaration of staticMethodDeclarations) {
2012
2018
  const staticMethodName = staticMethodDeclaration.getName();
2013
- if (staticMethodName.startsWith("#"))
2019
+ if (isPrivateId(staticMethodName))
2014
2020
  continue;
2015
2021
  const method = {
2016
2022
  name: staticMethodName,
@@ -2022,7 +2028,7 @@ var parseElement = async (declaration, file, project2) => {
2022
2028
  const propDeclarations = declaration.getInstanceProperties();
2023
2029
  for (const propDeclaration of propDeclarations) {
2024
2030
  const propName = propDeclaration.getName();
2025
- if (propName.startsWith("#"))
2031
+ if (isPrivateId(propName))
2026
2032
  continue;
2027
2033
  if (lifecyclePopsOrMethods.includes(propName))
2028
2034
  continue;
@@ -2067,7 +2073,7 @@ var parseElement = async (declaration, file, project2) => {
2067
2073
  const methodDeclarations = declaration.getInstanceMethods();
2068
2074
  for (const methodDeclaration of methodDeclarations) {
2069
2075
  const methodName = methodDeclaration.getName();
2070
- if (methodName.startsWith("#"))
2076
+ if (isPrivateId(methodName))
2071
2077
  continue;
2072
2078
  if (lifecyclePopsOrMethods.includes(methodName))
2073
2079
  continue;
@@ -2101,7 +2107,7 @@ var getElements = async (file, project2) => {
2101
2107
  for (const declaration of file.getClasses()) {
2102
2108
  const elementDecorators = declaration.getDecorators();
2103
2109
  const elementDeclaration = elementDecorators.find((decorator) => elementDecoratorName.includes(decorator.getName()));
2104
- const elementTag = elementDeclaration?.getCallExpression().getArguments()[0].getText().replace(/('|"|`)?(\S*)\1/, "$2") || declaration.getJsDocs().map((jsDoc) => jsDoc.getTags()).flat().find((e) => e.getTagName() === "customElement")?.getCommentText();
2110
+ const elementTag = elementDeclaration?.getCallExpression().getArguments()[0].getText().replace(/('|"|`)?(\S*)\1/, "$2") || declaration.getJsDocs().flatMap((jsDoc) => jsDoc.getTags()).find((e) => e.getTagName() === "customElement")?.getCommentText();
2105
2111
  if (elementTag) {
2106
2112
  const detail = {
2107
2113
  ...await parseElement(declaration, file, project2),
@@ -2121,8 +2127,8 @@ var ts = __toESM(require("typescript"));
2121
2127
  var dev = process.env.MODE === "dev";
2122
2128
  var elements = process.env.ELEMENTS?.split(",");
2123
2129
  function getElementPathList(elementsDir) {
2124
- const paths = (0, import_fs.readdirSync)(elementsDir).map((filename) => import_path.default.resolve(elementsDir, filename)).filter((elementFilePath, index) => {
2125
- if (!(0, import_fs.statSync)(elementFilePath).isFile()) return false;
2130
+ const paths = (0, import_node_fs.readdirSync)(elementsDir).map((filename) => import_node_path.default.resolve(elementsDir, filename)).filter((elementFilePath, index) => {
2131
+ if (!(0, import_node_fs.statSync)(elementFilePath).isFile()) return false;
2126
2132
  if (elements) {
2127
2133
  return elements?.some((ele) => elementFilePath.includes(ele));
2128
2134
  } else if (dev) {
@@ -2144,13 +2150,13 @@ var getChain = (detail) => {
2144
2150
  }
2145
2151
  return result;
2146
2152
  };
2147
- var mergeElementDetail = (elementFilePath, detail) => {
2153
+ var mergeElementDetail = (detail) => {
2148
2154
  const chain = getChain(detail);
2149
2155
  return {
2150
2156
  ...detail,
2151
- properties: chain.map((e) => e.properties).flat(),
2152
- methods: chain.map((e) => e.methods).flat(),
2153
- events: chain.map((e) => e.events).flat()
2157
+ properties: chain.flatMap((e) => e.properties),
2158
+ methods: chain.flatMap((e) => e.methods),
2159
+ events: chain.flatMap((e) => e.events)
2154
2160
  };
2155
2161
  };
2156
2162
  var elementCache = {};
@@ -2158,13 +2164,13 @@ var project = new import_ts_morph.Project({
2158
2164
  useInMemoryFileSystem: true,
2159
2165
  compilerOptions: { target: import_ts_morph.ts.ScriptTarget.ESNext }
2160
2166
  });
2161
- project.getFileSystem().readFile = (filePath) => import_fs.promises.readFile(filePath, "utf8");
2167
+ project.getFileSystem().readFile = (filePath) => import_node_fs.promises.readFile(filePath, "utf8");
2162
2168
  async function getFileElements(elementFilePath) {
2163
2169
  if (!elementCache[elementFilePath]) {
2164
- const text = (0, import_fs.readFileSync)(elementFilePath, { encoding: "utf-8" });
2170
+ const text = (0, import_node_fs.readFileSync)(elementFilePath, { encoding: "utf-8" });
2165
2171
  const file = project.getSourceFile(elementFilePath) || project.createSourceFile(elementFilePath, text);
2166
2172
  const details = await getElements(file, project);
2167
- elementCache[elementFilePath] = details.map((detail) => mergeElementDetail(elementFilePath, detail));
2173
+ elementCache[elementFilePath] = details.map((detail) => mergeElementDetail(detail));
2168
2174
  }
2169
2175
  return elementCache[elementFilePath];
2170
2176
  }
@@ -2175,8 +2181,8 @@ function getJsDocDescName(name2, deprecated) {
2175
2181
  return `${deprecated ? "/**@deprecated */\n" : ""}${name2}`;
2176
2182
  }
2177
2183
  function getRelativePath(elementFilePath, outDir) {
2178
- const basename = import_path.default.basename(elementFilePath, import_path.default.extname(elementFilePath));
2179
- const relativePath = import_path.default.relative(import_path.default.resolve(outDir), import_path.default.dirname(elementFilePath)).replace("src/", "");
2184
+ const basename = import_node_path.default.basename(elementFilePath, import_node_path.default.extname(elementFilePath));
2185
+ const relativePath = import_node_path.default.relative(import_node_path.default.resolve(outDir), import_node_path.default.dirname(elementFilePath)).replace("src/", "");
2180
2186
  return `${relativePath}/${basename}`;
2181
2187
  }
2182
2188
  function compile(outDir, fileSystem) {
@@ -2216,7 +2222,7 @@ async function createReactSourceFile(elementFilePath, outDir) {
2216
2222
  const getters = properties.filter((e) => e.getter);
2217
2223
  const settableProperties = properties.filter((e) => !e.getter && !e.event);
2218
2224
  return [
2219
- componentName + ".tsx",
2225
+ `${componentName}.tsx`,
2220
2226
  `
2221
2227
  import React, { HTMLAttributes, RefAttributes } from 'react';
2222
2228
  import React, { ForwardRefExoticComponent, forwardRef, useImperativeHandle, useRef, useLayoutEffect } from 'react';
@@ -2293,11 +2299,56 @@ async function compileReact(elementsDir, outDir) {
2293
2299
  compile(outDir, fileSystem);
2294
2300
  }
2295
2301
 
2302
+ // src/svelte.ts
2303
+ var import_node_path2 = __toESM(require("node:path"));
2304
+ async function compileSvelte(elementsDir, outDir, ns = "") {
2305
+ const fileSystem = {};
2306
+ const processFile = async (elementFilePath) => {
2307
+ const elementDetailList = await getFileElements(elementFilePath);
2308
+ Object.assign(
2309
+ fileSystem,
2310
+ Object.fromEntries(
2311
+ elementDetailList.map(({ name: tag, constructorName, properties }) => {
2312
+ const componentName = getComponentName(tag);
2313
+ const componentPropsName = `${componentName}Props`;
2314
+ const relativePath = getRelativePath(elementFilePath, outDir);
2315
+ const basename = import_node_path2.default.basename(relativePath);
2316
+ return [
2317
+ `${[ns, basename].filter((e) => !!e).join("-")}.ts`,
2318
+ `
2319
+ import type { HTMLAttributes } from "svelte/elements";
2320
+ import { ${constructorName} } from '${relativePath}';
2321
+ export * from '${relativePath}';
2322
+
2323
+ interface ${componentPropsName} extends HTMLAttributes<HTMLElement> {
2324
+ ${properties.map(
2325
+ ({ name: name2, getter, event, deprecated }) => event ? [
2326
+ getJsDocDescName(`'on:${event}'`, deprecated),
2327
+ `(event: CustomEvent<Parameters<${constructorName}['${name2}']>[0]>) => void`
2328
+ ].join("?:") : !getter ? [getJsDocDescName(name2, deprecated), `${constructorName}['${name2}']`].join("?:") : ""
2329
+ ).join(";\n")}
2330
+ };
2331
+
2332
+ declare module "svelte/elements" {
2333
+ interface SvelteHTMLElements {
2334
+ '${tag}': ${componentPropsName};
2335
+ }
2336
+ }
2337
+ `
2338
+ ];
2339
+ })
2340
+ )
2341
+ );
2342
+ };
2343
+ await Promise.all(getElementPathList(elementsDir).map(processFile));
2344
+ compile(outDir, fileSystem);
2345
+ }
2346
+
2296
2347
  // src/vue.ts
2297
- var import_fs2 = require("fs");
2298
- var import_path2 = require("path");
2348
+ var import_node_fs2 = require("node:fs");
2349
+ var import_node_path3 = require("node:path");
2299
2350
  async function generateVue(elementsDir, outDir) {
2300
- (0, import_fs2.mkdirSync)(outDir, { recursive: true });
2351
+ (0, import_node_fs2.mkdirSync)(outDir, { recursive: true });
2301
2352
  const processFile = async (elementFilePath) => {
2302
2353
  const elements2 = await getFileElements(elementFilePath);
2303
2354
  elements2.forEach(({ name: tag, properties, constructorName, methods, events }) => {
@@ -2306,8 +2357,8 @@ async function generateVue(elementsDir, outDir) {
2306
2357
  const relativePath = getRelativePath(elementFilePath, outDir);
2307
2358
  const settableProperties = properties.filter((e) => !e.getter && !e.event);
2308
2359
  const getters = properties.filter((e) => e.getter);
2309
- (0, import_fs2.writeFileSync)(
2310
- (0, import_path2.resolve)(outDir, componentName + ".vue"),
2360
+ (0, import_node_fs2.writeFileSync)(
2361
+ (0, import_node_path3.resolve)(outDir, `${componentName}.vue`),
2311
2362
  `
2312
2363
  <script setup lang="ts">
2313
2364
  import { ref, defineProps, defineEmits, defineExpose, onMounted } from 'vue'
@@ -2377,51 +2428,6 @@ async function generateVue(elementsDir, outDir) {
2377
2428
  await Promise.all(getElementPathList(elementsDir).map(processFile));
2378
2429
  }
2379
2430
 
2380
- // src/svelte.ts
2381
- var import_path3 = __toESM(require("path"));
2382
- async function compileSvelte(elementsDir, outDir, ns = "") {
2383
- const fileSystem = {};
2384
- const processFile = async (elementFilePath) => {
2385
- const elementDetailList = await getFileElements(elementFilePath);
2386
- Object.assign(
2387
- fileSystem,
2388
- Object.fromEntries(
2389
- elementDetailList.map(({ name: tag, constructorName, properties }) => {
2390
- const componentName = getComponentName(tag);
2391
- const componentPropsName = `${componentName}Props`;
2392
- const relativePath = getRelativePath(elementFilePath, outDir);
2393
- const basename = import_path3.default.basename(relativePath);
2394
- return [
2395
- [ns, basename].filter((e) => !!e).join("-") + ".ts",
2396
- `
2397
- import type { HTMLAttributes } from "svelte/elements";
2398
- import { ${constructorName} } from '${relativePath}';
2399
- export * from '${relativePath}';
2400
-
2401
- interface ${componentPropsName} extends HTMLAttributes<HTMLElement> {
2402
- ${properties.map(
2403
- ({ name: name2, getter, event, deprecated }) => event ? [
2404
- getJsDocDescName(`'on:${event}'`, deprecated),
2405
- `(event: CustomEvent<Parameters<${constructorName}['${name2}']>[0]>) => void`
2406
- ].join("?:") : !getter ? [getJsDocDescName(name2, deprecated), `${constructorName}['${name2}']`].join("?:") : ""
2407
- ).join(";\n")}
2408
- };
2409
-
2410
- declare module "svelte/elements" {
2411
- interface SvelteHTMLElements {
2412
- '${tag}': ${componentPropsName};
2413
- }
2414
- }
2415
- `
2416
- ];
2417
- })
2418
- )
2419
- );
2420
- };
2421
- await Promise.all(getElementPathList(elementsDir).map(processFile));
2422
- compile(outDir, fileSystem);
2423
- }
2424
-
2425
2431
  // src/index.ts
2426
2432
  var cliOptions = {
2427
2433
  outDir: "./",
@@ -2430,9 +2436,9 @@ var cliOptions = {
2430
2436
  var timer = setTimeout(() => import_commander.default.outputHelp());
2431
2437
  import_commander.default.name(name).description(description).version(version, "-v, --version").option("-o, --outdir <path>", `specify out dir`, (outdir) => cliOptions.outDir = outdir).option("--svelte-ns <ns>", `specify svelte element namespace`, (ns) => cliOptions.svelteNs = ns).arguments("<dir>").action(async (dir) => {
2432
2438
  clearTimeout(timer);
2433
- await compileReact(dir, import_path4.default.resolve(cliOptions.outDir, "react"));
2434
- await generateVue(dir, import_path4.default.resolve(cliOptions.outDir, "vue"));
2435
- await compileSvelte(dir, import_path4.default.resolve(cliOptions.outDir, "svelte"), cliOptions.svelteNs);
2439
+ await compileReact(dir, import_node_path4.default.resolve(cliOptions.outDir, "react"));
2440
+ await generateVue(dir, import_node_path4.default.resolve(cliOptions.outDir, "vue"));
2441
+ await compileSvelte(dir, import_node_path4.default.resolve(cliOptions.outDir, "svelte"), cliOptions.svelteNs);
2436
2442
  process.exit(0);
2437
2443
  });
2438
2444
  import_commander.default.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantou/gem-port",
3
- "version": "1.1.0",
3
+ "version": "1.2.4",
4
4
  "description": "Export React component",
5
5
  "keywords": [
6
6
  "gem",
@@ -19,12 +19,14 @@
19
19
  "prepublishOnly": "pnpm build"
20
20
  },
21
21
  "dependencies": {
22
- "@gemjs/config": "^2.1.0",
23
22
  "commander": "^7.2.0",
24
- "gem-analyzer": "^2.1.0",
25
- "ts-morph": "^13.0.0",
23
+ "gem-analyzer": "^2.2.4",
24
+ "ts-morph": "^12.0.0",
26
25
  "typescript": "^5.6.2"
27
26
  },
27
+ "devDependencies": {
28
+ "@gemjs/config": "^2.1.3"
29
+ },
28
30
  "author": "mantou132",
29
31
  "license": "MIT",
30
32
  "repository": {
@@ -35,6 +37,5 @@
35
37
  "bugs": {
36
38
  "url": "https://github.com/mantou132/gem/issues"
37
39
  },
38
- "homepage": "https://github.com/mantou132/gem#readme",
39
- "gitHead": "00c0b40a5f92eaf3fce6e92b1a1f656565e69811"
40
+ "homepage": "https://github.com/mantou132/gem#readme"
40
41
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021-present
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.