Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Deno :) #24

Closed
ry opened this issue May 7, 2021 · 15 comments
Closed

Port to Deno :) #24

ry opened this issue May 7, 2021 · 15 comments
Labels
feature New feature or request

Comments

@ry
Copy link

ry commented May 7, 2021

No description provided.

@Guergeiro
Copy link

@ry why not use Deno in first place? 😋

@antonmedv
Copy link
Collaborator

@ry this is definitely a good idea. Will take a look at how to do it.

@antonmedv antonmedv added the feature New feature or request label May 7, 2021
@Minigugus
Copy link

Minigugus commented May 8, 2021

Here is a working Deno version if you're interested https://deno.land/x/zx_deno. All features but chalk are preserved (replaced with deno.land/std/fmt/colors.ts): f021edc...Minigugus:main

deno install -A --unstable https://deno.land/x/zx_deno/mod.mjs

@josh-hemphill
Copy link

I was just thinking of this, Minigugus beat me to it. Deno would also make it really simple to distribute a simple binary that already has all the zx shortcuts pre-bundled, and distribute it to package managers if it gains enough traction.

@Minigugus what unstable features is your fork using?

@Minigugus
Copy link

Minigugus commented May 8, 2021

@josh-hemphill My fork use https://deno.land/std@0.95.0/node/os.ts and https://deno.land/std@0.95.0/node/fs.ts in order to preserve scripts compatibility with the original zx package, but they aren't stable yet 😕

And indeed, Deno is fine here, for instance it can run remote code natively, it have a native fetch implementation and the global Deno object provides mostly the same features as the fs and os packages.

It is even possible to run a remote zx script without an installation step in 1 command line: deno run -A --unstable https://deno.land/x/zx_deno/zx.mjs https://deno.land/x/zx_deno/examples/parallel.mjs 😮

EDIT: It is now available on deno.land: https://deno.land/x/zx_deno

@antonmedv
Copy link
Collaborator

Cool! I wonder if it possible to use compatibility later to make zx able to run on nodejs and deno simultaneously.

@Minigugus
Copy link

@antonmedv Thanks to the os and fs compatibility modules in the Deno standard library, a lot of zx scripts are already compatible. However, every script than manipulates global objects like process, Buffer, Deno, window, and so on is not compatible because theses objects that are plateform-specific, and add polyfills for both Node and Deno is way beyond zx... 😕

@antonmedv
Copy link
Collaborator

Fair point, so it’s better to have two separate implementation for different backends.

@Minigugus
Copy link

Minigugus commented May 8, 2021

@antonmedv Another solution could be to enhance the api exposed by zx so that users that wants compatibility just have to avoid backend specific code. For instance question, cd and fetch works in the same way for both the Node and Deno version, so maybe we could add zx versions of read, mkdir, open, and so on? At the same time, it would allow to transpose bash commands into zx functions (eg. for await (const file of ls(pwd())))

@jl4386
Copy link

jl4386 commented May 10, 2021

found this one too: https://github.com/linux-china/dx

This was referenced May 10, 2021
@antonmedv
Copy link
Collaborator

After thinking 💭 zx targets only node. And there’s already a few deno implementations.

@kidonng
Copy link

kidonng commented Jul 21, 2022

Adding some info here. While a direct Deno port is not available, zx is working fine through Deno's Node compat layer:

> deno eval '
    import "https://esm.sh/zx@6.1.0/src/globals.mjs?deps=chalk@4.1.2"
   
    const name = "zx"
    const runtime = "Deno"
    $`echo ${name} ❤️ ${runtime}`
'
$ echo zx ❤️ Deno
zx ❤️ Deno

Gotchas:

  • Any version newer than 6.1.0 throws an error, due to AsyncLocalStorage not implemented:

    > deno eval '
        import "https://esm.sh/zx@6.2.0/build/globals.js?deps=chalk@4.1.2"
    '
    error: Uncaught SyntaxError: The requested module 
    'https://deno.land/std@0.149.0/node/async_hooks.ts' does not provide an export named 
    'AsyncLocalStorage'
        at <anonymous> (https://esm.sh/v87/zx@6.2.0/X-ZC9jaGFsa0A0LjEuMg/deno/core.js:2:607)
    
  • chalk needs to be pinned to 4.x as esm.sh doesn't support the way newer versions import dependencies. This is not a Deno issue per se, it can also be avoided by e.g. importing zx bundled with dependencies.

@antonmedv
Copy link
Collaborator

This why I don’t think supporting both runtimes is something I want to do)

@mono0x
Copy link

mono0x commented Feb 21, 2023

Currently, it seems to the zx works with Deno thanks to AsyncLocalStorage becoming ready on Deno.

import "npm:zx@7.1.1/globals";
await $`echo "Hello, world!"`;
❯ deno --version
deno 1.30.3 (release, x86_64-unknown-linux-gnu)
v8 10.9.194.5
typescript 4.9.4

❯ deno run -A hello.ts
$ echo "Hello, world!"
Hello, world!

@antonmedv
Copy link
Collaborator

Wow! Supper impressive)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants