@lvce-editor/typescript-compile-process 0.29.6 → 1.0.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
- Copyright (c) 2022-present, Lvce Editor
3
+ Copyright (c) 2024 Lvce Editor
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # typescript-compile-process
2
+ TypeScript compile process
package/dist/index.js ADDED
@@ -0,0 +1,169 @@
1
+ import { object, string } from '@lvce-editor/assert';
2
+ import { handleJsonRpcMessage, resolve } from '@lvce-editor/json-rpc';
3
+ import { IpcChildWithWebSocket, IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithNodeWorker, IpcChildWithNodeForkedProcess } from '@lvce-editor/ipc';
4
+ import { VError } from '@lvce-editor/verror';
5
+
6
+ const state$1 = {
7
+ commands: Object.create(null)
8
+ };
9
+ const registerCommand = (key, fn) => {
10
+ state$1.commands[key] = fn;
11
+ };
12
+ const registerCommands = commandMap => {
13
+ for (const [key, value] of Object.entries(commandMap)) {
14
+ registerCommand(key, value);
15
+ }
16
+ };
17
+ const getCommand = key => {
18
+ return state$1.commands[key];
19
+ };
20
+
21
+ const execute = (command, ...args) => {
22
+ const fn = getCommand(command);
23
+ if (!fn) {
24
+ throw new Error(`Command not found ${command}`);
25
+ }
26
+ return fn(...args);
27
+ };
28
+
29
+ const prepare = error => {
30
+ return error;
31
+ };
32
+ const requiresSocket = method => {
33
+ return false;
34
+ };
35
+ const logError = (error, prettyError) => {
36
+ console.error(error);
37
+ };
38
+ const handleMessage = event => {
39
+ return handleJsonRpcMessage(event.target, event.data, execute, resolve, prepare, logError, requiresSocket);
40
+ };
41
+
42
+ const handleIpc = ipc => {
43
+ ipc.addEventListener('message', handleMessage);
44
+ };
45
+
46
+ const NodeWorker = 1;
47
+ const NodeForkedProcess = 2;
48
+ const ElectronUtilityProcess = 3;
49
+ const ElectronMessagePort = 4;
50
+ const WebSocket = 6;
51
+ const Auto = () => {
52
+ const {
53
+ argv
54
+ } = process;
55
+ if (argv.includes('--ipc-type=node-worker')) {
56
+ return NodeWorker;
57
+ }
58
+ if (argv.includes('--ipc-type=node-forked-process')) {
59
+ return NodeForkedProcess;
60
+ }
61
+ if (argv.includes('--ipc-type=electron-utility-process')) {
62
+ return ElectronUtilityProcess;
63
+ }
64
+ throw new Error(`[shared-process] unknown ipc type`);
65
+ };
66
+
67
+ const getModule = method => {
68
+ switch (method) {
69
+ case NodeForkedProcess:
70
+ return IpcChildWithNodeForkedProcess;
71
+ case NodeWorker:
72
+ return IpcChildWithNodeWorker;
73
+ case ElectronUtilityProcess:
74
+ return IpcChildWithElectronUtilityProcess;
75
+ case ElectronMessagePort:
76
+ return IpcChildWithElectronMessagePort;
77
+ case WebSocket:
78
+ return IpcChildWithWebSocket;
79
+ default:
80
+ throw new Error('unexpected ipc type');
81
+ }
82
+ };
83
+
84
+ const listen$1 = async ({
85
+ method,
86
+ ...params
87
+ }) => {
88
+ const module = await getModule(method);
89
+ // @ts-ignore
90
+ const rawIpc = await module.listen(params);
91
+ // @ts-ignore
92
+ if (module.signal) {
93
+ // @ts-ignore
94
+ module.signal(rawIpc);
95
+ }
96
+ // @ts-ignore
97
+ const ipc = module.wrap(rawIpc);
98
+ return ipc;
99
+ };
100
+
101
+ const handleElectronMessagePort = async (messagePort, ipcId) => {
102
+ object(messagePort);
103
+ // Assert.number(ipcId)
104
+ // TODO use handleIncomingIpc function
105
+ const ipc = await listen$1({
106
+ method: ElectronMessagePort,
107
+ messagePort
108
+ });
109
+ handleIpc(ipc);
110
+ };
111
+
112
+ const loadTypeScript = async typescriptPath => {
113
+ try {
114
+ const typescript = await import(typescriptPath);
115
+ const actual = typescript.default;
116
+ if (!actual) {
117
+ throw new Error('missing default export');
118
+ }
119
+ return actual;
120
+ } catch (error) {
121
+ throw new VError(error, `Failed to load typescript`);
122
+ }
123
+ };
124
+
125
+ const state = {
126
+ typescriptPath: ''
127
+ };
128
+ const get = () => {
129
+ return state.typescriptPath;
130
+ };
131
+ const set = value => {
132
+ state.typescriptPath = value;
133
+ };
134
+
135
+ const transpileTypeScript = async code => {
136
+ try {
137
+ string(code);
138
+ const typescriptUri = get();
139
+ const typescript = await loadTypeScript(typescriptUri);
140
+ const newContent = await typescript.transpileModule(code, {
141
+ compilerOptions: {
142
+ target: 'esnext'
143
+ }
144
+ });
145
+ return newContent;
146
+ } catch (error) {
147
+ throw new VError(error, `Failed to transpile typescript`);
148
+ }
149
+ };
150
+
151
+ const commandMap = {
152
+ 'TranspileTypeScript.transpileTypeScript': transpileTypeScript,
153
+ 'TypeScript.setTypeScriptPath': set,
154
+ 'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort
155
+ };
156
+
157
+ const listen = async () => {
158
+ const ipc = await listen$1({
159
+ method: Auto()
160
+ });
161
+ handleIpc(ipc);
162
+ };
163
+
164
+ const main = async () => {
165
+ registerCommands(commandMap);
166
+ await listen();
167
+ };
168
+
169
+ main();
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@lvce-editor/typescript-compile-process",
3
- "version": "0.29.6",
4
- "main": "index.js",
3
+ "version": "1.0.0",
4
+ "description": "TypeScript Compile Process",
5
+ "main": "dist/index.js",
5
6
  "type": "module",
6
7
  "keywords": [
7
8
  "Lvce Editor"
@@ -10,8 +11,7 @@
10
11
  "license": "MIT",
11
12
  "repository": {
12
13
  "type": "git",
13
- "url": "https://github.com/lvce-editor/lvce-editor.git",
14
- "directory": "packages/typescript-compile-process"
14
+ "url": "https://github.com/lvce-editor/typescript-compile-process.git"
15
15
  },
16
16
  "engines": {
17
17
  "node": ">=18"
@@ -22,5 +22,50 @@
22
22
  "@lvce-editor/json-rpc": "^1.3.0",
23
23
  "@lvce-editor/pretty-error": "^1.5.0",
24
24
  "@lvce-editor/verror": "^1.3.0"
25
+ },
26
+ "xo": {
27
+ "rules": {
28
+ "unicorn/filename-case": "off",
29
+ "indent": "off",
30
+ "semi": "off",
31
+ "no-unused-vars": "off",
32
+ "unicorn/numeric-separators-style": "off",
33
+ "no-extra-semi": "off",
34
+ "arrow-body-style": "off",
35
+ "padded-blocks": "off",
36
+ "capitalized-comments": "off",
37
+ "padding-line-between-statements": "off",
38
+ "arrow-parens": "off",
39
+ "no-warning-comments": "off",
40
+ "array-bracket-spacing": "off",
41
+ "comma-spacing": "off",
42
+ "unicorn/no-array-callback-reference": "off",
43
+ "comma-dangle": "off",
44
+ "operator-linebreak": "off",
45
+ "no-case-declarations": "off",
46
+ "no-undef": "off",
47
+ "object-curly-spacing": "off",
48
+ "object-shorthand": "off",
49
+ "complexity": "off",
50
+ "no-labels": "off",
51
+ "no-multi-assign": "off",
52
+ "max-params": "off",
53
+ "no-bitwise": "off",
54
+ "unicorn/prefer-math-trunc": "off",
55
+ "no-await-in-loop": "off",
56
+ "unicorn/prefer-add-event-listener": "off",
57
+ "no-unused-expressions": "off",
58
+ "node/prefer-global/process": "off",
59
+ "unicorn/prevent-abbreviations": "off",
60
+ "unicorn/no-process-exit": "off",
61
+ "quotes": "off",
62
+ "n/prefer-global/process": [
63
+ "error",
64
+ "always"
65
+ ]
66
+ },
67
+ "ignores": [
68
+ "distmin"
69
+ ]
25
70
  }
26
71
  }
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- import { dirname, isAbsolute, join } from 'node:path'
2
- import { fileURLToPath } from 'node:url'
3
-
4
- const __dirname = dirname(fileURLToPath(import.meta.url))
5
-
6
- export const typescriptCompileProcessPath = join(__dirname, 'src', 'typescriptCompileProcessMain.js')
@@ -1 +0,0 @@
1
- export * from '@lvce-editor/assert'
@@ -1 +0,0 @@
1
- export { resolve } from '@lvce-editor/json-rpc'
@@ -1,9 +0,0 @@
1
- import * as CommandState from '../CommandState/CommandState.js'
2
-
3
- export const execute = (command, ...args) => {
4
- const fn = CommandState.getCommand(command)
5
- if (!fn) {
6
- throw new Error(`Command not found ${command}`)
7
- }
8
- return fn(...args)
9
- }
@@ -1,7 +0,0 @@
1
- import * as HandleElectronMessagePort from '../HandleElectronMessagePort/HandleElectronMessagePort.js'
2
- import * as TranspileTypeScript from '../TranspileTypeScript/TranspileTypeScript.js'
3
-
4
- export const commandMap = {
5
- 'TranspileTypeScript.transpileTypeScript': TranspileTypeScript.transpileTypeScript,
6
- 'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
7
- }
@@ -1,17 +0,0 @@
1
- export const state = {
2
- commands: Object.create(null),
3
- }
4
-
5
- export const registerCommand = (key, fn) => {
6
- state.commands[key] = fn
7
- }
8
-
9
- export const registerCommands = (commandMap) => {
10
- for (const [key, value] of Object.entries(commandMap)) {
11
- registerCommand(key, value)
12
- }
13
- }
14
-
15
- export const getCommand = (key) => {
16
- return state.commands[key]
17
- }
@@ -1,22 +0,0 @@
1
- import { join } from 'node:path'
2
- import * as IsBuiltServer from '../IsBuiltServer/IsBuiltServer.js'
3
- import * as PlatformPaths from '../PlatformPaths/PlatformPaths.js'
4
- import * as Platform from '../Platform/Platform.js'
5
- import { pathToFileURL } from 'node:url'
6
-
7
- export const getTypeScriptUri = () => {
8
- if (!Platform.isProduction || IsBuiltServer.isBuiltServer) {
9
- return 'typescript'
10
- }
11
- const typescriptPath = join(
12
- PlatformPaths.getBuiltinExtensionsPath(),
13
- 'builtin.language-features-typescript',
14
- 'node',
15
- 'node_modules',
16
- 'typescript',
17
- 'lib',
18
- 'typescript.js',
19
- )
20
- const typescriptUri = pathToFileURL(typescriptPath).toString()
21
- return typescriptUri
22
- }
@@ -1,15 +0,0 @@
1
- import * as Assert from '../Assert/Assert.js'
2
- import * as HandleIpc from '../HandleIpc/HandleIpc.js'
3
- import * as IpcChild from '../IpcChild/IpcChild.js'
4
- import * as IpcChildType from '../IpcChildType/IpcChildType.js'
5
-
6
- export const handleElectronMessagePort = async (messagePort, ipcId) => {
7
- Assert.object(messagePort)
8
- // Assert.number(ipcId)
9
- // TODO use handleIncomingIpc function
10
- const ipc = await IpcChild.listen({
11
- method: IpcChildType.ElectronMessagePort,
12
- messagePort,
13
- })
14
- HandleIpc.handleIpc(ipc)
15
- }
@@ -1,5 +0,0 @@
1
- import * as HandleMessage from '../HandleMessage/HandleMessage.js'
2
-
3
- export const handleIpc = (ipc) => {
4
- ipc.addEventListener('message', HandleMessage.handleMessage)
5
- }
@@ -1,19 +0,0 @@
1
- import * as Callback from '../Callback/Callback.js'
2
- import * as Command from '../Command/Command.js'
3
- import * as JsonRpc from '../JsonRpc/JsonRpc.js'
4
-
5
- const prepare = (error) => {
6
- return error
7
- }
8
-
9
- const requiresSocket = (method) => {
10
- return false
11
- }
12
-
13
- const logError = (error, prettyError) => {
14
- console.error(error)
15
- }
16
-
17
- export const handleMessage = (event) => {
18
- return JsonRpc.handleJsonRpcMessage(event.target, event.data, Command.execute, Callback.resolve, prepare, logError, requiresSocket)
19
- }
@@ -1,15 +0,0 @@
1
- import * as IpcChildModule from '../IpcChildModule/IpcChildModule.js'
2
-
3
- export const listen = async ({ method, ...params }) => {
4
- const module = await IpcChildModule.getModule(method)
5
- // @ts-ignore
6
- const rawIpc = await module.listen(params)
7
- // @ts-ignore
8
- if (module.signal) {
9
- // @ts-ignore
10
- module.signal(rawIpc)
11
- }
12
- // @ts-ignore
13
- const ipc = module.wrap(rawIpc)
14
- return ipc
15
- }
@@ -1,25 +0,0 @@
1
- import {
2
- IpcChildWithElectronMessagePort,
3
- IpcChildWithElectronUtilityProcess,
4
- IpcChildWithNodeForkedProcess,
5
- IpcChildWithNodeWorker,
6
- IpcChildWithWebSocket,
7
- } from '@lvce-editor/ipc'
8
- import * as IpcChildType from '../IpcChildType/IpcChildType.js'
9
-
10
- export const getModule = (method) => {
11
- switch (method) {
12
- case IpcChildType.NodeForkedProcess:
13
- return IpcChildWithNodeForkedProcess
14
- case IpcChildType.NodeWorker:
15
- return IpcChildWithNodeWorker
16
- case IpcChildType.ElectronUtilityProcess:
17
- return IpcChildWithElectronUtilityProcess
18
- case IpcChildType.ElectronMessagePort:
19
- return IpcChildWithElectronMessagePort
20
- case IpcChildType.WebSocket:
21
- return IpcChildWithWebSocket
22
- default:
23
- throw new Error('unexpected ipc type')
24
- }
25
- }
@@ -1,20 +0,0 @@
1
- export const NodeWorker = 1
2
- export const NodeForkedProcess = 2
3
- export const ElectronUtilityProcess = 3
4
- export const ElectronMessagePort = 4
5
- export const NodeMessagePort = 5
6
- export const WebSocket = 6
7
-
8
- export const Auto = () => {
9
- const { argv } = process
10
- if (argv.includes('--ipc-type=node-worker')) {
11
- return NodeWorker
12
- }
13
- if (argv.includes('--ipc-type=node-forked-process')) {
14
- return NodeForkedProcess
15
- }
16
- if (argv.includes('--ipc-type=electron-utility-process')) {
17
- return ElectronUtilityProcess
18
- }
19
- throw new Error(`[shared-process] unknown ipc type`)
20
- }
@@ -1 +0,0 @@
1
- export const isBuiltServer = false
@@ -1 +0,0 @@
1
- export * from '@lvce-editor/json-rpc'
@@ -1,8 +0,0 @@
1
- import * as HandleIpc from '../HandleIpc/HandleIpc.js'
2
- import * as IpcChild from '../IpcChild/IpcChild.js'
3
- import * as IpcChildType from '../IpcChildType/IpcChildType.js'
4
-
5
- export const listen = async () => {
6
- const ipc = await IpcChild.listen({ method: IpcChildType.Auto() })
7
- HandleIpc.handleIpc(ipc)
8
- }
@@ -1,14 +0,0 @@
1
- import { VError } from '@lvce-editor/verror'
2
-
3
- export const loadTypeScript = async (typescriptPath) => {
4
- try {
5
- const typescript = await import(typescriptPath)
6
- const actual = typescript.default
7
- if (!actual) {
8
- throw new Error('missing default export')
9
- }
10
- return actual
11
- } catch (error) {
12
- throw new VError(error, `Failed to load typescript`)
13
- }
14
- }
@@ -1,8 +0,0 @@
1
- import * as CommandMap from '../CommandMap/CommandMap.js'
2
- import * as CommandState from '../CommandState/CommandState.js'
3
- import * as Listen from '../Listen/Listen.js'
4
-
5
- export const main = async () => {
6
- CommandState.registerCommands(CommandMap.commandMap)
7
- await Listen.listen()
8
- }
@@ -1,21 +0,0 @@
1
- import * as NodePath from 'node:path'
2
-
3
- export const join = (...parts) => {
4
- return NodePath.join(...parts)
5
- }
6
-
7
- export const dirname = (path) => {
8
- return NodePath.dirname(path)
9
- }
10
-
11
- export const basename = (path) => {
12
- return NodePath.basename(path)
13
- }
14
-
15
- export const resolve = (path) => {
16
- return NodePath.resolve(path)
17
- }
18
-
19
- export const isAbsolute = (path) => {
20
- return NodePath.isAbsolute(path)
21
- }
@@ -1,5 +0,0 @@
1
- export const isProduction = false
2
-
3
- export const isArchLinux = false
4
-
5
- export const isAppImage = false
@@ -1,6 +0,0 @@
1
- import * as Path from '../Path/Path.js'
2
- import * as Root from '../Root/Root.js'
3
-
4
- export const getBuiltinExtensionsPath = () => {
5
- return process.env.BUILTIN_EXTENSIONS_PATH || Path.join(Root.root, 'extensions')
6
- }
@@ -1,6 +0,0 @@
1
- import { dirname, resolve } from 'node:path'
2
- import { fileURLToPath } from 'node:url'
3
-
4
- const __dirname = dirname(fileURLToPath(import.meta.url))
5
-
6
- export const root = resolve(__dirname, '../../../../../')
@@ -1,20 +0,0 @@
1
- import { VError } from '@lvce-editor/verror'
2
- import * as Assert from '../Assert/Assert.js'
3
- import * as GetTypeScriptPath from '../GetTypeScriptPath/GetTypeScriptPath.js'
4
- import * as LoadTypeScript from '../LoadTypeScript/LoadTypeScript.js'
5
-
6
- export const transpileTypeScript = async (code) => {
7
- try {
8
- Assert.string(code)
9
- const typescriptUri = GetTypeScriptPath.getTypeScriptUri()
10
- const typescript = await LoadTypeScript.loadTypeScript(typescriptUri)
11
- const newContent = await typescript.transpileModule(code, {
12
- compilerOptions: {
13
- target: 'esnext',
14
- },
15
- })
16
- return newContent
17
- } catch (error) {
18
- throw new VError(error, `Failed to transpile typescript`)
19
- }
20
- }
@@ -1,3 +0,0 @@
1
- import * as Main from './parts/Main/Main.js'
2
-
3
- Main.main()