{"size": 3427, "ext": "js", "max_stars_repo_path": "gulpfile.js", "max_stars_repo_name": "argus-cs/generate-md-icons", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "gulpfile.js", "max_issues_repo_name": "argus-cs/generate-md-icons", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "gulpfile.js", "max_forks_repo_name": "argus-cs/generate-md-icons", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.1338028169, "max_line_length": 112, "alphanum_fraction": 0.6069448497}
const through = require('through2');
const gulp = require('gulp');
const concat = require('gulp-concat');
const prettier = require('gulp-prettier');
const gutil = require('gulp-util');
const svgmin = require('gulp-svgmin');
const svgson = require('svgson');
const path = require('path');
const File = gutil.File;

/**
 * parse a string like snack_case or dont-know-the-name to 
 * camelCase.
 * 
 * @param {String} str String to be parsed to camelCase.
 */
function toCamelCase(str) {
  return str.replace(/([-_][a-z])/ig, ($1) => {
    return $1.toUpperCase()
      .replace('-', '')
      .replace('_', '');
  });
}

/**
 * generate the structure of the icon svg to easy to read json object.
 */
function generateIconData() {
  'use strict'
  return through.obj(function(file, enc, next) {
    const content = file.contents.toString('utf-8');

    const filename = path.basename(file.path, path.extname(file.path));
    const friendlyName = toCamelCase(filename);

    svgson.parse(content)
      .then(parsed => {

        const { width, height } = parsed.attributes;
        const { children } = parsed;

        const result = `"${friendlyName}": { w: ${width}, h: ${height}, children: ${JSON.stringify(children)} }`

        file.contents = Buffer.from(result)
        
        next(null, file);
      })
      .catch(err => console.error(err))
  })
}

/**
 * writte the json object into a file
 */
function generateFile() {
  'use strict'
  return through.obj(function(file, enc, next) {
    const base = path.join(file.path, '..');
    const filename = path.basename(file.path, path.extname(file.path));

    console.log('Generating object: ', filename)

    const fileIcon = new File({
      base: base,
      path: path.join(__dirname, './generated-icons.json'),
      contents: file.contents
    })

    this.push(fileIcon)

    next()
  })
}

/**
 * generate js file containing the icons
 * 
 * @param {String} filePath 
 * @param {String} filename 
 */
function createFile(filePath, filename) {
  return through.obj(function(file, enc, next) {
    var fileName = 'out';
    var pathFile = path.join(file.path, '..');
    var content = file.contents.toString('utf-8');

    if(filename !== undefined) {
      fileName = filename;
    }

    if(filePath !== undefined) {
      pathFile = filePath;
    }

    var template = `/**
    * This file is automatically generated by the a tool.
    * Manually changing this file will only lead to sadness.
    * 
    * see https://github.com/argus-cs/generate-md-icons#readme
    * for more info.
    */
   export const ${toCamelCase(fileName)} = {
     ${content}
   }`;

    console.log('Creating js file...');

    const create = new File({
      base: pathFile,
      path: path.join(__dirname, `${pathFile}/${fileName}.js`),
      contents: Buffer.from(template)
    });

    this.push(create);

    console.info('File created with success.');

    next();
  })
}

gulp.task('generate', () => {
  return gulp.src('./icons/*.svg')
    .pipe(svgmin({
      js2svg: {
        pretty: true
      }
    }))
    .pipe(generateIconData())
    .pipe(generateFile())
    .pipe(concat('icons.json', {newLine: ','}))
    .pipe(gulp.dest('dist/'))
});

gulp.task('create', () => {
  return gulp.src('./dist/icons.json')
    .pipe(createFile('./dist', 'icons-generated'))
    .pipe(prettier())
    .pipe(gulp.dest('dist/'))
})

gulp.task('build', gulp.series('generate', 'create'));