-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiBuilder.js
137 lines (105 loc) · 4.34 KB
/
apiBuilder.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
var fs = require('fs');
var colors = require('colors');
var exec = require('child_process').exec;
var rimraf = require('rimraf');
var jsonfile = require('jsonfile');
var detect = require('language-detect');
module.exports = function () {
var apiDir = process.cwd() + '/_api';
// Api director
if (!fs.existsSync(apiDir)) {
fs.mkdirSync(apiDir);
fs.mkdirSync(apiDir + "/algoLang");
fs.mkdirSync(apiDir + "/algos");
}
var adsnRepoDir = process.cwd() + '/Algo_Ds_Notes';
const build = () => {
var algoNames = dirs(process.cwd() + '/Algo_Ds_Notes');
algoNames.shift();
// Api for algo list
var algoList = {
availabe_algo: algoNames,
size: algoNames.length
};
var mainAPI_Dir = process.cwd() + '/_api/algoList.json';
jsonfile.writeFile(mainAPI_Dir, algoList, { spaces: 2 }, function (err) {
// console.error(err)
})
var languages_list = [];
algoNames.forEach(function (algo) {
if (algo != ".git") {
var algoDir = adsnRepoDir + '/' + algo;
// Reades files dir
var algoData = {};
var algoList = [];
var langList = [];
fs.readdirSync(algoDir).forEach(file => {
if (fs.lstatSync(algoDir + '/' + file).isFile()) {
if (!file.includes(".md")) {
var lang = detect.sync(algoDir + '/' + file)
if (lang != undefined) {
var algoContent = {};
algoList.push(file);
if (lang === "Smalltalk") {
langList.push("C#");
var mainAPI_Dir = process.cwd() + '/_api/algoLang/' + algo + "-" + "C#" + '.json';
languages_list.push("C#")
}
else {
langList.push(lang);
var mainAPI_Dir = process.cwd() + '/_api/algoLang/' + algo + "-" + lang + '.json';
languages_list.push(lang)
}
algoContent["mainALGO"] = `${fs.readFileSync(algoDir + '/' + file,'utf-8').trim().toString()}`
jsonfile.writeFile(mainAPI_Dir, algoContent, { spaces: 2 }, function (err) {
// console.error(err)
})
}
}
}
});
algoData['filenames'] = algoList;
algoData['langauges'] = langList;
algoData['no_of_files'] = langList.length;
var mainAPI_Dir = process.cwd() + '/_api/algos/' + algo + '.json';
jsonfile.writeFile(mainAPI_Dir, algoData, { spaces: 2 }, function (err) {
// console.error(err)
});
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var langData = {
languages: languages_list.filter(onlyUnique)
}
var lang_api = process.cwd() + '/_api/langs.json';
jsonfile.writeFile(lang_api, langData, { spaces: 2 }, function (err) {
// console.error(err)
});
console.log("[+] ".green + "Completed ".cyan + algo.yellow);
}
});
}
if (fs.existsSync(adsnRepoDir)) {
console.log("🍔 Thank you!".yellow)
build()
} else {
clone_repo();
}
function clone_repo() {
console.log("[+] ".green + "Cloning Algo_ds_Notes ".cyan);
exec('git clone https://github.com/algobook/Algo_Ds_Notes.git', (err, stdout, stderr) => {
if (err) {
console.error(err);
} else {
console.log("[+] Cloned 🎉".green);
build();
}
});
}
/**
* returns dirs in path
*/
function dirs(p) {
return fs.readdirSync(p).filter(f => fs.statSync(p + "/" + f).isDirectory());
}
}