forked from yesmeck/antd-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (122 loc) · 4.53 KB
/
index.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
138
const path = require('path');
const fs = require('fs');
const cheerio = require('cheerio');
const { execSync } = require('child_process');
const glob = require('glob');
const debug = require('debug')('antd-dash');
const sitePath = path.join(process.cwd(), 'ant-design', '_site');
const docsetPath = path.join(process.cwd(), 'Ant_Design.docset');
const contentsPath = path.join(docsetPath, 'Contents');
const resoucesPath = path.join(contentsPath, 'Resources');
const documentsPath = path.join(resoucesPath, 'Documents');
const dbPath = path.join(resoucesPath, 'docSet.dsidx');
const plistPath = path.join(contentsPath, 'Info.plist');
/****************************** help methods **********************************/
function query(sql) {
execSync(`echo "${sql}" | sqlite3 ${dbPath}`);
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function classify(string) {
return string.split('-').map(capitalize).join('');
}
/******************************************************************************/
function clean() {
debug(`Clean ${docsetPath}.`);
execSync(`rm -rf ${docsetPath}`);
}
function createFolder() {
debug(`Create ${docsetPath}.`);
execSync(`mkdir -p ${resoucesPath}`);
execSync(`cp icon.png ${docsetPath}`);
execSync(`cp [email protected] ${docsetPath}`);
}
function buildSite() {
debug('Build site.');
execSync('rm -rf ant-design');
execSync('git clone [email protected]:ant-design/ant-design.git --depth=1');
execSync('cd ant-design && npm install --no-package-lock ');
execSync('cd ant-design && patch -p1 < ../ant-design.patch');
execSync('cd ant-design/node_modules/bisheng && patch -p1 < ../../../bisheng.patch');
execSync('cd ant-design && npm run site');
}
function copyHTML() {
debug('Copy html files.');
execSync(`cp -r ${sitePath} ${documentsPath}`);
}
function fixStaticPath() {
debug('Fix static path.');
execSync(`perl -pi -e 's#components/:children/#components/:children/index.html#' ${documentsPath}/index.js`);
execSync(`perl -pi -e 's#"path":"changelog"#"path":"changelog.html"#' ${documentsPath}/index.js`);
glob.sync(`${documentsPath}/**/*.html`).forEach(file => {
const relativePath = file.replace(documentsPath, '');
const deep = (relativePath.match(/\//g) || []).length;
const path = deep === 1 ? './' : `./${Array(deep - 1).fill('..').join('/')}`;
const content = fs.readFileSync(file)
.toString()
.replace(/href="\/(.+).css"/g, `href="${path}/$1.css"`)
.replace(/src="\/(.+).js"/g, `src="${path}/$1.js"`)
.replace(/href="\//g, 'href="https://ant.design/')
.replace(/<html>/, `<html><!-- Online page at https://ant.design${relativePath.replace('index.html', '').replace('.html', '')} -->`);
fs.writeFileSync(file, content);
});;
}
function createDb() {
debug('Create db.');
query('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);');
query('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);')
}
function createPlist() {
debug('Create plist.');
execSync(`
cat << EOF > ${plistPath}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>antd</string>
<key>CFBundleName</key>
<string>Ant Design</string>
<key>DocSetPlatformFamily</key>
<string>antd</string>
<key>DashDocSetFamily</key>
<string>dashtoc</string>
<key>isDashDocset</key>
<true/>
<key>isJavaScriptEnabled</key>
<true/>
</dict>
</plist>
EOF
`);
}
function generateRecords() {
debug(`Generate records.`);
let $query;
// Components
const component =fs.readFileSync(path.join(documentsPath, 'docs', 'react', 'introduce.html')).toString();
$query = cheerio.load(component);
$query('.aside-container .ant-menu-submenu .ant-menu-item').each((i, item) => {
const name = $query(item).text();
const path = $query(item).find('a').attr('href').replace(/^\//, '') + 'index.html';
query(`INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('${name}', 'Component', '${path}');`)
});
$query('.aside-container > .ant-menu-item').each((i, item) => {
const name = $query(item).text();
const path = $query(item).find('a').attr('href').replace(/^\//, '') + '.html';
query(`INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('${name}', 'Guide', '${path}');`)
});
}
function main() {
buildSite();
clean();
createFolder();
copyHTML();
createPlist();
createDb();
generateRecords();
fixStaticPath();
}
main();