-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
packages/*/dist | ||
packages/*/react | ||
packages/*/bin | ||
packages/*/coverage | ||
packages/*/extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# Compiler output | ||
/lib/ | ||
/elements/ | ||
/react/ | ||
/locales/ | ||
/coverage/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "gem-port", | ||
"version": "0.0.1", | ||
"description": "Export React component", | ||
"keywords": [ | ||
"gem", | ||
"react", | ||
"generator" | ||
], | ||
"bin": { | ||
"gem-port": "bin/index.js" | ||
}, | ||
"files": [ | ||
"/bin/" | ||
], | ||
"scripts": { | ||
"build": "esbuild ./src/index.ts --outdir=./bin --bundle --platform=node --minify --sourcemap --external:typescript", | ||
"start": "yarn build --watch", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"dependencies": { | ||
"@gemjs/config": "^1.6.11", | ||
"commander": "^7.2.0", | ||
"gem-analyzer": "^1.7.0", | ||
"ts-morph": "^13.0.0", | ||
"typescript": "^4.5.0" | ||
}, | ||
"devDependencies": { | ||
}, | ||
"author": "mantou132", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mantou132/gem.git", | ||
"directory": "packages/gem-port" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/mantou132/gem/issues" | ||
}, | ||
"homepage": "https://github.com/mantou132/gem#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/env node | ||
|
||
import path from 'path'; | ||
import { readdirSync, readFileSync, statSync } from 'fs'; | ||
|
||
import { getElements } from 'gem-analyzer'; | ||
import { Project } from 'ts-morph'; | ||
import * as ts from 'typescript'; | ||
import program from 'commander'; | ||
|
||
import { name, description, version } from '../package.json'; | ||
|
||
const cliOptions = { | ||
outDir: './', | ||
}; | ||
|
||
function createReactSourceFile(elementFilePath: string, outDir: string) { | ||
const content = readFileSync(elementFilePath, { encoding: 'utf-8' }); | ||
const project = new Project({ useInMemoryFileSystem: true }); | ||
const file = project.createSourceFile(elementFilePath, content); | ||
const basename = path.basename(elementFilePath, path.extname(elementFilePath)); | ||
// FIXME | ||
const relativePath = path.relative(path.resolve(outDir), path.dirname(elementFilePath)).replace('src/', ''); | ||
return Object.fromEntries( | ||
getElements(file).map(({ name: tag, constructorName, properties, methods, events }) => { | ||
const componentName = constructorName.replace('Element', ''); | ||
const componentPropsName = `${componentName}Props`; | ||
const componentMethodsName = `${componentName}Methods`; | ||
return [ | ||
componentName + '.tsx', | ||
` | ||
import React, { ForwardRefExoticComponent, HTMLAttributes, RefAttributes, forwardRef, useImperativeHandle, useRef } from 'react'; | ||
import { TemplateResult } from '@mantou/gem/lib/element'; | ||
import { ${constructorName} } from '${relativePath}/${basename}'; | ||
export { ${constructorName} }; | ||
export type ${componentPropsName} = HTMLAttributes<HTMLDivElement> & RefAttributes<${constructorName}> & { | ||
${properties | ||
.map(({ name, attribute, reactive }) => | ||
!reactive ? '' : [name, attribute ? 'string' : 'any'].join('?:'), | ||
) | ||
.join(';')} | ||
${events.map((event) => [`on${event}`, `(arg: CustomEvent<any>) => any`].join('?:')).join(';')} | ||
}; | ||
export type ${componentMethodsName} = { | ||
${methods.map(({ name }) => [name, `typeof ${constructorName}.prototype.${name}`].join(': ')).join(';')} | ||
} | ||
declare global { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
'${tag}': ${componentPropsName}; | ||
} | ||
} | ||
} | ||
const ${componentName}: ForwardRefExoticComponent<Omit<${componentPropsName}, "ref"> & RefAttributes<${componentMethodsName}>> = forwardRef<${componentMethodsName}, ${componentPropsName}>(function (props, ref): JSX.Element { | ||
const elementRef = useRef<${constructorName}>(null); | ||
useImperativeHandle(ref, () => { | ||
return { | ||
${methods | ||
.map( | ||
({ name }) => ` | ||
${name}(...args) { | ||
elementRef.current?.${name}(...args) | ||
} | ||
`, | ||
) | ||
.join(',')} | ||
}; | ||
}, []); | ||
return <${tag} ref={elementRef} {...props}></${tag}>; | ||
}) | ||
export default ${componentName}; | ||
`, | ||
]; | ||
}), | ||
); | ||
} | ||
|
||
function createSourceFiles(elementsDir: string, outDir: string) { | ||
const fileSystem: Record<string, string> = {}; | ||
readdirSync(elementsDir).forEach((filename) => { | ||
const elementFilePath = path.resolve(elementsDir, filename); | ||
if (statSync(elementFilePath).isFile()) { | ||
// if (!elementFilePath.includes('color-pick') && !elementFilePath.includes('card')) return; | ||
Object.assign(fileSystem, createReactSourceFile(elementFilePath, outDir)); | ||
} | ||
}); | ||
return fileSystem; | ||
} | ||
|
||
function compile(elementsDir: string): void { | ||
const outDir = path.resolve(cliOptions.outDir, 'react'); | ||
const options: ts.CompilerOptions = { | ||
jsx: ts.JsxEmit.React, | ||
target: ts.ScriptTarget.ES2020, | ||
declaration: true, | ||
outDir, | ||
}; | ||
const fileSystem = createSourceFiles(elementsDir, outDir); | ||
const host = ts.createCompilerHost(options); | ||
const originReadFile = host.readFile; | ||
host.readFile = (filename: string) => { | ||
if (filename in fileSystem) return fileSystem[filename]; | ||
return originReadFile(filename); | ||
}; | ||
const program = ts.createProgram(Object.keys(fileSystem), options, host); | ||
program.emit(); | ||
} | ||
|
||
program | ||
.name(name) | ||
.description(description) | ||
.version(version, '-v, --version') | ||
.option('-o, --outdir <path>', `specify out dir`, (outdir: string) => (cliOptions.outDir = outdir), cliOptions.outDir) | ||
.arguments('<dir>') | ||
.action((dir: string) => { | ||
compile(dir); | ||
process.exit(0); | ||
}); | ||
|
||
program.parse(process.argv).outputHelp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@gemjs/config/tsconfig", | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
}, | ||
"include": ["src"], | ||
"exclude": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters