Created
September 7, 2017 00:24
-
-
Save tilleryd/49163fdc8631b816257b93d39cde45ad to your computer and use it in GitHub Desktop.
Retire obsolete files/directories to another directory.
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
/** | |
* retire.js - Retire obsolete files/directories to another directory. | |
*/ | |
const path = require('path'); | |
const fs = require('fs'); | |
const fse = require('fs-extra'); | |
const chalk = require('chalk'); | |
const appDirectory = fs.realpathSync(process.cwd()); | |
const resolveApp = relativePath => path.resolve(appDirectory, relativePath); | |
// Array to store all the files/dirs we want to retire. | |
const retirees = [ | |
'buildConfig', | |
'gulpTasks', | |
'src/system.config.js', | |
'src/jspm_packages', | |
'gulpfile.js', | |
'karma.conf.js' | |
], | |
succeeded = [], | |
failed = []; | |
async function retire() { | |
// Make a directory to store retired stuff. We don't want to delete them for | |
// all time because we may need to reference an old configuration. | |
const retired = await fse.mkdirs(resolveApp('retired')); | |
if (!fse.pathExists(retired)) error('Error creating retired dir'); | |
for (let r in retirees) { | |
const src = resolveApp(retirees[r]), | |
dest = resolveApp('retired/' + retirees[r]); | |
try { | |
// First, make a copy of the files/dirs we want to retire. | |
await fse.copy(src, dest); | |
succeeded.push(`${src} moved to ${dest}`); | |
// Second, if the file/dir was successfully copied, delete the original. | |
await fse.remove(src); | |
} catch (err) { | |
failed.push(error(err)); | |
} | |
} | |
} | |
function message(msg, type) { | |
switch (type) { | |
case 'error': | |
return chalk.red(msg); | |
case 'warn': | |
return chalk.yellow(msg); | |
default: | |
return chalk.green(msg); | |
} | |
} | |
function error(err) { | |
if (err.hasOwnProperty('code')) { | |
switch (err.code) { | |
case 'ENOENT': | |
return message(`No such file or directory ${err.path}`, 'error'); | |
default: | |
return message('An unkown error has occurred.', 'error'); | |
} | |
} else { | |
return message(err, 'error'); | |
} | |
} | |
function showSucceeded() { | |
if (succeeded.length === 0) return ''; | |
let msg = message( | |
`${succeeded.length} files/dirs were successfully retired.` | |
); | |
for (let s in succeeded) msg += message(`\n${succeeded[s]}`); | |
return msg; | |
} | |
function showFailed() { | |
if (failed.length === 0) return ''; | |
let msg = message( | |
`${failed.length} files/dirs were not successfully retired.`, | |
'error' | |
); | |
for (let f in failed) msg += message(`\n${failed[f]}`); | |
return msg; | |
} | |
retire().then(() => { | |
console.log( | |
`\nRetirement has completed. Here are the results.\n\n${showSucceeded()}\n\n${showFailed()}\n` | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment