在页面上挂载 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 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 root = mngFolder.isAmWiki(process.cwd());
const wikis = {}; const wikis = {};
@ -60,7 +63,7 @@ co(function*() {
//项目 files 文件夹路径 //项目 files 文件夹路径
const filesPath = mainPath.replace(/\\/g, '/').split('bin')[0] + '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) { if (!root2) {
break; break;
} }
@ -160,7 +163,7 @@ co(function*() {
//显示版本号 //显示版本号
case 'version': case 'version':
case '-v': case '-v':
printFn.ver(mainPath); printFn.ver(packageConf);
break; break;
//显示帮助 //显示帮助
case 'help': case 'help':

View File

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

View File

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

View File

@ -174,10 +174,10 @@ const makeMounts = (function () {
linksHtml += '<script src="amWiki/mounts/' + fileName + '"></script>'; linksHtml += '<script src="amWiki/mounts/' + fileName + '"></script>';
} }
let indexSrc = fs.readFileSync(rootPath + 'index.html', 'utf-8'); let indexSrc = fs.readFileSync(rootPath + 'index.html', 'utf-8');
const mountReg = /<div(.*?)aw-include="mountLinks"(.*?)>(.*?)<\/div>/; const mountReg = /<div(.*?)aw-include="mountLinks"(.*?)>([\s\S]*?)<\/div>/;
indexSrc = indexSrc.replace(mountReg, function (m, s1, s2) { indexSrc = indexSrc.replace(mountReg, function (m, s1, s2, s3) {
return '<div' + s1 + 'aw-include="mountLinks"' + s2 + '>' + return '<div' + s1 + 'aw-include="mountLinks"' + s2 + '>' +
linksHtml + '</div>'; linksHtml + s3 + '</div>';
}); });
fs.writeFileSync(rootPath + 'index.html', indexSrc, 'utf-8'); 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 = config['github-url'] || config.githubUrl;
config.githubUrl = typeof config.githubUrl === 'undefined' ? '' : config.githubUrl + ''; config.githubUrl = typeof config.githubUrl === 'undefined' ? '' : config.githubUrl + '';
delete config['github-url']; 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 文件 //自定义 css、js 文件
if (tools.isArray(config.imports) && config.imports.length > 0) { if (tools.isArray(config.imports) && config.imports.length > 0) {
const imports2 = { const imports2 = {

View File

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

View File

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