在页面上挂载 AWConfig 对象,config 配置可以被 web 端 js 访问

This commit is contained in:
Tevin 2017-07-29 11:04:46 +08:00
parent 7123238774
commit 6dd1652d3e
7 changed files with 32 additions and 19 deletions

View File

@ -35,6 +35,9 @@ const exportGithub = require('../build/exportGithub');
//格式化显示
const printFn = require('./printf');
//读取项目包配置
const packageConf = JSON.parse(fs.readFileSync(mainPath.split(/bin[\\\/]main/)[0] + 'package.json', 'utf-8'));
//项目根目录
const root = mngFolder.isAmWiki(process.cwd());
const wikis = {};
@ -60,7 +63,7 @@ co(function*() {
//项目 files 文件夹路径
const filesPath = mainPath.replace(/\\/g, '/').split('bin')[0] + 'files/';
//开始创建
const root2 = yield creator.create(configPath, filesPath);
const root2 = yield creator.create(configPath, filesPath, packageConf);
if (!root2) {
break;
}
@ -160,7 +163,7 @@ co(function*() {
//显示版本号
case 'version':
case '-v':
printFn.ver(mainPath);
printFn.ver(packageConf);
break;
//显示帮助
case 'help':

View File

@ -120,12 +120,10 @@ const printf = (function () {
},
/**
* 显示版本信息
* @param {String} mainPath
* @param {Object} packageConf
* @public
*/
ver: function (mainPath) {
const packageStr = fs.readFileSync(mainPath.split(/bin[\\\/]main/)[0] + 'package.json', 'utf-8');
const packageConf = JSON.parse(packageStr);
ver: function (packageConf) {
const text = [
'Package: amWiki',
`Version: ${clc(0, packageConf.version)}`,

View File

@ -17,10 +17,8 @@ const creator = (function () {
* @param {String} to
* @private
*/
_copyWikiFile: function (from, to) {
const encoding = from.indexOf('png') >= 0 ? 'binary' : 'utf-8';
const file = fs.readFileSync(from, encoding);
fs.writeFileSync(to, file, encoding);
_copyFile: function (from, to) {
fs.createReadStream(from).pipe(fs.createWriteStream(to));
},
/**
* 创建 amWiki 必要的文件夹
@ -205,10 +203,11 @@ const creator = (function () {
* 创建amWiki本地文件
* @param {String} configPath - config.json文件的路径
* @param {String} filesPath - 项目包files文件夹路径
* @param {Object} packageConf - 项目包配置
* @returns {Promise} 项目根目录
* @public
*/
create: function (configPath, filesPath) {
create: function (configPath, filesPath, packageConf) {
const that = this;
return co(function*() {
const {options, config} = yield that._checkConfig(configPath, filesPath);
@ -224,6 +223,12 @@ const creator = (function () {
indexPage = indexPage.replace(/\{\{name\}\}/g, config.name)
.replace('{{version}}', config.version)
.replace('{{logo}}', config.logo);
//嵌入配置
config.AWPackage = {
version: packageConf.version,
homepage: packageConf.homepage
};
indexPage = indexPage.replace('{{config}}', 'AWConfig=' + JSON.stringify(config));
//测试模块
if (config.testing) {
const testingTpl = fs.readFileSync(options.filesPath + 'amWiki.testing.tpl', 'utf-8');
@ -296,7 +301,7 @@ const creator = (function () {
['menubar_bg.png', 'amWiki/images/menubar_bg.png']
];
for (let file of fileList) {
that._copyWikiFile(options.filesPath + file[0], options.outputPath + file[1]);
that._copyFile(options.filesPath + file[0], options.outputPath + file[1]);
}
//如果没有library则复制一套默认文档
if (!hasLibrary) {
@ -319,7 +324,7 @@ const creator = (function () {
['doc.demo-long-article.md', 'library/002-文档示范/002-超长文档页内目录示例.md']
];
for (let file of fileList2) {
that._copyWikiFile(options.filesPath + file[0], options.outputPath + file[1]);
that._copyFile(options.filesPath + file[0], options.outputPath + file[1]);
}
}
return options.outputPath;

View File

@ -174,10 +174,10 @@ const makeMounts = (function () {
linksHtml += '<script src="amWiki/mounts/' + fileName + '"></script>';
}
let indexSrc = fs.readFileSync(rootPath + 'index.html', 'utf-8');
const mountReg = /<div(.*?)aw-include="mountLinks"(.*?)>(.*?)<\/div>/;
indexSrc = indexSrc.replace(mountReg, function (m, s1, s2) {
const mountReg = /<div(.*?)aw-include="mountLinks"(.*?)>([\s\S]*?)<\/div>/;
indexSrc = indexSrc.replace(mountReg, function (m, s1, s2, s3) {
return '<div' + s1 + 'aw-include="mountLinks"' + s2 + '>' +
linksHtml + '</div>';
linksHtml + s3 + '</div>';
});
fs.writeFileSync(rootPath + 'index.html', indexSrc, 'utf-8');
}

View File

@ -169,6 +169,10 @@ const manageWiki = (function () {
config.githubUrl = config['github-url'] || config.githubUrl;
config.githubUrl = typeof config.githubUrl === 'undefined' ? '' : config.githubUrl + '';
delete config['github-url'];
//转接 library 地址
config.transferLibPath = config['transfer-lib-path'] || config.transferLibPath;
config.transferLibPath = typeof config.transferLibPath === 'undefined' ? '' : config.transferLibPath + '';
delete config['transfer-lib-path'];
//自定义 css、js 文件
if (tools.isArray(config.imports) && config.imports.length > 0) {
const imports2 = {

View File

@ -158,7 +158,9 @@
</div>
</footer>
<!-- mounts -->
<div class="hidden" aw-include="mountLinks"></div>
<div class="hidden" aw-include="mountLinks">
<script>{{config}}</script>
</div>
<!-- js -->
<div class="hidden">
<script type="text/javascript" src="amWiki/js/gbk.js"></script>

View File

@ -99,8 +99,9 @@ const main = (function () {
alert('创建失败,当前不是"config.json"文件!');
return;
}
const filesPath = atom.configDirPath.replace(/\\/g, '/') + '/packages/amWiki/files/';
creator.create(editPath, filesPath).then((root) => {
const packagePath = atom.configDirPath.replace(/\\/g, '/') + '/packages/amWiki/';
const packageConf = JSON.parse(fs.readFileSync(packagePath + 'package.json', 'utf-8'));
creator.create(editPath, packagePath + 'files/', packageConf).then((root) => {
if (!root) {
return;
}