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

[WIP] Fix bug where running script without extension removes existing .mjs files #276

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ import {strict as assert} from 'assert'
}
}

{ // Scripts with no extension are working
{ // Scripts with no extension work and do not overwrite similarly named files.
await $`node zx.mjs tests/no-extension`
assert.match((await fs.readFile('tests/no-extension.mjs')).toString(), /Test file to verify no-extension didn't overwrite similarly name .mjs file./);
}

{ // require() is working from stdin
Expand Down
1 change: 1 addition & 0 deletions tests/no-extension.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Test file to verify no-extension didn't overwrite similarly name .mjs file.
4 changes: 3 additions & 1 deletion zx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ async function writeAndImport(script, filepath, origin = filepath) {

async function importPath(filepath, origin = filepath) {
let ext = extname(filepath)
let tmpFilename= Math.random().toString(36).substr(2) + '.mjs'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to consider: error messages.

User will get errors with file name is random which can lead to bad DX.

What if filename + '.mjs' is not exists? We can use it (same as old behavior).

In case there is a collision use: filename + random + '.mjs'.

WDYF?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good call. Yeah, that makes sense to me. Will push that change later today!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @antonmedv, sorry for the delay.

Just pushed the requested fix. If you feel like it's good to go, I'll let you hit resolve conversation. If you'd like me to make any additional fixes, though, just let me know. Thanks!


if (ext === '') {
return await writeAndImport(
await fs.readFile(filepath),
join(dirname(filepath), basename(filepath) + '.mjs'),
join(dirname(filepath), tmpFilename),
origin,
)
}
Expand Down