Skip to content

Instantly share code, notes, and snippets.

@bembelimen
Created March 10, 2019 03:26
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 bembelimen/817abf9078ba40d6f4c6460d2d517740 to your computer and use it in GitHub Desktop.
Save bembelimen/817abf9078ba40d6f4c6460d2d517740 to your computer and use it in GitHub Desktop.
const Fs = require('fs');
const Path = require('path');
const Recurs = require('recursive-readdir');
const UglyCss = require('uglifycss');
const MakeDir = require('./utils/make-dir.es6.js');
const CompileScss = require('./stylesheets/scss-transform.es6.js');
const RootPath = require('./utils/rootpath.es6.js')._();
/**
* Method that will crawl the media_source folder and
* compile any scss files to css and .min.css
* copy any css files to the appropriate destination and
* minify them in place
*
* Expects scss files to have ext: .scss
* css files to have ext: .css
* Ignores scss files that their filename starts with `_`
*
* @param {object} options The options
* @param {string} path The folder that needs to be compiled, optional
*/
module.exports.compile = (options, path) => {
Promise.resolve()
// Compile the scss files
.then(() => {
let files = [];
let folders = [];
if (path) {
const stats = Fs.lstatSync(`${RootPath}/${path}`);
if (stats.isDirectory()) {
folders.push(`${RootPath}/${path}`);
} else if (stats.isFile()) {
files.push(`${RootPath}/${path}`);
} else {
// eslint-disable-next-line no-console
console.error(`Unknown path ${path}`);
process.exit(1);
}
} else {
files = [
`${RootPath}/templates/cassiopeia/scss/offline.scss`,
`${RootPath}/templates/cassiopeia/scss/template.scss`,
`${RootPath}/templates/cassiopeia/scss/template-rtl.scss`,
`${RootPath}/administrator/templates/atum/scss/bootstrap.scss`,
`${RootPath}/administrator/templates/atum/scss/font-awesome.scss`,
`${RootPath}/administrator/templates/atum/scss/template.scss`,
`${RootPath}/administrator/templates/atum/scss/template-rtl.scss`,
`${RootPath}/installation/template/scss/template.scss`,
`${RootPath}/installation/template/scss/template-rtl.scss`,
];
folders = [
`${RootPath}/build/media_source`,
];
}
// Loop to get the files that should be compiled via parameter
folders.forEach((folder) => {
Recurs(folder, ['*.js', '*.map', '*.svg', '*.png', '*.gif', '*.swf', '*.html', '*.json', '*.css']).then(
(filesRc) => {
filesRc.forEach(
(file) => {
// Don't take files with "_" but "file" has the full path, so check via match
if (file.match(/\.scss$/) && !file.match(/(\/|\\)_[^\/\\]+$/)) {
files.push(file);console.log('File to push', files);
}
},
(error) => {
// eslint-disable-next-line no-console
console.error(`something exploded ${error}`);
},
);
},
);
});
files.forEach((inputFile) => {
CompileScss.compile(inputFile, options);
});
// Loop to get the files that should be copied
folders.forEach((folder) => {
Recurs(folder, ['*.js', '*.map', '*.svg', '*.png', '*.gif', '*.swf', '*.html', '*.json', '*.scss']).then(
(filesRc) => {
filesRc.forEach(
(file) => {
if (file.match(/\.css/)) {
// CSS file, we will copy the file and then minify it in place
// Ensure that the directories exist or create them
MakeDir.run(Path.dirname(file).replace('/build/media_source/', '/media/').replace('\\build\\media_source\\', '\\media\\'));
Fs.copyFileSync(file, file.replace('/build/media_source/', '/media/').replace('\\build\\media_source\\', '\\media\\'));
Fs.writeFileSync(
file.replace('/build/media_source/', '/media/').replace('\\build\\media_source\\', '\\media\\').replace('.css', '.min.css'),
UglyCss.processFiles([file], { expandVars: false }),
{ encoding: 'utf8' },
);
// eslint-disable-next-line no-console
console.log(`CSS file copied/minified: ${file}`);
}
},
(error) => {
// eslint-disable-next-line no-console
console.error(`something exploded ${error}`);
},
);
},
);
});
})
// Handle errors
.catch((error) => {
// eslint-disable-next-line no-console
console.error(`${error}`);
process.exit(1);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment