ms-types 0.12.7 → 0.12.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ms-types",
3
- "version": "0.12.7",
3
+ "version": "0.12.8",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "types": "./types/index.d.ts",
@@ -1,57 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  // IDEA 没有 typescriptServerPlugins,用 `-r` 预加载:在 JetBrains 的 tsc-ide-plugin 创建项目时,
4
- // 对快点JS项目调用 index.cjs 的 inject。tsserver 进程是所有工程共用的,所以要先判断是不是快点JS项目:
5
- // 从项目目录往上找快点JS项目:目录下有 package.json,并且 scripts 里有入口文件
6
- // (main.js / main.ts / main.py / main.lua 之一)。不看 package.json 的内容。
7
- // VS Code / IDEA 插件的项目识别也是这条规则,要一起改。
8
- const fs = require('fs');
9
- const path = require('path');
4
+ // 对快点JS项目调用与 VS Code 共用的声明注入。
10
5
  const Module = require('module');
11
- const { inject } = require('./index.cjs');
12
-
13
- const MAIN_SCRIPT_BASENAMES = ['main.js', 'main.ts', 'main.py', 'main.lua'];
14
- const MAX_ASCEND = 4;
15
- const projectCache = new Map();
16
-
17
- // 路径存在并且是文件(同名目录不算)。
18
- function isFile(file) {
19
- try {
20
- return fs.statSync(file).isFile();
21
- } catch (ignored) {
22
- return false;
23
- }
24
- }
25
-
26
- function hasKuaiJsPackage(dir) {
27
- return (
28
- isFile(path.join(dir, 'package.json')) &&
29
- MAIN_SCRIPT_BASENAMES.some((name) => isFile(path.join(dir, 'scripts', name)))
30
- );
31
- }
32
-
33
- function isKuaiJsProject(info) {
34
- let dir;
35
- try {
36
- dir = path.resolve(info.project.getCurrentDirectory());
37
- } catch (ignored) {
38
- return false;
39
- }
40
- const start = dir;
41
- if (projectCache.has(start)) return projectCache.get(start);
42
- let found = false;
43
- for (let i = 0; i < MAX_ASCEND && dir; i += 1) {
44
- if (hasKuaiJsPackage(dir)) {
45
- found = true;
46
- break;
47
- }
48
- const parent = path.dirname(dir);
49
- if (parent === dir) break;
50
- dir = parent;
51
- }
52
- projectCache.set(start, found);
53
- return found;
54
- }
6
+ const { inject, projectManifest } = require('./index.cjs');
55
7
 
56
8
  function wrapFactory(officialFactory) {
57
9
  if (officialFactory.__kuaijsWrapped) return officialFactory;
@@ -62,7 +14,9 @@ function wrapFactory(officialFactory) {
62
14
  create(info) {
63
15
  const languageService = official.create(info);
64
16
  try {
65
- if (isKuaiJsProject(info)) inject(modules.typescript, info);
17
+ if (projectManifest(info.project.getCurrentDirectory())) {
18
+ return inject(modules.typescript, info, languageService);
19
+ }
66
20
  } catch (ignored) {
67
21
  // 注入失败不能影响 IDE 自己的语言服务。
68
22
  }
@@ -1,33 +1,100 @@
1
1
  'use strict';
2
2
 
3
- // tsserver 插件:把本包的 types/index.d.ts 作为根文件加进项目,不用改项目的 tsconfig.json。
3
+ // 按快点JS项目的目标平台注入声明入口,无需修改项目的 tsconfig。
4
4
  // VS Code 的 kuaijs-ts-plugin 和 IDEA 的预加载(idea.cjs)共用这一份。
5
+ const fs = require('fs');
5
6
  const path = require('path');
6
7
 
7
8
  const typesIndex = path.join(__dirname, '..', 'types', 'index.d.ts');
9
+ const androidIndex = path.join(__dirname, '..', 'types', 'platform', 'android.d.ts');
10
+ const mainScriptBasenames = ['main.js', 'main.ts', 'main.py', 'main.lua'];
8
11
 
9
- function inject(ts, info) {
10
- const extra = ts.server.toNormalizedPath(typesIndex);
12
+ function isFile(file) {
13
+ try {
14
+ return fs.statSync(file).isFile();
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
19
+
20
+ function projectManifest(directory) {
21
+ let dir = path.resolve(directory);
22
+ while (true) {
23
+ const manifest = path.join(dir, 'package.json');
24
+ if (isFile(manifest) && mainScriptBasenames.some((name) => isFile(path.join(dir, 'scripts', name)))) {
25
+ return manifest;
26
+ }
27
+ const parent = path.dirname(dir);
28
+ if (parent === dir) return undefined;
29
+ dir = parent;
30
+ }
31
+ }
32
+
33
+ function useAndroidTypes(manifest) {
34
+ try {
35
+ return JSON.parse(fs.readFileSync(manifest, 'utf8')).projectType === 'Android';
36
+ } catch {
37
+ return false;
38
+ }
39
+ }
40
+
41
+ function inject(ts, info, service = info.languageService) {
11
42
  const host = info.languageServiceHost;
12
43
  const getScriptFileNames = host.getScriptFileNames.bind(host);
44
+ const directory = info.project.getCurrentDirectory();
45
+ const ownManifest = projectManifest(directory);
46
+ const manifestsByDirectory = new Map();
47
+ const watched = new Map();
48
+
49
+ function watch(manifest) {
50
+ if (!watched.has(manifest)) {
51
+ const state = { android: useAndroidTypes(manifest), watcher: undefined };
52
+ state.watcher = info.serverHost.watchFile?.(manifest, () => {
53
+ const android = useAndroidTypes(manifest);
54
+ if (state.android !== android) {
55
+ state.android = android;
56
+ info.project.markAsDirty();
57
+ }
58
+ });
59
+ watched.set(manifest, state);
60
+ }
61
+ return watched.get(manifest).android;
62
+ }
63
+
13
64
  host.getScriptFileNames = () => {
14
65
  const files = getScriptFileNames();
66
+ const manifests = new Set(ownManifest ? [ownManifest] : []);
67
+ for (const file of files) {
68
+ const parent = path.dirname(path.resolve(directory, file));
69
+ if (!manifestsByDirectory.has(parent)) {
70
+ manifestsByDirectory.set(parent, projectManifest(parent));
71
+ }
72
+ const manifest = manifestsByDirectory.get(parent);
73
+ if (manifest) manifests.add(manifest);
74
+ }
75
+ if (!manifests.size) return files;
76
+ const android = Array.from(manifests, watch).every(Boolean);
77
+ const extra = ts.server.toNormalizedPath(android ? androidIndex : typesIndex);
15
78
  return files.includes(extra) ? files : files.concat(extra);
16
79
  };
17
- const scriptInfo = info.project.projectService.getOrCreateScriptInfoForNormalizedPath(extra, false);
18
- if (scriptInfo) info.project.addRoot(scriptInfo);
80
+ const dispose = service.dispose.bind(service);
81
+ service.dispose = () => {
82
+ for (const { watcher } of watched.values()) watcher?.close();
83
+ dispose();
84
+ };
19
85
  info.project.markAsDirty();
86
+ return service;
20
87
  }
21
88
 
22
89
  function init(modules) {
23
90
  const ts = modules.typescript;
24
91
  return {
25
92
  create(info) {
26
- inject(ts, info);
27
- return info.languageService;
93
+ return inject(ts, info);
28
94
  },
29
95
  };
30
96
  }
31
97
 
32
98
  init.inject = inject;
99
+ init.projectManifest = projectManifest;
33
100
  module.exports = init;
package/types/node.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference path="android/node-fields.d.ts" />
2
-
3
1
  /**
4
2
  * 节点模块
5
3
  * @example node.createNodeSelector({ maxDepth: 50, mode: 1 })
@@ -42,7 +40,7 @@ declare interface NodeBoundsInfo {
42
40
  centerY: number;
43
41
  }
44
42
 
45
- declare interface NodeInfo extends AndroidNodeFields {
43
+ declare interface NodeInfo {
46
44
  /**
47
45
  * 节点id
48
46
  */
@@ -1,3 +1,5 @@
1
+ /// <reference path="../index.d.ts" />
2
+
1
3
  /**
2
4
  * Android 节点属性。除 visible 外,以下字段从 Android 1.4.3 起提供。
3
5
  */
@@ -37,3 +39,6 @@ interface AndroidNodeFields {
37
39
  /** 应用提供的状态描述(Android 无障碍,Android 11+)。 */
38
40
  stateDescription?: string;
39
41
  }
42
+
43
+ interface NodeInfo extends AndroidNodeFields {}
44
+ interface $节点信息 extends AndroidNodeFields {}
@@ -1,5 +1,4 @@
1
1
  /// <reference path="全局模块.d.ts" />
2
- /// <reference path="../android/node-fields.d.ts" />
3
2
  /**
4
3
  * 节点模块
5
4
  * @example $创建节点选择器({ maxDepth: 50, mode: 1 })
@@ -42,7 +41,7 @@ declare interface $节点位置信息 {
42
41
  centerY: 数字;
43
42
  }
44
43
 
45
- declare interface $节点信息 extends AndroidNodeFields {
44
+ declare interface $节点信息 {
46
45
  id: 字符串;
47
46
  /**
48
47
  * 节点标识符