Skip to content

Instantly share code, notes, and snippets.

@deguchi
Last active April 8, 2020 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deguchi/5bd90d341d71996d057f5c9022fc97d1 to your computer and use it in GitHub Desktop.
Save deguchi/5bd90d341d71996d057f5c9022fc97d1 to your computer and use it in GitHub Desktop.
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