Skip to content

Commit

Permalink
Expose packages' output namer for gulpTam
Browse files Browse the repository at this point in the history
  • Loading branch information
arrowrowe committed Nov 16, 2015
1 parent 53ecfd3 commit d3b3926
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var tam = {
worker: require('./lib/worker'),
log: require('./lib/log'),
run: require('./lib/cli/run'),
fillAssets: require('./lib/option/fill-assets')
fillAssets: require('./lib/option/fill-assets'),
namer: require('./lib/file/namer')
};

tam.tools = tam.worker.tools;
Expand Down
32 changes: 32 additions & 0 deletions lib/file/namer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var path = require('path');
var getRealPkgSrc = require('./get-real-pkg-src');

function trans(files, dist, srcLength) {
return files.map(function (file) {
return path.resolve(dist, file.substr(srcLength));
});
}

function getCompiledPath(scss) {
return scss.substr(0, scss.length - 4) + 'css';
}

module.exports = function (assets, pkg) {

var srcLength = (pkg.realSrc || getRealPkgSrc(assets, pkg)).length;

var pkgDist = pkg.dist || pkg.name;
var dist = path.resolve(assets.dist, pkgDist);

return function (command) {
if (command.behavior === 'copy') {
command.output = trans(command.files, dist, srcLength);
} else if (command.behavior === 'compile') {
command.output = trans(command.files, dist, srcLength).map(getCompiledPath);
} else if (command.behavior === 'compress') {
command.output = [path.resolve(dist, pkg.name + '.' + command.args.type)];
} else {
throw new Error('Unrecognized behavior [' + command.behavior + ']!');
}
};
};
31 changes: 2 additions & 29 deletions lib/file/output.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
var path = require('path');
var namer = require('./namer');

// NOTE: It does NOT actually output. It just generates the output paths.
// It does not return. The argument `commands` will change.
module.exports = function (assets, pkg, commands) {

if (commands.length === 0) {
return;
}

var srcLength = pkg.realSrc.length;
var pkgDist = pkg.dist || pkg.name;
var dist = path.resolve(assets.dist, pkgDist);

function trans(files) {
return files.map(function (file) {
return path.resolve(dist, file.substr(srcLength));
});
}

function getCompiledPath(scss) {
return scss.substr(0, scss.length - 4) + 'css';
}

commands.forEach(function (command) {
if (command.behavior === 'copy') {
command.output = trans(command.files);
} else if (command.behavior === 'compile') {
command.output = trans(command.files).map(getCompiledPath);
} else if (command.behavior === 'compress') {
command.output = [path.resolve(dist, pkg.name + '.' + command.args.type)];
} else {
throw new Error('Unrecognized behavior [' + command.behavior + ']!');
}
});

commands.forEach(namer(assets, pkg));
};
12 changes: 12 additions & 0 deletions test/lib/file/output-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,16 @@ describe('lib/file/output', function () {
expect(spy.callCount).to.equal(0);
});

it('accepts packages without realSrc', function () {
utilOutput({
dist: './runtime/dist',
src: './runtime/src'
}, {
name: 'b'
}, [{
behavior: 'copy',
files: ['a.js']
}]);
});

});

0 comments on commit d3b3926

Please sign in to comment.