Last active
April 8, 2020 01:40
-
-
Save deguchi/5bd90d341d71996d057f5c9022fc97d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const glob = require('glob'); | |
const sass = require('node-sass'); | |
const commandLineArgs = require('command-line-args'); | |
// コマンドラインオプションの定義 | |
const optionDefinitions = [ | |
{ | |
name: 'file', | |
alias: 'f', | |
type: String, | |
defaultValue: '' | |
}, | |
{ | |
name: 'output-style', | |
alias: 'o', | |
type: String, | |
defaultValue: 'compressed' | |
} | |
]; | |
const options = commandLineArgs(optionDefinitions); | |
console.log(options); | |
// ファイル指定がない場合、すべてのsassファイルをコンパイルする | |
if (options.file==='') { | |
const pattern = 'src/**/*.sass'; | |
glob(pattern, function (err, files) { | |
if(err) console.log(err); | |
console.log(pattern); | |
console.log(files); | |
files.map((file) => { | |
compileSass(file); | |
}); | |
}); | |
} else { | |
compileSass(options.file); | |
} | |
function compileSass(file) { | |
console.log(file) | |
const p = path.parse(file); | |
let saveDir = p.dir.replace('src', 'build'); | |
let saveFilePath = path.join(saveDir, p.base.replace('sass', 'css')); | |
sass.render({ | |
file: file, | |
outputStyle: options.outputStyle | |
}, function(err, result) { | |
// console.log(err) | |
// console.log(result) | |
if (!fs.existsSync('build')) fs.mkdirSync('build'); | |
if (!fs.existsSync(saveDir)) fs.mkdirSync(saveDir); | |
fs.writeFile(saveFilePath, result.css, (err, data) => { | |
if(err) console.log(err); | |
else console.log(file + ' compiled.'); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment