-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkthroughStorage.js
More file actions
136 lines (113 loc) · 3.88 KB
/
Copy pathwalkthroughStorage.js
File metadata and controls
136 lines (113 loc) · 3.88 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// simple storage for walkthrough data, everything is stored in a JSON
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const FILE_NAME = '.codetime_walkthroughs.json';
// storage file location
function getStoragePath() {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders || workspaceFolders.length === 0) return null;
const rootFolder = workspaceFolders[0].uri.fsPath;
return path.join(rootFolder, FILE_NAME);
}
// load entire walkthrough list
function loadAllWalkthroughs() {
const storagePath = getStoragePath();
if (!storagePath || !fs.existsSync(storagePath)) return [];
try {
const rawJson = fs.readFileSync(storagePath, 'utf8').trim();
if (!rawJson) return [];
const parsed = JSON.parse(rawJson);
return Array.isArray(parsed) ? parsed : [];
} catch (err) {
console.error('Failed to load walkthroughs:', err);
return [];
}
}
// save full walkthrough list back to the file
function saveAllWalkthroughs(allWalkthroughs) {
const storagePath = getStoragePath();
if (!storagePath) return;
const jsonText = JSON.stringify(allWalkthroughs, null, 2);
try {
fs.writeFileSync(storagePath, jsonText, 'utf8');
} catch (err) {
console.error('Failed to save walkthroughs:', err);
}
}
// create a new walkthrough entry
function createWalkthrough(name, description) {
const all = loadAllWalkthroughs();
const walkthrough = {
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
name,
description: description || '',
createdAt: new Date().toISOString(),
steps: [],
media: []
};
all.push(walkthrough);
saveAllWalkthroughs(all);
return walkthrough;
}
// get a single walkthrough by its id
function getWalkthroughById(walkthroughId) {
const all = loadAllWalkthroughs();
return all.find(w => w.id === walkthroughId) || null;
}
// add a step to a walkthrough
function addStepToWalkthrough(walkthroughId, stepInfo) {
const all = loadAllWalkthroughs();
const walkthrough = all.find(w => w.id === walkthroughId);
if (!walkthrough) return null;
walkthrough.steps.push({
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
...stepInfo
});
saveAllWalkthroughs(all);
return walkthrough;
}
// delete a single step from a walkthrough
function deleteStepFromWalkthrough(walkthroughId, stepId) {
const all = loadAllWalkthroughs();
const walkthrough = all.find(w => w.id === walkthroughId);
if (!walkthrough) return null;
const before = Array.isArray(walkthrough.steps) ? walkthrough.steps.length : 0;
walkthrough.steps = (Array.isArray(walkthrough.steps) ? walkthrough.steps : []).filter(s => s.id !== stepId);
const after = walkthrough.steps.length;
if (before === after) return walkthrough; // nothing removed
saveAllWalkthroughs(all);
return walkthrough;
}
// update fields inside a walkthrough (rename, description, steps, etc)
function updateWalkthrough(walkthroughId, updatedFields) {
const all = loadAllWalkthroughs();
const index = all.findIndex(w => w.id === walkthroughId);
if (index === -1) return null;
all[index] = {
...all[index],
...updatedFields
};
saveAllWalkthroughs(all);
return all[index];
}
// delete a walkthrough
function deleteWalkthrough(walkthroughId) {
const all = loadAllWalkthroughs();
const filtered = all.filter(w => w.id !== walkthroughId);
saveAllWalkthroughs(filtered);
return filtered.length !== all.length;
}
// return all walkthroughs
function getAllWalkthroughs() {
return loadAllWalkthroughs();
}
module.exports = {
createWalkthrough,
getWalkthroughById,
addStepToWalkthrough,
deleteStepFromWalkthrough,
updateWalkthrough,
deleteWalkthrough,
getAllWalkthroughs,
};