Git Product home page Git Product logo

Comments (6)

antonmedv avatar antonmedv commented on July 28, 2024 5

This looks already complicated =)
What about distributing package-lock.json with your script and add next line to beginning of your script:

await $`npm i`

from zx.

andyrichardson avatar andyrichardson commented on July 28, 2024 4

How about support for imports using the http protocol?

Unversioned esmodule

import yargs from 'https://unpkg.com/yargs?module';

Versioned commonjs

import yargs from 'https://unpkg.com/[email protected]';

from zx.

antonmedv avatar antonmedv commented on July 28, 2024

I thought about it, but this is a dangerous road. What about version, package-lock.json?

from zx.

OnurGvnc avatar OnurGvnc commented on July 28, 2024

I thought about it, but this is a dangerous road. What about version, package-lock.json?

The version issue may be solved as follows.

#!/usr/bin/env zx

deps({
  yargs: '^17.0',
  chokidar: '^3.4.3',
  colorette: '1.2',
  shelljs: '^0.8.4',
})

const yargs = await use('yargs')
const { hideBin } = await use('yargs/helpers')

const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}

console.log(argv)
`zx.mjs` experimental basic implementation
import { join, basename, dirname } from 'path'
import os, { tmpdir } from 'os'
import { promises as fs } from 'fs'
import url from 'url'
import { v4 as uuid } from 'uuid'
import { $, cd, question, fetch, chalk, ProcessOutput } from './index.mjs'
import { version } from './version.js'

let dependencies = {}

const deps = (deps) => {
  dependencies = deps
}

const use = async (packageNameOrPath) => {
  let packageName = packageNameOrPath

  const pathParts = packageName.split('/')

  if (packageName.startsWith('@') && pathParts.length > 1) {
    packageName = pathParts[0] + '/' + pathParts[1]
  } else if (pathParts.length > 1) {
    packageName = pathParts[0]
  }

  const __dirname = dirname(url.fileURLToPath(import.meta.url))

  let isExist = false
  try {
    isExist = (
      await fs.lstat(join(__dirname, 'node_modules', packageName))
    ).isDirectory()
  } catch (e) {}

  if (!isExist) {
    const cwdPrev = $.cwd
    $.cwd = __dirname

    let packageNameWithVersion = packageName
    if (dependencies[packageName]) {
      packageNameWithVersion = `${packageName}@${dependencies[packageName]}`
    }

    await $`npm install ${packageNameWithVersion}`

    $.cwd = cwdPrev
  }

  const p = await import(packageNameOrPath)
  return p.default ?? p
}

Object.assign(global, {
  $,
  cd,
  fetch,
  question,
  chalk,
  fs,
  os,
  use,
  deps,
})

....

I have no idea about package-lock.json

from zx.

mainrs avatar mainrs commented on July 28, 2024

I thought about it, but this is a dangerous road. What about version, package-lock.json?

I do agree with this. I think forking is probably the best thing to do if custom packages are wanted, at the end, it's a 5min change and merging back with upstream shouldn't be a problem in almost all cases.

Just as a side note: I had the same demand as I often use enquirer for scripts. I've gone ahead and used deno instead, which actually allows for remote imports natively.

from zx.

antonmedv avatar antonmedv commented on July 28, 2024

I think this is out of the scope of the zx project. Closing.

from zx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.