Skip to content

Commit 68231eb

Browse files
uu-zegoist
authored andcommitted
CLI support
1 parent c8953c8 commit 68231eb

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed

app/eme.sh

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
if [ "$(uname)" == 'Darwin' ]; then
4+
OS='Mac'
5+
else
6+
echo "Your platform ($(uname -a)) is not supported."
7+
exit 1
8+
fi
9+
10+
while getopts ":wtfvh-:" opt; do
11+
case "$opt" in
12+
-)
13+
case "${OPTARG}" in
14+
wait)
15+
WAIT=1
16+
;;
17+
help|version)
18+
REDIRECT_STDERR=1
19+
EXPECT_OUTPUT=1
20+
;;
21+
foreground|test)
22+
EXPECT_OUTPUT=1
23+
;;
24+
esac
25+
;;
26+
w)
27+
WAIT=1
28+
;;
29+
h|v)
30+
REDIRECT_STDERR=1
31+
EXPECT_OUTPUT=1
32+
;;
33+
f|t)
34+
EXPECT_OUTPUT=1
35+
;;
36+
esac
37+
done
38+
39+
if [ $REDIRECT_STDERR ]; then
40+
exec 2> /dev/null
41+
fi
42+
43+
if [ $EXPECT_OUTPUT ]; then
44+
export ELECTRON_ENABLE_LOGGING=1
45+
fi
46+
47+
if [ $OS == 'Mac' ]; then
48+
EME_APP_NAME="EME.app"
49+
if [ -z "${EME_PATH}"]; then
50+
if [ -x "/Applications/$EME_APP_NAME" ]; then
51+
EME_PATH="/Applications"
52+
elif [ -x "$HOME/Applications/$EME_APP_NAME" ]; then
53+
EME_PATH="$HOME/Applications"
54+
else
55+
if [ ! -x "$EME_PATH/$EME_APP_NAME" ]; then
56+
echo "Cannot locate EME.app, it is usually located /Applications. Set the EME_PATH environment variable to the directory containing EME.app. "
57+
exit 1
58+
fi
59+
fi
60+
fi
61+
62+
if [ $EXPECT_OUTPUT ]; then
63+
"$EME_PATH/$EME_APP_NAME/Contents/MacOS/EME" --executed-from="$(pwd)" --pid=$$ "$@"
64+
exit $?
65+
else
66+
open -a "$EME_PATH/$EME_APP_NAME" -n --args --executed-from="$(pwd)" --pid=$$ --path-environment="$PATH" "$@"
67+
fi
68+
fi

app/eme/menu.js

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const tildify = require('tildify')
1010
const axios = require('axios')
1111
const compare = require('semver-compare')
1212
const config = require('./config')
13+
const {InstallShell} = require('./shell')
1314

1415
const version = app.getVersion()
1516
const checkForUpdates = {
@@ -300,6 +301,18 @@ module.exports = cb => {
300301
{
301302
type: 'separator'
302303
},
304+
{
305+
type: 'separator'
306+
},
307+
{
308+
label: 'Install Shell Command',
309+
click() {
310+
new InstallShell().installShell()
311+
}
312+
},
313+
{
314+
type: 'separator'
315+
},
303316
{
304317
role: 'hide'
305318
},

app/eme/shell.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const spawn = require('child_process').spawn
5+
const yargs = require('yargs')
6+
const {app} = require('electron')
7+
8+
class InstallShell {
9+
10+
installShell() {
11+
const commandName = 'eme'
12+
const commandPath = path.join(this.getResourcesDirectory(), 'app', 'eme.sh')
13+
const destinationPath = path.join(this.getInstallDirectory(), commandName)
14+
this.createSymLink(commandPath, destinationPath)
15+
}
16+
17+
getInstallDirectory() {
18+
return '/usr/local/bin'
19+
}
20+
21+
getResourcesDirectory() {
22+
return process.resourcesPath
23+
}
24+
25+
createSymLink(commandPath, destinationPath) {
26+
try {
27+
spawn('rm', ['-f', destinationPath])
28+
spawn('mkdir', ['-p', path.dirname(destinationPath)])
29+
spawn('ln', ['-s', commandPath, destinationPath])
30+
} catch (e) {
31+
console.log(e)
32+
}
33+
}
34+
}
35+
36+
function writeFullVersion() {
37+
return (
38+
process.stdout.write(
39+
`
40+
Atom : ${app.getVersion()}
41+
Electron: ${process.versions.electron}
42+
Chrome : ${process.versions.chrome}
43+
Node : ${process.versions.node}
44+
`
45+
)
46+
)
47+
}
48+
49+
function parseShellCommand() {
50+
const options = yargs(process.argv.slice(1))
51+
options.usage(
52+
`
53+
Eme Editor
54+
55+
Usage: eme [options] [path ...]
56+
`
57+
)
58+
options.alias('h', 'help').boolean('h').describe('h', 'Print this usage message.')
59+
options.alias('v', 'version').boolean('v').describe('v', 'Print the version information.')
60+
61+
const args = options.argv
62+
if (args.help) {
63+
process.stdout.write(options.help())
64+
process.exit(0)
65+
}
66+
67+
if (args.version) {
68+
writeFullVersion()
69+
process.exit(0)
70+
}
71+
72+
const pathsToOpen = args._
73+
const resourcePath = args.executedFrom
74+
return {
75+
resourcePath, pathsToOpen
76+
}
77+
}
78+
79+
module.exports = {
80+
InstallShell,
81+
parseShellCommand
82+
}

app/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const windowStateKeeper = require('electron-window-state')
1212
const buildMenu = require('./eme/menu')
1313
const emeWindow = require('./eme/window')
1414
const config = require('./eme/config')
15+
const {parseShellCommand} = require('./eme/shell')
1516

1617
const platform = os.platform()
1718

@@ -34,9 +35,26 @@ const createMainWindow = () => {
3435

3536
let mainWindow // eslint-disable-line
3637
app.on('ready', () => {
38+
const args = parseShellCommand()
3739
Menu.setApplicationMenu(appMenu)
3840
mainWindow = createMainWindow()
3941

42+
if (args) {
43+
const {pathsToOpen, resourcePath} = args
44+
console.log(args)
45+
if (pathsToOpen) {
46+
const pathToOpen = pathsToOpen[0]
47+
const locationToOpen = `${resourcePath}/${pathToOpen}`
48+
console.log(locationToOpen)
49+
mainWindow.webContents.on('did-finish-load', () => {
50+
mainWindow.webContents.send('open-file', locationToOpen)
51+
})
52+
} else {
53+
// open dirctory
54+
// mainWindow.send('open-dirctory', resourcePath)
55+
}
56+
}
57+
4058
if (platform === 'darwin') {
4159
mainWindow.setSheetOffset(36)
4260
}

app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"electron-window-state": "^3.0.3",
2020
"match-at": "^0.1.0",
2121
"semver-compare": "^1.0.0",
22-
"tildify": "^1.2.0"
22+
"tildify": "^1.2.0",
23+
"yargs": "^5.0.0"
2324
}
2425
}

0 commit comments

Comments
 (0)