javascript

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExecJS = sync.OnceValue(func() Executor {
	jsSettings := settings.Javascript.Get()
	pathToEngine, err := exec.LookPath(jsSettings.Engine)
	if err != nil {
		return errorExecutor(err)
	}
	return binaryExecutor(pathToEngine)
})
View Source
var ExecTS = sync.OnceValue(func() Executor {
	jsSettings := settings.Javascript.Get()
	if jsSettings.Engine == "node" {
		loaderModule, err := ResolveGlobalImport(jsSettings.Transpiler, true)
		if err != nil {

			binaryName := strings.ReplaceAll(jsSettings.Transpiler, "/", "-")
			fullPath, err := exec.LookPath(binaryName)
			if err != nil {
				return errorExecutor(fmt.Errorf("transpiler %s not found: %w", jsSettings.Transpiler, err))
			}
			if binaryName == "ts-node" || binaryName == "ts-node-esm" || binaryName == "tsx" {
				log.Printf("[WARN] Failed to locate loader script, may cause sub-optimal process configuration: %v", err)
			}
			return binaryExecutor(fullPath)
		}

		return func(ctx context.Context, script string, args ...string) (*exec.Cmd, error) {
			args = append([]string{
				"--experimental-specifier-resolution=node",
				"--import",
				"file://" + loaderModule,
				script,
			}, args...)
			return ExecJS()(ctx, "", args...)
		}
	} else {
		return ExecJS()
	}
})
View Source
var UnmarshalWrapperCJS = minify(`
	const M = require('@');
	let R = M['default'] ?? M;
	R = typeof R == 'function' ? R(JSON.parse('$')) : R;
	console.error('\x0d\x01\x02'+JSON.stringify(R)+'\x03\x03\x0a')
`)
View Source
var UnmarshalWrapperESM = minify(`
	import * as M from 'file://@';
	let R = M['default'] ?? M;
	R = typeof R == 'function' ? R(JSON.parse('$')) : R;
	console.error('\x0d\x01\x02'+JSON.stringify(R)+'\x03\x03\x0a')
`)

Functions

func InstallGlobalPackage

func InstallGlobalPackage(packageName string) error

func ResolveGlobalImport

func ResolveGlobalImport(importPath string, orInstall bool) (string, error)

func UnmarshalDynamicWith

func UnmarshalDynamicWith(execute Executor, ctx context.Context, path string, code string, params any, out any) (err error)

Types

type Executor

type Executor = func(ctx context.Context, script string, args ...string) (*exec.Cmd, error)

type PackageManifest

type PackageManifest struct {
	Exports map[string]string `json:"exports"`
	Path    string            `json:"path"`
}

func ResolveGlobalPackage

func ResolveGlobalPackage(packageName string, orInstall bool) (manifest PackageManifest, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL