mya-command-release
Version:
mya release command.
75 lines (65 loc) • 2.03 kB
JavaScript
const _ = fis.util;
const fs = require('fs');
const path = require('path');
const rVconsole = /"(?:[^\\"\r\n\f]|\\[\s\S])*"|'(?:[^\\'\n\r\f]|\\[\s\S])*'|(<\/head>|<!--vconsole-->)/ig;
const cwd = process.cwd();
function getServerInfo() {
var conf = path.join(fis.project.getTempPath('server'), 'conf.json');
if (fis.util.isFile(conf)) {
return fis.util.readJSON(conf);
}
return {};
}
const serverRoot = (function () {
var key = 'FIS_SERVER_DOCUMENT_ROOT';
var serverInfo = getServerInfo();
if (process.env && process.env[key]) {
var path = process.env[key];
if (fis.util.exists(path) && !fis.util.isDir(path)) {
fis.log.error('invalid environment variable [' + key + '] of document root [' + path + ']');
}
return path;
} else if (serverInfo['root'] && fis.util.is(serverInfo['root'], 'String')) {
return serverInfo['root'];
} else {
return fis.project.getTempPath('www');
}
})();
function normalizePath(dest) {
if (dest === 'preview') {
return serverRoot;
}
return path.resolve(cwd, dest);
}
function syncVconsoleScript(obj) {
_.copy(
path.resolve(__dirname, '..', 'vendor', 'vconsole.min.js'),
path.resolve(normalizePath(obj.options.dest), 'resource', 'vconsole.min.js')
)
}
function injectVconsoleScript(file) {
var content = file.getContent();
if (!file.isHtmlLike || typeof content !== 'string') {
return;
}
content = content.replace(rVconsole, function (all, token) {
if (token) {
all = `
<script charset="utf-8" src="/resource/vconsole.min.js"></script>
<script>
// init vConsole
var vConsole = new VConsole();
console.log('Hello world');
</script>${token}`;
}
return all
});
file.setContent(content);
}
exports.inject = function (obj, next) {
syncVconsoleScript(obj)
_.toArray(obj.modified).forEach(file => {
injectVconsoleScript(file);
});
next(null, obj);
};