unjs / jiti

Runtime TypeScript and ESM support for Node.js
MIT License
1.91k stars 62 forks source link

Common/Useful properties and settings. #336

Closed SelfhostedPro closed 4 weeks ago

SelfhostedPro commented 4 weeks ago

Describe the feature

Being able to define the working directory and commands could be very useful as I found the current way of doing it feeling a bit "hacky" as I'm relying on the arguments of the command I'm calling.

This is what it looks like to build a nuxt module programmatically right now and feels like it could use some improvement. I think it would be nice to have cwd as an option or to have the debug tag at least mention it. It also feels wrong to type things this way but they do work.

    const jiti = createJiti(`${dataPath}/plugins/${plugin.name}/`, { debug: true, })
    const jiti_process = await jiti.import('process') as typeof import('process')
    const { runCommand } = await jiti.import('citty') as typeof import('citty')
    const { main } = await jiti.import('nuxi') as typeof import('nuxi')

    console.log('running nuxi prepare')
    await runCommand(main, { rawArgs: [`prepare`, `--cwd`, `${dataPath}/plugins/${plugin.name}/`] })

    console.log('running nuxt-module-builder prepare')
    await runCommand(prepare, { rawArgs: ['--cwd', `${dataPath}/plugins/${plugin.name}/`] })

    console.log('running nuxt module build')
    await runCommand(build, { rawArgs: ['--cwd', `${dataPath}/plugins/${plugin.name}/`] })

Additional information

pi0 commented 4 weeks ago

I'm not sure to understand how it relates to jiti itself. runCommand is a citty utility that accepts cwd argument. (i welcome any ideas how to improve it)

jiti.import() is just same as import()

pi0 commented 4 weeks ago

Regarding typing, i also did little research. Supporting auto inference of types still needs a typescript feature for dynamic import(T) support.

Tracker: https://github.com/microsoft/TypeScript/issues/31090

pi0 commented 4 weeks ago

Closing issue in meanwhile as is not something we could plan to do on jiti side. please feel free to comment or open another more clear issue.

SelfhostedPro commented 4 weeks ago

Yeah, that’s fair. I had a fundamental misunderstanding of how things were working. Sorry for the ticket but thanks for the information.

SelfhostedPro commented 4 weeks ago

I think part of the issue was seeing examples of running code from jiti via the cli and thinking it was an easy way of running other arbitrary scripts/code in a programmatic way. (End goal for what I’m writing is a runtime plugin/module/layer architecture for nuxt projects and at least jiti helps with dynamically importing things)