-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathutils.ts
More file actions
143 lines (115 loc) · 3.96 KB
/
utils.ts
File metadata and controls
143 lines (115 loc) · 3.96 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
import chalk from 'chalk';
import { existsSync, mkdirSync, readFileSync, readdir, writeFileSync } from 'fs';
import { dirname, join, parse } from 'path';
import { convertHeadingsPlugin, markdown } from './markdown';
import { FrontMatter } from './models';
import matter = require('gray-matter');
import hash = require('shorthash');
markdown.use(convertHeadingsPlugin);
export const localeEnUS = 'en-US';
export const langages = [localeEnUS];
export const buildChecklist = async contentFolder => {
const checklist = {
categories: {},
items: {}
};
try {
const categories = await readdirAsync(contentFolder);
for (const category of categories) {
const categoryPath = join(contentFolder, category);
const files = await readdirAsync(categoryPath);
const META_DATA_FILE = '.category';
const categoryInfo = files.find(file => file === META_DATA_FILE);
const items = files.filter(file => file !== META_DATA_FILE);
if (!categoryInfo) {
throwError(`No metadata found for category ${category}. Please create a .category file.`);
}
const { data: frontMatter } = extractFrontMatter(join(categoryPath, categoryInfo));
if (!frontMatter.title || !frontMatter.summary) {
throwError(`No title or summary defined for category ${category}.`);
}
const compiledItems = compileFilesForCategory(items, category, categoryPath);
checklist.categories[category] = {
...frontMatter,
slug: category,
items: compiledItems.map(item => item.id)
};
checklist.items = {
...checklist.items,
...compiledItems.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {})
};
}
} catch (error) {
console.log(error);
return null;
}
return checklist;
};
export const extractFrontMatter = (filePath: string): FrontMatter => {
return matter(readFileSync(filePath)) as FrontMatter;
};
export const compileFilesForCategory = (files: Array<string>, category: string, categoryPath: string) => {
return files.map(file => {
const { data: frontMatter, content } = extractFrontMatter(join(categoryPath, file));
if (!Object.keys(frontMatter).length || !frontMatter.title) {
throwError(`No metadata defined for ${category}/${file}. You must define at least a title.`);
}
const id = hash.unique(file);
const slug = cleanFileName(file);
return {
id,
slug,
category,
...frontMatter,
content: markdown.render(content)
};
});
};
export const dumpDataToDisk = (filename: string, data: any, dest: string) => {
const dir = dirname(dest);
if (!existsSync(dir)) {
mkdirSync(dir);
}
try {
writeFileSync(join(dest, `${filename}.json`), generateJSON(data));
printSuccess(`${filename}.json created`);
} catch (error) {
console.log(error);
}
};
export const cleanFileName = (fileName: string) => {
return parse(fileName).name;
};
export const generateJSON = (data: any) => {
return JSON.stringify(data, null, 2);
};
export const readdirAsync = (path: string): Promise<Array<string>> => {
return new Promise((resolve, reject) => {
readdir(path, (error, files) => {
error ? reject(error) : resolve(files);
});
});
};
export const printSuccess = (message: string, type = 'Sucess', addNewLine = false) => {
console.log(`${addNewLine ? '\n' : ''}${chalk.green(`[${type}]`)} ${message}`);
};
export const throwError = (message: string) => {
throw chalk.red(`[Error] ${message}`);
};
export const logWarning = (message: string) => {
console.log(`${chalk.yellow(message)}`);
};
export const transrateDeepMerge = (target, source) => {
if (typeof source === 'undefined') {
return target;
} else if (target && typeof target === 'object' && !Array.isArray(target)) {
return Object.keys(target)
.map(key => ({ [key]: transrateDeepMerge(target[key], source[key]) }))
.reduce((p, c) => ({ ...p, ...c }), {});
} else {
return source;
}
};