2 Star 0 Fork 0

lazycatcloud/lzc-file-picker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gen_fp_dts.js 4.84 KB
一键复制 编辑 原始数据 按行查看 历史
dyh 提交于 2025-04-01 15:00 +08:00 . backend/feat:init file picker
#!/usr/bin/env node
const path = require('path')
const { join } = path
const fs = require('fs')
const { execSync } = require('child_process')
const workspace = join(__dirname, 'libs/lzc-file-pickers')
const dts = join(workspace, 'dts')
const types = join(dts, 'types.d.ts')
const jsx = join(dts, 'jsx.d.ts')
const buildVueSFC = join(__dirname, 'dist/PP/FilePicker.ce.vue.d.ts')
const vueSFC = join(dts, 'vue_sfc.d.ts')
const vue3 = join(dts, 'vue3.d.ts')
const vue27 = join(dts, 'vue27.d.ts')
const vue26 = join(dts, 'vue26.d.ts')
const sourcePath = join(__dirname, 'src/components/FilePicker.ce.vue')
const MATCH_REGEXP = /\/\/===([\s\S]*?)\/\/===/
function mkdirpSync(dir) {
if (fs.existsSync(dir)) {
return
}
const parent = path.dirname(dir)
if (parent !== dir) {
mkdirpSync(parent)
}
fs.mkdirSync(dir)
}
mkdirpSync(dts)
console.log('current workspace is ', workspace)
console.log('current sourcePath is ', sourcePath)
const code = fs.readFileSync(sourcePath, 'utf-8')
const reg = code.match(MATCH_REGEXP)
// if (!reg) {
// console.log('ooops!! match code error')
// process.exit(0)
// }
const [, target] = reg
const FS_interface = `
/// <reference types="node" />
export type IMixinWebcomponent<T> = {
_instance: {
exposed: T
}
}
export interface FileStat {
filename: string;
basename: string;
lastmod: string;
size: number;
type: "file" | "directory";
etag: string | null;
mime?: string;
props?: any; // FIXME: Are you sure you need this field?
}
`.trim()
const output = `${FS_interface}${target}`
fs.writeFileSync(types, output, 'utf-8')
console.log('write types to ', types)
const jsxSupport = `
import { IProps, IExpose, IMixinWebcomponent, FileStat, Arrayable, Promiseable, FileStatAndWebdavFile } from './types'
interface IRefObject<T> {
readonly current: T | null
}
interface CHMap extends HTMLElementEventMap {
'submit': any
'visible': any
'done': any
}
export type IFilePickerInstance = IMixinWebcomponent<IExpose> & HTMLElement & {
addEventListener<K extends keyof CHMap>(type: K, listener: (this: HTMLElement, ev: CHMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof CHMap>(type: K, listener: (this: HTMLElement, ev: CHMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
export type IFilePickerInstanceRef = IRefObject<IFilePickerInstance>
export type IRef = {
ref: IFilePickerInstanceRef | Function
}
export interface CustomEventAttributes {
onSubmit?: (data: Arrayable<FileStat> | Promiseable<FileStatAndWebdavFile | PromiseSettledResult<FileStatAndWebdavFile>[]>) => void;
onVisible?: (show: boolean) => void;
onDone?: (status: Promiseable<boolean> | FileStat) => void;
}
export type IComponent = IProps & Partial<Readonly<IRef>> & Readonly<CustomEventAttributes>
declare global {
namespace JSX {
interface IntrinsicElements {
['lzc-file-picker']: IComponent
}
}
}
export {}
`.trimStart()
fs.writeFileSync(jsx, jsxSupport, 'utf-8')
console.log('write jsx type to ', jsx)
console.log('start build SFC')
// remove this
// execSync("npm run build-lib", { stdio: "inherit" })
// execSync("sleep 1")
let SFC = fs.readFileSync(buildVueSFC, 'utf-8')
SFC = SFC.replace(
'import type { FileStat } from "@lazycatcloud/webdav/web";',
`import type { FileStat /* , Arrayable, Promiseable, FileStatAndWebdavFile */ } from './types'`,
)
SFC = SFC.replace(
`import type { LzcWebdavClient } from '../shared/utils/lzcWebdavClient';`,
'export type LzcWebdavClient = any; /* TODO: add this type :^) */',
)
SFC = SFC.replace(
`import { isMobile } from '../shared/utils/isMobile';`,
'export type isMobile = ()=> boolean;',
)
SFC = SFC.replace(
/onSubmit.*\((.*)\[\]\)(.*)\|/,
'onSubmit?: (data: Arrayable<FileStat> | Promiseable<FileStatAndWebdavFile | PromiseSettledResult<FileStatAndWebdavFile>[]>) => void |',
).replace(
/onDone.*\((.*)\[\]\)(.*)\|/,
'onDone?: (status: Promiseable<boolean> | FileStat) => void |',
)
fs.writeFileSync(vueSFC, SFC, 'utf-8')
const vue3Support = `
declare module '@vue/runtime-core' {
export interface GlobalComponents {
LzcFilePicker: typeof import('./vue_sfc')
}
}
export {}
`.trimStart()
fs.writeFileSync(vue3, vue3Support, 'utf-8')
const v27Support = `
declare module 'vue' {
export interface GlobalComponents {
LzcFilePicker: typeof import('./vue_sfc')
}
}
export {}
`
const v26Support = `
declare module '@vue/runtime-dom' {
export interface GlobalComponents {
LzcFilePicker: typeof import('./vue_sfc')
}
}
export {}
`
fs.writeFileSync(vue26, v27Support, 'utf-8')
fs.writeFileSync(vue27, v26Support, 'utf-8')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lazycatcloud/lzc-file-picker.git
git@gitee.com:lazycatcloud/lzc-file-picker.git
lazycatcloud
lzc-file-picker
lzc-file-picker
master

搜索帮助