feat: add dash build (#91)

This commit is contained in:
SHANG殇 2019-05-05 19:58:36 +08:00 committed by 小弟调调™
parent 57c2c31098
commit b9aa227d8c
4 changed files with 920 additions and 2 deletions

BIN
assets/dash-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

118
build/dash.js Normal file
View File

@ -0,0 +1,118 @@
const fs = require('fs-extra');
const { resolve: pathResolve } = require('path');
const pkg = require('../package.json');
const sqlite3 = require('sqlite3');
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);
}
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);
}
build()
.then(() => {
console.info(`file at ${DOCSET_DIR}`);
})
.catch((e) => {
console.warn(e);
});

800
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,8 @@
"scripts": {
"start": "npm run build && npm run deploy",
"build": "node build/build.js",
"deploy": "node build/deploy.js"
"deploy": "node build/deploy.js",
"dash": "npm run build && node build/dash.js"
},
"files": [
"command",
@ -32,6 +33,7 @@
"marked": "^0.6.2",
"path": "^0.12.7",
"prismjs": "^1.16.0",
"sqlite3": "^4.0.6",
"stylus": "^0.54.5",
"uglify-js": "^3.4.9"
},