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

Xplorer Cli #43

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/docs/guides/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ This feature hasn't optimized yet. It works but it might be laggy. Will be optim
Xplorer CLI:

```bash
xplorer [dir1] [dir2] [dir3]
xplorer <options> [dir1] [dir2] [dir3]
```

Xplorer will open `dir`, `dir2`, `dir3` as tabs on Xplorer. If there's no any dir passed into the command, Xplorer will starts at Home page.

Options:

| Command | Alias | Description |
| ----------- | ----- | ---------------------------------------------- |
| `--help` | `-h` | Show help |
| `--version` | `-v` | Show version number |
| `--reveal` | `-r` | Open the containing folder and select the file |

<details>
<summary>
<code>xplorer: command not found</code> error on Windows
Expand Down
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"prism-react-renderer": "^1.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"url-loader": "^4.1.1"
"url-loader": "^4.1.1",
"yargs": "^17.1.1"
},
"browserslist": {
"production": [
Expand Down
34 changes: 33 additions & 1 deletion docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,15 @@ cliui@^5.0.0:
strip-ansi "^5.2.0"
wrap-ansi "^5.1.0"

cliui@^7.0.2:
version "7.0.4"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
integrity sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.0"
wrap-ansi "^7.0.0"

clone-deep@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/clone-deep/-/clone-deep-4.0.1.tgz#c19fd9bdbbf85942b4fd979c84dcf7d5f07c2387"
Expand Down Expand Up @@ -4215,7 +4224,7 @@ gensync@^1.0.0-beta.1, gensync@^1.0.0-beta.2:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

get-caller-file@^2.0.1:
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
Expand Down Expand Up @@ -9182,6 +9191,11 @@ y18n@^4.0.0:
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.3.tgz#b5f259c82cd6e336921efd7bfd8bf560de9eeedf"
integrity sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==

y18n@^5.0.5:
version "5.0.8"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"
integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==

yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
Expand All @@ -9200,6 +9214,11 @@ yargs-parser@^13.1.2:
camelcase "^5.0.0"
decamelize "^1.2.0"

yargs-parser@^20.2.2:
version "20.2.9"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==

yargs@^13.3.2:
version "13.3.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.3.2.tgz#ad7ffefec1aa59565ac915f82dccb38a9c31a2dd"
Expand All @@ -9216,6 +9235,19 @@ yargs@^13.3.2:
y18n "^4.0.0"
yargs-parser "^13.1.2"

yargs@^17.1.1:
version "17.1.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.1.1.tgz#c2a8091564bdb196f7c0a67c1d12e5b85b8067ba"
integrity sha512-c2k48R0PwKIqKhPMWjeiF6y2xY/gPMUlro0sgxqXpbOIohWiLNXWslsootttv7E1e73QPAMQSg5FeySbVcpsPQ==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.0"
y18n "^5.0.5"
yargs-parser "^20.2.2"

yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
Expand Down
26 changes: 13 additions & 13 deletions src/Components/Context Menu/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ interface MenuItem{
type Menu = MenuItem[][];

type openFileWithDefaultApp = (file: string) => void;
type openDir = (dir:string) => void;
type open = (dir:string) => void;

const ContextMenuInner = (target: HTMLElement, coorX:number, coorY:number, openDir:openDir) => {
const ContextMenuInner = (target: HTMLElement, coorX:number, coorY:number, open:open) => {
if (target.classList.contains("home-section")) target = document.getElementById("workspace") // If context menu target is on home-section, use main element as target instead.
while (!target.dataset.path) {
target = target.parentNode as HTMLElement
Expand Down Expand Up @@ -280,32 +280,32 @@ const ContextMenuInner = (target: HTMLElement, coorX:number, coorY:number, openD
case "A-Z":
sort[currentPath] = "A" // A = A - Z
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
case "Z-A":
sort[currentPath] = "Z" // Z = Z - A
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
case "Last Modified":
sort[currentPath] = "L" // L = Last Modified
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
case "First Modified":
sort[currentPath] = "F" // F = First Modified
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
case "Size":
sort[currentPath] = "S" // S = Size
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
case "Type":
sort[currentPath] = "T" // T = Type
storage.set("sort", sort)
openDir(currentPath)
open(currentPath)
break;
}
if (target.innerHTML.startsWith("File")) {
Expand Down Expand Up @@ -345,13 +345,13 @@ const ContextMenuInner = (target: HTMLElement, coorX:number, coorY:number, openD
* Create context menu of an elememt
* @param {HTMLElement} element - Element you want to create context menu of
* @param {openFileWithDefaultApp} openFileWithDefaultApp - openFileWithDefaultApp function (optional), pass in the function to avoid circular dependencies
* @param {openDir} openDir - openDir function (optional), pass in the function to avoid circular dependencies
* @param {open} open - open function (optional), pass in the function to avoid circular dependencies
* @returns {void}
*/
const ContextMenu = (element:HTMLElement, openFileWithDefaultApp?: openFileWithDefaultApp, openDir?: openDir):void => {
const ContextMenu = (element:HTMLElement, openFileWithDefaultApp?: openFileWithDefaultApp, open?: open):void => {
// Escape circular dependency
if (!openFileWithDefaultApp) openFileWithDefaultApp = require('../Files/File Operation/open').openFileWithDefaultApp //eslint-disable-line @typescript-eslint/no-var-requires
if (!openDir) openDir = require('../Files/File Operation/open').openDir //eslint-disable-line @typescript-eslint/no-var-requires
if (!open) open = require('../Files/File Operation/open').open //eslint-disable-line @typescript-eslint/no-var-requires

const { reload } = require("../Layout/windowManager"); //eslint-disable-line @typescript-eslint/no-var-requires

Expand All @@ -368,7 +368,7 @@ const ContextMenu = (element:HTMLElement, openFileWithDefaultApp?: openFileWithD

contextMenu.style.left = coorX + "px";
contextMenu.style.top = coorY + "px";
ContextMenuInner(e.target as HTMLElement, coorX, coorY, openDir)
ContextMenuInner(e.target as HTMLElement, coorX, coorY, open)

contextMenu.querySelectorAll("span").forEach(menu => {
menu.addEventListener("click", () => {
Expand Down Expand Up @@ -400,7 +400,7 @@ const ContextMenu = (element:HTMLElement, openFileWithDefaultApp?: openFileWithD
}
else storage.set('recent', [filePath])
} else {
openDir(filePath)
open(filePath)
}
break;
case "openMultipleTabs":
Expand Down
25 changes: 18 additions & 7 deletions src/Components/Files/File Operation/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {isHiddenFile} from "is-hidden-file";
import { ContextMenu } from "../../Context Menu/contextMenu";
import formatBytes from "../../Functions/filesize";
import getType from "../File Type/type";
import { SelectListener } from "./select";
import { SelectListener, Select } from "./select";
import { InfoLog, ErrorLog } from "../../Functions/log";
import { closePreviewFile } from "../File Preview/preview";
import {dialog} from "@electron/remote";
Expand Down Expand Up @@ -93,7 +93,7 @@ const openFileHandler = (e:Event):void => {
}
else storage.set('recent', [filePath])
} else {
openDir(filePath)
open(filePath)
}
}

Expand All @@ -120,7 +120,7 @@ const listenOpen = (elements: NodeListOf<HTMLElement>):void => {
* @param {string} dir - directory base path
* @returns {void}
*/
const displayFiles = async (files: fileData[], dir:string) => {
const displayFiles = async (files: fileData[], dir:string, options?: {reveal: boolean, initialDirToOpen: string}) => {
const hideSystemFile = storage.get("preference")?.data?.hideSystemFiles ?? true
const dirAlongsideFiles = storage.get("preference")?.data?.dirAlongsideFiles ?? false
const layout = storage.get("layout")?.data?.[dir] ?? storage.get("preference")?.data?.layout ?? "s"
Expand Down Expand Up @@ -193,8 +193,13 @@ const displayFiles = async (files: fileData[], dir:string) => {
`
MAIN_ELEMENT.appendChild(fileGrid)

ContextMenu(fileGrid, openFileWithDefaultApp, openDir)
ContextMenu(fileGrid, openFileWithDefaultApp, open)
})
if(options.reveal || !fs.statSync(dir)?.isDirectory()){
Select(document.querySelector<HTMLElement>(
`[data-path="${escape(options.initialDirToOpen)}"]`
), false, false, document.querySelectorAll(".file"))
}

updateTheme()
nativeDrag(document.querySelectorAll(".file"), dir)
Expand All @@ -211,9 +216,14 @@ const displayFiles = async (files: fileData[], dir:string) => {
/**
* Open a directory on Xplorer
* @param {string} dir
* @param {boolean} boolean - Open the parent directory and select the file/dir
* @returns {Promise<void>}
*/
const openDir = async (dir:string):Promise<void> => {
const open = async (dir:string, reveal?:boolean):Promise<void> => {
const initialDirToOpen = dir;
if(reveal || !fs.statSync(dir)?.isDirectory()){
dir = path.dirname(dir)
}
closePreviewFile()
timeStarted = Date.now()
startLoading()
Expand Down Expand Up @@ -306,7 +316,7 @@ const openDir = async (dir:string):Promise<void> => {
})
}
const files = getFiles()
displayFiles(files, dir)
displayFiles(files, dir, {reveal, initialDirToOpen})
// Watch the directory
watcher?.close()
watcher = fs.watch(dir, async (_, filePath) => {
Expand All @@ -319,5 +329,6 @@ const openDir = async (dir:string):Promise<void> => {
})
}
}


export { listenOpen, openDir, openFileWithDefaultApp, closeWatcher }
export { listenOpen, open, openFileWithDefaultApp, closeWatcher }
24 changes: 12 additions & 12 deletions src/Components/Layout/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const createNewTab = (path?: string): void => {
storage.set(`tabs-${windowGUID}`, tabs);
newTab.parentElement.removeChild(newTab);

const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
openDir(tabs.tabs[tabs.focus].position);
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
open(tabs.tabs[tabs.focus].position);
}
});
createNewTabElement.parentElement.insertBefore(newTab, createNewTabElement); // Insert the new tab
Expand All @@ -71,8 +71,8 @@ const createNewTab = (path?: string): void => {
tabsInfo.focusHistory.push(tabsInfo.latestIndex);
storage.set(`tabs-${windowGUID}`, tabsInfo);

const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
openDir(path || 'xplorer://Home');
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
open(path || 'xplorer://Home');

newTab.addEventListener('click', () => {
SwitchTab(newTab.dataset.tabIndex);
Expand All @@ -91,8 +91,8 @@ const SwitchTab = (tabIndex: number | string): void => {
tabs.focusHistory.push(parseInt(String(tabIndex)));
tabs.tabs[tabs.focus].currentIndex -= 1;
storage.set(`tabs-${windowGUID}`, tabs);
const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
openDir(tabs.tabs[tabIndex].position);
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
open(tabs.tabs[tabIndex].position);
};

/**
Expand All @@ -101,10 +101,10 @@ const SwitchTab = (tabIndex: number | string): void => {
*/
const goBack = (): void => {
const tabs = storage.get(`tabs-${windowGUID}`)?.data;
const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
const _focusingTab = tabs.tabs[tabs.focus];
if (_focusingTab.currentIndex > 0) {
openDir(_focusingTab.history[_focusingTab.currentIndex - 1]);
open(_focusingTab.history[_focusingTab.currentIndex - 1]);
}
};

Expand All @@ -114,13 +114,13 @@ const goBack = (): void => {
*/
const goForward = (): void => {
const tabs = storage.get(`tabs-${windowGUID}`)?.data;
const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
const _focusingTab = tabs.tabs[tabs.focus];
if (
_focusingTab.currentIndex >= 0 &&
_focusingTab.history?.[_focusingTab.currentIndex + 1]
) {
openDir(_focusingTab.history[_focusingTab.currentIndex + 1]);
open(_focusingTab.history[_focusingTab.currentIndex + 1]);
}
};
/**
Expand Down Expand Up @@ -168,8 +168,8 @@ const Tab = (): void => {
delete tabs.tabs[index + 1];
storage.set(`tabs-${windowGUID}`, tabs);

const { openDir } = require('../Files/File Operation/open'); //eslint-disable-line
openDir(tabs.tabs[tabs.focus].position);
const { open } = require('../Files/File Operation/open'); //eslint-disable-line
open(tabs.tabs[tabs.focus].position);
}
});
tab.appendChild(closeTab);
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Layout/windowManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openDir } from '../Files/File Operation/open';
import { open } from '../Files/File Operation/open';
import storage from 'electron-json-storage-sync';
import { BrowserWindow } from '@electron/remote';
import createSidebar from './sidebar';
Expand All @@ -12,7 +12,7 @@ import windowGUID from '../Constants/windowGUID';
const reload = async (): Promise<void> => {
const tabs = storage.get(`tabs-${windowGUID}`)?.data;
await createSidebar();
openDir(tabs.tabs[tabs.focus].position);
open(tabs.tabs[tabs.focus].position);
closePreviewFile();
document.querySelector<HTMLElement>('.properties').style.animation =
'close-properties 1s forwards';
Expand Down Expand Up @@ -61,7 +61,7 @@ const windowManager = (): void => {
.addEventListener(
'change',
(event: Event & { target: HTMLInputElement }) => {
openDir(event.target.value);
open(event.target.value);
}
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Shortcut/shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ const Shortcut = (): void => {
exec(`code "${targetPath.replaceAll('"', '\\"')}"`);
} else {
const {
openDir,
open,
openFileWithDefaultApp,
} = require('../Files/File Operation/open'); //eslint-disable-line
if (isDir) {
openDir(selectedFilePath);
open(selectedFilePath);
} else {
openFileWithDefaultApp(selectedFilePath);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import os from 'os';
import windowStateKeeper from 'electron-window-state';
import log from 'electron-log';
import { autoUpdater } from 'electron-updater';
import yargs from 'yargs/yargs';

const args = yargs(process.argv.slice(isDev ? 2 : 1))
.usage('Usage: $0 <options> <dir1> <dir2> <dir3>')
.alias('h', 'help')
.command('reveal', 'Open the containing folder and select the file')
.alias('r', 'reveal')
.alias('v', 'version').argv;
console.log(args);
autoUpdater.logger = log;

/**
Expand Down Expand Up @@ -55,7 +63,7 @@ const FILES_ON_OPERATION: string[] = [];
let id: string;

ipcMain.on('args', (e) => {
e.returnValue = isDev ? process.argv.slice(2) : process.argv.slice(1);
e.returnValue = args;
});
ipcMain.on('GUID', (_, arg: string) => {
id = arg;
Expand Down
Loading