Skip to content

Commit

Permalink
1、针对迁移脚本添加文件编码转化功能
Browse files Browse the repository at this point in the history
2、添加.npmignore,避免test被打包
  • Loading branch information
neekey committed Aug 2, 2013
1 parent 7ec8d7a commit 2cfb66a
Show file tree
Hide file tree
Showing 25 changed files with 649 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
test
.idea
45 changes: 43 additions & 2 deletions migrate/index.js
Expand Up @@ -2,6 +2,7 @@ var generator = require('abc-generator');
var util = require('util');
var Path = require('path');
var FS = require( 'fs-extra' );
var IConv = require( 'iconv-lite' );

var Generator = module.exports = function Generator() {
generator.UIBase.apply(this, arguments);
Expand All @@ -23,7 +24,8 @@ util.inherits(Generator, generator.UIBase);

Generator.prototype.welcome = function () {
this.log(this.abcLogo);
this.log( '\033[1;33m!注意\033[0m:由于KISSY-Cake中去掉了page的\033[0;36m原码版本\033[0m,因此会自动抓取\033[0;36m最新\033[0m的版本进行迁移!\n' )
this.log( '\033[1;33m!注意\033[0m:由于KISSY-Cake中去掉了page的\033[0;36m原码版本\033[0m,因此会自动抓取\033[0;36m最新\033[0m的版本进行迁移!' )
this.log( '\033[1;33m!注意\033[0m:由于KISSY-Cake中统一使用\033[0;36mUTF8\033[0m,请设置\033[0;36m原项目文件编码\033[0m,脚本将统一进行文件转化。\033[1;31m(若原项目编码较混乱,建议手动更改)\033[0m\n' )
};

Generator.prototype.askfor = function () {
Expand All @@ -41,13 +43,20 @@ Generator.prototype.askfor = function () {
message: '请输入需要迁入的目录地址(绝对地址)',
default: process.cwd(),
warning: ''
},
{
name: 'oldCharset',
message: '旧项目编码( 统一转码为UTF8,若要手动转码请留空回车)',
default: '',
warning: ''
}
];

this.prompt(prompts, function (props) {

this.newDir = props.newDir;
this.srcDir = props.srcDir;
this.oldCharset = props.oldCharset;

if( FS.existsSync( this.newDir ) ){

Expand All @@ -70,7 +79,7 @@ Generator.prototype.askfor = function () {
/**
* 创建用户文件
*/
Generator.prototype.copyFiles = function app() {
Generator.prototype.copyFiles = function() {

var self = this;
var done = this.async();
Expand Down Expand Up @@ -176,3 +185,35 @@ Generator.prototype.copyFiles = function app() {
});
};

Generator.prototype.covertCharset = function() {

var self = this;

if( this.oldCharset ){

console.log( '\n开始对项目进行文件编码转化为UTF8:\n' );

var targets = this.expandFiles( 'src/**', { cwd: this.newDir } );

targets.forEach(function( file ){
var absPath = Path.resolve( self.newDir, file )
var extname = Path.extname( file );
var filename = Path.basename( file, extname );
var extFilter = /js|css|less|scss|sass|html/;
// 对文件进行过滤
if( extFilter.test( extname ) && filename !== 'Gruntfile' ){

try {
var buf = FS.readFileSync( absPath );
var str = IConv.decode( buf, self.oldCharset );
FS.writeFileSync( absPath, IConv.encode( str, 'utf8' ) );
console.log( ' \033[1;32m转化\033[0m ' + absPath );
}
catch( e ){
console.log( ' \033[1;31m转化失败,请手动更改\033[0m ' + absPath );
}
}
});
}
};

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,8 @@
"license": "MIT",
"dependencies": {
"abc-generator": "~0.1.4",
"fs-extra": "~0.6.3"
"fs-extra": "~0.6.3",
"iconv-lite": "~0.2.11"
},
"devDependencies": {
"mocha": "~1.12.0"
Expand Down
36 changes: 36 additions & 0 deletions test/migrate.js
Expand Up @@ -44,4 +44,40 @@ describe('ABC - KISSY-PIE generator', function () {
});
});
});

it('编码转化测试', function (done) {

helpers.testDirectory(Path.join(__dirname, 'temp'), function (err) {
if (err) {
done(err);
}
var KISSYPieMigrate = helpers.createGenerator( 'kissy-cake:migrate', [
'../../migrate'
]);

helpers.mockPrompt( KISSYPieMigrate, {
newDir: Path.join(__dirname, 'temp'),
srcDir: Path.resolve( __dirname, 'migrate_test_gbk' ),
oldCharset: 'gbk'
});

KISSYPieMigrate.run({}, function(){
helpers.assertFiles([
[ 'src/pages/home/page/init2.js', /下面的方式将/ ],
'src/pages/home/page/index2.css',
[ 'src/pages/index/page/init5.js', /下面的方式将/ ],
'src/pages/index/page/index5.css',
'src/common/package-config.js',
'src/utils/index.js',
'src/.editorconfig',
'src/.gitattributes',
'src/.gitignore',
'src/.jshintrc',
'build'
]);

done();
});
});
});
});
21 changes: 21 additions & 0 deletions test/migrate_test_gbk/.editorconfig
@@ -0,0 +1,21 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions test/migrate_test_gbk/.gitattributes
@@ -0,0 +1 @@
* text=auto
5 changes: 5 additions & 0 deletions test/migrate_test_gbk/.gitignore
@@ -0,0 +1,5 @@
node_modules
dist
.tmp
.sass-cache
app/components
22 changes: 22 additions & 0 deletions test/migrate_test_gbk/.jshintrc
@@ -0,0 +1,22 @@
{
"node": true,
"browser": true,
"es5": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
}

0 comments on commit 2cfb66a

Please sign in to comment.