|
| 1 | +var elmCss = require('elm-css'); |
| 2 | +var gutil = require('gulp-util'); |
| 3 | +var path = require('path'); |
| 4 | +var temp = require('temp').track(); |
| 5 | +var through = require('through2'); |
| 6 | +var Vinyl = require('vinyl'); |
| 7 | + |
| 8 | +var PluginError = gutil.PluginError; |
| 9 | + |
| 10 | +const PLUGIN_NAME = 'gulp-elm-css'; |
| 11 | + |
| 12 | +function css(options = {}) { |
| 13 | + return through.obj(function(file, enc, cb) { |
| 14 | + var projectDir = options.projectDir || __dirname; |
| 15 | + var stylesheetsPath = path.basename(file.path); |
| 16 | + var stylesheetsModule = path.basename(file.path, path.extname(file.path)); |
| 17 | + var stylesheetsPort = options.port || 'files'; |
| 18 | + var pathToMake = options.pathToMake; |
| 19 | + tempDir({prefix: PLUGIN_NAME + '-tmp-'}) |
| 20 | + .then(outputDir => elmCss( |
| 21 | + projectDir, |
| 22 | + stylesheetsPath, |
| 23 | + outputDir, |
| 24 | + stylesheetsModule, |
| 25 | + stylesheetsPort, |
| 26 | + pathToMake |
| 27 | + )) |
| 28 | + .then(cssFiles => { |
| 29 | + cssFiles.forEach(cssFile => |
| 30 | + this.push(new Vinyl({ |
| 31 | + cwd: file.cwd, |
| 32 | + base: file.base, |
| 33 | + path: path.join(path.dirname(file.path), cssFile.filename), |
| 34 | + contents: new Buffer(cssFile.content) |
| 35 | + })) |
| 36 | + ); |
| 37 | + cb(); |
| 38 | + }) |
| 39 | + .catch(error => cb(new gutil.PluginError(PLUGIN_NAME, error))); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function tempDir(affixes) { |
| 44 | + return new Promise((resolve, reject) => |
| 45 | + temp.mkdir(affixes, (err, dirPath) => |
| 46 | + (err ? reject(err) : resolve(dirPath)) |
| 47 | + ) |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +module.exports = css; |
0 commit comments