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

Improve Windows compatibility #5

Merged
merged 9 commits into from
Mar 10, 2021
Merged

Conversation

TheBrenny
Copy link
Contributor

Added a method to acquire the session history on windows. It doesn't get the history, and also doesn't check to see if the user has a HISTFILE environment variable. But I don't know if any windows user actually has one for CMD.

This won't work if invoked through PowerShell though, because PS uses the history command.

🙏

@sindresorhus
Copy link
Owner

I don't really understand the point. It gets only the session history, but not permanent history? However, the session history is nothing as we're spawning the terminal. Can you please elaborate?

@TheBrenny
Copy link
Contributor Author

I know it doesn't get a persistent history, but getting the session's history is also a common use case (I imagine). Otherwise, if a HISTFILE env variable is set, that file should be used - because there are tools out there which persist CMD history across sessions.

Otherwise, a session history use case would be:

$ git clone https://github.com/sindresorhus/shell-history.git && alert

Where alert is an alias to node C:\\alert.js (for example), alert.js can read the session's history to make a toast notification with "git completed!" as the title.

I hope that makes more sense.

@sindresorhus
Copy link
Owner

but getting the session's history is also a common use case (I imagine).

But does it actually get the history of the whole session? Like commands executed before shell-history was running.

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Please always run npm test locally before committing.

@sindresorhus
Copy link
Owner

The special Windows behavior (of only getting the session history) needs to be documented.

@sindresorhus sindresorhus changed the title Use doskey on windows platforms Improve Windows compatibility Oct 9, 2020
@TheBrenny
Copy link
Contributor Author

Please always run npm test locally before committing.

I tried running the tests but it just isn't working. First, there are issues because eslint-plugin-ava causes an undefined TypeError to throw, because that's how it's made. I did a clean install on WSL (which defeats the purpose of the test) which failed anyway because of the previous issue.

I then decided to skip the whole xo process (because none of Node12(Win), Node12(Linux) or Node14(Linux)) worked.

Calling node_modules/.bin/ava (both the sh and cmd variants on Win and WSL) didn't work because test.js uses ES6 syntax, which I'm not compatible with apparently...

At the end of the day, I converted test.js to Node's require syntax, called node_modules/.bin/ava and magic almost happened - I gave the test a name, and then it worked with 1 test passed.

I didn't plan on giving you my life story on trying to run XO and Ava, so sorry about that! 😅

The special Windows behavior (of only getting the session history) needs to be documented.

Doneskies.

Send feedback! 🎉

@rocktimsaikia rocktimsaikia mentioned this pull request Oct 13, 2020
@sindresorhus
Copy link
Owner

I then decided to skip the whole xo process (because none of Node12(Win), Node12(Linux) or Node14(Linux)) worked.

XO works fine on all those setups. This is a problem with your setup. But you can look at the lint failures in the Travis output and fix manually.

index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

#5 (comment) is not resolved. Please read my comment again. I think it's quite clear.

@TheBrenny
Copy link
Contributor Author

but getting the session's history is also a common use case (I imagine).

But does it actually get the history of the whole session? Like commands executed before shell-history was running.

Sorry I missed this comment -- I've added test_win.bat which shows how this works. I don't know how to turn that into a "test" per se, but it's a visual tester I guess... The only bug which I know of for this is when using clink (which overrides doskey, so it's a problem on that end, not here).


#6 Fixed my problems (big ty @rocktimsaikia 🎉) -- Test now passes correctly.


#5 (comment) is not resolved. Please read my comment again. I think it's quite clear.

You are correct it wasn't resolved - I didn't realise that spawnSync had an encoding option. (Off-topic, but what's the benefit over toString if its default behaviour is to encode to utf8?)

This has now been resolved.

readme.md Outdated

### shellHistory.path()

Get the path of the file containing the shell history.

On Windows, this will return either the `HISTFILE` environment variable, or null.
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see any null in the code.

index.js Outdated
@@ -63,7 +60,17 @@ module.exports = options => {
options = options || {};

if (process.platform === 'win32') {
return [];
const histFile = getPath(options);
Copy link
Owner

Choose a reason for hiding this comment

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

The naming is an implementation detail of the getPath method.

Suggested change
const histFile = getPath(options);
const historyPath = getPath(options);

index.js Outdated

// No histfile used -- spawn a doskey
const childProcess = require('child_process');
const doskey = childProcess.spawnSync('doskey', ['/history'], {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const doskey = childProcess.spawnSync('doskey', ['/history'], {
const {stdout} = childProcess.spawnSync('doskey', ['/history'], {

index.js Outdated
const doskey = childProcess.spawnSync('doskey', ['/history'], {
encoding: 'utf8'
});
return doskey.stdout.trim().split('\r\n') || []; // Enforce array
Copy link
Owner

Choose a reason for hiding this comment

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

The comment is moot. The code is clear enough.

index.js Outdated
}

// No histfile used -- spawn a doskey
const childProcess = require('child_process');
Copy link
Owner

Choose a reason for hiding this comment

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

Imports should be at the top of the file.

test_win.bat Outdated
@echo off

echo Print entire history
node -p "const history = require('./'); history()"
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
node -p "const history = require('./'); history()"
node -p "const history = require('.'); history()"

test_win.bat Outdated
pause
echo.
echo Print last command (which will be "test_win.bat")
node -p "const history = require('./'); let h = history(); h[h.length-1]"
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
node -p "const history = require('./'); let h = history(); h[h.length-1]"
node -p "const history = require('.'); let h = history(); h[h.length - 1]"

test_win.bat Outdated
@@ -0,0 +1,12 @@
:: Acts as a windows tester.
Copy link
Owner

Choose a reason for hiding this comment

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

This sentence is not clear enough and it's Windows.

index.js Outdated
return '';
}

// HISTFILE might be cross-platform, so win32 check is after!
Copy link
Owner

Choose a reason for hiding this comment

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

"might" is not a good explanation. Explain exactly when it would be.

@sindresorhus
Copy link
Owner

I don't know how to turn that into a "test" per se, but it's a visual tester I guess...

Then that should be explained.

@sindresorhus
Copy link
Owner

Off-topic, but what's the benefit over toString if its default behaviour is to encode to utf8?)

No technical benefit in this case. But it's more readable to declare it upfront. It does matter when it comes to streams though, as it might end up splitting Unicode characters incorrectly.

@sindresorhus
Copy link
Owner

Please spend some more time perfecting this pull request.

.gitignore Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test_win.bat Outdated Show resolved Hide resolved
test_win.bat Outdated Show resolved Hide resolved
test_win.bat Outdated Show resolved Hide resolved
test_win.bat Outdated Show resolved Hide resolved
test_win.bat Outdated Show resolved Hide resolved
Base automatically changed from master to main January 23, 2021 18:46
@sindresorhus
Copy link
Owner

Bump

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants