Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 lint 配置以及相应模板 #80

Merged
merged 11 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ packages/*/lib
packages/*/dist
packages/*/test/fixtures
packages/*/coverage
packages/wepy-cli/templates
gulpfile.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ demo/
coverage
lerna-debug.log
/packages/*/lib
.DS_Store
27 changes: 0 additions & 27 deletions packages/wepy-cli/empty/src/app.wpy

This file was deleted.

21 changes: 0 additions & 21 deletions packages/wepy-cli/empty/src/pages/index.wpy

This file was deleted.

55 changes: 0 additions & 55 deletions packages/wepy-cli/empty/wepy.config.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/wepy-cli/src/compile-wpy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import {DOMParser} from 'xmldom';
import eslint from './eslint';
import cache from './cache';
import util from './util';

Expand Down Expand Up @@ -288,6 +289,10 @@ export default {
}
},

lint (filepath) {
eslint(filepath);
},

compile (opath) {
let filepath = path.join(opath.dir, opath.base);
let src = cache.getSrc();
Expand All @@ -314,6 +319,8 @@ export default {
util.log('Other: ' + relative, '编译');
}

this.lint(filepath);

let wpy = this.resolveWpy(opath);

if (type === 'app') { // 第一个编译
Expand Down
16 changes: 16 additions & 0 deletions packages/wepy-cli/src/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import loader from './loader';
import util from './util';

export default function (filepath) {
let config = util.getConfig();

if (config.eslint) {
const compiler = loader.load('wepy-eslint');
// 使用 eslint
const esConfig = Object.assign({
useEslintrc: true,
extensions: ['.js', config.wpyExt || '.wpy']
}, config.eslint === true ? {} : config.eslint);
compiler(esConfig, filepath);
}
};
69 changes: 48 additions & 21 deletions packages/wepy-cli/src/wepy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import {exec} from 'child_process';
import util from './util';
import compile from './compile';

const templateDir = path.join(util.cliDir, '../template', path.sep);
const emptyDir = path.join(util.cliDir, '../empty', path.sep);

let displayVersion = () => {
let version = util.getVersion();
let chars = [
Expand Down Expand Up @@ -40,6 +37,11 @@ let generateProject = (name, config) => {
return;
}

const tplDir = '../templates/';
const templateDir = path.join(util.cliDir, tplDir + 'template', path.sep);
const emptyDir = path.join(util.cliDir, tplDir + 'empty', path.sep);
const confDir = path.join(util.cliDir, tplDir + 'conf', path.sep);

let template = config.empty ? emptyDir : templateDir;

let pkg = path.join(template, 'package.json');
Expand All @@ -48,7 +50,9 @@ let generateProject = (name, config) => {
pkg.name = name;

let dependencies = [
'wepy',
'wepy'
];
const devDependencies = [
'wepy-compiler-babel',
'babel-plugin-syntax-export-extensions',
'babel-plugin-transform-export-extensions',
Expand All @@ -57,31 +61,60 @@ let generateProject = (name, config) => {
'babel-preset-stage-1',
'cross-env'
];
const eslintDeps = [
'eslint@3.18.0',
'babel-eslint@7.2.1',
'eslint-config-standard@7.1.0',
'eslint-friendly-formatter@2.0.7',
'eslint-plugin-html@2.0.1',
'eslint-plugin-promise@2.0.1',
'eslint-plugin-standard@2.0.1',
'wepy-eslint'
];

if (!config.empty) {
dependencies.push('wepy-com-toast');
dependencies.push('wepy-async-function');
}

if (config.lint) {
devDependencies.push.apply(devDependencies, eslintDeps);
}

util.writeFile(packagePath, JSON.stringify(pkg));
util.log('配置: ' + 'package.json', '写入');

let files = util.getFiles(template);

files.forEach((file) => {
let target = path.join(util.currentDir, file);
let opath = path.parse(target);

util.writeFile(target, util.readFile(path.join(template, file)));
util.log('文件: ' + file, '拷贝');
});
const copyFn = function (sourcePath) {
return function (file) {
let target = path.join(util.currentDir, file);
let fileContent = util.readFile(path.join(sourcePath, file));

// --on-lint will not copy eslint config
if (['.editorconfig', '.eslintignore', '.eslintrc'].indexOf(file) !== -1 && !config.lint)
return;
if (file === 'wepy.config.js') {
if (!config.lint) {
// 去掉 eslint: true,
fileContent = fileContent.replace(/\s*eslint\: true,/ig, '')
}
}
util.writeFile(target, fileContent);
util.log('文件: ' + file, '拷贝');
}
}
files.forEach(copyFn(template));

let cmd = 'npm install --save ' + dependencies.join(' ');
let cmdDev = 'npm install --save-dev ' + devDependencies.join(' ');
util.log('执行命令: ' + cmd, '执行');
util.log('执行命令: ' + cmdDev, '执行');
util.log('可能需要几分钟, 请耐心等待...', '信息');


util.exec(cmd).then((d) => {
// 不能并行执行安装依赖
util.exec(cmd).then(d => {
return util.exec(cmdDev)
}).then(d => {
util.log('安装依赖完成', '完成');

let cmd = 'wepy build';
Expand Down Expand Up @@ -187,14 +220,8 @@ commander.option('-t, --target <target>', '生成代码目录');
commander.option('-f, --file <file>', '待编译wpy文件');
commander.option('--no-cache', '对于引用到的文件,即使无改动也会再次编译');
commander.option('--empty', '使用new生成项目时,生成空项目内容');
commander.option('--no-lint', '使用new生成项目时,禁用eslint');
commander.option('-w, --watch', '监听文件改动');
/*
commander.option('-m, --mode <mode>', 'project mode type(normal, module), default is module, used in `new` command', mode => {
if(modeList.indexOf(mode) === -1){
console.log('mode value must one of ' + modeList.join(', '));
process.exit();
}
});*/

commander.command('build').description('编译项目').action(projectPath => {
if (!util.isDir(path.join(util.currentDir, 'node_modules'))) {
Expand Down
72 changes: 0 additions & 72 deletions packages/wepy-cli/template/src/app.wpy

This file was deleted.

Loading