Skip to content

Instantly share code, notes, and snippets.

@rabehasy
Last active December 17, 2019 14:03
Show Gist options
  • Save rabehasy/4f4ce205c210fa981d851d1b9711596f to your computer and use it in GitHub Desktop.
Save rabehasy/4f4ce205c210fa981d851d1b9711596f to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/**
* This script can be run on windows machine
* It simulates the command 'cp -Rs' in linux
* This command create a symlink for each files from one folder to another one
*/
/**
* package.json
{
"dependencies": {
"chalk": "^2.4.2",
"commander": "^2.19.0",
"is-admin": "^2.1.1"
}
"bin": {
"cpRs": "./cprs.js"
}
}
* Go to folder and run npm link
* cd "C:\scripts\";
* npm link
*/
const isAdmin = require('is-admin');
const exec = require('child_process').exec;
const chalk = require('chalk');
const logBgYellow = chalk.bgYellowBright.black;
const logbgRedWhite = chalk.bgRed.white;
const path = require('path');
const fs = require('fs');
// RUN if is admin
isAdmin().then(admin => {
if (!admin) {
console.log(
'\n' +
logBgYellow(
'\n \n' +
'\n' + ' This script must be run on administrator mode ' +
'\n \n'
)
+ '\n');
// die exit
process.exit();
}
// If user mode is Admin - everything is good
if (admin) {
if (process.argv.length != 4) {
console.log(
'\n'
+ logBgYellow('\n ' + ' Use: cpRs "{source}" "{dest}" [option]')
+ logBgYellow('\n ' + ' Example: cpRs "C:\\www\\mySrcFolder" "C:\\www\\myDestFolder"')
+ logBgYellow('\n ' + ' Example (with verbose option): cpRs "C:\\www\\mySrcFolder" "C:\\www\\myDestFolder" -v')
+ logBgYellow('\n ' + ' Folders {source} and {dest} must exist.')
);
process.exit();
}
// Source
let src = process.argv[2];
// Destination
let dest = process.argv[3];
// Verbose option
let isVerbose = process.argv[4] === '-v';
// Source folder does not exists
if( !fs.existsSync(src) ) {
console.log(
'\n'
+ logbgRedWhite('\n Folder "' + src + "\" does not exists.")
);
process.exit();
}
// Dest folder does not exists
if( !fs.existsSync(dest) ) {
console.log(
'\n'
+ logbgRedWhite('\n Folder "' + dest + "\" does not exists.")
);
process.exit();
}
// Log
console.log("src: ",src);
console.log("dest: ",dest);
console.log("verbose: ", isVerbose);
// Create empties folders from {src} to {dest}
let cmd = 'xcopy /t /e "' + src + '" "' + dest + '"';
exec(cmd);
// Create symlink of each files from {src} to {dest}
cmd = 'dir /s/b "' + src + '"';
exec(cmd, (error, stdout, stderr) => {
// Command return
let consoleContent = stdout.trim();
consoleContent.split("\n").map((v) => {
// Clean up
v = v.substr(src.length+1);
v = v.trim();
// It is a folder
let isDir = fs.lstatSync(src + "\\" + v).isDirectory();
// If is file
if (!isDir) {
// Create symlink if dest file does not exists
let srcFile = src + "\\" + v.trim();
let destFile = dest + "\\" + v.trim();
if (!fs.existsSync(destFile)) {
// Show or not console
if (isVerbose) {
console.log("mklink " + destFile + " " + srcFile);
}
fs.symlinkSync(srcFile, destFile);
}
}
});
});
}
});
// Usage
cpRs "C:\www\source" "D:\destination"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment