-
Notifications
You must be signed in to change notification settings - Fork 995
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
Road to ESM/Deno support #1706
Comments
@QmarkC let me know if you're interested in taking on any of these tasks, and we can toss our names next to them as we do the work. |
I'll look around and see if I'm up to any of the tasks above but that'll have to wait until September, holidays are coming :) |
@quilicicf please give the following a try: import { Yargs, YargsType, Arguments } from 'https://deno.land/x/yargs/deno.ts'
Yargs()
.command('download <files...>', 'download a list of files', (yargs: YargsType) => {
return yargs.positional('files', {
describe: 'a list of files to do something with'
})
}, (argv: Arguments) => {
console.info(argv)
})
.strictCommands()
.demandCommand(1)
.parse(Deno.args) It's not 100% feature compatible with Node.js:
But any ways, I think this is a good start. |
Awesome! |
As for the terminal width, there have been discussions about it and I've taken the time to follow the thread. It's been implemented in Deno 1.3.1 but is still unstable. You can currently get the terminal size with: Deno.consoleSize(Deno.stdout.rid); // returns something like { columns: 93, rows: 50 } But you'd have to run the script with: deno run --unstable yourscript.ts I have found no clue about whether/when this can land in the stable features but one can always ask :)
There are other solutions but this is the most elegant as it will eventually be portable (I think it currently does not work on windows). You can for example use const cmd = Deno.run({ cmd: [ 'tput', 'cols'], stdout:'piped' });
const columnsUintArray = await cmd.output();
const columnsString = new TextDecoder().decode(columnsUintArray);
cmd.close(); But you'll need to use something other than |
@quilicicf I was thinking we probably just wait for |
This would be the safest option IMO too yes 👍 |
In beginning project with package.json "type": "module", I installed yargs@next "v16.0.0-deno.beta.1" (which I think is "modified-api-surface"?) Tried out main.js:
import futhark from './futhark.js'
...
.command(futhark)
futhark.js:
const command = 'futhark [rune]'
const describe = 'rune faster'
const builder = {
// elder: {
// default: true
// },
}
const handler = function (argv) {
// do something with argv
console.log(' futhark():', argv)
}
const module = {
command,
describe,
builder,
handler,
}
export default module Thank you for this timely (for me anyway) work on enabling Yargs for ESM. Will this issue be the center for news on ESM work? I barely caught the transition to |
Yes, and I expect there will be a couple more beta releases before we publish for real. |
@bcoe I tried taking Yargs for a spin with Deno and I'm facing some weird issue. My code runs OK when run via The trace:
I've had a look at https://deno.land/x/yargs@v16.0.0-deno.beta.1/deno and I don't have a clue what's going on ATM. Any idea? Or should I open an issue on Deno's repo? |
@quilicicf types are in their own file: import yargs from 'https://deno.land/x/yargs/deno.ts'
import { Arguments, YargsType } from 'https://deno.land/x/yargs/types.ts'
yargs()
.command('download <files...>', 'download a list of files', (yargs: YargsType) => {
return yargs.positional('files', {
describe: 'a list of files to do something with'
})
}, (argv: Arguments) => {
console.info(argv)
})
.strictCommands()
.demandCommand(1)
.parse(Deno.args) |
Ooooh, it's changed a bit since your first code snippet apparently. |
@tshinnic I would just install |
this work is shipped in |
Doesn't work anymore. See #1794 |
- ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> - ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706) ## discussion As suggested in "Node Modules at War", an ESM wrapper is used to provide basic ESM support. However, `deno` is unable to use that ESM module, failing to load the transitive CJS module with `'../index.js' does not provide an export named 'default'`. So, `deno` is instead referred to a full ESM transpile. As `deno` is it's own animal, there is no issue with CJS and ESM referring to differing modules, either for installation or creating subtle code issues.
- ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> - ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706) ## discussion As suggested in "Node Modules at War", an ESM wrapper is used to provide basic ESM support. However, `deno` is unable to use that ESM module, failing to load the transitive CJS module with `'../index.js' does not provide an export named 'default'`. So, `deno` is instead referred to a full ESM transpile. As `deno` is it's own animal, there is no issue with CJS and ESM referring to differing modules, either for installation or creating subtle code issues.
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War" * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War" * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War" * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War" * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War" * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
- reveals ESM wrapper providing ESM support (as suggested in "Node Modules at War") * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706).
- reveals ESM wrapper providing ESM support (as suggested in "Node Modules at War") * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706).
- reveals ESM wrapper providing ESM support (as suggested in "Node Modules at War") * ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg> * ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706).
With ESM modules landing in Node 14, our ongoing effort to convert fully to TypeScript (which easily targets ESM), and Deno (which leverages ESM and TypeScript) reaching v1, I think it's worth the effort to try to build yargs such that it targets all these environments.
I did an initial spike on yargs-parser and was able to achieve this goal.
Known limitations/challenges:
All this said, I think it's a worthwhile goal to target these other environments. The work to be done is as follows.
In Yargs Itself
@types/yargs
.In Dependencies
require-directory will not work well with ESM, come up with plan for replacement (help wanted).for the next major version of yargs, I would like to finally tackle async functionality in a more holistic way. If you had toawait yargs.parse()
, then we can use Dynamic imports for this functionality.CC: @QmarkC, @quilicicf
The text was updated successfully, but these errors were encountered: