Skip to content

Instantly share code, notes, and snippets.

@JeongHoJeong
Created February 8, 2018 20:32
Show Gist options
  • Select an option

  • Save JeongHoJeong/c1adbde0d16af87593798beb5e7b66af to your computer and use it in GitHub Desktop.

Select an option

Save JeongHoJeong/c1adbde0d16af87593798beb5e7b66af to your computer and use it in GitHub Desktop.
const path = require('path')
const fs = require('fs')
const config = require('./config.json')
function getFiles(rootPath, regex) {
if (fs.existsSync(rootPath)) {
const stats = fs.statSync(rootPath)
if (stats.isDirectory()) {
const files = fs.readdirSync(rootPath)
return files.reduce((reduction, filename) => {
const filePath = path.join(rootPath, filename)
const stats = fs.statSync(filePath)
if (stats.isDirectory()) {
return reduction.concat([getFiles(filePath, regex)])
} else if (stats.isFile() && regex.test(filePath)) {
return reduction.concat([filePath])
} else {
return reduction
}
}, []).reduce((reduction, file) => {
if (file instanceof Array) {
return reduction.concat(file)
} else {
return reduction.concat([file])
}
}, [])
} else {
throw Error(`${rootPath} is not a directory.`)
}
} else {
throw Error(`${rootPath} does not exist.`)
}
}
class Color {
constructor(filename, offset, string) {
this.filename = filename
this.offset = offset
this.string = string
}
}
class ColorOccurrence {
constructor(colorString, occurrence = 1) {
this.colorString = colorString
this.occurrence = occurrence
}
increaseOccurrence(occurrence = 1) {
this.occurrence += occurrence
}
}
function extractColors(filename) {
if (fs.existsSync(filename)) {
const contents = fs.readFileSync(filename, 'utf8')
const regex = /(#[0-9a-fA-F]{3,6})\b/g
const result = []
contents.replace(regex, (match, p1, offset) => {
result.push(new Color(filename, offset, match))
})
return result
} else {
throw Error(`${filename} does not exist.`)
}
}
function main() {
if (!config.paths) {
throw Error('config.json does not have `paths` key.')
} else if (!(config.paths instanceof Array)) {
throw Error('`paths` in config.json must be an array.')
}
const targets = config.paths
.map(path => getFiles(path, /\.(scss|js)$/))
.reduce((reduction, array) => reduction.concat(array), [])
console.log(`Total ${targets.length} files are found.`)
const colors = targets
.map(target => extractColors(target))
.reduce((reduction, array) => reduction.concat(array), [])
const colorSet = {}
colors.forEach(color => {
const colorName = color.string
if (colorName in colorSet) {
colorSet[colorName].increaseOccurrence()
} else {
colorSet[colorName] = new ColorOccurrence(colorName)
}
})
const result =
Object.keys(colorSet).sort().reduce((reduction, colorName) => {
return `${reduction}\n${colorName},${colorSet[colorName].occurrence}`
}, '')
const resultFilePath = path.resolve(__dirname, 'result.csv')
fs.writeFileSync(resultFilePath, result)
console.log(`Result file generated: ${resultFilePath}`)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment