This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract.js
93 lines (88 loc) · 2.68 KB
/
extract.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
const { promisify } = require('util');
const { resolve, join, extname, dirname } = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const readFile = promisify(fs.readFile);
function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
const foldersToExtract = ['quickstarts', 'tutorials', 'samples'];
let fileToWrite = {};
async function getFilePaths(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(
subdirs.map(async subdir => {
const foldersToExclude = ['node_modules', 'public'];
if (foldersToExclude.includes(subdir)) return false;
const res = resolve(dir, subdir);
if ((await stat(res)).isDirectory()) {
return getFilePaths(res);
}
const exts = [
'.html',
'.css',
'.js',
'.jsx',
'.ts',
'.tsx',
'.go',
'.cpp',
'.py',
'.graphql'
];
if (exts.includes(extname(res))) {
return res;
}
return false;
})
);
return files.reduce((a, f) => (f ? a.concat(f) : a), []);
}
async function parseFiles(paths) {
await Promise.all(
paths.map(async p => {
let fileExt = extname(p);
let file = await readFile(p, 'utf8');
let jsonFilePath = p.replace(
/quickstarts.*|tutorials.*|samples.*/,
'data/examples.json'
);
let sectionNumber = 1;
while (true) {
let extractSectionPattern = new RegExp(
`.*CODE:BEGIN:(\\S+${sectionNumber}).*\\n([\\s\\S]*)\\n.*CODE:END:\\1`,
'g'
);
let excludeTagsPattern = new RegExp(
'^((?!CODE:BEGIN|CODE:END)[\\s\\S])+$',
'gm'
);
let matches = extractSectionPattern.exec(file);
if (!matches || matches.length <= 0) break;
let cleanResults = matches[0].match(excludeTagsPattern);
let sectionName = matches[1];
if (!cleanResults || cleanResults.length <= 0) break;
let section = {};
section[sectionName] = '';
cleanResults.forEach(
codeBlock =>
(section[sectionName] = section[sectionName].concat(codeBlock))
);
fileToWrite = { ...fileToWrite, ...section };
sectionNumber++;
}
if (isEmpty(fileToWrite)) return;
await mkdirp(dirname(jsonFilePath));
fs.writeFile(jsonFilePath, JSON.stringify(fileToWrite, '', 2), err => {
if (err) throw err;
});
})
);
}
foldersToExtract.forEach(folder => {
getFilePaths(join(__dirname, folder))
.then(filePaths => parseFiles(filePaths))
.catch(e => console.error(e));
});