-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
249 lines (233 loc) · 7.98 KB
/
index.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env node
import { DeepClient, parseJwt } from "@deep-foundation/deeplinks/imports/client.js";
import { generateApolloClient } from "@deep-foundation/hasura/client.js";
import { createRequire } from "module";
import { generateMutation, generateSerial, deleteMutation } from '@deep-foundation/deeplinks/imports/gql/index.js';
import _ from 'lodash';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
let deepClient = {};
const require = createRequire(import.meta.url);
const { program } = require('commander')
const watch = require('watch');
const fs = require('fs');
const path = require('path');
let files = {};
let containTypeId;
let syncTextFile;
let pendingRenames = {};
const argv = yargs(hideBin(process.argv))
.option('url', {
alias: 'u',
type: 'string',
description: 'URL deeplinks'
})
.option('space-id', {
alias: 's',
type: 'string',
description: 'Space ID'
})
.option('folder-path', {
alias: 'f',
type: 'string',
description: 'Path to folder'
})
.option('token', {
alias: 't',
type: 'string',
description: 'Token',
default: ''
})
.argv;
const token = argv.token;
let dirPath = argv['folder-path'];
const spaceIdArgument = argv['space-id'];
let urn = argv.url;
let ssl;
const url_protocol = urn.match(/^(http:|https:)/)[0];
if (url_protocol === "https:") {
ssl = true;
} else if (url_protocol === "http:") {
ssl = false;
} else {
throw new Error(`Unsupported protocol: ${url.protocol}`);
}
if (!urn.endsWith("/gql")) {
urn += "/gql";
}
urn = urn.replace(/^https:\/\//, "");
urn.replace(/^http:\/\//, "");
const GQL_URN = process.env.GQL_URN || urn
const GQL_SSL = process.env.GQL_SSL || ssl;
const makeDeepClient = token => {
if (!token) throw new Error("No token provided")
const decoded = parseJwt(token)
const linkId = decoded.userId
const apolloClient = generateApolloClient({
path: GQL_URN,
ssl: !!+GQL_SSL,
token
})
const deepClient = new DeepClient({ apolloClient, linkId, token })
//console.log(deepClient);
return deepClient
}
async function addedTextLinks(fileData, deep){
const syncTextFileTypeId = await deep.id('@deep-foundation/core', 'SyncTextFile');
const syncTextFile = (await deep.insert({
type_id: syncTextFileTypeId,
}, { name: 'INSERT_HANDLER_SYNC_TEXT_FILE' })).data[0];
const syncTextFileValue = (await deep.insert({ link_id: syncTextFile.id, value: fileData }, { table: 'strings' })).data[0];
//console.log(fileData);
return syncTextFile;
}
async function addedContainLinks(spaceIdArgument, syncTextFile, deep, fileName){
const spaceId = spaceIdArgument || (await deep.id('deep', 'admin'));
const containTypeId = await deep.id('@deep-foundation/core', 'Contain');
const spaceContainSyncTextFile = (await deep.insert({
from_id: spaceId,
type_id: containTypeId,
string: { data: { value: fileName } },
to_id: syncTextFile.id,
}, { name: 'INSERT_SYNC_TEXT_FILE_CONTAIN' })).data[0];
//console.log(spaceIdArgument, spaceContainSyncTextFile);
return containTypeId;
}
async function deleteLinks(containTypeId, fileName, deep){
//const syncTextFileId = syncTextFile.id;
//console.log(containTypeId, fileName);
const { data } = await deep.select({
in: {
type_id: containTypeId,
string: { value: { _eq: fileName } }
}
});
const syncTextFile = data[0];
await deep.delete({
_or: [
{
id: syncTextFile.id,
},
{
type_id: containTypeId,
to_id: syncTextFile.id
}
]
});
}
async function updateLinkValue(valueText, deep, containTypeId, fileName){
fileName = fileName.replace(/\.txt/g, "");
console.log(valueText);
const { data } = await deep.select({
in: {
type_id: containTypeId,
string: { value: { _eq: fileName } }
}
});
const syncTextFile = data[0];
console.log(syncTextFile, valueText);
await deep.update(
{
link_id: syncTextFile.id
},
{
value: valueText
},
{
table: (typeof valueText) + 's'
}
)
}
async function updateLinkName(deep, containTypeId, fileName, newName){
fileName = fileName.replace(/\.txt/g, "");
const { data } = await deep.select({
type_id: containTypeId,
string: { value: { _eq: fileName } }
});
const syncTextFile = data[0];
console.log(containTypeId, fileName);
await deep.update(
{
link_id: syncTextFile.id
},
{
value: newName
},
{
table: (typeof newName) + 's'
}
)
}
async function handleFileChange(absoluteFilePath, current, previous) {
let currentFileName = path.basename(absoluteFilePath);
currentFileName = currentFileName.replace(/\.txt/g, "");
// if file added
if (previous === null) {
//проверка наличия файла по его абсолютному пути
if (files[absoluteFilePath] === undefined) {
// проверка идентификатора --> rename или add
if (pendingRenames[current.ino]) {
const previousAbsoluteFilePath = pendingRenames[current.ino];
const previousFileName = path.basename(previousAbsoluteFilePath);
const fileData = files[previousAbsoluteFilePath];
delete files[previousAbsoluteFilePath];
files[absoluteFilePath] = fileData;
delete pendingRenames[current.ino];
updateLinkName(deepClient, 3, previousFileName, currentFileName);
console.log(`File ${previousFileName} renamed to ${currentFileName}`);
console.log(JSON.stringify(files, null, 2));
}
else {
const fileData = fs.readFileSync(absoluteFilePath, { encoding: 'utf8' });
files[absoluteFilePath] = fileData;
syncTextFile = await addedTextLinks(fileData, deepClient);
containTypeId = await addedContainLinks(spaceIdArgument, syncTextFile, deepClient, currentFileName);
//console.log(`File ${currentFileName} added`);
//console.log(JSON.stringify(files, null, 2));
}
}
} else if (current.nlink === 0) {
// file removed
//if (inodeMap[prev.ino]) {
// wait a moment to see if this inode comes back (rename)
pendingRenames[previous.ino] = absoluteFilePath;
//delete inodeMap[prev.ino];
setTimeout(() => {
if (pendingRenames[previous.ino]) {
delete pendingRenames[previous.ino];
delete files[absoluteFilePath];
deleteLinks(3, currentFileName, deepClient);
//console.log(`File ${currentFileName} removed`);
//console.log(JSON.stringify(files, null, 2));
}
}, 100);
// }
} else {
// file changed
const fileData = fs.readFileSync(absoluteFilePath, { encoding: 'utf8' });
files[absoluteFilePath] = fileData;
updateLinkValue(fileData, deepClient, 3, currentFileName);
console.log(`File ${currentFileName} changed`);
console.log(JSON.stringify(files, null, 2));
}
}
// Monitor the directory
watch.watchTree(dirPath, { interval: 1 }, async (f, curr, prev) => {
//console.log(f, curr, prev);
if (typeof f === "object") {
// Initial scanning complete
deepClient = makeDeepClient(token);
const filesStats = f;
Object.keys(filesStats).forEach(fileName => {
if (fileName == dirPath) {
return;
}
const fileData = fs.readFileSync(fileName, { encoding: 'utf8' });
files[fileName] = fileData;
})
console.log(`Files initialized `);
console.log(JSON.stringify(files, null, 2));
} else {
await handleFileChange(f, curr, prev);
}
});