forked from jaywcjlove/linux-command
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash.js
162 lines (136 loc) · 4.36 KB
/
dash.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs-extra');
const { resolve: pathResolve, join: pathJoin } = require('path');
const sqlite3 = require('sqlite3');
const archiver = require('archiver');
const pkg = require('../package.json');
const DATA_DIR = pathResolve(__dirname, '../assets/');
const INDEX_JSON_PATH = pathResolve(__dirname, '../dist/data.json');
const DETAIL_DIR = pathResolve(__dirname, '../.deploy/');
const CP_DIRS = [
pathResolve(DETAIL_DIR, 'c'),
pathResolve(DETAIL_DIR, 'css'),
pathResolve(DETAIL_DIR, 'img'),
pathResolve(DETAIL_DIR, 'js'),
];
const DOC_NAME = pkg.name;
const DOC_ROOT_DIR = pathResolve(__dirname, `../.deploy/${DOC_NAME}`);
const DOCSET_DIR = `${DOC_ROOT_DIR}.docset`;
const RESOURCES_DIR = `${DOCSET_DIR}/Contents/Resources/`;
const DB_PATH = `${DOCSET_DIR}/Contents/Resources/docSet.dsidx`;
const DIR_STRUCT = `${DOCSET_DIR}/Contents/Resources/Documents/`;
const PLIST = {
dist: `${DOCSET_DIR}/Contents/Info.plist`,
content: `
<?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>DOC_NAME</string>
<key>CFBundleName</key>
<string>DOC_NAME</string>
<key>DocSetPlatformFamily</key>
<string>DOC_NAME</string>
<key>isDashDocset</key>
<true/>
</dict>
</plist>
`,
};
const ICON = {
dist: `${DOCSET_DIR}/icon.png`,
src: `${DATA_DIR}/dash-icon.png`,
};
function createDatabase(apiList, dbPath) {
const db = new sqlite3.Database(dbPath);
db.serialize(() => {
db.run('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);');
db.run('CREATE UNIQUE INDEX anchor ON searchIndex (name,type,path);');
let stmt = db.prepare('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?, ?, ?)');
apiList.forEach(({ name, type, path }) => {
stmt.run(name, type, path);
});
stmt.finalize();
});
db.close();
}
async function clean() {
console.info('========= do clean =========');
try {
await fs.remove(DOCSET_DIR);
} catch (e) {}
}
async function copyResource() {
await fs.copy(ICON.src, ICON.dist);
await fs.writeFile(PLIST.dist, PLIST.content.replace(/DOC_NAME/gi, DOC_NAME));
for await (const dir of CP_DIRS) {
await fs.copy(dir, pathResolve(DIR_STRUCT, dir.substr(dir.lastIndexOf('/') + 1)));
}
}
async function getIndex() {
let obj = await fs.readJSON(INDEX_JSON_PATH, { encoding: 'utf8' });
return Object.keys(obj).map((key) => {
return {
name: obj[key].n,
type: 'Guide',
path: `./c${obj[key].p}.html`,
};
});
}
async function buildApi(dbPath) {
let arr = await getIndex();
await createDatabase(arr, dbPath);
}
function compressing() {
new Promise((resolve, reject) => {
const outputPaht = pathJoin(process.cwd(), '.deploy', 'linux-command.docset.zip');
// create a file to stream archive data to.
const output = fs.createWriteStream(outputPaht);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', () => {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
resolve();
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.log('warning:::', err)
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
reject(err);
});
// pipe archive data to the file
archive.pipe(output);
archive.directory(pathJoin(process.cwd(), '.deploy', 'linux-command.docset'), false);
archive.finalize();
})
}
async function build() {
console.log(`mkdir -p ${RESOURCES_DIR}`);
await clean();
await fs.ensureDir(RESOURCES_DIR);
console.log('build resources...');
await copyResource();
console.info('build documents');
await buildApi(DB_PATH);
console.info('compressing zip');
await compressing();
}
build()
.then(() => {
console.info(`file at ${DOCSET_DIR}`);
})
.catch((e) => {
console.warn(e);
});