Skip to content

Commit

Permalink
update esprima, update cubeignore, update version to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
剪巽 committed Dec 18, 2015
1 parent be67302 commit 75b89e5
Show file tree
Hide file tree
Showing 11 changed files with 626 additions and 196 deletions.
8 changes: 7 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
**.min.js
**/*.min.js
example/**
test/**
node_modules/**
runtime/cube_css.strict.js
runtime/jade_runtime.js
runtime/ejs_runtime.js
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Cube支持三种类型:
npm install -g node-cube

推荐全局安装,有cli命令支持

## Getting Start

初始化cube,通过cube命令,生成工程结构,包含 `cube.min.js``index.html`
Expand Down Expand Up @@ -165,7 +165,24 @@ build参数:
--merge if merged dependences into on file 是否合并
```

在静态资源目录下,编写 `.cubeignore`来排除不需要被处理的文件,格式和.gitignore一样
在静态资源目录下,编写 `.cubeignore`来排除不需要被处理的文件,格式和.gitignore类似:

```
[skip]
/node_modules/jquery/jquery.min.js
[ignore]
/node_modules/.bin
```
- 匹配`skip`段的文件,将跳过编译,直接copy到目标目录
- 匹配`ignore`段的文件,将直接忽略,build之后不会出现在目标目录中

不添加标记的时候,默认都为skip, 例如:
```
/test/
```
cube 在build的时候将直接copy文件,而不会build代码

.cubeignore 文件的寻址 会从build目录开始逐级往上寻找,直到找到为止

## Cube的结构:客户端、服务端。

Expand All @@ -191,7 +208,7 @@ var path = require('path');
function Processor(cube) {
/*
cube: {
config,
fixupResPath
wrapTemplate,
Expand All @@ -205,9 +222,9 @@ Processor.ext = ['.sass'];
Processor.prototype.process = function (relpath, options, callback) {
// get the source file abs path, so you can read the source
var fpath = path.join(options.root, relpath);
// do your processing
// do your processing
return {
}
};
```
Expand Down
69 changes: 69 additions & 0 deletions bin/action/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

var fs = require('xfs');
var path = require('path');

function filePath(file) {
var base = getCwd();
return path.join(base, file);
}
function checkFile(file) {
return fs.existsSync(filePath(file));
}

function getCwd() {
return process.cwd();
}

module.exports = function (option) {
// TODO init project
// copy cube.js
if (!checkFile('./cube.min.js') || option.force) {
var codeCube = fs.readFileSync(sourcePath('./runtime/cube.min.js')).toString();
if (option.css) {
console.log('enable mcss');
codeCube += '\n//cube_css.min.js\n' + fs.readFileSync(sourcePath('./runtime/cube_css.min.js'));
optmizeInfo();
}
fs.sync().save(filePath('./cube.min.js'), codeCube);

if (option.jade) {
console.log('enable jade');
codeCube = '\n//jade_runtime\n' + fs.readFileSync(sourcePath('./runtime/jade_runtime.min.js'));
fs.sync().save(filePath('./node_modules/jade_runtime.js'), codeCube);
}
if (option.ejs) {
console.log('enable ejs');
codeCube = '\n//ejs_runtime\n' + fs.readFileSync(sourcePath('./runtime/ejs_runtime.min.js'));
fs.sync().save(filePath('./node_modules/ejs_runtime.js'), codeCube);
}
console.log('inited cube.min.js');
} else {
console.log('file already exist: ./cube.min.js');
}
if (!checkFile('./start.html')) {
fs.sync().save(filePath('./start.html'), fs.readFileSync(sourcePath('./runtime/start.html')));
console.log('inited start.html');
} else {
console.log('file already exist: ./start.html');
}
if (!checkFile('./package.json')) {
fs.sync().save(filePath('./package.json'), fs.readFileSync(sourcePath('./runtime/cube.json')));
console.log('inited cube.json');
} else {
console.log('file already exist: ./cube.json');
}
if (!checkFile('./main.js')) {
fs.sync().save(filePath('./main.js'),
'document.getElementById("msg").innerHTML = "hello, Cube";\n' +
'document.getElementById("show").value = document.getElementById("initscript").innerHTML;\n' +
'exports.run = function () {console.log("app started!")};'
);
console.log('inited main.js');
} else {
console.log('file already exist: ./main.js');
}
fs.sync().mkdir('test');

console.log('successfully inited');
}
156 changes: 92 additions & 64 deletions bin/cube
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,30 @@ function findCube(cb) {
function optmizeInfo() {
console.log('[INFO] you can custom cube.min.js file, i.e : css_runtime');
}

function filePath(file) {
var base = getCwd();
return path.join(base, file);
}
function checkFile(file) {
return fs.existsSync(filePath(file));
}

function sourcePath(file) {
var base = __dirname;
return path.join(base, '../', file);
}

function buildOutput(err, info) {
console.log('==================');
console.log('Files:', info.total, 'Cost:', info.time + 's');
if (err.length) {
console.log('Error:', err.length);
err.forEach(function (e) {
console.log('\t*', e.code, e.message);
});
} else {
console.log('Build successfully');
}
console.log('==================');
console.log(err.length ? 'Build Finished, Total Errors: ' + err.length : ' Build Successfully');
console.log('Files:', info.total, 'Cost:', info.time + 's');
console.log('==================');
}

cmd.version('v' + pkg.version);
Expand Down Expand Up @@ -124,58 +124,7 @@ cmd.command('init')
.option('--ejs', 'with ejs runtime')
.option('--css', 'with dynamic css load support')
.option('-f, --force', 'force update cube.min.js')
.action(function (option) {
// TODO init project
// copy cube.js
if (!checkFile('./cube.min.js') || option.force) {
var codeCube = fs.readFileSync(sourcePath('./runtime/cube.min.js')).toString();
if (option.css) {
console.log('enable mcss');
codeCube += '\n//cube_css.min.js\n' + fs.readFileSync(sourcePath('./runtime/cube_css.min.js'));
optmizeInfo();
}
fs.sync().save(filePath('./cube.min.js'), codeCube);

if (option.jade) {
console.log('enable jade');
codeCube = '\n//jade_runtime\n' + fs.readFileSync(sourcePath('./runtime/jade_runtime.min.js'));
fs.sync().save(filePath('./node_modules/jade_runtime.js'), codeCube);
}
if (option.ejs) {
console.log('enable ejs');
codeCube = '\n//ejs_runtime\n' + fs.readFileSync(sourcePath('./runtime/ejs_runtime.min.js'));
fs.sync().save(filePath('./node_modules/ejs_runtime.js'), codeCube);
}
console.log('inited cube.min.js');
} else {
console.log('file already exist: ./cube.min.js');
}
if (!checkFile('./start.html')) {
fs.sync().save(filePath('./start.html'), fs.readFileSync(sourcePath('./runtime/start.html')));
console.log('inited start.html');
} else {
console.log('file already exist: ./start.html');
}
if (!checkFile('./package.json')) {
fs.sync().save(filePath('./package.json'), fs.readFileSync(sourcePath('./runtime/cube.json')));
console.log('inited cube.json');
} else {
console.log('file already exist: ./cube.json');
}
if (!checkFile('./main.js')) {
fs.sync().save(filePath('./main.js'),
'document.getElementById("msg").innerHTML = "hello, Cube";\n' +
'document.getElementById("show").value = document.getElementById("initscript").innerHTML;\n' +
'exports.run = function () {console.log("app started!")};'
);
console.log('inited main.js');
} else {
console.log('file already exist: ./main.js');
}
fs.sync().mkdir('test');

console.log('successfully inited');
});
.action();

cmd.command('install')
.description('install dependences')
Expand Down Expand Up @@ -231,13 +180,14 @@ function isAbsPath(p) {
return /^\//.test(p);
}
}

cmd.command('build')
/**
* source , -o,--output, -b, --base 是基于cwd路径, 互相之间没有依赖关系
*/
.usage('-p processor1,processor2 your_code_dir')
.description('build the hole project, including: less->css->min, script->wraped->min')
.option('-p, --processors [value]', 'build in modules, tell cube ignore them')
.option('-p, --processors [value]', 'loading plugin processors, like `cube-coffee,cube-jsx`')
.option('-o, --output [value]', 'set the output path')
.option('-b, --base [value]', 'set the cube base')
.option('-r, --resbase [value]', 'the http base for resouce')
Expand Down Expand Up @@ -309,6 +259,87 @@ cmd.command('build')
}
});

/**
* build工程目录,并进行自动合并优化
* node_modules目录将进行选择性build
*
*/
cmd.command('smart-build')
/**
* source , -o,--output, -b, --base 是基于cwd路径, 互相之间没有依赖关系
*/
.usage('-p processor1,processor2 your_code_dir')
.description('single build the hole project, including: less->css->min, script->wraped->min')
.option('-p, --processors [value]', 'loading plugin processors, like `cube-coffee,cube-jsx`')
.option('-o, --output [value]', 'set the output path')
.option('-b, --base [value]', 'set the cube base')
.option('-r, --resbase [value]', 'the http base for resouce')
.option('--remote [value]', 'set the namespace for remote call')
.option('--auto-merge', 'if switch on auto-merge deps')
.option('--without-compress', 'do not compress code, output the source with wrap')
.action(function (source, args) {
if (!args || !source) {
return this.help();
}
if (args.processors) {
args.processors = args.processors.split(',');
args.processors.forEach(function (v, i, a) {
a[i] = v.trim();
});
}

var cwd = getCwd();
var fstat;
var inputPath, outputPath, cube, tool, root;
var compress = args.withoutCompress ? false : true;

root = args.base ? args.base : '';
if (root) {
root = isAbsPath(root) ? root : path.join(cwd, root);
}
source = isAbsPath(source) ? source : path.join(cwd, source);
inputPath = source;
try {
fstat = fs.statSync(source);
} catch (e) {
console.log('source not fould', e);
this.help();
return;
}

if (fstat.isFile()) {
outputPath = args.output ? (isAbsPath(args.output) ? args.output : path.join(cwd, args.output)) : source.replace(/\.(\w+)$/, '.min.$1');
root = (root ? root : path.dirname(source)).replace(/(\/|\\)$/, '');
cube = new Cube({
release: true,
root: root,
compress: compress,
middleware: false,
processors: args.processors,
resBase: args.resbase,
remote: args.remote
});
tool = Cube.getTool();
tool.processFile(cube, source, outputPath, {withSource: args.withSource}, buildOutput);
} else if (fstat.isDirectory()) {
outputPath = args.output ? (isAbsPath(args.output) ? args.output : path.join(cwd, args.output)) : (source.replace(/(\/|\\)$/, '') + '.release');
root = (root ? root : source).replace(/(\/|\\)$/, '');
cube = new Cube({
release: true,
root: root,
compress: compress,
middleware: false,
processors: args.processors,
resBase: args.resbase,
remote: args.remote
});
tool = Cube.getTool();
tool.processDir(cube, inputPath, outputPath, {withSource: args['with-source']}, buildOutput);
} else {
console.log('unknow type input source', source);
}
});

cmd.command('dag')
.usage('your_code_dir')
.description('gen DAG graph')
Expand All @@ -334,17 +365,17 @@ cmd.command('dag')
// var stat = fs.statSync(source);
if (flagFile) {
source = path.basename(source);
processor.process(source, {root: root, moduleWrap: true}, function (err, data) {
processor.process(source, {root: root, moduleWrap: true}, function () {
end();
});
} else {
fs.walk(source, /\.(js|coffee)$/, function (err, file, done) {
fs.walk(source, /\.(js|coffee|jsx)$/, function (err, file, done) {
if (err) {
console.log('make dag error', err);
process.exit(1);
}
file = file.substr(root.length);
processor.process(file, {root: root, moduleWrap: true}, function (err, data) {
processor.process(file, {root: root, moduleWrap: true}, function () {
done();
});
}, end);
Expand Down Expand Up @@ -378,6 +409,3 @@ cmd.parse(process.argv);
if (!cmd.args.length) {
cmd.help();
}



6 changes: 5 additions & 1 deletion example/.cubeignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/lib/
[skip]
/lib/

[ignore]
/test/test_ignore.js
1 change: 1 addition & 0 deletions example/test/test_ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_ignore
Loading

0 comments on commit 75b89e5

Please sign in to comment.