Created
January 7, 2020 17:31
-
-
Save akhilome/1fbcfd95a4fa4b631794dc236931ecb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const { join } = require('path'); | |
const { promisify } = require('util'); | |
const shell = require('child_process').execSync; | |
const readDir = promisify(fs.readdir); | |
const tempPath = join(__dirname, 'temp'); | |
const distPath = join(__dirname, 'dist'); | |
readDir(__dirname) | |
.then(res => res.filter(dir => /[0-9]{2}/g.test(dir))) | |
.then(_dirs => { | |
/** | |
* โ make temp && cp matched dirs to temp | |
*/ | |
_dirs.forEach(_dir => { | |
shell(`mkdir -p ${tempPath}`); | |
shell(`cp -rf ${_dir} ${tempPath}`); | |
}); | |
return _dirs; | |
}) | |
.then(dirs => | |
/** | |
* โ create object maping each month to subfolders | |
* exp. ๐๐พ | |
* { | |
* '08': ['18', '19', '20', ...], | |
* '09': ['01', '02', '03', ...] | |
* } | |
*/ | |
dirs | |
.map(dir => ({ [dir]: fs.readdirSync(join(__dirname, 'temp', dir)) })) | |
.reduce( | |
(acc, cur) => ({ | |
...acc, | |
[Object.keys(cur)[0]]: Object.values(cur)[0] | |
}), | |
{} | |
) | |
) | |
.then(res => { | |
/** | |
* โ create level 2 object maping imgs to each sub dir | |
* exp. ๐๐พ | |
* { | |
* '08': { | |
* '18': ['img1.ext', 'img2.ext', 'img3.ext', ...], | |
* '19': ['img1.ext', 'img2.ext', 'img3.ext', ...], | |
* ... | |
* }, | |
* ... | |
* } | |
*/ | |
const dirss = Object.keys(res) | |
.map(k => | |
res[k] | |
.map(dir => ({ | |
[`${dir}`]: fs.readdirSync(join(__dirname, 'temp', k, dir, 'shots')) | |
})) | |
.reduce( | |
(acc, cur) => ({ | |
...acc, | |
[Object.keys(cur)[0]]: Object.values(cur)[0] | |
}), | |
{} | |
) | |
) | |
.reduce( | |
(acc, cur, idx) => ({ | |
...acc, | |
[Object.keys(res)[idx]]: cur | |
}), | |
{} | |
); | |
return dirss; | |
}) | |
.then(res => { | |
/** | |
* โ cp files to dist & rename | |
*/ | |
try { | |
fs.accessSync(distPath, fs.constants.F_OK); | |
console.log('๐คฎ purge previous dist'); | |
shell(`rm -r ${distPath}`); | |
} catch (error) { | |
console.warn("โ ๏ธ dist folder doesn't exist. Skipping initial purge"); | |
} | |
shell(`mkdir ${distPath}`); | |
Object.keys(res).forEach(key => | |
Object.keys(res[key]).forEach(kKey => { | |
res[key][kKey].forEach(k => { | |
const filePath = join(__dirname, 'temp', key, kKey, 'shots', k); | |
const newFileName = `${key}_${kKey}_${k}`; | |
console.log(`โ copying ${newFileName}`); | |
// cp | |
shell(`cp ${filePath} ${distPath}`); | |
// rename | |
shell( | |
`mv ${join(__dirname, 'dist', k)} ${join( | |
__dirname, | |
'dist', | |
newFileName | |
)}` | |
); | |
}); | |
}) | |
); | |
}) | |
.then(() => { | |
/** | |
* โ sort files | |
*/ | |
const transportationPath = `${distPath}/transportation`; | |
const foodPath = `${distPath}/food`; | |
const entertainmentPath = `${distPath}/entertainment`; | |
shell(`mkdir -p ${transportationPath}`); | |
shell(`mkdir -p ${foodPath}`); | |
shell(`mkdir -p ${entertainmentPath}`); | |
console.log('๐ง sorting files'); | |
const imgFiles = fs.readdirSync(distPath).filter(p => /\..{3,4}$/g.test(p)); | |
// food | |
imgFiles | |
.filter( | |
img => | |
/(-j-|titos|parfait|ma?rt|ma?ll)/gi.test(img) && | |
!/(bolt|uber)/gi.test(img) | |
) | |
.forEach(fileName => shell(`mv ${distPath}/${fileName} ${foodPath}`)); | |
// transport | |
imgFiles | |
.filter(img => /(bolt|uber)/gi.test(img)) | |
.forEach(fileName => | |
shell(`mv ${distPath}/${fileName} ${transportationPath}`) | |
); | |
// entertainment | |
imgFiles | |
.filter( | |
img => | |
!/(-j-|titos|parfait|ma?rt|ma?ll)/gi.test(img) && | |
!/(bolt|uber)/gi.test(img) | |
) | |
.forEach(fileName => | |
shell(`mv ${distPath}/${fileName} ${entertainmentPath}`) | |
); | |
}) | |
.catch(err => { | |
console.error(err); | |
}) | |
.finally(() => { | |
// cleanup | |
console.log('๐ฅ removing temp files'); | |
shell(`rm -r ${tempPath}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment