Extract object functions to static

main
Kyle Taylor 4 years ago
parent 67f690014e
commit ebc24e2daf

@ -16,64 +16,73 @@ const appPaths = () => {
} }
} }
// Main script /**
fs.readdirSync(appPaths().iconPath).forEach((fileName) => {
const image = {
id: fileName,
path: `${appPaths().iconPath}/${fileName}`,
convertPath: `${appPaths().convertPath}/${fileName}`,
/**
* Copy files from source to destination. * Copy files from source to destination.
* @param source * @param source
* @param destination * @param destination
*/ */
copyFiles(source, destination) { function copyFiles(source, destination) {
// File destination will be created or overwritten by default. // File destination will be created or overwritten by default.
fs.copyFileSync(source, destination) fs.copyFileSync(source, destination)
console.log(`Copied icon: ${this.id}`) // console.log(`${source} -> ${destination}`)
}, }
/**
/**
* Get extension of image file. * Get extension of image file.
* @returns {string} * @returns {string}
*/ */
getFileExtension() { function getFileExtension(filePath) {
return path.extname(this.path) return path.extname(filePath)
}, }
/**
/**
* Get base name of image file. * Get base name of image file.
* @returns {string} * @returns {string}
*/ */
getFileName() { function getFileName(filePath) {
return path.basename(this.path, this.getFileExtension()) return path.basename(filePath, getFileExtension(filePath))
}, }
getConvertFileName() {
const name = this.getFileName() function getConvertFileName(filePath) {
const name = getFileName(filePath)
return `${appPaths().convertPath}/${name}.png` return `${appPaths().convertPath}/${name}.png`
}, }
/**
/**
* Check if converted image exists * Check if converted image exists
* @returns {boolean} * @returns {boolean}
*/ */
checkFileExists(imagePath) { function checkFileExists(imagePath) {
const fileExists = fs.existsSync(imagePath) const fileExists = fs.existsSync(imagePath)
return fileExists return fileExists
}, }
checkIfFile() {
return fs.statSync(this.path).isFile() function checkIfFile(filePath) {
}, return fs.statSync(filePath).isFile()
diffFiles(fileOne, fileTwo) { }
function diffFiles(fileOne, fileTwo) {
const f1 = fs.readFileSync(fileOne) const f1 = fs.readFileSync(fileOne)
const f2 = fs.readFileSync(fileTwo) const f2 = fs.readFileSync(fileTwo)
return f1.equals(f2) return f1.equals(f2)
}, }
dateModified(file) {
function dateModified(file) {
return fs.statSync(file).mtime return fs.statSync(file).mtime
}, }
dateDiff(file) {
function dateDiff(file) {
const now = new Date().getTime() const now = new Date().getTime()
const then = this.dateModified(file).getTime() const then = dateModified(file).getTime()
return Math.round(Math.abs((then - now) / 86400000)) return Math.round(Math.abs((then - now) / 86400000))
}, }
// Main script
fs.readdirSync(appPaths().iconPath).forEach((fileName) => {
const image = {
id: fileName,
path: `${appPaths().iconPath}/${fileName}`,
convertPath: `${appPaths().convertPath}/${fileName}`,
async convertAndCopy() { async convertAndCopy() {
await convertFile(this.path, { await convertFile(this.path, {
height: 32, height: 32,
@ -85,15 +94,15 @@ fs.readdirSync(appPaths().iconPath).forEach((fileName) => {
}, },
processFile() { processFile() {
// Setup variables. // Setup variables.
const ext = this.getFileExtension() const ext = getFileExtension(this.path)
// If SVG, run checks. // If SVG, run checks.
if (ext === '.svg') { if (ext === '.svg') {
// Check if converted file exists. // Check if converted file exists.
const convertFilePath = this.getConvertFileName() const convertFilePath = getConvertFileName(this.path)
if (this.checkFileExists(convertFilePath)) { if (checkFileExists(convertFilePath)) {
// If file has changed in past 7 days. // If file has changed in past 7 days.
if (this.dateDiff(this.path) > 8) { if (dateDiff(this.path) > 8) {
console.log(`File exists, skipping: ${this.id}`) console.log(`File exists, skipping: ${this.id}`)
return null return null
} }
@ -103,8 +112,8 @@ fs.readdirSync(appPaths().iconPath).forEach((fileName) => {
} else { } else {
// If PNG or other, just copy the file as-is. // If PNG or other, just copy the file as-is.
// eslint-disable-next-line no-lonely-if // eslint-disable-next-line no-lonely-if
if (this.checkIfFile()) { if (checkIfFile(this.path)) {
this.copyFiles(this.path, this.convertPath) copyFiles(this.path, this.convertPath)
} else { } else {
console.info('Not a file, skipping...') console.info('Not a file, skipping...')
} }