Skip to content

Commit

Permalink
update static-http-server
Browse files Browse the repository at this point in the history
fix some bug
  • Loading branch information
keelii committed Feb 21, 2016
1 parent 69d20f4 commit ecc38b7
Show file tree
Hide file tree
Showing 27 changed files with 372 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "4.3.1"
- "4.0.0"
after_success:
- npm run cover
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ A FE Build tool with easy cli.
│││║ ║║ ║║ ║
└┴┘╚═╝╚═╝╚═╝
```

## Requirement

> "node" : ">=4.0.0"
## Install globally

> npm install wooo -g
Expand Down
19 changes: 9 additions & 10 deletions cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ Processor.imagemin = function (config, input, callback) {
Processor.copy = function (config, input, callback) {
callback = callback || function() {};
let source = input || config.assets;
let stream = vfs.src(source,
{ base: config._SOURCE_ROOT});

stream.pipe(vfs.dest(config._DEST_ROOT));
stream.on('end', callback);
vfs.src(source, {base: config._SOURCE_ROOT})
.pipe(vfs.dest(config._DEST_ROOT))
.on('end', callback);
};

function getSources(input, config) {
Expand Down Expand Up @@ -120,21 +119,21 @@ function build(config, input, callback) {
}

if (s.uglify.length && config._isPrd) {
tasks.push(cb => Processor.uglify(config, s.uglify, cb));
//tasks.push(cb => Processor.uglify(config, s.uglify, cb));
}
if (s.sass.length) {
tasks.push(cb => Processor.sass(config, s.sass, cb));
//tasks.push(cb => Processor.sass(config, s.sass, cb));
}
if (s.imagemin.length) {
tasks.push(cb => Processor.imagemin(config, s.imagemin, cb));
//tasks.push(cb => Processor.imagemin(config, s.imagemin, cb));
}
if (s.nunjucks.length) {
if (config._arg.nunjucks || config._isDev) {
tasks.push(cb => Processor.nunjucks(config, s.nunjucks, cb));
}
}
if (s.copy.length) {
tasks.push(cb => Processor.copy(config, s.copy, cb));
//tasks.push(cb => Processor.copy(config, s.copy, cb));
}

async.series(tasks, callback);
Expand All @@ -154,7 +153,7 @@ module.exports = function (config, input, callback) {

// build --sprite
if (cmd.sprite) {
return Sprite(config);
return Sprite(config, callback);
}

// build --nunjucks
Expand All @@ -176,7 +175,7 @@ module.exports = function (config, input, callback) {
cb => build(config, input, cb)
], function(err) {
if (err) return console.log(err);
console.log('[DONE] build.');
callback();
});
} else {
build(config, input, callback);
Expand Down
13 changes: 6 additions & 7 deletions cli/clear.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const rimraf = require('rimraf');
const async = require('async');

module.exports = function (config) {
module.exports = function (config, callback) {
callback = callback || function() {};

async.each([
config.dest,
config.server.dir
], rimraf, function (err) {
if (err) return console.log(err);
console.log('[DONE] clear.');
});
config._DEST_ROOT,
config._SERVER_ROOT
], rimraf, callback);
};
2 changes: 1 addition & 1 deletion cli/release.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function(config) {
console.log('release processor');
return 'to be done.';
};
31 changes: 22 additions & 9 deletions cli/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
const chokidar = require('chokidar');
const shs = require('static-http-server');
const build = require('./build');

const async = require('async');
const utils = require('../lib/utils');

function server(config) {
shs(config.server.dir, {
function server(config, callback) {
callback = callback || function() {};

shs(config._SERVER_ROOT, {
nolog: config.nolog,
index: config.server.index,
port: config.server.port
});
}, callback);
}

function watch(config){
function watch(config, callback){
callback = callback || function() {};

function log(e, rPath) {
console.log('[%s] => %s', e.toUpperCase(), utils.relativeDir(rPath));
}
Expand All @@ -29,10 +36,16 @@ function watch(config){
log(event, path);
build(config, config.templates[0]);
});

callback(null);
}

module.exports = function(config) {
build(config);
server(config);
watch(config);
};
module.exports = function(config, callback) {
callback = callback || function() {};

async.series([
cb => build(config, null, cb),
cb => server(config, cb),
cb => watch(config, cb)
], callback);
};
17 changes: 9 additions & 8 deletions default.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ const defaults = {
}
};

function addRuntimeVal(arg) {
function addRuntimeVal(arg, configPath, isDev) {
configPath = configPath || process.cwd();
let config = {};

try {
config = require(process.cwd() + '/config.js');
config = require(path.join(configPath, 'config.js'));
} catch (err) {
console.error('Config file not found.');
}
Expand All @@ -74,22 +75,22 @@ function addRuntimeVal(arg) {

options._arg = arg;
options._cmd = arg._[0];
options._CWD = process.cwd();
options._CWD = configPath || process.cwd();
options._VERSION = arg.ver || options.version;
options._SOURCE_ROOT = path.join(options._CWD, options.source);
options._VIEW_ROOT = path.join(options._CWD, options.view);
options._SERVER_ROOT = path.join(path.resolve(options.server.dir));
options._VIEW_ROOT = path.join(options._SOURCE_ROOT, options.view);
options._SERVER_ROOT = path.join(options._CWD, options.server.dir);

// project_name/version_number
options._PRD_PREFIX = path.join(options.name, options._VERSION);

// Development
options._isDev = cmdMap[options._cmd] == 'start';
options._isDev = isDev || cmdMap[options._cmd] === 'start';
// Production
options._isPrd = /build|release|deploy/.test(cmdMap[options._cmd]);

options._DEST_ROOT = options._isDev
? options.server.dir
? options._SERVER_ROOT
: path.join(options.dest, options._PRD_PREFIX);

// component data
Expand All @@ -98,4 +99,4 @@ function addRuntimeVal(arg) {
return options;
}

module.exports = (arg) => addRuntimeVal(arg);
module.exports = addRuntimeVal;
14 changes: 10 additions & 4 deletions lib/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ function sprite(config, sprites, callback) {
}

module.exports = function (config, callback) {
let items = config.sprites.items.reduce(item => {
let files = globby.sync(item);
if (files.length) return [files];
});
let items = [];

if (config.sprites.items.length) {
items = config.sprites.items.reduce(item => {
let files = globby.sync(item);
if (files.length) return [files];
});
} else {
return callback(null);
}

let tasks = [];

Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wooo",
"version": "0.0.1",
"version": "0.0.2",
"author": "keelii <[email protected]>",
"description": "FE Build tool with easy cli.",
"license": "MIT",
Expand All @@ -12,10 +12,13 @@
"bin": {
"wo": "index.js"
},
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"prepublish": "",
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec && ./node_modules/.bin/codecov",
"test": "./node_modules/.bin/mocha test/main.js --reporter spec"
"test": "./node_modules/.bin/mocha test/test.js --reporter spec"
},
"repository": {
"type": "git",
Expand All @@ -36,14 +39,15 @@
"nunjucks": "^2.3.0",
"rimraf": "^2.5.2",
"spritesmith": "^3.1.0",
"static-http-server": "0.0.4",
"static-http-server": "0.0.5",
"uglify-js": "^2.6.1",
"vinyl-fs": "^2.3.1",
"vinyl-ftp": "^0.4.5"
},
"devDependencies": {
"codecov": "^1.0.1",
"istanbul": "^0.4.2",
"mocha": "^2.4.5"
"mocha": "^2.4.5",
"request": "^2.69.0"
}
}
19 changes: 19 additions & 0 deletions test/app/components/main/__sprite.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

.sprite-1 {
width: 21px;
height: 21px;
background-image: url(i/__sprite.png);
background-position: -0px -0px;
}
.sprite-2 {
width: 21px;
height: 21px;
background-image: url(i/__sprite.png);
background-position: -21px -0px;
}
.sprite-3 {
width: 21px;
height: 21px;
background-image: url(i/__sprite.png);
background-position: -0px -21px;
}
103 changes: 103 additions & 0 deletions test/app/components/main/_sprite.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
SCSS variables are information about icon's compiled state, stored under its original file name
.icon-home {
width: $icon-home-width;
}
The large array-like variables contain all information about a single icon
$icon-home: x y offset_x offset_y width height total_width total_height image_path;
At the bottom of this section, we provide information about the spritesheet itself
$spritesheet: width height image $spritesheet-sprites;
*/
$sprite-1-name: 'sprite-1';
$sprite-1-x: 0px;
$sprite-1-y: 0px;
$sprite-1-offset-x: 0px;
$sprite-1-offset-y: 0px;
$sprite-1-width: 21px;
$sprite-1-height: 21px;
$sprite-1-total-width: 42px;
$sprite-1-total-height: 42px;
$sprite-1-image: 'i/_sprite.png';
$sprite-1: (0px, 0px, 0px, 0px, 21px, 21px, 42px, 42px, 'i/_sprite.png', 'sprite-1', );
$sprite-2-name: 'sprite-2';
$sprite-2-x: 21px;
$sprite-2-y: 0px;
$sprite-2-offset-x: -21px;
$sprite-2-offset-y: 0px;
$sprite-2-width: 21px;
$sprite-2-height: 21px;
$sprite-2-total-width: 42px;
$sprite-2-total-height: 42px;
$sprite-2-image: 'i/_sprite.png';
$sprite-2: (21px, 0px, -21px, 0px, 21px, 21px, 42px, 42px, 'i/_sprite.png', 'sprite-2', );
$sprite-3-name: 'sprite-3';
$sprite-3-x: 0px;
$sprite-3-y: 21px;
$sprite-3-offset-x: 0px;
$sprite-3-offset-y: -21px;
$sprite-3-width: 21px;
$sprite-3-height: 21px;
$sprite-3-total-width: 42px;
$sprite-3-total-height: 42px;
$sprite-3-image: 'i/_sprite.png';
$sprite-3: (0px, 21px, 0px, -21px, 21px, 21px, 42px, 42px, 'i/_sprite.png', 'sprite-3', );
$spritesheet-width: 42px;
$spritesheet-height: 42px;
$spritesheet-image: 'i/_sprite.png';
$spritesheet-sprites: ($sprite-1, $sprite-2, $sprite-3, );
$spritesheet: (42px, 42px, 'i/_sprite.png', $spritesheet-sprites, );

/*
The provided mixins are intended to be used with the array-like variables
.icon-home {
@include sprite-width($icon-home);
}
.icon-email {
@include sprite($icon-email);
}
*/
@mixin sprite-width($sprite) {
width: nth($sprite, 5);
}

@mixin sprite-height($sprite) {
height: nth($sprite, 6);
}

@mixin sprite-position($sprite) {
$sprite-offset-x: nth($sprite, 3);
$sprite-offset-y: nth($sprite, 4);
background-position: $sprite-offset-x $sprite-offset-y;
}

@mixin sprite-image($sprite) {
$sprite-image: nth($sprite, 9);
background-image: url(#{$sprite-image});
}

@mixin sprite($sprite) {
@include sprite-image($sprite);
@include sprite-position($sprite);
@include sprite-width($sprite);
@include sprite-height($sprite);
}

/*
The `sprites` mixin generates identical output to the CSS template
but can be overridden inside of SCSS
@include sprites($spritesheet-sprites);
*/
@mixin sprites($sprites) {
@each $sprite in $sprites {
$sprite-name: nth($sprite, 10);
.#{$sprite-name} {
@include sprite($sprite);
}
}
}
Binary file added test/app/components/main/i/__sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/app/components/main/i/_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/app/components/main/i/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/app/components/main/i/sprite-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/app/components/main/i/sprite-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/app/components/main/i/sprite-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ecc38b7

Please sign in to comment.