-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeLog New.js
90 lines (73 loc) · 2.63 KB
/
LifeLog New.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: play-circle;
///<reference path="./types/lifeLog.d.ts" />
// @ts-ignore
// eslint-disable-next-line
try { require; } catch(e) { require = importModule; }
const { getInput, output, error, string } = require('./lib/lib.js');
const {
pathLog, pathActivities,
camelCaseToTitle,
makeActivityId,
readLog, readActivities, writeLifeLogData
} = require('./lib/lifelog.js');
const help = `Creates a new Activity in the LifeLog Activities JSON.
LifeLog JSON Path: ${pathLog}
LifeLog JSON Type: $/types/lifeLog.d.ts::LifeLog
LifeLog Activities JSON Path: ${pathActivities}
LifeLog Activities JSON Type: $/types/lifeLog.d.ts::LifeLogActivities`;
const main = async () => {
const log = await readLog();
const acts = await readActivities();
/** @type {LifeLogTypeList} */
const types = [ 'movie', 'tv', 'book', 'game' ];
const typeOptions = types.map(type => ({
title: camelCaseToTitle(type),
code: type
}));
const input = await getInput({
name: 'LifeLog New',
help,
inScriptable: false,
args: [{
name: 'name',
shortName: 'n',
type: 'string',
help: 'The name of the activity.'
}, {
name: 'type',
shortName: 't',
type: 'enum',
choices: typeOptions,
help: 'The type of activity.'
}, {
name: 'subType',
shortName: 's',
type: 'string',
help: 'The activity sub-type ' +
'(book author, movie year, game system).'
}]
});
if (!input) { return; }
const name = string(input.name);
const type = types.find(x => input && x === input.type);
const argSubType = string(input.subType);
if (!name || !type || !argSubType) {
const errorKind = !name ? 'Name' : (!type ? 'Type' : 'Sub-Type');
return error('LifeLog New', `Invalid ${errorKind}!`);
}
// Write new Activity to `acts`
const timeCreated = (new Date()).getTime();
const subType = argSubType.replaceAll('/', '-');
const key = `lifelog://${type}/${subType}/${name}`;
const isOld = key in acts;
acts[key] = acts[key] || { key, type, subType, name, timeCreated };
// Activate new Activity in `log`
makeActivityId(key, log);
log.active = Array.from((new Set(log.active)).add(key));
await writeLifeLogData(log, acts, true);
const outVerb = isOld ? 'Reactivated' : 'Added new';
output('LifeLog New', `${outVerb} activity: ${name}`);
};
main();