2021-07-18 01:39:20 -04:00
|
|
|
const gulp = require("gulp");
|
|
|
|
const svgmin = require("gulp-svgmin");
|
2021-08-10 01:51:02 -07:00
|
|
|
const sass = require("gulp-sass")(require("sass"));
|
2021-08-13 11:51:22 -07:00
|
|
|
const footer = require("gulp-footer");
|
2021-07-18 01:39:20 -04:00
|
|
|
const yargs = require("yargs");
|
|
|
|
const fsPromise = require("fs").promises;
|
2020-09-11 09:45:54 -07:00
|
|
|
const path = require("path");
|
2021-09-08 13:52:53 -07:00
|
|
|
const {execSync} = require("child_process")
|
2015-01-27 10:43:36 +01:00
|
|
|
|
2020-09-14 10:19:34 -07:00
|
|
|
// global const
|
2021-08-13 11:51:22 -07:00
|
|
|
const deviconBaseCSSName = "devicon-base.css"
|
2020-09-30 10:06:56 -07:00
|
|
|
const deviconJSONName = "devicon.json";
|
|
|
|
const aliasSCSSName = "devicon-alias.scss";
|
|
|
|
const colorsCSSName = "devicon-colors.css";
|
|
|
|
const finalMinSCSSName = "devicon.min.scss";
|
|
|
|
|
2021-04-29 01:04:32 -07:00
|
|
|
//////// CSS Tasks ////////
|
2020-09-14 10:19:34 -07:00
|
|
|
|
2020-09-11 09:45:54 -07:00
|
|
|
/**
|
2020-09-13 22:31:22 -07:00
|
|
|
* Create the devicon.min.css by creating needed
|
2020-09-14 10:19:34 -07:00
|
|
|
* css files and compiling them together using Sass.
|
2020-09-13 22:31:22 -07:00
|
|
|
*/
|
|
|
|
async function createDeviconMinCSS() {
|
2021-07-18 01:39:20 -04:00
|
|
|
await createCSSFiles();
|
|
|
|
|
|
|
|
let deviconMinPath = path.join(__dirname, finalMinSCSSName);
|
2021-08-13 11:51:22 -07:00
|
|
|
// recall that devicon-alias.scss imported the devicon-base.css => don't need
|
2021-07-18 01:39:20 -04:00
|
|
|
// to reimport that file.
|
|
|
|
const fileContent = `@use "${aliasSCSSName}";@use "${colorsCSSName}";`;
|
|
|
|
await fsPromise.writeFile(deviconMinPath, fileContent, "utf8");
|
|
|
|
|
|
|
|
return gulp
|
|
|
|
.src(finalMinSCSSName)
|
|
|
|
.pipe(sass.sync({ outputStyle: "compressed" }).on("error", sass.logError))
|
|
|
|
.pipe(gulp.dest("./"));
|
2020-09-13 22:31:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the devicon-alias.scss and the
|
|
|
|
* devicon-colors.css from the devicon.json.
|
2020-09-11 09:45:54 -07:00
|
|
|
*/
|
|
|
|
async function createCSSFiles() {
|
2021-07-18 01:39:20 -04:00
|
|
|
const deviconJson = JSON.parse(
|
|
|
|
await fsPromise.readFile(path.join(__dirname, deviconJSONName), "utf8")
|
|
|
|
);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
createAliasSCSS(deviconJson),
|
|
|
|
createColorsCSS(deviconJson)
|
|
|
|
]);
|
2020-09-11 09:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-13 22:31:22 -07:00
|
|
|
* Create an alias scss file in the root dir based on the devicon.json.
|
|
|
|
* This function will use sass instead of normal css.
|
|
|
|
* This is due to sass's ability to extend classes => Make it easier
|
|
|
|
* to create aliases classes.
|
2020-09-11 09:45:54 -07:00
|
|
|
* @param {Object} deviconJson, the object read from the
|
2021-07-18 01:39:20 -04:00
|
|
|
* devicon.json file.
|
2020-09-13 22:31:22 -07:00
|
|
|
* @return a Promise that'll resolve when the devicon-alias.scss is
|
|
|
|
* created.
|
2020-09-11 09:45:54 -07:00
|
|
|
*/
|
2020-09-13 22:31:22 -07:00
|
|
|
function createAliasSCSS(deviconJson) {
|
2021-07-18 01:39:20 -04:00
|
|
|
let statements = deviconJson.map(createAliasStatement).join(" ");
|
2021-08-13 11:51:22 -07:00
|
|
|
let sass = `@use "${deviconBaseCSSName}";${statements}`;
|
2021-07-18 01:39:20 -04:00
|
|
|
let sassPath = path.join(__dirname, aliasSCSSName);
|
|
|
|
return fsPromise.writeFile(sassPath, sass, "utf8");
|
2020-09-11 09:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-18 01:39:20 -04:00
|
|
|
* Create the aliases statement by searching for the
|
2020-09-11 09:45:54 -07:00
|
|
|
* techname in the statement and finding its aliases in
|
|
|
|
* the deviconJson.
|
2020-09-13 22:31:22 -07:00
|
|
|
* @param {Object} fontObj, a devicon font object.
|
|
|
|
* @return a string representing a css statement of the
|
|
|
|
* devicon-alias.scss.
|
2020-09-11 09:45:54 -07:00
|
|
|
*/
|
2020-09-13 22:31:22 -07:00
|
|
|
function createAliasStatement(fontObj) {
|
2021-07-18 01:39:20 -04:00
|
|
|
let { name, aliases } = fontObj;
|
2020-09-13 22:31:22 -07:00
|
|
|
|
2021-07-18 01:39:20 -04:00
|
|
|
return aliases
|
|
|
|
.map(aliasObj => {
|
|
|
|
return `.devicon-${name}-${aliasObj.alias} {
|
2020-09-13 22:31:22 -07:00
|
|
|
@extend .devicon-${name}-${aliasObj.base};
|
|
|
|
}`;
|
2021-07-18 01:39:20 -04:00
|
|
|
})
|
|
|
|
.join(" ");
|
2020-09-11 09:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a colors css file in the root dir based on the deviconJson.
|
|
|
|
* @param {Object} deviconJson, the object read from the
|
2021-07-18 01:39:20 -04:00
|
|
|
* devicon.json file.
|
2020-09-13 22:31:22 -07:00
|
|
|
* @return a Promise that'll resolve when the devicon-alias.scss is
|
|
|
|
* created.
|
2020-09-11 09:45:54 -07:00
|
|
|
*/
|
|
|
|
function createColorsCSS(deviconJson) {
|
2021-07-18 01:39:20 -04:00
|
|
|
// create the color statements for each font object
|
|
|
|
let statements = deviconJson
|
|
|
|
.map(fontObj => {
|
|
|
|
let {
|
|
|
|
name,
|
|
|
|
versions: { font: fonts },
|
|
|
|
color,
|
|
|
|
aliases
|
|
|
|
} = fontObj;
|
|
|
|
|
|
|
|
if (fonts.length === 0 || typeof color !== "string") {
|
|
|
|
console.log(`This object doesn't have a font or a color: ${name}`);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the icons in the font attr
|
|
|
|
let cssClasses = fonts.map((font) => `.devicon-${name}-${font}.colored`);
|
|
|
|
|
|
|
|
// process the icons in the aliases attr
|
|
|
|
aliases.forEach(aliasObj => {
|
|
|
|
cssClasses.push(`.devicon-${name}-${aliasObj["alias"]}.colored`);
|
|
|
|
});
|
|
|
|
|
|
|
|
return `${cssClasses.join(",")}{color: ${color}}`;
|
|
|
|
})
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
let cssPath = path.join(__dirname, colorsCSSName);
|
|
|
|
return fsPromise.writeFile(cssPath, statements, "utf8");
|
2020-09-11 09:45:54 -07:00
|
|
|
}
|
|
|
|
|
2020-09-14 10:19:34 -07:00
|
|
|
/**
|
2021-07-18 01:39:20 -04:00
|
|
|
* Remove the devicon-alias.scss, devicon-colors.css,
|
2020-09-14 10:19:34 -07:00
|
|
|
* and the devicon.min.scss.
|
|
|
|
*/
|
|
|
|
function cleanUp() {
|
2021-07-18 01:39:20 -04:00
|
|
|
let fileNames = [aliasSCSSName, colorsCSSName, finalMinSCSSName];
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
fileNames.map(name => {
|
|
|
|
try {
|
|
|
|
let filePath = path.join(__dirname, name);
|
|
|
|
return fsPromise.unlink(filePath);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-09-14 10:19:34 -07:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:14:13 -07:00
|
|
|
//////// Update SVG Task ////////
|
|
|
|
/**
|
2021-07-18 01:39:20 -04:00
|
|
|
* Update the svg by optimizing it
|
2021-05-15 12:14:13 -07:00
|
|
|
* and prefixing its ids so it's unique across the repo.
|
2021-07-18 01:39:20 -04:00
|
|
|
*
|
2021-05-15 12:14:13 -07:00
|
|
|
* This requires a json list of svg file names to update.
|
|
|
|
* This must be passed through the commandline arguments.
|
|
|
|
*/
|
|
|
|
function optimizeSvg() {
|
2021-07-18 01:39:20 -04:00
|
|
|
let svgGlob = JSON.parse(yargs.argv.svgFiles);
|
|
|
|
return gulp
|
|
|
|
.src(svgGlob)
|
2021-05-15 12:14:13 -07:00
|
|
|
.pipe(svgmin(configOptionCallback))
|
2021-08-13 11:51:22 -07:00
|
|
|
.pipe(footer("\n"))
|
2021-07-18 01:39:20 -04:00
|
|
|
.pipe(
|
|
|
|
gulp.dest(file => {
|
|
|
|
return file.base;
|
|
|
|
})
|
|
|
|
);
|
2021-05-15 12:14:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a config option for each file.
|
2021-07-18 01:39:20 -04:00
|
|
|
* @param {Object} file - Gulp Vinyl instance of the file
|
2021-05-15 12:14:13 -07:00
|
|
|
* being processed.
|
|
|
|
* @returns a SVGO config object.
|
|
|
|
*/
|
|
|
|
function configOptionCallback(file) {
|
|
|
|
return {
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
prefixIds: {
|
|
|
|
prefix: file.stem, // add file name to ids
|
|
|
|
delim: "-"
|
2021-07-18 01:39:20 -04:00
|
|
|
},
|
2021-05-15 12:14:13 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
removeViewBox: false // keep viewbox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
removeDimensions: true // remove height and width
|
2022-03-13 15:02:41 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "removeAttrs",
|
|
|
|
params: {
|
|
|
|
attrs: "svg:(x|y)"
|
|
|
|
}
|
2021-05-15 12:14:13 -07:00
|
|
|
}
|
|
|
|
]
|
2021-07-18 01:39:20 -04:00
|
|
|
};
|
2021-05-15 12:14:13 -07:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:52:53 -07:00
|
|
|
/**
|
|
|
|
* Bump the NPM version of this project.
|
|
|
|
* This is called via the command line
|
|
|
|
* using the format "npm run bump -- -v='MAJOR.MINOR.PATCH'"
|
|
|
|
* @returns a Promise.resolve()
|
|
|
|
*/
|
|
|
|
function bumpVersion() {
|
|
|
|
let newVersion = yargs.argv.v
|
|
|
|
let command = `npm version v${newVersion} -m "bump npm version to v${newVersion}"`
|
|
|
|
console.log("Running command: " + command)
|
|
|
|
let stdout = execSync(command)
|
|
|
|
console.log("Command ran. Here's the result:\n" + stdout)
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:31:22 -07:00
|
|
|
exports.updateCss = createDeviconMinCSS;
|
2020-12-28 19:43:26 +01:00
|
|
|
exports.clean = cleanUp;
|
2021-05-15 12:14:13 -07:00
|
|
|
exports.optimizeSvg = optimizeSvg;
|
2021-09-08 13:52:53 -07:00
|
|
|
exports.bumpVersion = bumpVersion
|