-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.ts
166 lines (152 loc) · 4.41 KB
/
init.ts
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
import * as fs from 'fs';
import * as path from 'path';
import globby from 'globby';
import slugify from '@sindresorhus/slugify';
import humanize from 'humanize-string';
import { parseMdx, getMetadata } from './utils/mdx';
import { template, pathClient } from './config/paths';
import { tplCompile } from './utils/fs';
import * as docgen from 'react-docgen';
import { loadFileConfig } from './utils/fs';
const createEntries = (entries: any) => {
const sentries = {};
for (let i = 0; i < Object.keys(entries).length; i++) {
const key = Object.keys(entries)[i];
const entry = entries[key];
const { menu } = entry;
if(menu && sentries[menu]) {
sentries[menu].children = [
...sentries[menu].children,
entries[key]
];
continue;
}
if(menu && !sentries[menu]) {
sentries[menu] = {
name: menu,
children: [entries[key]]
};
continue;
}
sentries[key] = {
...entries[key]
};
}
return sentries;
};
const orderChildrenEntries = (entries: any) => {
const newEntries = { ...entries };
const compare = (entry, nextEntry) => {
if(entry.name < nextEntry.name) {
return -1;
}
if(entry.name > nextEntry.name) {
return 1;
}
return 0;
};
Object.values(newEntries).forEach((entry: any) => {
if(entry.children && entry.children.length) {
let orderChildren = entry.children.sort(compare);
entry.children = orderChildren;
}
});
return newEntries;
};
export const init = (argv: any) => {
return new Promise(async resolve => {
try {
await fs.mkdirSync(pathClient);
} catch (e) { }
const pattern = argv.typescript ? '**/*.{ts,tsx}' : '**/*.{js,jsx}';
const paths = await globby([
'**/*.mdx',
'!node_modules',
'!dist'
]);
const ignore = argv.ignore.map(ignore => `!${ignore}`);
const pathsComponent = await globby([
pattern,
'!node_modules',
'!dist',
...ignore
]);
// create db.json for entries
let planEntries = {};
let props = {};
pathsComponent.forEach(pathcomponent => {
const filePath = path.resolve(process.cwd(), `./${pathcomponent}`);
try {
const propsComponent = docgen.parse(fs.readFileSync(filePath));
props[path.normalize(pathcomponent)] = {
...propsComponent
};
} catch (e) { }
});
await Promise.all(
paths.map(async item => {
const filePath = path.resolve(process.cwd(), `./${item}`);
const ast = await parseMdx(filePath);
const metasArray = getMetadata(ast);
const finalRoute = path.basename(item).replace(path.extname(item), '');
planEntries[item] = {
filepath: item
};
metasArray &&
metasArray.forEach(({ key, value }) => {
if (key && value) planEntries[item][key] = value;
});
planEntries[item] = {
...planEntries[item],
name: planEntries[item].name || humanize(slugify(finalRoute)),
route: planEntries[item].route || `/${slugify(finalRoute)}`,
nameChunk: `${slugify(finalRoute)}`,
path: item,
};
})
);
// loadfile papyrumrc
const config = loadFileConfig('papyrum');
// create imports
const appFile = fs.readFileSync(template('app.txt'), 'utf8');
const rootFile = fs.readFileSync(template('root.txt'), 'utf8');
var templateFn = await tplCompile(template('imports.tpl.js'), {
minimize: false
});
const compare = (entry, nextEntry) => {
if(entry.name < nextEntry.name) {
return -1;
}
if(entry.name > nextEntry.name) {
return 1;
}
return 0;
};
const imports = templateFn({
planEntries: Object.values(planEntries).sort(compare)
});
fs.writeFileSync(pathClient + '/imports.js', imports);
fs.writeFileSync(path.resolve(pathClient, './root.js'), rootFile);
fs.writeFileSync(path.resolve(pathClient, './App.js'), appFile);
// create db
let entries = createEntries(planEntries);
let entriesOrder = orderChildrenEntries(entries);
fs.writeFileSync(
pathClient + '/db.json',
JSON.stringify(
{
config: {
...argv,
...config
},
plain: Object.values(planEntries).sort(compare),
entries: entriesOrder,
props
},
null,
2
)
);
resolve('ok');
});
};