Skip to content

Commit

Permalink
README opens correctly and added unit tests (#28)
Browse files Browse the repository at this point in the history
* Alternative approach

* Switch from a regex approach to startswith/endswith

* Increase version

* Update changelog

* Remove unnecessary comma

* Fix typo

* Add version to package json

* Initial tests

* Add more test cases for case insensitiveness

* Add extra case for README.MD (all uppercase) and failure tests

* Add another test about nothing happening when user has already seen README
  • Loading branch information
sander1095 authored Oct 16, 2024
1 parent 19be391 commit a851a4e
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the "readme-auto-open" extension will be documented in this file.

## [1.0.1] - 2024-10-16

- Fix issues with incorrect files being opened when the extension loads ([#27](https://github.com/sander1095/vscode-readme-auto-open/issues/27))

## [1.0.0] - 2024-10-15

- Initial release
147 changes: 145 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "readme-auto-open",
"displayName": "Readme Auto Open",
"description": "A VSCode plug-in that automatically opens the README when you open a project for the first time, ensuring people read it",
"version": "1.0.0",
"version": "1.0.1",
"publisher": "sandertenbrinke",
"author": "Sander ten Brinke",
"categories": [
Expand Down Expand Up @@ -62,6 +62,7 @@
"devDependencies": {
"@types/mocha": "^10.0.8",
"@types/node": "20.x",
"@types/sinon": "^17.0.3",
"@types/vscode": "^1.94.0",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
Expand All @@ -70,6 +71,7 @@
"esbuild": "^0.24.0",
"eslint": "^9.11.1",
"npm-run-all": "^4.1.5",
"sinon": "^19.0.2",
"typescript": "^5.6.2"
}
}
44 changes: 28 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';

const readmeRegex = /readme(\..*)?/i;
const readmeStateKey = 'hasSeenReadme';

export async function activate(context: vscode.ExtensionContext) {
Expand All @@ -15,41 +14,54 @@ export async function activate(context: vscode.ExtensionContext) {
}

// We grab all the files in the root instead of using the glob-pattern to search for a README file.
// The reason is that glob pattern searching doesn't support support case-insensitive search, so it's easier to use a regex instead.
// The reason is that glob pattern searching doesn't support case-insensitive search, so it's easier to use a regex instead.
const allFilesInRootDirectory = await vscode.workspace.findFiles('*');

const readme = allFilesInRootDirectory.find(file => file.fsPath.match(readmeRegex));
// To prevent false positives when a PATH contains the word "readme" we only consider the file name for the search.
// We lowercase the file name to make the search case-insensitive.
const allFileNamesWithUri = allFilesInRootDirectory.map(p => {
const fileName = p?.fsPath?.split(/[\\/]/)?.pop()?.toLowerCase();
return { uri: p, fileName: fileName };
});

// We support both README files with and without extensions.
// Perhaps a regex would be better, but this is simpler.
const readme = allFileNamesWithUri.find(file => file.fileName?.endsWith('readme') || file.fileName?.startsWith('readme.'));

if (!readme) {
console.log('README was not found.');
if (!readme || !readme.fileName) {
console.log('Readme Auto Open: README was not found.');
return;
}

const readmeCanBePreviewed = readme.fsPath.toLowerCase().endsWith('.md');
const readmeCanBePreviewed = readme.fileName.endsWith('.md');
if (readmeCanBePreviewed) {
console.log('Readme Auto Open: Attempting to open README in preview mode.');

try {
await vscode.commands.executeCommand('markdown.showPreview', readme);
} catch (error) {
console.error('Readme Auto Open: Error opening README in preview mode. It will be opened in the editor instead', error);
await openReadmeInEditor(readme);
}
await openReadmeInPreviewWindow(readme.uri);
} else {
await openReadmeInEditor(readme);
await openReadmeInEditor(readme.uri);
}

// Set the workspace state to indicate that the user has seen the README.
await context.workspaceState.update(readmeStateKey, true);

console.log(`Readme Auto Open: Opened ${readme.fsPath}.`);
console.log(`Readme Auto Open: Opened ${readme.uri.fsPath}.`);
}

async function openReadmeInEditor(readme: vscode.Uri) {
console.log('Readme Auto Open: Opening README in text editor.');
await vscode.window.showTextDocument(readme);
}

async function openReadmeInPreviewWindow(readme: vscode.Uri) {
console.log('Readme Auto Open: Attempting to open README in preview mode.');

try {
await vscode.commands.executeCommand('markdown.showPreview', readme);
} catch (error) {
console.error('Readme Auto Open: Error opening README in preview mode. It will be opened in the editor instead');
await openReadmeInEditor(readme);
}
}

function registerResetStateCommand(context: vscode.ExtensionContext) {
// See package.json for command information
const disposable = vscode.commands.registerCommand('readmeAutoOpen.resetState', async () => {
Expand Down
Loading

0 comments on commit a851a4e

Please sign in to comment.