node-environ
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -12,5 +12,5 @@ { | ||
"program": "${workspaceFolder}/index.js", | ||
"args": ["--config", "example/.environ", "--use", "profile2"] | ||
"args": ["backup", "profile2", "--config", "example/.environ"] | ||
} | ||
] | ||
} |
185
index.js
@@ -12,6 +12,10 @@ #!/usr/bin/env node | ||
const command = argv._[0] | ||
const useProfile = argv._[1] | ||
// 0. check params | ||
const confPath = argv.config | ||
? path.resolve(cwd, argv.config) | ||
: path.resolve(cwd, '.environ') | ||
const confPath = argv.config ? | ||
path.resolve(cwd, argv.config) : | ||
path.resolve(cwd, '.environ') | ||
if (!fs.existsSync(confPath)) | ||
@@ -27,11 +31,10 @@ throw new Error('Missing `.environ` project file. ' + confPath) | ||
throw new Error('target path not exists!') | ||
} else if (!argv.use) { | ||
throw new Error('Usage: environ --use <profile-id>') | ||
} | ||
} else if (!useProfile) { | ||
throw new Error('Usage: environ <command> <profile-id>') | ||
} | ||
const useProfile = argv.use | ||
const profilePath = path.resolve(sourcesPath, useProfile) | ||
if (!fs.existsSync(profilePath)) { | ||
throw new Error('directory profile <profile-id> ' + useProfile + ' not exists. use real path') | ||
} else if(!environ.profiles[useProfile]){ | ||
} else if (!environ.profiles[useProfile]) { | ||
throw new Error('profile ' + useProfile + ' is not defined in .environ conf') | ||
@@ -45,48 +48,68 @@ } else { | ||
// 1. action | ||
(environ.__current // make backup if __current is set | ||
? ensureDir(path.resolve(sourcesPath, environ.__current, '__backup')) | ||
.then(()=>{ | ||
return copyProfile(environ.__current, // first backup profile itself | ||
path.resolve(sourcesPath, environ.__current), | ||
path.resolve(sourcesPath, environ.__current, '__backup')) | ||
}) | ||
.then(()=>{ // copy current profile | ||
return copyProfile(environ.__current, | ||
targetPath, | ||
path.resolve(sourcesPath, environ.__current)) | ||
}) | ||
.then(()=>{ // cleanup current | ||
switch (command) { | ||
case 'use': | ||
use() | ||
break | ||
case 'backup': | ||
backup(useProfile) | ||
break | ||
default : | ||
console.log('Wrong or absent action name'); | ||
} | ||
function use() { | ||
return (environ.__current ? // make backup if __current is set | ||
backup(environ.__current).then(() => { // cleanup current | ||
return cleanupProfile(environ.__current, targetPath) | ||
}) | ||
: Promise.resolve() | ||
).then(()=>{ | ||
// push in profile | ||
return copyProfile(useProfile, profilePath, targetPath) | ||
}) | ||
.then(()=>{ | ||
}) : | ||
Promise.resolve() | ||
).then(() => { | ||
// push in profile | ||
return copyProfile(useProfile, profilePath, targetPath) | ||
}) | ||
.then(() => { | ||
//end | ||
environ.__current = useProfile | ||
fs.writeFileSync(confPath, JSON.stringify(environ, null, ' ')) | ||
}) | ||
.catch(error => { | ||
console.error(error.stack) | ||
}) | ||
//end | ||
environ.__current = useProfile | ||
fs.writeFileSync(confPath, JSON.stringify(environ, null, ' ')) | ||
}) | ||
} | ||
function backup(profile) { | ||
const backupDir = path.resolve(sourcesPath, profile, '__backup', profile + '_' +Date.now()) | ||
return ensureDir(backupDir) | ||
.then(() => { | ||
return copyProfile(profile, // first backup profile itself | ||
path.resolve(sourcesPath, profile), | ||
backupDir) | ||
}) | ||
.then(() => { // copy current profile | ||
return copyProfile(profile, | ||
targetPath, | ||
path.resolve(sourcesPath, profile)) | ||
}) | ||
.catch(error => { | ||
console.error(error.stack) | ||
}) | ||
} | ||
// ==================================== | ||
// functions | ||
function copyProfile(profile, from, to ){ | ||
function copyProfile(profile, from, to) { | ||
debug('copyProfile', ['profile', 'from', 'to'], arguments) | ||
return Promise.all(environ.profiles[profile].map( item => { | ||
return Promise.all(environ.profiles[profile].map(item => { | ||
const itemPath = path.resolve(from, item) | ||
if(!fs.existsSync(itemPath)) console.log('Warning: source ' + itemPath + ' from ' + profile+ ' not exists') | ||
if (!fs.existsSync(itemPath)) console.log('Warning: source ' + itemPath + ' from ' + profile + ' not exists') | ||
const itemStat = fs.statSync(itemPath) | ||
const itemTarget = path.resolve(to, item) | ||
if(itemStat.isDirectory()){ | ||
if (itemStat.isDirectory()) { | ||
return copyDirRecursive(itemPath, itemTarget) | ||
@@ -96,3 +119,3 @@ } else { | ||
} | ||
})) | ||
})) | ||
} | ||
@@ -102,5 +125,5 @@ | ||
debug('copyDirRecursive', ['from', 'to'], arguments) | ||
return ensureDir(to).then(()=>{ | ||
return Promise.all(fs.readdirSync(from).map( it =>{ | ||
return ensureDir(to).then(() => { | ||
return Promise.all(fs.readdirSync(from).map(it => { | ||
const itsPath = path.resolve(from, it); | ||
@@ -112,3 +135,3 @@ const itsTarget = path.resolve(to, it); | ||
} else { | ||
return copyFile(itsPath, itsTarget) | ||
return copyFile(itsPath, itsTarget) | ||
} | ||
@@ -119,6 +142,7 @@ })) | ||
function copyFile(from, to) { | ||
function copyFile(from, to) { | ||
debug('copyFile', ['from', 'to'], arguments) | ||
return new Promise(function(resolve, reject) { | ||
return ensureDir(path.dirname(to)).then(()=>{ | ||
return new Promise((resolve, reject) => { | ||
var rd = fs.createReadStream(from); | ||
@@ -128,20 +152,23 @@ rd.on('error', rejectCleanup); | ||
wr.on('error', rejectCleanup); | ||
function rejectCleanup(err) { | ||
rd.destroy(); | ||
wr.end(); | ||
reject(err); | ||
rd.destroy(); | ||
wr.end(); | ||
reject(err); | ||
} | ||
wr.on('finish', resolve); | ||
rd.pipe(wr); | ||
}); | ||
}); | ||
}) | ||
} | ||
function cleanupProfile(profile, dir){ | ||
function cleanupProfile(profile, dir) { | ||
debug('cleanupProfile', ['profile', 'dir'], arguments) | ||
return Promise.all(environ.profiles[profile].map( item => { | ||
return Promise.all(environ.profiles[profile].map(item => { | ||
const itemPath = path.resolve(dir, item) | ||
const itemStat = fs.statSync(itemPath) | ||
if(itemStat.isDirectory()){ | ||
if (itemStat.isDirectory()) { | ||
return cleanupDirRecursive(itemPath) | ||
@@ -151,8 +178,8 @@ } else { | ||
return Promise.resolve() | ||
} | ||
} | ||
})) | ||
} | ||
function ensureDir(dir){ | ||
if(!fs.existsSync(dir)){ | ||
function ensureDir(dir) { | ||
if (!fs.existsSync(dir)) { | ||
console.log('Warning: dir ' + dir + ' doesnt exist. create'); | ||
@@ -164,28 +191,28 @@ mkdirp.sync(dir) | ||
function cleanupDirRecursive(dir){ | ||
function cleanupDirRecursive(dir) { | ||
debug('cleanupDirRecursive', ['dir'], arguments) | ||
return Promise.all(fs.readdirSync(dir).map(item=>{ | ||
const itemPath = path.resolve(dir, item) | ||
const itemStat = fs.statSync(itemPath) | ||
return Promise.all(fs.readdirSync(dir).map(item => { | ||
const itemPath = path.resolve(dir, item) | ||
const itemStat = fs.statSync(itemPath) | ||
if(itemStat.isDirectory()){ | ||
return cleanupDirRecursive(itemPath) | ||
} else { | ||
fs.unlinkSync(itemPath) | ||
if (itemStat.isDirectory()) { | ||
return cleanupDirRecursive(itemPath) | ||
} else { | ||
fs.unlinkSync(itemPath) | ||
return Promise.resolve() | ||
} | ||
})) | ||
.then(() => { | ||
fs.rmdirSync(dir) | ||
return Promise.resolve() | ||
} | ||
})) | ||
.then(()=>{ | ||
fs.rmdirSync(dir) | ||
return Promise.resolve() | ||
}) | ||
}) | ||
} | ||
function debug(title, names, values){ | ||
function debug(title, names, values) { | ||
console.log('\n' + title); | ||
names.forEach((it, idx)=>{ | ||
console.log( it.padStart(8) + ': ' + values[idx] ); | ||
}) | ||
names.forEach((it, idx) => { | ||
console.log(it.padStart(8) + ': ' + values[idx]); | ||
}) | ||
} |
{ | ||
"name": "node-environ", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "environment switcher", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2001126
35
381