Skip to content

Commit b9aa227

Browse files
xinshangshangxinjaywcjlove
authored andcommitted
feat: add dash build (#91)
1 parent 57c2c31 commit b9aa227

File tree

4 files changed

+920
-2
lines changed

4 files changed

+920
-2
lines changed

assets/dash-icon.png

2.14 KB
Loading

build/dash.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const fs = require('fs-extra');
2+
const { resolve: pathResolve } = require('path');
3+
const pkg = require('../package.json');
4+
const sqlite3 = require('sqlite3');
5+
6+
const DATA_DIR = pathResolve(__dirname, '../assets/');
7+
const INDEX_JSON_PATH = pathResolve(__dirname, '../dist/data.json');
8+
const DETAIL_DIR = pathResolve(__dirname, '../.deploy/');
9+
const CP_DIRS = [
10+
pathResolve(DETAIL_DIR, 'c'),
11+
pathResolve(DETAIL_DIR, 'css'),
12+
pathResolve(DETAIL_DIR, 'img'),
13+
pathResolve(DETAIL_DIR, 'js'),
14+
];
15+
16+
const DOC_NAME = pkg.name;
17+
const DOC_ROOT_DIR = pathResolve(__dirname, `../.deploy/${DOC_NAME}`);
18+
const DOCSET_DIR = `${DOC_ROOT_DIR}.docset`;
19+
const RESOURCES_DIR = `${DOCSET_DIR}/Contents/Resources/`;
20+
21+
const DB_PATH = `${DOCSET_DIR}/Contents/Resources/docSet.dsidx`;
22+
const DIR_STRUCT = `${DOCSET_DIR}/Contents/Resources/Documents/`;
23+
24+
const PLIST = {
25+
dist: `${DOCSET_DIR}/Contents/Info.plist`,
26+
content: `
27+
<?xml version="1.0" encoding="UTF-8"?>
28+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
29+
<plist version="1.0">
30+
<dict>
31+
<key>CFBundleIdentifier</key>
32+
<string>DOC_NAME</string>
33+
<key>CFBundleName</key>
34+
<string>DOC_NAME</string>
35+
<key>DocSetPlatformFamily</key>
36+
<string>DOC_NAME</string>
37+
<key>isDashDocset</key>
38+
<true/>
39+
</dict>
40+
</plist>
41+
`,
42+
};
43+
const ICON = {
44+
dist: `${DOCSET_DIR}/icon.png`,
45+
src: `${DATA_DIR}/dash-icon.png`,
46+
};
47+
48+
function createDatabase(apiList, dbPath) {
49+
const db = new sqlite3.Database(dbPath);
50+
51+
db.serialize(() => {
52+
db.run('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);');
53+
db.run('CREATE UNIQUE INDEX anchor ON searchIndex (name,type,path);');
54+
55+
let stmt = db.prepare('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?, ?, ?)');
56+
57+
apiList.forEach(({ name, type, path }) => {
58+
stmt.run(name, type, path);
59+
});
60+
61+
stmt.finalize();
62+
});
63+
64+
db.close();
65+
}
66+
67+
async function clean() {
68+
console.info('========= do clean =========');
69+
try {
70+
await fs.remove(DOCSET_DIR);
71+
} catch (e) {}
72+
}
73+
74+
async function copyResource() {
75+
await fs.copy(ICON.src, ICON.dist);
76+
await fs.writeFile(PLIST.dist, PLIST.content.replace(/DOC_NAME/gi, DOC_NAME));
77+
78+
for await (const dir of CP_DIRS) {
79+
await fs.copy(dir, pathResolve(DIR_STRUCT, dir.substr(dir.lastIndexOf('/') + 1)));
80+
}
81+
}
82+
83+
async function getIndex() {
84+
let obj = await fs.readJSON(INDEX_JSON_PATH, { encoding: 'utf8' });
85+
86+
return Object.keys(obj).map((key) => {
87+
return {
88+
name: obj[key].n,
89+
type: 'Guide',
90+
path: `./c${obj[key].p}.html`,
91+
};
92+
});
93+
}
94+
95+
async function buildApi(dbPath) {
96+
let arr = await getIndex();
97+
await createDatabase(arr, dbPath);
98+
}
99+
100+
async function build() {
101+
console.log(`mkdir -p ${RESOURCES_DIR}`);
102+
await clean();
103+
await fs.ensureDir(RESOURCES_DIR);
104+
105+
console.log('build resources...');
106+
await copyResource();
107+
108+
console.info('build documents');
109+
await buildApi(DB_PATH);
110+
}
111+
112+
build()
113+
.then(() => {
114+
console.info(`file at ${DOCSET_DIR}`);
115+
})
116+
.catch((e) => {
117+
console.warn(e);
118+
});

0 commit comments

Comments
 (0)