2017-05-23 18:44:31 +02:00
|
|
|
import * as fs from 'fs';
|
2017-05-24 00:46:34 +02:00
|
|
|
import * as gulp from 'gulp';
|
2017-05-23 18:44:31 +02:00
|
|
|
import * as gutil from 'gulp-util';
|
2017-05-24 00:46:34 +02:00
|
|
|
import * as mustache from 'mustache';
|
2017-05-23 18:44:31 +02:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
import { HR, MESSAGE_GENERATED, MESSAGE_ICON_ERROR } from './../consts/log';
|
|
|
|
|
|
|
|
import { CHARSET } from '../consts/files';
|
|
|
|
import { IIcon } from './../interfaces/iicon';
|
|
|
|
import { IPlainObject } from '../interfaces/iplain-object';
|
|
|
|
import paths from '../consts/paths';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object implementing the IIcon interface
|
|
|
|
* @param {string} fileName
|
|
|
|
* @returns {IIcon}
|
|
|
|
*/
|
|
|
|
function iconFactory(fileName: string): IIcon {
|
|
|
|
let name: string = path.basename(fileName, path.extname(fileName));
|
|
|
|
let last: boolean = false;
|
2017-05-23 18:44:31 +02:00
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
return { name, last } as IIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* > Build Icons
|
|
|
|
* @returns {gulp.Gulp}
|
|
|
|
*/
|
|
|
|
export default gulp.task('build:icons', cb => {
|
|
|
|
let contents: string;
|
|
|
|
let fileNames: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/svgs`));
|
|
|
|
let icons: IIcon[] = fileNames.map(fileName => iconFactory(fileName));
|
|
|
|
let partials: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/partials`));
|
|
|
|
let partialsData: IPlainObject = {};
|
|
|
|
let pathTemp: string = './themes/.material-theme-icons.tmp';
|
2017-05-23 18:44:31 +02:00
|
|
|
|
|
|
|
icons[icons.length - 1].last = true;
|
|
|
|
|
|
|
|
partials.forEach(partial => {
|
2017-05-24 00:46:34 +02:00
|
|
|
partialsData[path.basename(partial, path.extname(partial))] = fs.readFileSync(path.join(paths.SRC, `./icons/partials`, `./${partial}`), CHARSET);
|
2017-05-23 18:44:31 +02:00
|
|
|
});
|
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
contents = mustache.render(
|
|
|
|
fs.readFileSync(path.join(paths.SRC, `./icons/icons-theme.json`), CHARSET)
|
|
|
|
, { icons }
|
|
|
|
, partialsData
|
2017-05-23 18:44:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
contents = JSON.stringify(JSON.parse(contents), null, 2);
|
2017-05-24 00:46:34 +02:00
|
|
|
} catch (error) {
|
|
|
|
gutil.log(gutil.colors.red(MESSAGE_ICON_ERROR), error);
|
|
|
|
cb(error);
|
2017-05-23 18:44:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(pathTemp, contents, CHARSET);
|
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
gutil.log(gutil.colors.gray(HR));
|
|
|
|
gutil.log(MESSAGE_GENERATED, gutil.colors.green(pathTemp));
|
|
|
|
gutil.log(gutil.colors.gray(HR));
|
2017-05-23 18:44:31 +02:00
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|