@tarojs/plugin-platform-harmony-ets 3.7.0-alpha.27

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.
Files changed (212) hide show
  1. package/LICENSE +160 -0
  2. package/dist/apis/apis.ts +15 -0
  3. package/dist/apis/base/index.ts +5 -0
  4. package/dist/apis/base/system.ts +77 -0
  5. package/dist/apis/device/accelerometer.ts +76 -0
  6. package/dist/apis/device/battery.ts +24 -0
  7. package/dist/apis/device/clipboard.ts +70 -0
  8. package/dist/apis/device/index.ts +9 -0
  9. package/dist/apis/device/keyboard.ts +62 -0
  10. package/dist/apis/device/memory.ts +4 -0
  11. package/dist/apis/device/network.ts +72 -0
  12. package/dist/apis/device/phone.ts +43 -0
  13. package/dist/apis/device/screen.ts +79 -0
  14. package/dist/apis/device/vibrate.ts +24 -0
  15. package/dist/apis/files/index.ts +135 -0
  16. package/dist/apis/files/manager.ts +941 -0
  17. package/dist/apis/framework/index.ts +32 -0
  18. package/dist/apis/index.ts +124 -0
  19. package/dist/apis/location/index.ts +133 -0
  20. package/dist/apis/media/common.ts +74 -0
  21. package/dist/apis/media/image.ts +122 -0
  22. package/dist/apis/media/index.ts +3 -0
  23. package/dist/apis/media/video.ts +86 -0
  24. package/dist/apis/navigate/index.ts +8 -0
  25. package/dist/apis/network/downloadFile.ts +113 -0
  26. package/dist/apis/network/index.ts +4 -0
  27. package/dist/apis/network/request.ts +113 -0
  28. package/dist/apis/network/uploadFile.ts +158 -0
  29. package/dist/apis/network/webSocket.ts +130 -0
  30. package/dist/apis/open-api/index.ts +7 -0
  31. package/dist/apis/open-api/user-info/index.ts +85 -0
  32. package/dist/apis/page/index.ts +92 -0
  33. package/dist/apis/route/index.ts +138 -0
  34. package/dist/apis/storage/index.ts +133 -0
  35. package/dist/apis/tabbar/index.ts +147 -0
  36. package/dist/apis/ui/animation.ts +222 -0
  37. package/dist/apis/ui/background.ts +35 -0
  38. package/dist/apis/ui/index.ts +14 -0
  39. package/dist/apis/ui/interaction/index.ts +195 -0
  40. package/dist/apis/ui/navigation-bar/index.ts +33 -0
  41. package/dist/apis/ui/pull-down-refresh.ts +41 -0
  42. package/dist/apis/utils/handler.ts +116 -0
  43. package/dist/apis/utils/index.ts +85 -0
  44. package/dist/apis/utils/types.ts +12 -0
  45. package/dist/apis/utils/validate.ts +87 -0
  46. package/dist/apis/wxml/IntersectionObserver.ts +119 -0
  47. package/dist/apis/wxml/index.ts +12 -0
  48. package/dist/apis/wxml/nodesRef.ts +56 -0
  49. package/dist/apis/wxml/selectorQuery.ts +287 -0
  50. package/dist/components/components-react.js +51 -0
  51. package/dist/components/components-react.js.map +1 -0
  52. package/dist/components-harmony/button/index.css +109 -0
  53. package/dist/components-harmony/button/index.hml +31 -0
  54. package/dist/components-harmony/button/index.js +105 -0
  55. package/dist/components-harmony/camera/index.hml +6 -0
  56. package/dist/components-harmony/camera/index.js +12 -0
  57. package/dist/components-harmony/checkbox/index.hml +11 -0
  58. package/dist/components-harmony/checkbox/index.js +60 -0
  59. package/dist/components-harmony/checkbox-group/index.hml +3 -0
  60. package/dist/components-harmony/checkbox-group/index.js +22 -0
  61. package/dist/components-harmony/cover-image/index.css +4 -0
  62. package/dist/components-harmony/cover-image/index.hml +9 -0
  63. package/dist/components-harmony/cover-image/index.js +15 -0
  64. package/dist/components-harmony/form/index.hml +10 -0
  65. package/dist/components-harmony/form/index.js +11 -0
  66. package/dist/components-harmony/icon/index.css +27 -0
  67. package/dist/components-harmony/icon/index.hml +54 -0
  68. package/dist/components-harmony/icon/index.js +17 -0
  69. package/dist/components-harmony/image/index.css +5 -0
  70. package/dist/components-harmony/image/index.hml +11 -0
  71. package/dist/components-harmony/image/index.js +28 -0
  72. package/dist/components-harmony/input/index.hml +21 -0
  73. package/dist/components-harmony/input/index.js +98 -0
  74. package/dist/components-harmony/label/index.hml +3 -0
  75. package/dist/components-harmony/label/index.js +6 -0
  76. package/dist/components-harmony/navbar/index.css +56 -0
  77. package/dist/components-harmony/navbar/index.hml +9 -0
  78. package/dist/components-harmony/navbar/index.js +30 -0
  79. package/dist/components-harmony/navigator/index.css +8 -0
  80. package/dist/components-harmony/navigator/index.hml +9 -0
  81. package/dist/components-harmony/navigator/index.js +104 -0
  82. package/dist/components-harmony/picker/index.hml +25 -0
  83. package/dist/components-harmony/picker/index.js +132 -0
  84. package/dist/components-harmony/picker-view/index.hml +23 -0
  85. package/dist/components-harmony/picker-view/index.js +161 -0
  86. package/dist/components-harmony/picker-view-column/index.hml +3 -0
  87. package/dist/components-harmony/picker-view-column/index.js +3 -0
  88. package/dist/components-harmony/progress/index.css +4 -0
  89. package/dist/components-harmony/progress/index.hml +28 -0
  90. package/dist/components-harmony/progress/index.js +23 -0
  91. package/dist/components-harmony/radio/index.hml +11 -0
  92. package/dist/components-harmony/radio/index.js +41 -0
  93. package/dist/components-harmony/radio-group/index.hml +3 -0
  94. package/dist/components-harmony/radio-group/index.js +11 -0
  95. package/dist/components-harmony/rich-text/index.hml +5 -0
  96. package/dist/components-harmony/rich-text/index.js +16 -0
  97. package/dist/components-harmony/scroll-view/index.css +5 -0
  98. package/dist/components-harmony/scroll-view/index.hml +12 -0
  99. package/dist/components-harmony/scroll-view/index.js +65 -0
  100. package/dist/components-harmony/slider/index.hml +16 -0
  101. package/dist/components-harmony/slider/index.js +53 -0
  102. package/dist/components-harmony/swiper/index.css +3 -0
  103. package/dist/components-harmony/swiper/index.hml +14 -0
  104. package/dist/components-harmony/swiper/index.js +19 -0
  105. package/dist/components-harmony/switch/index.hml +7 -0
  106. package/dist/components-harmony/switch/index.js +16 -0
  107. package/dist/components-harmony/tabbar/index.css +7 -0
  108. package/dist/components-harmony/tabbar/index.hml +26 -0
  109. package/dist/components-harmony/tabbar/index.js +42 -0
  110. package/dist/components-harmony/textarea/index.hml +24 -0
  111. package/dist/components-harmony/textarea/index.js +92 -0
  112. package/dist/components-harmony/utils/index.js +41 -0
  113. package/dist/components-harmony/video/index.hml +7 -0
  114. package/dist/components-harmony/video/index.js +73 -0
  115. package/dist/components-harmony/web-view/index.hml +8 -0
  116. package/dist/components-harmony/web-view/index.js +22 -0
  117. package/dist/components-harmony-ets/base.ets +63 -0
  118. package/dist/components-harmony-ets/button.ets +119 -0
  119. package/dist/components-harmony-ets/checkbox.ets +219 -0
  120. package/dist/components-harmony-ets/element.ets +223 -0
  121. package/dist/components-harmony-ets/form.ets +149 -0
  122. package/dist/components-harmony-ets/icon.ets +84 -0
  123. package/dist/components-harmony-ets/image.ets +85 -0
  124. package/dist/components-harmony-ets/index.ts +49 -0
  125. package/dist/components-harmony-ets/innerHtml.ets +79 -0
  126. package/dist/components-harmony-ets/input.ets +195 -0
  127. package/dist/components-harmony-ets/label.ets +111 -0
  128. package/dist/components-harmony-ets/picker.ets +370 -0
  129. package/dist/components-harmony-ets/radio.ets +225 -0
  130. package/dist/components-harmony-ets/richText.ets +97 -0
  131. package/dist/components-harmony-ets/scrollView.ets +199 -0
  132. package/dist/components-harmony-ets/slider.ets +159 -0
  133. package/dist/components-harmony-ets/swiper.ets +204 -0
  134. package/dist/components-harmony-ets/switch.ets +113 -0
  135. package/dist/components-harmony-ets/text.ets +97 -0
  136. package/dist/components-harmony-ets/textArea.ets +169 -0
  137. package/dist/components-harmony-ets/utils/AttributeManager.ets +250 -0
  138. package/dist/components-harmony-ets/utils/DynamicCenter.ts +48 -0
  139. package/dist/components-harmony-ets/utils/constant/event.ets +25 -0
  140. package/dist/components-harmony-ets/utils/constant/icon.ts +19 -0
  141. package/dist/components-harmony-ets/utils/constant/style.ets +88 -0
  142. package/dist/components-harmony-ets/utils/events.ts +24 -0
  143. package/dist/components-harmony-ets/utils/flexManager.ets +107 -0
  144. package/dist/components-harmony-ets/utils/helper.ets +99 -0
  145. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +103 -0
  146. package/dist/components-harmony-ets/utils/htmlParser/index.ts +56 -0
  147. package/dist/components-harmony-ets/utils/index.ts +83 -0
  148. package/dist/components-harmony-ets/utils/styles.ets +91 -0
  149. package/dist/components-harmony-ets/video.ets +161 -0
  150. package/dist/components-harmony-ets/view.ets +79 -0
  151. package/dist/index.js +1183 -0
  152. package/dist/index.js.map +1 -0
  153. package/dist/runtime-ets/bom/document.ts +38 -0
  154. package/dist/runtime-ets/bom/getComputedStyle.ts +5 -0
  155. package/dist/runtime-ets/bom/navigator.ts +21 -0
  156. package/dist/runtime-ets/bom/raf.ts +37 -0
  157. package/dist/runtime-ets/bom/window.ts +44 -0
  158. package/dist/runtime-ets/constant.ts +29 -0
  159. package/dist/runtime-ets/current.ts +16 -0
  160. package/dist/runtime-ets/dom/class-list.ts +117 -0
  161. package/dist/runtime-ets/dom/comment.ts +10 -0
  162. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +105 -0
  163. package/dist/runtime-ets/dom/document.ts +108 -0
  164. package/dist/runtime-ets/dom/element.ts +457 -0
  165. package/dist/runtime-ets/dom/event.ts +164 -0
  166. package/dist/runtime-ets/dom/eventTarget.ts +98 -0
  167. package/dist/runtime-ets/dom/node.ts +215 -0
  168. package/dist/runtime-ets/dom/stylesheet/index.ts +687 -0
  169. package/dist/runtime-ets/dom/stylesheet/type.ts +136 -0
  170. package/dist/runtime-ets/dom/stylesheet/util.ts +134 -0
  171. package/dist/runtime-ets/dom/text.ts +19 -0
  172. package/dist/runtime-ets/index.ts +19 -0
  173. package/dist/runtime-ets/interface/event.ts +9 -0
  174. package/dist/runtime-ets/interface/index.ts +1 -0
  175. package/dist/runtime-ets/utils/bind.ts +35 -0
  176. package/dist/runtime-ets/utils/index.ts +135 -0
  177. package/dist/runtime-ets/utils/info.ts +128 -0
  178. package/dist/runtime-framework/react/app.ts +256 -0
  179. package/dist/runtime-framework/react/connect.ts +24 -0
  180. package/dist/runtime-framework/react/constant.ts +5 -0
  181. package/dist/runtime-framework/react/hooks.ts +101 -0
  182. package/dist/runtime-framework/react/index.ts +16 -0
  183. package/dist/runtime-framework/react/page.ts +263 -0
  184. package/dist/runtime-framework/react/utils/index.ts +25 -0
  185. package/dist/runtime-framework/react/utils/is.ts +46 -0
  186. package/dist/runtime-framework/solid/README.md +99 -0
  187. package/dist/runtime-framework/solid/app.ts +200 -0
  188. package/dist/runtime-framework/solid/connect.ts +11 -0
  189. package/dist/runtime-framework/solid/contant.ts +5 -0
  190. package/dist/runtime-framework/solid/hooks.ts +88 -0
  191. package/dist/runtime-framework/solid/index.ts +10 -0
  192. package/dist/runtime-framework/solid/page.ts +202 -0
  193. package/dist/runtime-framework/solid/reconciler/h.ts +132 -0
  194. package/dist/runtime-framework/solid/reconciler/index.ts +3 -0
  195. package/dist/runtime-framework/solid/reconciler/props.ts +151 -0
  196. package/dist/runtime-framework/solid/reconciler/render.ts +61 -0
  197. package/dist/runtime-framework/solid/reconciler/use.ts +8 -0
  198. package/dist/runtime-framework/solid/utils/index.ts +49 -0
  199. package/dist/runtime-framework/solid/utils/is.ts +46 -0
  200. package/dist/runtime-utils.js +4149 -0
  201. package/dist/runtime-utils.js.map +1 -0
  202. package/dist/runtime.js +4150 -0
  203. package/dist/runtime.js.map +1 -0
  204. package/dist/template/container.js +7 -0
  205. package/index.js +4 -0
  206. package/package.json +54 -0
  207. package/types/api.d.ts +4 -0
  208. package/types/component.d.ts +4 -0
  209. package/types/harmony.d.ts +11 -0
  210. package/types/runtime.d.ts +15 -0
  211. package/types/taro.d.ts +7 -0
  212. package/types/webpack-sources.d.ts +108 -0
@@ -0,0 +1,941 @@
1
+ /**
2
+ * HarmonyOS 文档:
3
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687
4
+ *
5
+ * WX 文档:
6
+ * https://developers.weixin.qq.com/miniprogram/dev/api/file/FileSystemManager.html
7
+ *
8
+ * Taro.js 文档
9
+ * https://taro-docs.jd.com/taro/docs/apis/files/FileSystemManager
10
+ *
11
+ * HarmonyOS 不支持接口:
12
+ * readCompressedFile
13
+ * readCompressedFileSync
14
+ * readdirSync
15
+ * readZipEntry
16
+ *
17
+ * HarmonyOS 差异性接口:
18
+ * readFile:encoding 仅支持 utf-8
19
+ * readFileSync:encoding 仅支持 utf-8
20
+ * write:encoding 仅支持 utf-8
21
+ * writeSync:encoding 仅支持 utf-8
22
+ * rmdirSync:recursive 参数无效(即不支持递归删除)
23
+ * statSync:recursive 参数无效(即不支持递归获取目录下的每个文件的 Stats 信息)
24
+ * getSavedFileList:返回值 fileList 中的每一项不包含 createTime 属性
25
+ */
26
+
27
+ import fileio from '@ohos.fileio'
28
+ import zlib from '@ohos.zlib'
29
+ import app from '@system.app'
30
+ import file from '@system.file'
31
+ import { isNumber, isString } from '@tarojs/shared'
32
+ import Taro from '@tarojs/taro'
33
+
34
+ import { callCallbackFail, callCallbackSuccess, temporarilyNotSupport, validateParams } from '../utils'
35
+
36
+ const rootDataPath = `/data/data/${app.getInfo()?.appID || 'app'}`
37
+ const rootSavedFilePath = `${rootDataPath}/files`
38
+
39
+ const pathSchema = {
40
+ path: 'String'
41
+ }
42
+
43
+ const copyFileSchema = {
44
+ srcPath: 'String',
45
+ destPath: 'String'
46
+ }
47
+
48
+ const filePathSchema = {
49
+ filePath: 'String'
50
+ }
51
+
52
+ const dirPathSchema = {
53
+ dirPath: 'String'
54
+ }
55
+
56
+ const readSchema = {
57
+ fd: 'String',
58
+ arrayBuffer: 'Object'
59
+ }
60
+
61
+ const writeSchema = {
62
+ fd: 'String',
63
+ data: 'String'
64
+ }
65
+
66
+ const renameSchema = {
67
+ newPath: 'String',
68
+ oldPath: 'String'
69
+ }
70
+
71
+ const fdSchema = {
72
+ fd: 'String'
73
+ }
74
+
75
+ const saveFileSchema = {
76
+ tempFilePath: 'String'
77
+ }
78
+
79
+ const unzipSchema = {
80
+ targetPath: 'String',
81
+ zipFilePath: 'String'
82
+ }
83
+ interface ReadOption {
84
+ offset?: number
85
+ length?: number
86
+ position?: number
87
+ }
88
+
89
+ interface WriteOption extends ReadOption {
90
+ encoding: 'utf-8'
91
+ }
92
+
93
+ interface ReadFileOption {
94
+ position?: number
95
+ length?: number
96
+ encoding: 'utf-8'
97
+ }
98
+ interface ReadResult {
99
+ bytesRead: number
100
+ arrayBuffer: ArrayBuffer
101
+ }
102
+
103
+ interface WriteResult {
104
+ bytesWritten: number
105
+ }
106
+
107
+ interface StatWithFdResult {
108
+ stats: Taro.Stats | null
109
+ error: Taro.FileSystemManager.StatFailCallbackResult | null
110
+ }
111
+
112
+ interface GetDirFilesResult {
113
+ files: string[]
114
+ dirs: string[]
115
+ }
116
+
117
+ function convertFilePathToUri (filePath: string): string {
118
+ let uri = filePath.replace(/\/+/g, '/').replace(rootDataPath, 'internal:').replace('internal:/files', 'internal:/app')
119
+ uri = /\/$/.test(uri) ? uri : `${uri}/`
120
+ return uri.replace(/\//g, '//')
121
+ }
122
+
123
+ function convertUriToFilePath (uri: string): string {
124
+ return uri.replace(/\/+/g, '/').replace('internal:/app', 'internal:/files').replace('internal:', rootDataPath)
125
+ }
126
+
127
+ function convertDataToString (data: string | ArrayBuffer): string {
128
+ if (isString(data)) {
129
+ return data
130
+ }
131
+ return String.fromCharCode.apply(null, new Uint8Array(data))
132
+ }
133
+
134
+ function convertReadOption (option: Taro.FileSystemManager.ReadOption | Taro.FileSystemManager.ReadSyncOption): ReadOption {
135
+ const result: ReadOption = {}
136
+ const { offset, length, position } = option
137
+ if (isNumber(offset)) {
138
+ result.offset = offset
139
+ }
140
+ if (isNumber(length)) {
141
+ result.length = length
142
+ }
143
+ if (isNumber(position)) {
144
+ result.position = position
145
+ }
146
+ return result
147
+ }
148
+
149
+ function convertWriteOption (option: Taro.FileSystemManager.WriteOption | Taro.FileSystemManager.WriteSyncOption): WriteOption {
150
+ const result: WriteOption = {
151
+ encoding: 'utf-8'
152
+ }
153
+ const { offset, length, position } = option
154
+ if (isNumber(offset)) {
155
+ result.offset = offset
156
+ }
157
+ if (isNumber(length)) {
158
+ result.length = length
159
+ }
160
+ if (isNumber(position)) {
161
+ result.position = position
162
+ }
163
+ return result
164
+ }
165
+
166
+ function convertReadFileOption (option: Taro.FileSystemManager.ReadFileOption): ReadFileOption {
167
+ const result: ReadFileOption = {
168
+ encoding: 'utf-8'
169
+ }
170
+ const { position, length } = option
171
+ if (isNumber(position)) {
172
+ result.position = position
173
+ }
174
+ if (isNumber(length)) {
175
+ result.length = length
176
+ }
177
+ return result
178
+ }
179
+
180
+ function convertFd (fd: string): number {
181
+ return parseInt(fd, 10)
182
+ }
183
+
184
+ /**
185
+ * HarmonyOS flag 详情参见:
186
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687#section1153565865716
187
+ */
188
+ function convertOpenFlag (flag?: keyof Taro.FileSystemManager.flag): number {
189
+ if (typeof flag === 'string') {
190
+ switch (flag) {
191
+ case 'a':
192
+ return 0o1 | 0o100 | 0o2000
193
+ case 'ax':
194
+ return 0o1 | 0o100 | 0o2000 | 0o200
195
+ case 'a+':
196
+ return 0o2 | 0o100 | 0o2000
197
+ case 'ax+':
198
+ return 0o2 | 0o100 | 0o2000 | 0o200
199
+ case 'as':
200
+ return 0o1 | 0o100 | 0o2000 | 0o4010000
201
+ case 'as+':
202
+ return 0o2 | 0o100 | 0o2000 | 0o4010000
203
+ case 'r':
204
+ return 0o0
205
+ case 'r+':
206
+ return 0o2
207
+ case 'w':
208
+ return 0o1 | 0o100 | 0o1000
209
+ case 'wx':
210
+ return 0o1 | 0o100 | 0o200
211
+ case 'w+':
212
+ return 0o2 | 0o100 | 0o1000
213
+ case 'wx+':
214
+ return 0o2 | 0o100 | 0o200
215
+ default:
216
+ return 0o0
217
+ }
218
+ } else {
219
+ return 0o0
220
+ }
221
+ }
222
+
223
+ export function validateSavedFilePath (savedFilePath: string) {
224
+ if (savedFilePath.indexOf(rootSavedFilePath) !== 0) {
225
+ throw new Error(`The filePath should in ${rootSavedFilePath}`)
226
+ }
227
+ }
228
+
229
+ function parseSavedFilePath (srcPath: string, savedFilePath: string | undefined) : string {
230
+ if (isString(savedFilePath) && savedFilePath.length > 0) {
231
+ validateSavedFilePath(savedFilePath)
232
+ return savedFilePath
233
+ }
234
+ return `${rootSavedFilePath}/${srcPath.split('/').pop()}`
235
+ }
236
+
237
+ function isFileExist (filePath: string): boolean {
238
+ try {
239
+ accessSync(filePath)
240
+ return true
241
+ } catch (_) {
242
+ return false
243
+ }
244
+ }
245
+
246
+ function getDirFiles (dirPath: string): Promise<GetDirFilesResult> {
247
+ return new Promise((resolve, reject) => {
248
+ file.list({
249
+ uri: convertFilePathToUri(dirPath),
250
+ success: ({ fileList }) => {
251
+ resolve({
252
+ files: fileList.filter(({ type }) => type.toLowerCase() === 'file').map(({ uri }) => convertUriToFilePath(uri)),
253
+ dirs: fileList.filter(({ type }) => type.toLowerCase() === 'dir').map(({ uri }) => convertUriToFilePath(uri))
254
+ })
255
+ },
256
+ fail: (error) => {
257
+ reject(new Error(error))
258
+ }
259
+ })
260
+ })
261
+ }
262
+
263
+ const readCompressedFile = temporarilyNotSupport('readCompressedFile')
264
+
265
+ function readCompressedFileSync (_: Taro.FileSystemManager.readCompressedFileSync.Option): ArrayBuffer {
266
+ temporarilyNotSupport('readCompressedFileSync')(_)
267
+ return new ArrayBuffer(0)
268
+ }
269
+
270
+ function readdirSync (_: string): string[] {
271
+ temporarilyNotSupport('readdirSync')(_)
272
+ return []
273
+ }
274
+
275
+ const readZipEntry = temporarilyNotSupport('readZipEntry')
276
+
277
+ function access (option: Taro.FileSystemManager.AccessOption) {
278
+ try {
279
+ validateParams('access', option, pathSchema)
280
+ } catch (error) {
281
+ const res = { errMsg: error.message }
282
+ return callCallbackFail(res, option)
283
+ }
284
+ fileio.access(option.path, (error) => {
285
+ if (error) {
286
+ const res = { errMsg: error.message ? error.message : error }
287
+ callCallbackFail(res, option)
288
+ } else {
289
+ callCallbackSuccess(undefined, option)
290
+ }
291
+ })
292
+ }
293
+
294
+ function accessSync (path: string) {
295
+ fileio.accessSync(path)
296
+ }
297
+
298
+ function mkdir (option: Taro.FileSystemManager.MkdirOption) {
299
+ try {
300
+ validateParams('mkdir', option, dirPathSchema)
301
+ } catch (error) {
302
+ const res = { errMsg: error.message }
303
+ return callCallbackFail(res, option)
304
+ }
305
+ file.mkdir({
306
+ uri: convertFilePathToUri(option.dirPath),
307
+ recursive: option.recursive,
308
+ success: () => callCallbackSuccess(undefined, option),
309
+ fail: (error, code) => {
310
+ const res = { errMsg: `mkdir invoke failed, code: ${code}, error: ${error}` }
311
+ callCallbackFail(res, option)
312
+ }
313
+ })
314
+ }
315
+
316
+ function mkdirSync (dirPath: string, recursive?: boolean) {
317
+ if (recursive === true) {
318
+ const pathParts = dirPath.split('/')
319
+ for (let index = 0; index < pathParts.length; index++) {
320
+ const filePath = pathParts.slice(0, index + 1).join('/')
321
+ if (filePath.length === 0 || isFileExist(filePath)) {
322
+ continue
323
+ }
324
+ fileio.mkdirSync(filePath)
325
+ }
326
+ } else {
327
+ fileio.mkdirSync(dirPath)
328
+ }
329
+ }
330
+
331
+ function rmdir (option: Taro.FileSystemManager.RmdirOption) {
332
+ try {
333
+ validateParams('rmdir', option, dirPathSchema)
334
+ } catch (error) {
335
+ const res = { errMsg: error.message }
336
+ return callCallbackFail(res, option)
337
+ }
338
+ file.rmdir({
339
+ uri: convertFilePathToUri(option.dirPath),
340
+ recursive: option.recursive,
341
+ success: () => callCallbackSuccess(undefined, option),
342
+ fail: (error, code) => {
343
+ const res = { errMsg: `rmdir invoke failed, code: ${code}, error: ${error}` }
344
+ callCallbackFail(res, option)
345
+ }
346
+ })
347
+ }
348
+
349
+ function rmdirSync (dirPath: string, recursive?: boolean) {
350
+ if (recursive === true) {
351
+ temporarilyNotSupport('rmdirSync recursive')(dirPath)
352
+ } else {
353
+ fileio.rmdirSync(dirPath)
354
+ }
355
+ }
356
+
357
+ function readdir (option: Taro.FileSystemManager.ReaddirOption) {
358
+ try {
359
+ validateParams('readdir', option, dirPathSchema)
360
+ } catch (error) {
361
+ const res = { errMsg: error.message }
362
+ return callCallbackFail(res, option)
363
+ }
364
+
365
+ getDirFiles(option.dirPath).then(({ files, dirs }) => {
366
+ callCallbackSuccess({ files: [...files, ...dirs] }, option)
367
+ }).catch((error) => {
368
+ const res = { errMsg: error.message }
369
+ callCallbackFail(res, option)
370
+ })
371
+ }
372
+
373
+ function copyFile (option: Taro.FileSystemManager.CopyFileOption) {
374
+ try {
375
+ validateParams('copyFile', option, copyFileSchema)
376
+ } catch (error) {
377
+ const res = { errMsg: error.message }
378
+ return callCallbackFail(res, option)
379
+ }
380
+ fileio.copyFile(option.srcPath, option.destPath, 0, (error) => {
381
+ if (error) {
382
+ const res = { errMsg: error.message ? error.message : error }
383
+ callCallbackFail(res, option)
384
+ } else {
385
+ callCallbackSuccess(undefined, option)
386
+ }
387
+ })
388
+ }
389
+
390
+ function copyFileSync (srcPath: string, destPath: string) {
391
+ fileio.copyFileSync(srcPath, destPath, 0)
392
+ }
393
+
394
+ function open (option: Taro.FileSystemManager.OpenOption) {
395
+ try {
396
+ validateParams('open', option, filePathSchema)
397
+ } catch (error) {
398
+ const res = { errMsg: error.message }
399
+ return callCallbackFail(res, option)
400
+ }
401
+ // 由于 HarmonyOS 异步方法 fileio.open 在【文件不存在则创建】模式下依旧抛出 No such file or directory 异常,
402
+ // 为保证接口的可用性,此处降级使用 fileio.openSync 方法。
403
+ const openPromise = new Promise((resolve, reject) => {
404
+ try {
405
+ const fd = fileio.openSync(option.filePath, convertOpenFlag(option.flag), 0o666)
406
+ resolve({ fd: fd.toString() })
407
+ } catch (error) {
408
+ reject(new Error(error.message ? error.message : error))
409
+ }
410
+ })
411
+ openPromise.then((res) => callCallbackSuccess(res, option))
412
+ .catch((error) => callCallbackFail({ errMsg: error.message }, option))
413
+ }
414
+
415
+ function openSync (option: Taro.FileSystemManager.OpenSyncOption): string {
416
+ validateParams('openSync', option, filePathSchema)
417
+ return fileio.openSync(option.filePath, convertOpenFlag(option.flag), 0o666).toString()
418
+ }
419
+
420
+ function read (option: Taro.FileSystemManager.ReadOption) {
421
+ try {
422
+ validateParams('read', option, readSchema)
423
+ } catch (error) {
424
+ const res = { errMsg: error.message }
425
+ return callCallbackFail(res, option)
426
+ }
427
+
428
+ fileio.read(convertFd(option.fd), option.arrayBuffer, convertReadOption(option), (error, readOut) => {
429
+ if (error) {
430
+ const res = { errMsg: error.message ? error.message : error }
431
+ callCallbackFail(res, option)
432
+ } else {
433
+ callCallbackSuccess({ bytesRead: readOut.bytesRead, arrayBuffer: readOut.buffer }, option)
434
+ }
435
+ })
436
+ }
437
+
438
+ function readSync (option: Taro.FileSystemManager.ReadSyncOption): ReadResult {
439
+ validateParams('readSync', option, readSchema)
440
+ return {
441
+ bytesRead: fileio.readSync(convertFd(option.fd), option.arrayBuffer, convertReadOption(option)),
442
+ arrayBuffer: option.arrayBuffer
443
+ }
444
+ }
445
+
446
+ /**
447
+ * HarmonyOS encoding 目前仅支持 utf-8,详情参见:
448
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687#section17477155374810
449
+ */
450
+ function readFile (option: Taro.FileSystemManager.ReadFileOption) {
451
+ try {
452
+ validateParams('readFile', option, filePathSchema)
453
+ } catch (error) {
454
+ const res = { errMsg: error.message }
455
+ return callCallbackFail(res, option)
456
+ }
457
+
458
+ fileio.readText(option.filePath, convertReadFileOption(option), (error, data) => {
459
+ if (error) {
460
+ const res = { errMsg: error.message ? error.message : error }
461
+ callCallbackFail(res, option)
462
+ } else {
463
+ callCallbackSuccess({ data }, option)
464
+ }
465
+ })
466
+ }
467
+
468
+ /**
469
+ * HarmonyOS encoding 目前仅支持 utf-8,详情参见:
470
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687#section4147155065718
471
+ */
472
+ function readFileSync (filePath: string, encoding?: keyof Taro.FileSystemManager.Encoding, position?: number, length?: number): string | ArrayBuffer {
473
+ return fileio.readTextSync(filePath, convertReadFileOption({ filePath, encoding, length, position }))
474
+ }
475
+
476
+ /**
477
+ * HarmonyOS encoding 目前仅支持 utf-8,详情参见:
478
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687#section140547195118
479
+ */
480
+ function write (option: Taro.FileSystemManager.WriteOption) {
481
+ const data = convertDataToString(option.data)
482
+ try {
483
+ validateParams('write', { ...option, data }, writeSchema)
484
+ } catch (error) {
485
+ const res = { errMsg: error.message }
486
+ return callCallbackFail(res, option)
487
+ }
488
+ fileio.write(convertFd(option.fd), data, convertWriteOption(option), (error, bytesWritten) => {
489
+ if (error) {
490
+ const res = { errMsg: error.message ? error.message : error }
491
+ callCallbackFail(res, option)
492
+ } else {
493
+ callCallbackSuccess({ bytesWritten }, option)
494
+ }
495
+ })
496
+ }
497
+
498
+ /**
499
+ * HarmonyOS encoding 目前仅支持 utf-8,详情参见:
500
+ * https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fileio-0000001168366687#section144923345218
501
+ */
502
+ function writeSync (option: Taro.FileSystemManager.WriteSyncOption): WriteResult {
503
+ const data = convertDataToString(option.data)
504
+ validateParams('writeSync', { ...option, data }, writeSchema)
505
+ return {
506
+ bytesWritten: fileio.writeSync(convertFd(option.fd), data, convertWriteOption(option))
507
+ }
508
+ }
509
+
510
+ function writeFileWithFlag (option: Taro.FileSystemManager.WriteFileOption, flag: keyof Taro.FileSystemManager.flag) {
511
+ open({
512
+ flag,
513
+ filePath: option.filePath,
514
+ success: ({ fd }) => {
515
+ const writePromise = new Promise((resolve) => {
516
+ write({
517
+ fd,
518
+ data: option.data,
519
+ success: (res) => resolve({
520
+ error: null,
521
+ res
522
+ }),
523
+ fail: (res) => resolve({
524
+ error: res,
525
+ res: null
526
+ })
527
+ })
528
+ })
529
+ writePromise.then(({ error, res }) => {
530
+ close({
531
+ fd,
532
+ success: () => {
533
+ if (error === null) {
534
+ callCallbackSuccess(res, option)
535
+ } else {
536
+ callCallbackFail(error, option)
537
+ }
538
+ },
539
+ fail: (res) => callCallbackFail(res, option)
540
+ })
541
+ })
542
+ },
543
+ fail: (res) => callCallbackFail(res, option)
544
+ })
545
+ }
546
+
547
+ function writeFile (option: Taro.FileSystemManager.WriteFileOption) {
548
+ writeFileWithFlag(option, 'w')
549
+ }
550
+
551
+ function writeFileSyncWithFlag (filePath: string, data: string | ArrayBuffer, flag: keyof Taro.FileSystemManager.flag) {
552
+ const fd = openSync({ filePath, flag })
553
+ writeSync({ fd, data })
554
+ closeSync({ fd })
555
+ }
556
+
557
+ function writeFileSync (filePath: string, data: string | ArrayBuffer) {
558
+ writeFileSyncWithFlag(filePath, data, 'w')
559
+ }
560
+
561
+ function appendFile (option: Taro.FileSystemManager.AppendFileOption) {
562
+ writeFileWithFlag(option, 'a')
563
+ }
564
+
565
+ function appendFileSync (filePath: string, data: string | ArrayBuffer) {
566
+ writeFileSyncWithFlag(filePath, data, 'a')
567
+ }
568
+
569
+ function rename (option: Taro.FileSystemManager.RenameOption) {
570
+ try {
571
+ validateParams('rename', option, renameSchema)
572
+ } catch (error) {
573
+ const res = { errMsg: error.message }
574
+ return callCallbackFail(res, option)
575
+ }
576
+ fileio.rename(option.oldPath, option.newPath, (error) => {
577
+ if (error) {
578
+ const res = { errMsg: error.message ? error.message : error }
579
+ callCallbackFail(res, option)
580
+ } else {
581
+ callCallbackSuccess(undefined, option)
582
+ }
583
+ })
584
+ }
585
+
586
+ function renameSync (oldPath: string, newPath: string) {
587
+ fileio.renameSync(oldPath, newPath)
588
+ }
589
+
590
+ function unlink (option: Taro.FileSystemManager.UnlinkOption) {
591
+ try {
592
+ validateParams('unlink', option, filePathSchema)
593
+ } catch (error) {
594
+ const res = { errMsg: error.message }
595
+ return callCallbackFail(res, option)
596
+ }
597
+
598
+ fileio.unlink(option.filePath, (error) => {
599
+ if (error) {
600
+ const res = { errMsg: error.message ? error.message : error }
601
+ callCallbackFail(res, option)
602
+ } else {
603
+ callCallbackSuccess(undefined, option)
604
+ }
605
+ })
606
+ }
607
+
608
+ function unlinkSync (filePath: string) {
609
+ fileio.unlinkSync(filePath)
610
+ }
611
+
612
+ function fstat (option: Taro.FileSystemManager.FstatOption) {
613
+ try {
614
+ validateParams('fstat', option, fdSchema)
615
+ } catch (error) {
616
+ const res = { errMsg: error.message }
617
+ return callCallbackFail(res, option)
618
+ }
619
+ fileio.fstat(convertFd(option.fd), (error, stats) => {
620
+ if (error) {
621
+ const res = { errMsg: error.message ? error.message : error }
622
+ callCallbackFail(res, option)
623
+ } else {
624
+ const res = {
625
+ mode: stats.mode.toString(),
626
+ size: stats.size,
627
+ lastAccessedTime: stats.atime,
628
+ lastModifiedTime: stats.mtime,
629
+ isDirectory: () => stats.isDirectory(),
630
+ isFile: () => stats.isFile()
631
+ }
632
+ callCallbackSuccess({ stats: res }, option)
633
+ }
634
+ })
635
+ }
636
+
637
+ function fstatSync (option: Taro.FileSystemManager.FstatSyncOption): Taro.Stats {
638
+ validateParams('fstatSync', option, fdSchema)
639
+ const stats = fileio.fstatSync(convertFd(option.fd))
640
+ return {
641
+ mode: stats.mode.toString(),
642
+ size: stats.size,
643
+ lastAccessedTime: stats.atime,
644
+ lastModifiedTime: stats.mtime,
645
+ isDirectory: () => stats.isDirectory(),
646
+ isFile: () => stats.isFile()
647
+ }
648
+ }
649
+
650
+ function statWithFd (fd: string): Promise<StatWithFdResult> {
651
+ return new Promise((resolve) => {
652
+ fstat({
653
+ fd,
654
+ success: ({ stats }) => resolve({
655
+ stats,
656
+ error: null
657
+ }),
658
+ fail: (error) => resolve({
659
+ error,
660
+ stats: null
661
+ })
662
+ })
663
+ })
664
+ }
665
+
666
+ function statWithPath (path: string): Promise<Taro.Stats> {
667
+ return new Promise((resolve, reject) => {
668
+ open({
669
+ filePath: path,
670
+ flag: 'r',
671
+ success: ({ fd }) => {
672
+ statWithFd(fd).then(({ error, stats }) => {
673
+ close({
674
+ fd,
675
+ success: () => {
676
+ if (error === null) {
677
+ resolve(stats!)
678
+ } else {
679
+ reject(error)
680
+ }
681
+ },
682
+ fail: (error) => reject(error)
683
+ })
684
+ })
685
+ },
686
+ fail: (error) => reject(error)
687
+ })
688
+ })
689
+ }
690
+
691
+ async function statWithRecursive (rootPath: string): Promise<Record<string, Taro.Stats>> {
692
+ let result: Record<string, Taro.Stats> = {}
693
+ const { files, dirs } = await getDirFiles(rootPath)
694
+ for (const dir of dirs) {
695
+ result[dir] = await statWithPath(dir)
696
+ result = {
697
+ ...result,
698
+ ...(await statWithRecursive(dir))
699
+ }
700
+ }
701
+ for (const file of files) {
702
+ result[file] = await statWithPath(file)
703
+ }
704
+ return result
705
+ }
706
+
707
+ function stat (option: Taro.FileSystemManager.StatOption) {
708
+ try {
709
+ validateParams('fstat', option, pathSchema)
710
+ } catch (error) {
711
+ const res = { errMsg: error.message }
712
+ return callCallbackFail(res, option)
713
+ }
714
+
715
+ const statPromise = option.recursive === true ? statWithRecursive(option.path) : statWithPath(option.path)
716
+ statPromise.then((stats) => {
717
+ callCallbackSuccess({ stats }, option)
718
+ }).catch((error) => {
719
+ const res = { errMsg: error.message ? error.message : error }
720
+ callCallbackFail(res, option)
721
+ })
722
+ }
723
+
724
+ function statSync (path: string, recursive?: boolean): Taro.Stats | any {
725
+ if (recursive === true) {
726
+ return temporarilyNotSupport('statSync recursive')(path)
727
+ }
728
+ const fd = openSync({ filePath: path, flag: 'r' })
729
+ const stats = fstatSync({ fd })
730
+ closeSync({ fd })
731
+ return stats
732
+ }
733
+
734
+ function getFileInfo (option: Taro.FileSystemManager.getFileInfoOption) {
735
+ statWithPath(option.filePath).then((stats) => {
736
+ callCallbackSuccess({ size: stats.size }, option)
737
+ }).catch((error) => {
738
+ const res = { errMsg: error.message ? error.message : error }
739
+ callCallbackFail(res, option)
740
+ })
741
+ }
742
+
743
+ function getSavedFileList (option: Taro.FileSystemManager.getSavedFileListOption) {
744
+ statWithRecursive(rootSavedFilePath).then((stats) => {
745
+ const fileList = Object.keys(stats).map((filePath) => {
746
+ const file = stats[filePath]
747
+ if (file.isFile()) {
748
+ return {
749
+ filePath,
750
+ size: file.size
751
+ }
752
+ }
753
+ return null
754
+ }).filter((file) => file !== null)
755
+ callCallbackSuccess({ fileList }, option)
756
+ }).catch((error) => {
757
+ const res = { errMsg: error.message ? error.message : error }
758
+ callCallbackFail(res, option)
759
+ })
760
+ }
761
+
762
+ function ftruncate (option: Taro.FileSystemManager.FtruncateOption) {
763
+ try {
764
+ validateParams('ftruncate', option, fdSchema)
765
+ } catch (error) {
766
+ const res = { errMsg: error.message }
767
+ return callCallbackFail(res, option)
768
+ }
769
+
770
+ fileio.ftruncate(convertFd(option.fd), option.length ?? 0, (error) => {
771
+ if (error) {
772
+ const res = { errMsg: error.message ? error.message : error }
773
+ callCallbackFail(res, option)
774
+ } else {
775
+ callCallbackSuccess(undefined, option)
776
+ }
777
+ })
778
+ }
779
+
780
+ function ftruncateSync (option: Taro.FileSystemManager.FtruncateSyncOption) {
781
+ validateParams('ftruncateSync', option, fdSchema)
782
+ fileio.ftruncateSync(convertFd(option.fd), option.length ?? 0)
783
+ }
784
+
785
+ function truncate (option: Taro.FileSystemManager.TruncateOption) {
786
+ try {
787
+ validateParams('truncate', option, filePathSchema)
788
+ } catch (error) {
789
+ const res = { errMsg: error.message }
790
+ return callCallbackFail(res, option)
791
+ }
792
+
793
+ fileio.truncate(option.filePath, option.length ?? 0, (error) => {
794
+ if (error) {
795
+ const res = { errMsg: error.message ? error.message : error }
796
+ callCallbackFail(res, option)
797
+ } else {
798
+ callCallbackSuccess(undefined, option)
799
+ }
800
+ })
801
+ }
802
+
803
+ function truncateSync (option: Taro.FileSystemManager.TruncateSyncOption) {
804
+ validateParams('truncateSync', option, filePathSchema)
805
+ fileio.truncateSync(option.filePath, option.length ?? 0)
806
+ }
807
+
808
+ function close (option: Taro.FileSystemManager.CloseOption) {
809
+ try {
810
+ validateParams('close', option, fdSchema)
811
+ } catch (error) {
812
+ const res = { errMsg: error.message }
813
+ return callCallbackFail(res, option)
814
+ }
815
+ fileio.close(convertFd(option.fd), (error) => {
816
+ if (error) {
817
+ const res = { errMsg: error.message ? error.message : error }
818
+ callCallbackFail(res, option)
819
+ } else {
820
+ callCallbackSuccess(undefined, option)
821
+ }
822
+ })
823
+ }
824
+
825
+ function closeSync (option: Taro.FileSystemManager.CloseSyncOption) {
826
+ validateParams('closeSync', option, fdSchema)
827
+ fileio.closeSync(convertFd(option.fd))
828
+ }
829
+
830
+ function saveFile (option: Taro.FileSystemManager.SaveFileOption) {
831
+ try {
832
+ validateParams('saveFile', option, saveFileSchema)
833
+ } catch (error) {
834
+ const res = { errMsg: error.message }
835
+ return callCallbackFail(res, option)
836
+ }
837
+
838
+ try {
839
+ const savedFilePath = parseSavedFilePath(option.tempFilePath, option.filePath)
840
+ rename({
841
+ oldPath: option.tempFilePath,
842
+ newPath: savedFilePath,
843
+ success: () => {
844
+ callCallbackSuccess({ savedFilePath }, option)
845
+ },
846
+ fail: (res) => {
847
+ callCallbackFail(res, option)
848
+ }
849
+ })
850
+ } catch (error) {
851
+ const res = { errMsg: error.message }
852
+ return callCallbackFail(res, option)
853
+ }
854
+ }
855
+
856
+ function saveFileSync (tempFilePath: string, filePath?: string): string {
857
+ const savedFilePath = parseSavedFilePath(tempFilePath, filePath)
858
+ renameSync(tempFilePath, savedFilePath)
859
+ return savedFilePath
860
+ }
861
+
862
+ function removeSavedFile (option: Taro.FileSystemManager.RemoveSavedFileOption) {
863
+ try {
864
+ validateParams('removeSavedFile', option, filePathSchema)
865
+ } catch (error) {
866
+ const res = { errMsg: error.message }
867
+ return callCallbackFail(res, option)
868
+ }
869
+ try {
870
+ validateSavedFilePath(option.filePath)
871
+ } catch (error) {
872
+ const res = { errMsg: error.message }
873
+ return callCallbackFail(res, option)
874
+ }
875
+ unlink(option)
876
+ }
877
+
878
+ function unzip (option: Taro.FileSystemManager.UnzipOption) {
879
+ try {
880
+ validateParams('unzip', option, unzipSchema)
881
+ } catch (error) {
882
+ const res = { errMsg: error.message }
883
+ return callCallbackFail(res, option)
884
+ }
885
+ zlib.unzipFile(option.zipFilePath, option.targetPath).then(() => {
886
+ callCallbackSuccess(undefined, option)
887
+ }).catch(error => {
888
+ const res = { errMsg: error.message ? error.message : error }
889
+ callCallbackFail(res, option)
890
+ })
891
+ }
892
+
893
+ export function getFileSystemManager (): Taro.FileSystemManager {
894
+ return {
895
+ access,
896
+ accessSync,
897
+ appendFile,
898
+ appendFileSync,
899
+ close,
900
+ closeSync,
901
+ copyFile,
902
+ copyFileSync,
903
+ fstat,
904
+ fstatSync,
905
+ ftruncate,
906
+ ftruncateSync,
907
+ getFileInfo,
908
+ getSavedFileList,
909
+ mkdir,
910
+ mkdirSync,
911
+ open,
912
+ openSync,
913
+ read,
914
+ readCompressedFile,
915
+ readCompressedFileSync,
916
+ readdir,
917
+ readdirSync,
918
+ readFile,
919
+ readFileSync,
920
+ readSync,
921
+ readZipEntry,
922
+ removeSavedFile,
923
+ rename,
924
+ renameSync,
925
+ rmdir,
926
+ rmdirSync,
927
+ saveFile,
928
+ saveFileSync,
929
+ stat,
930
+ statSync,
931
+ truncate,
932
+ truncateSync,
933
+ unlink,
934
+ unlinkSync,
935
+ unzip,
936
+ write,
937
+ writeFile,
938
+ writeFileSync,
939
+ writeSync
940
+ }
941
+ }