@lvce-editor/typescript-compile-process 0.0.0-dev

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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present, Lvce Editor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@lvce-editor/typescript-compile-process",
3
+ "version": "0.0.0-dev",
4
+ "main": "index.js",
5
+ "type": "module",
6
+ "keywords": [
7
+ "Lvce Editor"
8
+ ],
9
+ "author": "Lvce Editor",
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/lvce-editor/lvce-editor.git",
14
+ "directory": "packages/typescript-compile-process"
15
+ },
16
+ "engines": {
17
+ "node": ">=18"
18
+ },
19
+ "dependencies": {
20
+ "@lvce-editor/assert": "^1.2.0",
21
+ "@lvce-editor/ipc": "^8.2.0",
22
+ "@lvce-editor/json-rpc": "^1.3.0",
23
+ "@lvce-editor/pretty-error": "^1.5.0",
24
+ "@lvce-editor/verror": "^1.2.0"
25
+ }
26
+ }
@@ -0,0 +1 @@
1
+ export * from '@lvce-editor/assert'
@@ -0,0 +1 @@
1
+ export { resolve } from '@lvce-editor/json-rpc'
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1,7 @@
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
+ }
@@ -0,0 +1,17 @@
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
+ }
@@ -0,0 +1,22 @@
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
+ }
@@ -0,0 +1,15 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ import * as HandleMessage from '../HandleMessage/HandleMessage.js'
2
+
3
+ export const handleIpc = (ipc) => {
4
+ ipc.addEventListener('message', HandleMessage.handleMessage)
5
+ }
@@ -0,0 +1,19 @@
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
+ }
@@ -0,0 +1,15 @@
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
+ }
@@ -0,0 +1,25 @@
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
+ }
@@ -0,0 +1,20 @@
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
+ }
@@ -0,0 +1 @@
1
+ export const isBuiltServer = false
@@ -0,0 +1 @@
1
+ export * from '@lvce-editor/json-rpc'
@@ -0,0 +1,8 @@
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
+ }
@@ -0,0 +1,14 @@
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
+ }
@@ -0,0 +1,8 @@
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
+ }
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ export const isProduction = false
2
+
3
+ export const isArchLinux = false
4
+
5
+ export const isAppImage = false
@@ -0,0 +1,6 @@
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
+ }
@@ -0,0 +1,6 @@
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, '../../../../../')
@@ -0,0 +1,20 @@
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
+ }
@@ -0,0 +1,3 @@
1
+ import * as Main from './parts/Main/Main.js'
2
+
3
+ Main.main()