-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from jannis-baum/issue/103-api-enhancements
- Loading branch information
Showing
26 changed files
with
399 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import axios from 'axios'; | ||
import { existsSync } from 'fs'; | ||
import open from 'open'; | ||
import { resolve as presolve } from 'path'; | ||
import { address } from './config.js'; | ||
import { pathToURL, preferredPath } from './utils/path.js'; | ||
|
||
// exported for unit test | ||
export const getPathAndLine = ( | ||
target: string, | ||
): { path: string | undefined; line: number | undefined } => { | ||
const exp = /^(?<path>(?:.*?)(?<!\\)(?:\\\\)*)(?::(?<line>\d+))?$/; | ||
const groups = target.match(exp)?.groups; | ||
if (groups === undefined || !groups['path']) { | ||
return { path: undefined, line: undefined }; | ||
} | ||
const path = groups['path'].replace('\\:', ':').replace('\\\\', '\\'); | ||
const line = groups['line'] ? parseInt(groups['line']) : undefined; | ||
return { path, line }; | ||
}; | ||
|
||
export const openFileAt = async (path: string) => | ||
open(`${address}${pathToURL(preferredPath(path))}`); | ||
|
||
const openTarget = async (target: string) => { | ||
const { path, line } = getPathAndLine(target); | ||
if (!path) { | ||
console.log(`Invalid target: ${target}`); | ||
return; | ||
} | ||
if (!existsSync(path)) { | ||
console.log(`File not found: ${path}`); | ||
return; | ||
} | ||
|
||
const resolvedPath = presolve(path); | ||
try { | ||
if (line !== undefined) { | ||
await axios.post(`${address}/_open`, { | ||
path: resolvedPath, | ||
command: 'SCROLL', | ||
value: line, | ||
}); | ||
} else { | ||
await openFileAt(resolvedPath); | ||
} | ||
} catch { | ||
console.log(`Failed to open ${target}`); | ||
} | ||
}; | ||
|
||
export const handleArgs = async () => { | ||
try { | ||
const args = process.argv.slice(2); | ||
const positionals: string[] = []; | ||
let parseOptions = true; | ||
|
||
for (let i = 0; i < args.length; i++) { | ||
const arg = args[i]; | ||
if (!(arg.startsWith('-') && parseOptions)) { | ||
positionals.push(arg); | ||
continue; | ||
} | ||
switch (arg) { | ||
case '-v': | ||
case '--version': | ||
console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`); | ||
break; | ||
case '--': | ||
parseOptions = false; | ||
break; | ||
default: | ||
console.log(`Unknown option "${arg}"`); | ||
} | ||
} | ||
await Promise.all(positionals.map((target) => openTarget(target))); | ||
} finally { | ||
if (process.env['NODE_ENV'] !== 'development') { | ||
// - viv executable waits for this string and then stops printing | ||
// vivify-server's output and terminates | ||
// - the string itself is not shown to the user | ||
console.log('STARTUP COMPLETE'); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Request, Response, Router } from 'express'; | ||
import { openAndMessage } from '../app.js'; | ||
import { openFileAt } from '../cli.js'; | ||
|
||
// this route should only be used internally between vivify processes | ||
export const router = Router(); | ||
|
||
router.post('/', async (req: Request, res: Response) => { | ||
const { path, command, value } = req.body; | ||
|
||
if (!path) { | ||
res.status(400).send('Bad request.'); | ||
return; | ||
} | ||
|
||
try { | ||
if (command) { | ||
await openAndMessage(path, `${command}: ${value}`); | ||
} else { | ||
await openFileAt(path); | ||
} | ||
} catch { | ||
res.status(500).end(); | ||
return; | ||
} | ||
|
||
res.end(); | ||
}); |
Oops, something went wrong.