Skip to content

Instantly share code, notes, and snippets.

@akhilome
Created January 7, 2020 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhilome/1fbcfd95a4fa4b631794dc236931ecb6 to your computer and use it in GitHub Desktop.
Save akhilome/1fbcfd95a4fa4b631794dc236931ecb6 to your computer and use it in GitHub Desktop.
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