您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
106 行
2.7 KiB
106 行
2.7 KiB
var path = require('path');
|
|
var chalk = require('chalk');
|
|
var glob = require("glob");
|
|
var fs = require('fs');
|
|
var nunjucks = require('nunjucks');
|
|
var chokidar = require('chokidar');
|
|
|
|
exports.command = 'codegen [dir] [file]';
|
|
exports.desc = "generate mixin code";
|
|
exports.builder = function (yargs) {
|
|
return yargs.positional('dir', {
|
|
describe: 'the working directory',
|
|
type: 'string',
|
|
default: '.'
|
|
}).positional('file', {
|
|
describe: 'the target file if specified',
|
|
type: 'string',
|
|
default: ''
|
|
}).option('watch', {
|
|
alias: 'w',
|
|
describe: 'Watch for file changes',
|
|
boolean: true
|
|
});
|
|
};
|
|
|
|
exports.handler = function (argv) {
|
|
var cwd = path.resolve(__dirname, '../..', argv.dir);
|
|
var file = ''
|
|
if (argv.file != '') {
|
|
file = argv.file;
|
|
}
|
|
var data = {};
|
|
var env = nunjucks.configure(cwd, {
|
|
trimBlocks: true,
|
|
lstripBlocks: true,
|
|
noCache: true
|
|
});
|
|
|
|
glob('**/*.njk', {
|
|
strict: true,
|
|
cwd: cwd,
|
|
ignore: '**/_*.*'
|
|
}, function (err, files) {
|
|
if (err) {
|
|
return console.error(chalk.red(err));
|
|
}
|
|
if (files.indexOf(file) >=0) {
|
|
render(env, cwd, file, data);
|
|
}
|
|
else {
|
|
renderAll(env, cwd, files, data);
|
|
}
|
|
});
|
|
|
|
if (argv.watch) {
|
|
var watcher = chokidar.watch('**/*.njk', {
|
|
persistent: true,
|
|
cwd: cwd,
|
|
awaitWriteFinish: {
|
|
stabilityThreshold: 300,
|
|
pollInterval: 50
|
|
}
|
|
});
|
|
|
|
var layouts = [];
|
|
var templates = [];
|
|
|
|
watcher.on('ready', function () {
|
|
console.log(chalk.gray('Watching templates...'));
|
|
});
|
|
|
|
watcher.on('add', function (file) {
|
|
if (path.basename(file).indexOf('_') === 0) {
|
|
layouts.push(file);
|
|
} else {
|
|
templates.push(file);
|
|
}
|
|
});
|
|
|
|
watcher.on('change', function (file) {
|
|
if (layouts.indexOf(file) >= 0) {
|
|
renderAll(env, cwd, templates, data);
|
|
} else {
|
|
render(env, cwd, file, data);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
function render(env, cwd, file, data) {
|
|
env.render(file, data, function (err, res) {
|
|
if (err) {
|
|
return console.error(chalk.red(err));
|
|
}
|
|
|
|
var outputFile = file.replace(/\.\w+$/, '') + '.gen.cs';
|
|
console.log(chalk.blue('Rendering: ' + file));
|
|
fs.writeFileSync(path.resolve(cwd, outputFile), res);
|
|
});
|
|
}
|
|
|
|
function renderAll(env, cwd, files, data) {
|
|
for (var i = 0; i < files.length; i++) {
|
|
render(env, cwd, files[i], data);
|
|
}
|
|
}
|