index.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. import EventEmitter from 'events';
  2. // eslint-disable-next-line import/no-extraneous-dependencies
  3. import { Command } from 'commander';
  4. declare module 'minidev' {
  5. export type DeepPartial<T> = {
  6. [P in keyof T]?:T[P] extends Record<any, any> ? DeepPartial<T[P]> : T[P];
  7. };
  8. export interface IConfigInits {
  9. defaults: {
  10. [key: string]: any
  11. };
  12. globalConfigFilePath: string;
  13. projectConfigRelativePath: string;
  14. }
  15. export interface IMinidevLibDefaults {
  16. name: string;
  17. description: string;
  18. config: IConfigInits;
  19. }
  20. /**
  21. * 配置默认项
  22. * @param overrides
  23. */
  24. export function useDefaults(overrides: DeepPartial<IMinidevLibDefaults>): void;
  25. export function usePlugin(plugin: IMinidevPlugin): void;
  26. export function setSilent(boolean): void;
  27. export function setSpinnerEnabled(enabled: boolean): void;
  28. export function setLogLevelVerboseEnabled(enabled: boolean): void;
  29. export type MaybePromise<T = void> = T | Promise<T>;
  30. export interface IMinidevPlugin {
  31. name: string,
  32. contribute(registry: IMinidevPluginRegistry, injector: { get: (token: any) => any}): void;
  33. }
  34. export interface IMinidevPluginRegistry {
  35. hooks: {
  36. onCreateDevServer(hook: (context: {
  37. devServer: IDevServer;
  38. }) => void, weight?: number | undefined);
  39. onDidRunLocalBuild(hook: (context: {
  40. build: ILocalBuildCompileBuild;
  41. }) => void, weight?: number | undefined);
  42. }
  43. }
  44. export interface IAuthPathOptions {
  45. identityKeyPath?: string;
  46. }
  47. export interface IVersionListResult {
  48. /**
  49. * 端参数,用于查询多端版本,不传默认为支付宝端。 高德端:com.amap.app
  50. */
  51. bundleId?: string;
  52. appVersion: string;
  53. versionDescription: string;
  54. versionStatus: string;
  55. createTime: string;
  56. operateName: string;
  57. canRelease: any;
  58. }
  59. export const minidev: IMinidev;
  60. export default minidev;
  61. export function getProgram(): Command;
  62. export interface IMinidev {
  63. /**
  64. * 构建编译小程序
  65. * @param opts
  66. */
  67. build(opts: IBuildArgs): Promise<ILocalBuildCompileBuild>;
  68. /**
  69. * 启动开发服务器 DevServer
  70. * @param opts
  71. * @param onDone
  72. */
  73. dev(opts: IDevCommandArgs, onDone?: () => any): Promise<IDevServerCompileBuild>
  74. /**
  75. * 使用 DevServer 产物发起真机预览 (此模式仅整包, 无法验证分包可用性)
  76. * 需要在运行 dev 之后运行
  77. * @param opts
  78. */
  79. devPreview(opts: IDevPreviewArgs, build?: IDevServerCompileBuild): Promise<IGenerateDebugQRCodeResult>;
  80. devWebSimulator(opts:IDevWebSimulatorArgs, build?: IDevServerCompileBuild): Promise<IWebSimulatorLauncher>
  81. /**
  82. * 使用 DevServer 产物发起真机调试 (此模式仅整包, 无法验证分包可用性)
  83. * 需要在运行 dev 之后运行
  84. * @param opts
  85. */
  86. devRemoteDebug(opts: IDevRemoteDebugArgs, build?: IDevServerCompileBuild): Promise<IRemoteDebugReturn>
  87. /**
  88. * 启动小程序开发者工具模拟器对 DevServer 产物进行调试
  89. * 需要在运行 dev 之后运行
  90. * @param opts
  91. */
  92. startIdeForDevServer(opts: IDevIDEOptions, build?: IDevServerCompileBuild): Promise<void>;
  93. /**
  94. * 进行工具授权
  95. * @param opts
  96. */
  97. login(opts: IClientTypeOptions, onCreateLoginTask?: (loginTask: ILoginTask) => void): Promise<void>;
  98. /**
  99. * 上传发布小程序
  100. * @param opts
  101. * @param @optional hooks
  102. */
  103. upload(opts: IUploadOptions, hooks?: IPublishHooks): Promise<IPublishResult>;
  104. /**
  105. * 构建并发起真机预览
  106. * @param opts
  107. */
  108. preview(opts: IPreviewArgs): Promise<IGenerateDebugQRCodeResult>;
  109. /**
  110. * 构建并发起远程调试
  111. * @param opts
  112. */
  113. remoteDebug(opts: IRemoteDebugArgs): Promise<IRemoteDebugReturn>
  114. /**
  115. * 清除构建缓存
  116. */
  117. cleanBuildCache(): Promise<void>;
  118. /**
  119. * 打包源码
  120. * @param opts
  121. */
  122. pack(opts: IPackSourceOptions): Promise<void>;
  123. /**
  124. * 启动 IDE
  125. * @param opts
  126. */
  127. startIde(opts: IIDECommandOptions): Promise<void>;
  128. /**
  129. * 设置相关操作
  130. */
  131. config: IMinidevConfig;
  132. /**
  133. * 小程序管理相关操作
  134. */
  135. app: IMinidevApp
  136. }
  137. export interface ILoginTask {
  138. on(event: 'qrcode-generated', listener: (qrCodeUrl: string) => any): any;
  139. on(event: 'success', listener: () => any): any;
  140. on(event: 'scan', listener: () => any): any;
  141. on(event: 'polling', listener: (remainingMs: number) => any): any;
  142. removeListener(event: string, listener: (...args) => any): any;
  143. }
  144. export interface IRemoteDebugReturn extends IGenerateDebugQRCodeResult{
  145. /**
  146. * 调试地址
  147. */
  148. debugUrl: string;
  149. /**
  150. * 停止调试服务
  151. */
  152. stopServer: () => Promise<void>;
  153. }
  154. export interface IPublishHooks {
  155. onLog?:(data: string) => void;
  156. onTaskCreated?: (taskId: string) => void;
  157. onVersionCreated?: (version: string) => void;
  158. }
  159. export interface IMinidevConfig {
  160. get<T = any>(configName: string, opts?: IConfigOptions): Promise<T>;
  161. set<T = any>(configName: string, value: T, opts?: IConfigOptions): Promise<void>;
  162. /**
  163. * 设置 **RUNTIME** 的配置项
  164. */
  165. useRuntime(config: Record<string, any>): Promise<void>;
  166. }
  167. export interface IAppInfo {
  168. appId: string;
  169. appName: string;
  170. logoUrl: string;
  171. subApplicationType: 'TINYAPP_NORMAL' | 'TINYAPP_PLUGIN' | 'TINYAPP_TEMPLATE'
  172. }
  173. export interface IGetAppUploadedVersionArgs extends IAppIdArgs, IClientTypeOptions, IAuthPathOptions {
  174. }
  175. export interface IVersionListOptions extends IAppIdArgs, IClientTypeOptions, IAuthPathOptions {
  176. /**
  177. * 版本状态列表
  178. */
  179. versionStatus?: string;
  180. /**
  181. * 以 JSON 格式打印
  182. */
  183. json?: boolean;
  184. }
  185. export interface IMinidevApp {
  186. /**
  187. * 获取小程序列表
  188. * @param options
  189. */
  190. getList(options: IClientTypeOptions): Promise<IAppInfo>;
  191. /**
  192. * 获取小程序的最新上传版本号
  193. * @param options
  194. */
  195. getUploadedVersion(options: IGetAppUploadedVersionArgs): Promise<string>;
  196. /**
  197. * 获取小程序的上传版本列表
  198. * @param options
  199. */
  200. getUploadedVersionList(options: IVersionListOptions): Promise<IVersionListResult>;
  201. /**
  202. * 删除指定版本
  203. * 如果该版本为体验版,会先取消该体验版
  204. * @param options
  205. */
  206. deleteVersion(options: IAppIdAndVersionOptions): Promise<void>;
  207. /**
  208. * 取消体验版
  209. * @param options
  210. */
  211. cancelExperience(options: IAppIdAndVersionOptions): Promise<void>;
  212. /**
  213. * 设置体验版
  214. * @param options
  215. * @return qrCodeUrl 体验版二维码
  216. */
  217. setExperience(options: IAppIdAndVersionOptions): Promise<{qrCodeUrl: string}>;
  218. }
  219. export interface IAppIdAndVersionOptions extends IAppIdArgs, IClientTypeOptions, IAuthPathOptions {
  220. version: string;
  221. }
  222. export interface IWebSimulatorLauncher {
  223. bundled: string;
  224. /**
  225. * 这里返回的 simulator 和 devtool 均为不含 uuid 的模板地址
  226. * 如何使用:
  227. * const simulatorUrl = separated.simulator.replace('___uuid___', '当前会话的 uuid')
  228. * const devtoolUrl = separated.devtool.replace('___uuid___', '当前会话的 uuid')
  229. */
  230. separated: {
  231. simulator: string;
  232. devtool: string;
  233. }
  234. }
  235. export interface IConfigOptions extends IProjectArgs {
  236. scope?: ConfigScope.GLOBAL | ConfigScope.PROJECT;
  237. }
  238. export enum ConfigScope {
  239. /**
  240. * 用户配置
  241. */
  242. GLOBAL = 'global',
  243. /**
  244. * 当前项目的配置
  245. */
  246. PROJECT = 'project',
  247. }
  248. export interface IDevPreviewArgs extends IGenerateQrCodeArgs, IAppIdArgs, IAuthPathOptions {
  249. }
  250. export interface IDevWebSimulatorArgs extends IOptionalAppIdArgs, IClientTypeOptions, ICompileModeOptions {
  251. /**
  252. * 自动打开模拟器, 默认行为为打开
  253. */
  254. autoOpen?: boolean;
  255. lyraParams?: ILyraParams;
  256. }
  257. export interface ILyraParams {
  258. rendererExtend?: string[];
  259. }
  260. export interface IGenerateQrCodeArgs extends IClientTypeOptions, ICompileModeOptions, IAMPEOptions {
  261. /**
  262. * @deprecated 同 ignoreHttpDomainCheck
  263. */
  264. ignoreHttpReqPermission?: boolean;
  265. /**
  266. * 忽略 http 请求白名单校验
  267. */
  268. ignoreHttpDomainCheck?: boolean;
  269. /**
  270. * 忽略 webview 加载域名白名单校验
  271. */
  272. ignoreWebViewDomainCheck?: boolean;
  273. /**
  274. * 是否自动推送到客户端
  275. */
  276. autoPush?: boolean;
  277. /**
  278. * 自定义启动参数
  279. */
  280. launchParams?: any;
  281. }
  282. export interface ICompileModeOptions {
  283. /**
  284. * 入口页面
  285. */
  286. page?: string;
  287. /**
  288. * 页面参数, 可在当前页面的 onLoad 中取得,如: name=vendor&color=black
  289. */
  290. pageQuery?: string;
  291. /**
  292. * 全局参数,app.js 的 onLaunch 中取得,如: name=vendor&color=black
  293. */
  294. query?: string;
  295. /**
  296. * 场景值
  297. */
  298. scene?: string;
  299. /**
  300. * 模拟更新
  301. */
  302. simulateUpdate?: boolean;
  303. }
  304. export interface IAMPEOptions {
  305. /**
  306. * 移动应用 ID
  307. */
  308. ampeProductId?: string;
  309. /**
  310. * 设备 ID
  311. */
  312. ampeDeviceId?: string;
  313. /**
  314. * 产品 ID
  315. */
  316. ampeHostAppId?: string;
  317. }
  318. export interface IClientTypeOptions {
  319. /**
  320. * 端类型
  321. */
  322. clientType?: string;
  323. /**
  324. * <高级> 开放平台 bundleId, 此项会覆盖 clientType 的效果
  325. */
  326. bundleId?: string;
  327. }
  328. export interface IDevCommandArgs extends IBuildArgs {
  329. /**
  330. * 是否开启 HMR
  331. */
  332. hmr?: boolean;
  333. /**
  334. * 是否启用 https
  335. */
  336. // https?: boolean;
  337. /**
  338. * 指定 Host,默认 Host 为 127.0.0.1
  339. */
  340. host?: string,
  341. /**
  342. * devServer 的端口号
  343. */
  344. port?: number,
  345. /**
  346. * log 名,api 调用时使用
  347. */
  348. logName?: string;
  349. }
  350. export interface IBuildArgs extends IProjectArgs, IOptionalAppIdArgs {
  351. /**
  352. * 指定构建缓存路径, 默认为系统缓存路径
  353. */
  354. cacheDir?: string;
  355. /**
  356. * 产物路径
  357. */
  358. output?: string;
  359. /**
  360. * 是否开启 sourceMap
  361. */
  362. sourceMap?: boolean;
  363. /**
  364. * 插件 Id,构建插件时将生成到代码中
  365. */
  366. pluginId?: string;
  367. /**
  368. * 多进程编译
  369. */
  370. parallel?: boolean;
  371. /**
  372. * 是否需要压缩,默认不开
  373. */
  374. minify?: boolean;
  375. /**
  376. * 构建类型
  377. */
  378. buildTarget?: EBuildTarget;
  379. /**
  380. * 是否开启 less 编译
  381. */
  382. enableLess?: boolean;
  383. /**
  384. * 是否开启 ts 编译
  385. */
  386. enableTypescript?: boolean;
  387. /**
  388. * 是否开启紧凑产物模式
  389. */
  390. compact?: boolean;
  391. }
  392. export interface IProjectArgs {
  393. /**
  394. * 项目路径, 默认 cwd
  395. */
  396. project?: string;
  397. }
  398. export interface IOptionalAppIdArgs {
  399. /**
  400. * 小程序应用 id
  401. */
  402. appId?: string;
  403. }
  404. export interface IDevServerCompileBuild {
  405. devServer: IDevServer;
  406. project: string;
  407. dist: string;
  408. }
  409. export interface IAppIdArgs extends IOptionalAppIdArgs {
  410. /**
  411. * 小程序应用 id
  412. */
  413. appId: string;
  414. }
  415. export interface IDevServer extends EventEmitter {
  416. readonly id: string;
  417. /**
  418. * 服务地址
  419. */
  420. readonly server: string;
  421. readonly host: string
  422. readonly port: number;
  423. /**
  424. * devServer 的产物路径
  425. */
  426. dist: string;
  427. /**
  428. * devServer 启动
  429. * @param event
  430. * @param listener
  431. */
  432. on(event: 'start', listener: () => any): this;
  433. /**
  434. * 开始编译
  435. * @param event
  436. * @param listener
  437. */
  438. on(event: 'compile', listener: () => any): this;
  439. /**
  440. * 编译成功完成
  441. * @param event
  442. * @param listener
  443. */
  444. on(event: 'done', listener: (result: IDevDone) => any): this;
  445. /**
  446. * 编译出错
  447. * @param event
  448. * @param listener
  449. */
  450. on(event: 'error', listener: (error: IDevError) => any): this;
  451. /**
  452. * devServer 进程被关闭
  453. * @param event
  454. * @param listener
  455. */
  456. on(event: 'exit', listener: (code?: number) => any): this;
  457. /**
  458. * 开启 hmr 时,当 hmr 完成会发送这个事件
  459. * @param event
  460. * @param listener
  461. */
  462. on(event: 'hmr-done', listener: () => any): this;
  463. /**
  464. * 开启 hmr 时,当 hmr 出错时会发送这个事件
  465. * @param event
  466. * @param listener
  467. */
  468. on(event: 'hmr-error', listener: (error: IDevError) => any): this;
  469. /**
  470. * 发生在 mini.project.json 文件被变更的时候, 这种时机一般期望重新启动 devServer
  471. * @param event
  472. */
  473. on(event: 'project-config-change', listener: () => any): this;
  474. /**
  475. * devServer 输出日志
  476. * @param event
  477. */
  478. on(event: 'log', listener: (data: string) => any): this;
  479. once(event: 'start', listener: () => any): this;
  480. once(event: 'compile', listener: () => any): this;
  481. once(event: 'done', listener: (result: IDevDone) => any): this;
  482. once(event: 'error', listener: (error: IDevError) => any): this;
  483. once(event: 'exit', listener: (code?: number) => any): this;
  484. once(event: 'hmr-done', listener: () => any): this;
  485. once(event: 'hmr-error', listener: (error: IDevError) => any): this;
  486. once(event: 'project-config-change', listener: () => any): this;
  487. once(event: 'log', listener: (data: string) => any): this;
  488. restart(options?: {appId?: string}): Promise<void>;
  489. stop(): Promise<void>;
  490. }
  491. export enum EBuildTarget {
  492. Preview = 'Preview',
  493. RemoteLegacy = 'RemoteLegacy',
  494. RemoteX = 'RemoteX',
  495. RemoteXLite = 'RemoteXLite',
  496. RemoteBoatman = 'RemoteBoatman',
  497. Publish = 'Publish'
  498. }
  499. export interface IDevError {
  500. message: string;
  501. stack?: any;
  502. }
  503. export interface IDevDone {
  504. success: boolean;
  505. }
  506. export interface IDevIDEOptions extends IOptionalAppIdArgs, IIDECommandOptions {
  507. }
  508. export interface IGenerateDebugQRCodeResult {
  509. /**
  510. * 二维码
  511. */
  512. qrcodeUrl: string;
  513. /**
  514. *
  515. */
  516. qrcodeSchema?: string;
  517. /**
  518. * 生成的版本号
  519. */
  520. version: string;
  521. }
  522. export interface IIDECommandOptions extends IProjectArgs {
  523. /**
  524. * lite 模式启动
  525. */
  526. lite?: boolean;
  527. /**
  528. * 项目类型
  529. */
  530. projectType?: string
  531. /**
  532. * IDE 安装路径
  533. */
  534. appPath?: string;
  535. }
  536. export interface IDevRemoteDebugArgs extends IGenerateQrCodeArgs, IAppIdArgs, IAuthPathOptions {
  537. /**
  538. * 是否自动打开 devtool,cli 模式默认自动打开, jsApi 调用默认不打开
  539. */
  540. autoOpenDevtool?: boolean;
  541. }
  542. export interface IPublishResult {
  543. version: string;
  544. /**
  545. * 如果设置为体验版,则添加这个字段,为体验包二维码
  546. */
  547. experienceQrCodeUrl?: string;
  548. }
  549. export interface IUploadOptions extends IAppIdArgs, IClientTypeOptions, IProjectArgs, IAuthPathOptions {
  550. /**
  551. * 上传的版本号,如果不传入,会尝试从后台拉取版本后自动添加一位 patch 号 (例: 1.1.1 => 1.1.2)
  552. * 参考语义化版本:
  553. * [major].[minor].[patch]
  554. */
  555. version?: string;
  556. /**
  557. * 上传成功后,自动设置为体验版本 (需要对应权限)
  558. */
  559. experience?: boolean;
  560. /**
  561. * 上传时删除指定版本号
  562. */
  563. deleteVersion?: string;
  564. /**
  565. * 是否小游戏
  566. */
  567. isGame?: boolean;
  568. /**
  569. * 版本描述,用于在开放平台显示
  570. */
  571. versionDescription?: string;
  572. }
  573. export interface IPreviewArgs extends IBuildArgs, IGenerateQrCodeArgs, IAuthPathOptions {
  574. /**
  575. * 小程序应用 id
  576. */
  577. appId: string;
  578. }
  579. export interface ILocalBuildCompileBuild {
  580. result: TBuildPackResult
  581. project: string;
  582. dist: string;
  583. buildTarget: EBuildTarget;
  584. }
  585. export type TBuildPackResult = INormalBuildPackResult | ISubPackBuildPackResult | IPluginBuildPackResult;
  586. export interface IPluginBuildPackResult {
  587. compileType: ECompileType.Plugin;
  588. enableSubPack: false;
  589. client: Omit<INormalBuildPackResult, 'compileType' | 'enableSubPack' | 'extendInfo'>;
  590. plugin: Omit<INormalBuildPackResult, 'compileType' | 'enableSubPack' | 'extendInfo'>;
  591. components?: string[];
  592. /**
  593. * 额外的启动参数
  594. */
  595. extraLaunchParams?: Record<string, unknown>;
  596. }
  597. export enum ECompileType {
  598. MiniProgram = 'mini',
  599. Plugin = 'plugin'
  600. }
  601. export interface INormalBuildPackResult {
  602. compileType: ECompileType.MiniProgram;
  603. enableSubPack: false;
  604. tarFilePath: string;
  605. size: number;
  606. outPath?: string;
  607. components?: string[];
  608. gzipFilePath?: string;
  609. sourceMapZipFile?: string;
  610. /**
  611. * 额外的启动参数
  612. */
  613. extraLaunchParams?: Record<string, unknown>;
  614. webpackStatPath?: string;
  615. statPath?: string;
  616. }
  617. export interface ISubPackBuildPackResult {
  618. compileType: ECompileType.MiniProgram;
  619. enableSubPack: true;
  620. tarFilePath: string;
  621. size: number;
  622. subPackages: ISubPackageResult[];
  623. outPath?: string;
  624. components?: string[];
  625. gzipFilePath?: string;
  626. sourceMapZipFile?: string;
  627. subPackSourceMapZipFile?: string;
  628. /**
  629. * 额外的启动参数
  630. */
  631. extraLaunchParams?: Record<string, unknown>;
  632. webpackStatPath?: string;
  633. statPath?: string;
  634. webpackChunkStatPath?: string;
  635. chunkStatPath?: string;
  636. }
  637. export interface ISubPackageResult {
  638. type: 'MAIN' | 'SUB';
  639. path: string;
  640. size: number;
  641. tarFilePath: string;
  642. gzipFilePath?: string;
  643. statPath?: string;
  644. webpackStatPath?: string;
  645. }
  646. export interface IRemoteDebugArgs extends IBuildArgs, IGenerateQrCodeArgs, IAuthPathOptions {
  647. /**
  648. * 小程序应用 id
  649. */
  650. appId: string;
  651. /**
  652. * 是否自动打开 devtool,cli 模式默认自动打开, jsApi 调用默认不打开
  653. */
  654. autoOpenDevtool?: boolean;
  655. }
  656. export interface IPackSourceOptions extends IProjectArgs {
  657. /**
  658. * 输出文件夹
  659. */
  660. dir?: string;
  661. /**
  662. * 输出文件名
  663. */
  664. filename?: string;
  665. }
  666. }