Skip to content

Commit

Permalink
add -o option for kissy-xtemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Jul 4, 2014
1 parent e80fe24 commit dbb0c6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
37 changes: 26 additions & 11 deletions bin/xtemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
var program = require('commander');
var compileModuleCode = require('../lib/xtemplate/compile-module');
var encoding = 'utf-8';
var util = require('../lib/util'),
chokidar = require('chokidar'),
fs = require('fs'),
path = require('path');
var util = require('../lib/util');
var chokidar = require('chokidar');
var fs = require('fs');
var path = require('path');
var fsExtra = require('fs-extra');
var cwd = process.cwd();

program
.option('-p, --folderPath <folderPath>', 'Set template folder path')
.option('-o, --outPath [outPath]', 'Set template js path, default to folderPath')
.option('-s, --suffix [suffix]', 'Set xtemplate file suffix', '')
.option('-w, --watch', 'Watch xtemplate file change')
.option('--no-kwrap', 'Wrap code by KISSY module')
Expand All @@ -28,33 +31,45 @@ options.forEach(function (o) {
}
});

var folderPath = program.folderPath,
suffix = program.suffix || 'xtpl',
kwrap = program.kwrap,
cwd = process.cwd();
var folderPath = path.resolve(cwd, program.folderPath);
var outPath = program.outPath;
if (outPath) {
outPath = path.resolve(cwd, program.outPath);
}
var suffix = program.suffix || 'xtpl';
var kwrap = program.kwrap;

var suffixReg = new RegExp('\\.' + util.escapeRegExp(suffix) + '$', 'g');

folderPath = path.resolve(cwd, folderPath);
var folderPathReg = new RegExp('^' + util.escapeRegExp(normalizeSlash(folderPath)), 'i');

function normalizeSlash(str) {
return str.replace(/\\/g, '/');
}

function getGenerateFilePath(srcPath) {
if (outPath) {
srcPath = normalizeSlash(srcPath);
return srcPath.replace(folderPathReg, outPath);
} else {
return srcPath;
}
}

function compile(tplFilePath, modulePath, kwrap) {
var moduleCode = compileModuleCode({
kwrap: kwrap,
encoding: encoding,
path: tplFilePath
});
fsExtra.mkdirsSync(path.dirname(modulePath));
fs.writeFileSync(modulePath, moduleCode, encoding);
console.info('generate xtpl module: ' + modulePath + ' at ' + (new Date().toLocaleString()));
}

function generate(filePath) {
var modulePath;
if (filePath.match(suffixReg)) {
modulePath = filePath.replace(suffixReg, '.js');
modulePath = getGenerateFilePath(filePath.replace(suffixReg, '.js'));
compile(filePath, modulePath, kwrap);
}
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"kissy-xtemplate": "./bin/xtemplate.js"
},
"scripts": {
"test":"sleep 2 && exit 0",
"test": "sleep 2 && exit 0",
"start": "node ./tools/server/start",
"stop": "node ./tools/server/stop"
},
Expand All @@ -44,10 +44,10 @@
"node-jscover": "*",
"mime": "~1.2.11",
"jshint": "~2.5.1",
"estraverse": "~1.5.0",
"fs-extra": "~0.8.1"
"estraverse": "~1.5.0"
},
"dependencies": {
"fs-extra": "~0.8.1",
"esprima": "~1.2.2",
"escodegen": "~1.3.3",
"commander": "~2.2.0",
Expand Down

0 comments on commit dbb0c6b

Please sign in to comment.