Skip to content

Commit 615c63b

Browse files
committed
Merge pull request #6 from JedWatson/refacto
Basic refactor
2 parents e24993d + c580b4b commit 615c63b

File tree

3 files changed

+32
-100
lines changed

3 files changed

+32
-100
lines changed

example_config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ var pkg = JSON.parse(require('fs').readFileSync('./package.json'));
33

44
// Get default dependencies from package.json.
55
// Dependencies can be customised by hard-coding this array.
6-
var dependencies = [];
7-
Object.keys(pkg.dependencies).forEach(function(i) {
8-
dependencies.push(i);
9-
});
6+
var dependencies = [].concat(Object.keys(pkg.dependencies));
107

118
module.exports = {
12-
139
component: {
1410
file: 'MyComponent.js',
1511
name: 'MyComponent',

index.js

Lines changed: 22 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,16 @@
1-
var _ = require('lodash');
1+
var defaults = require('defaults')
2+
var capitalize = require('capitalize')
3+
var camelCase = require('camelcase')
24

3-
/**
4-
* Check that a compatible version of gulp is available in the project
5-
*/
6-
7-
function fatal() {
8-
var msg = '\n\n';
9-
for (var i = 0; i < arguments.length; i++) {
10-
msg += arguments[i] + '\n\n';
11-
}
12-
console.log(msg);
13-
process.exit(1);
14-
}
15-
16-
try {
17-
var projectGulpVersion = require(module.parent.paths[0] + '/gulp/package.json').version;
18-
} catch(e) {
19-
// If we can't find gulp in the parent project, it's a fatal problem.
20-
fatal(
21-
'You do not seem to have Gulp installed in your project.',
22-
'Please add gulp ^' + packageGulpVersion + ' to your package.json, npm install and try again.'
23-
);
24-
}
25-
try {
26-
// Check to make sure the local gulp and the project gulp match.
27-
var packageGulpVersion = require('./node_modules/gulp/package.json').version;
28-
if (!semver.satisfies(projectGulpVersion, '^' + packageGulpVersion)) {
29-
fatal(
30-
'You have an incompatible version of Gulp installed (' + projectGulpVersion + ').',
31-
'Please add gulp ^' + packageGulpVersion + ' to your package.json, npm install and try again.'
32-
);
33-
}
34-
} catch(e) {
35-
// Assume gulp has been loaded from ../node_modules and it matches the requirements.
36-
}
37-
38-
/**
39-
* Helper method to extract metadata from package.json
40-
*/
41-
42-
function readPackageJSON() {
5+
// Extract package.json metadata
6+
function readPackageJSON () {
437
var pkg = JSON.parse(require('fs').readFileSync('./package.json'));
44-
var deps = [];
45-
if (pkg.dependencies) {
46-
Object.keys(pkg.dependencies).forEach(function(i) {
47-
deps.push(i);
48-
});
49-
}
50-
if (pkg.peerDependencies) {
51-
Object.keys(pkg.peerDependencies).forEach(function(i) {
52-
deps.push(i);
53-
});
54-
}
8+
var dependencies = Object.keys(pkg.dependencies);
9+
var peerDependencies = Object.keys(pkg.peerDependencies);
10+
5511
return {
5612
name: pkg.name,
57-
deps: deps,
13+
deps: dependencies.concat(peerDependencies),
5814
aliasify: pkg.aliasify
5915
};
6016
}
@@ -63,50 +19,30 @@ function readPackageJSON() {
6319
* This package exports a function that binds tasks to a gulp instance
6420
* based on the provided config.
6521
*/
66-
67-
function initTasks(gulp, config) {
68-
22+
function initTasks (gulp, config) {
6923
var pkg = readPackageJSON();
24+
var name = capitalize(camelCase(config.component.pkgName || pkg.name))
7025

71-
if (!config) config = {};
72-
if (!config.component) config.component = {};
73-
74-
if (!config.component.pkgName || !config.component.deps) {
75-
_.defaults(config.component, {
76-
pkgName: pkg.name,
77-
dependencies: pkg.deps
78-
});
79-
}
80-
81-
if (!config.component.name) {
82-
config.component.name = _.capitalize(_.camelCase(config.component.pkgName));
83-
}
84-
85-
if (!config.aliasify) {
86-
config.aliasify = pkg.aliasify;
87-
}
88-
89-
_.defaults(config.component, {
26+
config = defaults(config, { aliasify: pkg.aliasify })
27+
config.component = defaults(config.component, {
28+
pkgName: pkg.name,
29+
dependencies: pkg.deps,
30+
name: name,
9031
src: 'src',
9132
lib: 'lib',
9233
dist: 'dist',
93-
file: config.component.name + '.js'
34+
file: (config.component.name || name) + '.js'
9435
});
9536

9637
if (config.example) {
9738
if (config.example === true) config.example = {};
98-
_.defaults(config.example, {
39+
40+
defaults(config.example, {
9941
src: 'example/src',
10042
dist: 'example/dist',
101-
files: [
102-
'index.html'
103-
],
104-
scripts: [
105-
'example.js'
106-
],
107-
less: [
108-
'example.less'
109-
]
43+
files: ['index.html'],
44+
scripts: ['example.js'],
45+
less: ['example.less']
11046
});
11147
}
11248

@@ -132,7 +68,6 @@ function initTasks(gulp, config) {
13268

13369
gulp.task('build', buildTasks);
13470
gulp.task('clean', cleanTasks);
135-
13671
}
13772

13873
module.exports = initTasks;

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
},
1212
"dependencies": {
1313
"aliasify": "^1.7.2",
14-
"babel-core": "^5.2.16",
1514
"babel-plugin-object-assign": "^1.1.0",
16-
"babelify": "^6.0.2",
17-
"browserify": "^10.1.1",
15+
"babelify": "^6.1.0",
16+
"browserify": "^10.2.0",
1817
"browserify-shim": "^3.8.6",
18+
"camelcase": "^1.1.0",
19+
"capitalize": "^1.0.0",
1920
"chalk": "^1.0.0",
20-
"del": "^1.1.1",
21-
"gulp": "^3.8.11",
21+
"defaults": "^1.0.2",
22+
"del": "^1.2.0",
2223
"gulp-babel": "^5.1.0",
2324
"gulp-bump": "^0.3.0",
2425
"gulp-connect": "^2.2.0",
@@ -29,13 +30,13 @@
2930
"gulp-streamify": "^0.0.5",
3031
"gulp-uglify": "^1.2.0",
3132
"gulp-util": "^3.0.4",
32-
"lodash": "^3.8.0",
3333
"merge-stream": "^0.1.7",
34-
"run-sequence": "^1.1.0",
35-
"semver": "^4.3.4",
3634
"vinyl-source-stream": "^1.1.0",
3735
"watchify": "^3.2.1"
3836
},
37+
"peerDependencies": {
38+
"gulp": "^3.8.0"
39+
},
3940
"scripts": {
4041
"test": "echo \"no tests yet\" && exit 0"
4142
},

0 commit comments

Comments
 (0)