-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
148 lines (130 loc) · 4 KB
/
Copy pathapp.ts
File metadata and controls
148 lines (130 loc) · 4 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
137
138
139
140
141
142
143
144
145
146
147
148
import * as fs from 'fs';
import inquirer from 'inquirer';
import * as Config from './config';
import {
aggregateDuplicateBookmarks,
checkAndGroupDuplicates,
copyAndDeduplicate,
groupFolders
} from './src/bookmark-utils';
import { parseBookmarkData, readBookmarkFile, writeResults } from './src/file-utils';
import { BookmarkBarChild, Bookmarks } from './src/types';
preparePrompt();
function showPrompt(paths: string[]): void {
inquirer
.createPromptModule()({
name: 'paths',
message: 'Select the files you want to process',
choices: paths,
type: 'checkbox',
default: paths.length === 1 ? paths : []
})
.then((result: { paths: string[] }) => {
processPaths(result.paths);
})
.catch(e => {
console.error(e);
});
}
function preparePrompt(): Promise<Error | void> {
return Promise.all([importPathsFromFile(), getLocalBookmarksFile()])
.then(([pathsFromFile, localBookmarksFilePath]) => {
// TODO: Consider using a data type for paths instead of bare strings
let pathsToProcess: string[] = [];
if (pathsInputIsValid(pathsFromFile.default)) {
pathsToProcess.push(...pathsFromFile.default);
} else {
console.info(
`No paths found. Falling back to ${Config.DEFAULT_INPUT_FILENAME} in the project root.`
);
}
if (pathsInputIsValid(localBookmarksFilePath)) {
// TODO: Fix this case so that `Bookmarks` is written to the project directory
pathsToProcess.push(fs.realpathSync(localBookmarksFilePath[0])); // convert to absolute path so that it can be processed like the rest
} else {
console.info(
`No ${Config.DEFAULT_INPUT_FILENAME} file found in root of project.`
);
}
if (pathsToProcess.length === 0) {
throw new Error(
`Neither paths in ${'./paths'} nor ${
Config.DEFAULT_INPUT_FILENAME
} found. Exiting...`
);
}
return showPrompt(pathsToProcess);
})
.catch(e => {
throw e;
});
}
function importPathsFromFile(
path: string = './paths'
): Promise<Object & { default: string[] }> {
return import(path as any);
}
function getLocalBookmarksFile(
path: string = Config.DEFAULT_INPUT_FILENAME
): Promise<string[]> {
return new Promise<string[]>(resolve => resolve(fs.existsSync(path) ? [path] : []));
}
function pathsInputIsValid(paths: string[]): boolean {
return (
paths instanceof Array &&
paths.length > 0 &&
paths.every(path => fs.existsSync(path))
);
}
function processPaths(paths: string[]): Promise<string | void | any[]> {
const promises = paths.map((path: string) => init(path));
return Promise.all(promises).catch(e => console.error(e));
}
function init(filePath: string): Promise<string | void> {
return readBookmarkFile(filePath)
.then(data => parseBookmarkData<Bookmarks>(data))
.then(parsedData => {
const allFolders: BookmarkBarChild[] = groupFolders(parsedData);
const groupedDuplicates: BookmarkBarChild[][] = [
...checkAndGroupDuplicates(allFolders)
];
return {
bookmarks: parsedData,
duplicateBookmarks: aggregateDuplicateBookmarks(groupedDuplicates)
};
})
.then(({ bookmarks, duplicateBookmarks }) => {
if (duplicateBookmarks.length === 0) {
throw new Error(
`No duplicates found. New output files won't be written.`
);
}
return { bookmarks, duplicateBookmarks };
})
.then(async ({ bookmarks, duplicateBookmarks }) => {
const bookmarksForPrompt = duplicateBookmarks.map(bookmark => ({
name: `${bookmark.name} ${bookmark.url}`,
value: bookmark.id
}));
console.clear();
const idsToRemove: string[] = await inquirer
.createPromptModule()({
name: 'bookmarksToRemove',
message: 'Select bookmarks to remove',
choices: bookmarksForPrompt,
pageSize: 30,
type: 'checkbox'
})
.then((result: { bookmarksToRemove: string[] }) => {
return result.bookmarksToRemove;
})
.catch(e => {
throw e;
});
return copyAndDeduplicate(bookmarks, idsToRemove);
})
.then(cleanedUpBookmarks => {
return writeResults(filePath, cleanedUpBookmarks);
})
.catch(e => console.error(e));
}