This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve string parsing, now requires id for project, labels and sections(BREAKING) Searching of tasks by section and setting of sections to tasks now possible Fix update error #128 (bug) Set time to next update check (settings) Optionally receive pre-releases of this workflow Show task title in notification when created #126 Set cache timeout for tasks Allow filtering tasks by project, label, priority and section #20 Improved error handling and feedback, easily fill out a bug report when an unexpected error is encountered Show more helpful errors when a call to the Todoist API fails #124 Improved typing and code documentation for contributors BREAKING CHANGE: Improve string parsing, now requires id for project, labels and sections ISSUES CLOSED: #128 #126 #124 #20
- Loading branch information
Showing
70 changed files
with
24,842 additions
and
699 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,89 +1,37 @@ | ||
import '@babel/polyfill'; | ||
import 'loud-rejection/register'; | ||
import { | ||
AlfredError, | ||
cache, | ||
Command, | ||
getSetting, | ||
handleError, | ||
serialize, | ||
} from '@/project'; | ||
/* eslint simple-import-sort/sort: 0 */ | ||
import compareVersions from 'compare-versions'; | ||
|
||
/** | ||
* CLI argument parsing | ||
*/ | ||
|
||
/** @hidden */ | ||
const argv = Object.assign([], process.argv); | ||
argv.splice(0, 2); | ||
/** @hidden */ | ||
const type = argv.shift(); | ||
/** @hidden */ | ||
const query = argv.join(' '); | ||
/** @hidden */ | ||
const command = Command(); | ||
import { getCurrentCall } from '@/lib/cli-args'; | ||
import command from '@/lib/command'; | ||
import { AlfredError, Errors, funnelError } from '@/lib/error'; | ||
import { checkForWorkflowUpdate } from '@/lib/updater'; | ||
import { ENV } from '@/lib/utils'; | ||
|
||
/** | ||
* Serialize cache back to JSON | ||
* | ||
* @hidden | ||
* Make sure minimum node version is satisfied. | ||
*/ | ||
function handleSerialization(): Promise<void> { | ||
return serialize(cache.dump()).catch(handleError); | ||
} | ||
if (compareVersions.compare(process.version, ENV.requirements.nodejs, '>=')) { | ||
(async (): Promise<void | null> => { | ||
const call = getCurrentCall(); | ||
|
||
/** | ||
* Updater | ||
*/ | ||
try { | ||
command.updateWorkflowVersion(); | ||
} catch (error) { | ||
handleError(error); | ||
} | ||
/** | ||
* Updater. | ||
*/ | ||
if (call.name === 'parse' || call.name === 'read') { | ||
await checkForWorkflowUpdate(); | ||
} | ||
|
||
/** | ||
* CLI option logic | ||
*/ | ||
if (type === 'read') { | ||
command | ||
.read(query) | ||
.catch(handleError) | ||
.finally(handleSerialization); | ||
} else if (type === 'create') { | ||
try { | ||
command.create(query); | ||
} catch (error) { | ||
handleError(error); | ||
} finally { | ||
handleSerialization(); | ||
} | ||
} else if (type === 'submit') { | ||
command | ||
.submit( | ||
Object.assign(JSON.parse(query), { due_lang: getSetting('language') }) | ||
) | ||
.catch(handleError) | ||
.finally(handleSerialization); | ||
} else if (type === 'remove') { | ||
command | ||
.remove(JSON.parse(query)) | ||
.catch(handleError) | ||
.finally(handleSerialization); | ||
} else if (type === 'settings' && query.trim() !== '') { | ||
const [key, value] = query.trim().split(' '); | ||
command.verifySetting(key, value); | ||
} else if (type === 'settings') { | ||
command.listSettings(); | ||
} else if (type === 'settings:store') { | ||
command.saveSetting(JSON.parse(query)).catch(handleError); | ||
/** | ||
* Command distribution. | ||
*/ | ||
return command(call); | ||
})().catch(funnelError); | ||
} else { | ||
handleError(new AlfredError(`Invalid command ${type} (${query})`)); | ||
funnelError( | ||
new AlfredError( | ||
Errors.InvalidNodeJS, | ||
'Please upgrade your Node.js version to 10.x or higher for this workflow to work', | ||
{ isSafe: true } | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Catch any unhandled exception or rejected promise and handle here | ||
*/ | ||
|
||
process.on('uncaughtException', handleError); | ||
// Process.on('unhandledRejection', handleError) | ||
(process as NodeJS.EventEmitter).on('unhandledRejection', handleError); |
This file was deleted.
Oops, something went wrong.
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,109 @@ | ||
import { TodoistTask, TodoistTaskOptions } from 'todoist-rest-api'; | ||
import { isPrimitive } from 'util'; | ||
|
||
import { Settings } from '@/lib/stores/settings-store'; | ||
|
||
import { ResourceName } from './todoist/local-rest-adapter'; | ||
|
||
type Arg = | ||
| string | ||
| TodoistTaskOptions | ||
| TodoistTask | ||
| { | ||
key: string; | ||
value: string | number | boolean; | ||
}; | ||
|
||
export type Call = | ||
| { | ||
name: 'parse' | 'read' | 'readSettings' | 'openUrl'; | ||
args: string; | ||
} | ||
| { | ||
name: 'create'; | ||
args: TodoistTaskOptions; | ||
} | ||
| { | ||
name: 'remove'; | ||
args: TodoistTask; | ||
} | ||
| { | ||
name: 'writeSetting'; | ||
args: { | ||
key: keyof Settings; | ||
value: string | number | boolean; | ||
}; | ||
} | ||
| { | ||
name: 'refreshCache'; | ||
args: ResourceName; | ||
}; | ||
|
||
const argv: string[] = Object.assign([], process.argv); | ||
argv.splice(0, 2); | ||
|
||
function assertValidArgs(args: Arg): asserts args is Arg { | ||
if (args == null) { | ||
throw new TypeError( | ||
`Property args should not be null or undefined, was ${args}` | ||
); | ||
} | ||
} | ||
|
||
function assertValidCall(call: Call): asserts call is Call { | ||
if (!call || isPrimitive(call)) { | ||
throw new TypeError(`The call should be a an object, was ${call}`); | ||
} | ||
|
||
if (!call.name || typeof call.name !== 'string') { | ||
throw new TypeError( | ||
`Expected call.name to be a string was ${call.name} (${typeof call.name})` | ||
); | ||
} | ||
|
||
assertValidArgs(call.args); | ||
} | ||
|
||
function serialize(call: Call): string | never { | ||
assertValidCall(call); | ||
|
||
return JSON.stringify(call); | ||
} | ||
|
||
function deserialize(serialized: string): Call | never { | ||
if (typeof serialized !== 'string') { | ||
throw new TypeError( | ||
`Expected a string in deserialize, got ${serialized} (${typeof serialized})` | ||
); | ||
} | ||
|
||
try { | ||
const call = JSON.parse(serialized) as Call; | ||
assertValidCall(call); | ||
|
||
return call; | ||
} catch (error) { | ||
throw new TypeError( | ||
`Expected a JSON string, got '${serialized}' (${typeof serialized})` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the call passed to current alfred input or action. | ||
* | ||
* @returns A `Call` object. | ||
*/ | ||
export function getCurrentCall(): Call { | ||
return deserialize(argv.join(' ')); | ||
} | ||
|
||
/** | ||
* Validates and creates a serialized call. | ||
* | ||
* @param call A `Call` object. | ||
* @returns A serialized call. | ||
*/ | ||
export function createCall(call: Call): string { | ||
return serialize(call); | ||
} |
Oops, something went wrong.