@mantou/gem-port 1.0.0 → 1.2.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/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,28 +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.0.0";
1857
+ var version = "1.2.1";
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, fromEntries, entries, keys } = Object;
1865
1866
  function camelToKebabCase(str) {
1866
- return str.replace(/[A-Z]/g, ($1) => "-" + $1.toLowerCase());
1867
+ return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
1867
1868
  }
1868
1869
 
1869
1870
  // ../gem-analyzer/lib/utils.js
1870
1871
  function getJsDoc(declaration) {
1871
1872
  if ("getJsDocs" in declaration) {
1872
1873
  const jsDocs = declaration.getJsDocs();
1873
- 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");
1874
1875
  const comment = jsDocs.map((jsDoc) => jsDoc.getCommentText()).join("\n\n");
1875
1876
  return {
1876
1877
  deprecated,
@@ -1890,6 +1891,10 @@ function isGetter(declaration, kind = "get") {
1890
1891
  function isSetter(declaration) {
1891
1892
  return isGetter(declaration, "set");
1892
1893
  }
1894
+ function isPrivateId(name2) {
1895
+ if (name2.startsWith("#") || name2.startsWith("_"))
1896
+ return true;
1897
+ }
1893
1898
 
1894
1899
  // ../gem-analyzer/index.js
1895
1900
  var shadowDecoratorName = ["shadow"];
@@ -1953,23 +1958,25 @@ var parseElement = async (declaration, file, project2) => {
1953
1958
  };
1954
1959
  const className = declaration.getName();
1955
1960
  const constructorExtendsName = declaration.getExtends()?.getText();
1956
- const constructor = declaration.getConstructors()[0];
1957
- 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;
1958
1965
  declaration.getJsDocs().forEach((jsDoc) => appendElementDesc(jsDoc.getCommentText()));
1959
1966
  if (className && constructorExtendsName) {
1960
1967
  detail.extend = await getExtendsClassDetail(constructorExtendsName, file, project2);
1961
1968
  detail.constructorName = className;
1962
1969
  detail.constructorExtendsName = constructorExtendsName;
1963
- if (constructor) {
1970
+ if (cons) {
1964
1971
  const params = {};
1965
- const jsDocs = constructor.getJsDocs();
1972
+ const jsDocs = cons.getJsDocs();
1966
1973
  jsDocs.forEach((jsDoc) => {
1967
1974
  appendElementDesc(jsDoc.getDescription());
1968
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 }) => {
1969
1976
  params[name2] = comment;
1970
1977
  });
1971
1978
  });
1972
- detail.constructorParams = constructor.getParameters().map((param) => ({
1979
+ detail.constructorParams = cons.getParameters().map((param) => ({
1973
1980
  name: param.getName(),
1974
1981
  type: getTypeText(param),
1975
1982
  description: params[param.getName()]
@@ -1979,7 +1986,7 @@ var parseElement = async (declaration, file, project2) => {
1979
1986
  const staticPropertiesDeclarations = declaration.getStaticProperties();
1980
1987
  for (const staticPropDeclaration of staticPropertiesDeclarations) {
1981
1988
  const staticPropName = staticPropDeclaration.getName();
1982
- if (staticPropName.startsWith("#"))
1989
+ if (isPrivateId(staticPropName))
1983
1990
  continue;
1984
1991
  const prop = {
1985
1992
  name: staticPropName,
@@ -2009,7 +2016,7 @@ var parseElement = async (declaration, file, project2) => {
2009
2016
  const staticMethodDeclarations = declaration.getStaticMethods();
2010
2017
  for (const staticMethodDeclaration of staticMethodDeclarations) {
2011
2018
  const staticMethodName = staticMethodDeclaration.getName();
2012
- if (staticMethodName.startsWith("#"))
2019
+ if (isPrivateId(staticMethodName))
2013
2020
  continue;
2014
2021
  const method = {
2015
2022
  name: staticMethodName,
@@ -2021,7 +2028,7 @@ var parseElement = async (declaration, file, project2) => {
2021
2028
  const propDeclarations = declaration.getInstanceProperties();
2022
2029
  for (const propDeclaration of propDeclarations) {
2023
2030
  const propName = propDeclaration.getName();
2024
- if (propName.startsWith("#"))
2031
+ if (isPrivateId(propName))
2025
2032
  continue;
2026
2033
  if (lifecyclePopsOrMethods.includes(propName))
2027
2034
  continue;
@@ -2066,7 +2073,7 @@ var parseElement = async (declaration, file, project2) => {
2066
2073
  const methodDeclarations = declaration.getInstanceMethods();
2067
2074
  for (const methodDeclaration of methodDeclarations) {
2068
2075
  const methodName = methodDeclaration.getName();
2069
- if (methodName.startsWith("#"))
2076
+ if (isPrivateId(methodName))
2070
2077
  continue;
2071
2078
  if (lifecyclePopsOrMethods.includes(methodName))
2072
2079
  continue;
@@ -2100,7 +2107,7 @@ var getElements = async (file, project2) => {
2100
2107
  for (const declaration of file.getClasses()) {
2101
2108
  const elementDecorators = declaration.getDecorators();
2102
2109
  const elementDeclaration = elementDecorators.find((decorator) => elementDecoratorName.includes(decorator.getName()));
2103
- 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();
2104
2111
  if (elementTag) {
2105
2112
  const detail = {
2106
2113
  ...await parseElement(declaration, file, project2),
@@ -2120,8 +2127,8 @@ var ts = __toESM(require("typescript"));
2120
2127
  var dev = process.env.MODE === "dev";
2121
2128
  var elements = process.env.ELEMENTS?.split(",");
2122
2129
  function getElementPathList(elementsDir) {
2123
- const paths = (0, import_fs.readdirSync)(elementsDir).map((filename) => import_path.default.resolve(elementsDir, filename)).filter((elementFilePath, index) => {
2124
- 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;
2125
2132
  if (elements) {
2126
2133
  return elements?.some((ele) => elementFilePath.includes(ele));
2127
2134
  } else if (dev) {
@@ -2143,13 +2150,13 @@ var getChain = (detail) => {
2143
2150
  }
2144
2151
  return result;
2145
2152
  };
2146
- var mergeElementDetail = (elementFilePath, detail) => {
2153
+ var mergeElementDetail = (detail) => {
2147
2154
  const chain = getChain(detail);
2148
2155
  return {
2149
2156
  ...detail,
2150
- properties: chain.map((e) => e.properties).flat(),
2151
- methods: chain.map((e) => e.methods).flat(),
2152
- 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)
2153
2160
  };
2154
2161
  };
2155
2162
  var elementCache = {};
@@ -2157,13 +2164,13 @@ var project = new import_ts_morph.Project({
2157
2164
  useInMemoryFileSystem: true,
2158
2165
  compilerOptions: { target: import_ts_morph.ts.ScriptTarget.ESNext }
2159
2166
  });
2160
- project.getFileSystem().readFile = (filePath) => import_fs.promises.readFile(filePath, "utf8");
2167
+ project.getFileSystem().readFile = (filePath) => import_node_fs.promises.readFile(filePath, "utf8");
2161
2168
  async function getFileElements(elementFilePath) {
2162
2169
  if (!elementCache[elementFilePath]) {
2163
- const text = (0, import_fs.readFileSync)(elementFilePath, { encoding: "utf-8" });
2170
+ const text = (0, import_node_fs.readFileSync)(elementFilePath, { encoding: "utf-8" });
2164
2171
  const file = project.getSourceFile(elementFilePath) || project.createSourceFile(elementFilePath, text);
2165
2172
  const details = await getElements(file, project);
2166
- elementCache[elementFilePath] = details.map((detail) => mergeElementDetail(elementFilePath, detail));
2173
+ elementCache[elementFilePath] = details.map((detail) => mergeElementDetail(detail));
2167
2174
  }
2168
2175
  return elementCache[elementFilePath];
2169
2176
  }
@@ -2174,8 +2181,8 @@ function getJsDocDescName(name2, deprecated) {
2174
2181
  return `${deprecated ? "/**@deprecated */\n" : ""}${name2}`;
2175
2182
  }
2176
2183
  function getRelativePath(elementFilePath, outDir) {
2177
- const basename = import_path.default.basename(elementFilePath, import_path.default.extname(elementFilePath));
2178
- 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/", "");
2179
2186
  return `${relativePath}/${basename}`;
2180
2187
  }
2181
2188
  function compile(outDir, fileSystem) {
@@ -2215,7 +2222,7 @@ async function createReactSourceFile(elementFilePath, outDir) {
2215
2222
  const getters = properties.filter((e) => e.getter);
2216
2223
  const settableProperties = properties.filter((e) => !e.getter && !e.event);
2217
2224
  return [
2218
- componentName + ".tsx",
2225
+ `${componentName}.tsx`,
2219
2226
  `
2220
2227
  import React, { HTMLAttributes, RefAttributes } from 'react';
2221
2228
  import React, { ForwardRefExoticComponent, forwardRef, useImperativeHandle, useRef, useLayoutEffect } from 'react';
@@ -2292,11 +2299,56 @@ async function compileReact(elementsDir, outDir) {
2292
2299
  compile(outDir, fileSystem);
2293
2300
  }
2294
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
+
2295
2347
  // src/vue.ts
2296
- var import_fs2 = require("fs");
2297
- var import_path2 = require("path");
2348
+ var import_node_fs2 = require("node:fs");
2349
+ var import_node_path3 = require("node:path");
2298
2350
  async function generateVue(elementsDir, outDir) {
2299
- (0, import_fs2.mkdirSync)(outDir, { recursive: true });
2351
+ (0, import_node_fs2.mkdirSync)(outDir, { recursive: true });
2300
2352
  const processFile = async (elementFilePath) => {
2301
2353
  const elements2 = await getFileElements(elementFilePath);
2302
2354
  elements2.forEach(({ name: tag, properties, constructorName, methods, events }) => {
@@ -2305,8 +2357,8 @@ async function generateVue(elementsDir, outDir) {
2305
2357
  const relativePath = getRelativePath(elementFilePath, outDir);
2306
2358
  const settableProperties = properties.filter((e) => !e.getter && !e.event);
2307
2359
  const getters = properties.filter((e) => e.getter);
2308
- (0, import_fs2.writeFileSync)(
2309
- (0, import_path2.resolve)(outDir, componentName + ".vue"),
2360
+ (0, import_node_fs2.writeFileSync)(
2361
+ (0, import_node_path3.resolve)(outDir, `${componentName}.vue`),
2310
2362
  `
2311
2363
  <script setup lang="ts">
2312
2364
  import { ref, defineProps, defineEmits, defineExpose, onMounted } from 'vue'
@@ -2376,51 +2428,6 @@ async function generateVue(elementsDir, outDir) {
2376
2428
  await Promise.all(getElementPathList(elementsDir).map(processFile));
2377
2429
  }
2378
2430
 
2379
- // src/svelte.ts
2380
- var import_path3 = __toESM(require("path"));
2381
- async function compileSvelte(elementsDir, outDir, ns = "") {
2382
- const fileSystem = {};
2383
- const processFile = async (elementFilePath) => {
2384
- const elementDetailList = await getFileElements(elementFilePath);
2385
- Object.assign(
2386
- fileSystem,
2387
- Object.fromEntries(
2388
- elementDetailList.map(({ name: tag, constructorName, properties }) => {
2389
- const componentName = getComponentName(tag);
2390
- const componentPropsName = `${componentName}Props`;
2391
- const relativePath = getRelativePath(elementFilePath, outDir);
2392
- const basename = import_path3.default.basename(relativePath);
2393
- return [
2394
- [ns, basename].filter((e) => !!e).join("-") + ".ts",
2395
- `
2396
- import type { HTMLAttributes } from "svelte/elements";
2397
- import { ${constructorName} } from '${relativePath}';
2398
- export * from '${relativePath}';
2399
-
2400
- interface ${componentPropsName} extends HTMLAttributes<HTMLElement> {
2401
- ${properties.map(
2402
- ({ name: name2, getter, event, deprecated }) => event ? [
2403
- getJsDocDescName(`'on:${event}'`, deprecated),
2404
- `(event: CustomEvent<Parameters<${constructorName}['${name2}']>[0]>) => void`
2405
- ].join("?:") : !getter ? [getJsDocDescName(name2, deprecated), `${constructorName}['${name2}']`].join("?:") : ""
2406
- ).join(";\n")}
2407
- };
2408
-
2409
- declare module "svelte/elements" {
2410
- interface SvelteHTMLElements {
2411
- '${tag}': ${componentPropsName};
2412
- }
2413
- }
2414
- `
2415
- ];
2416
- })
2417
- )
2418
- );
2419
- };
2420
- await Promise.all(getElementPathList(elementsDir).map(processFile));
2421
- compile(outDir, fileSystem);
2422
- }
2423
-
2424
2431
  // src/index.ts
2425
2432
  var cliOptions = {
2426
2433
  outDir: "./",
@@ -2429,9 +2436,9 @@ var cliOptions = {
2429
2436
  var timer = setTimeout(() => import_commander.default.outputHelp());
2430
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) => {
2431
2438
  clearTimeout(timer);
2432
- await compileReact(dir, import_path4.default.resolve(cliOptions.outDir, "react"));
2433
- await generateVue(dir, import_path4.default.resolve(cliOptions.outDir, "vue"));
2434
- 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);
2435
2442
  process.exit(0);
2436
2443
  });
2437
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.0.0",
3
+ "version": "1.2.1",
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.0.0",
23
22
  "commander": "^7.2.0",
24
- "gem-analyzer": "^2.0.0",
23
+ "gem-analyzer": "^2.2.1",
25
24
  "ts-morph": "^13.0.0",
26
25
  "typescript": "^5.6.2"
27
26
  },
27
+ "devDependencies": {
28
+ "@gemjs/config": "^2.1.1"
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": "499af6e71e3c161bd74f87202e770ebefbc95b33"
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.