Last active
August 29, 2015 14:20
-
-
Save stefandz/8ea4ac5ef43cb2cf957f to your computer and use it in GitHub Desktop.
Locates the user's Arduino 1.5.X or earlier sketchbook folder
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
// locates the user's Arduino 1.5.X or earlier sketchbook folder | |
// 1.5.X folders are returned via STDOUT with a return code of 0 | |
// if only a pre 1.5.X folder can be found, it is also returned via STDOUT, but with a return code of 2 | |
// if no folder can be found, a return code of 1 is returned and STDOUT lets you know that no folder can be found | |
// tested and known working on: | |
// OSX 10.9.5 | |
// Windows 7 Ultimate SP1 (build 7601) | |
// Windows XP 32-bit SP3 (build 2600) | |
// Windows 8.1 64 bit (build 9600) | |
// Ubuntu x86 64 12.04.5 LTS | |
// Ubuntu x86 64 14.04 LTS | |
// Ubuntu x86 14.04 LTS | |
var os = require('os'); | |
var osenv = require('osenv'); | |
var fs = require('fs'); | |
var sketchbookPath = "sketchbook.path="; | |
if(os.type()=="Darwin"){ | |
var newPathToPrefs = osenv.home() + "/Library/Arduino15/preferences.txt"; | |
var oldPathToPrefs = osenv.home() + "/Library/Arduino/preferences.txt"; | |
} else if(os.type()=="Windows_NT") { | |
var newPathToPrefs = process.env.APPDATA + "\\Arduino15\\preferences.txt"; | |
var oldPathToPrefs = process.env.APPDATA + "\\Arduino\\preferences.txt"; | |
} else if(os.type()=="Linux"){ | |
var newPathToPrefs = osenv.home() + "/.arduino15/preferences.txt"; | |
var oldPathToPrefs = osenv.home() + "/.arduino/preferences.txt"; | |
} else { | |
quitCantFind; | |
} | |
if(newPathToPrefs!=""){ | |
var oldPrefsExists = false; | |
var newPrefsExists = false; | |
if(fs.existsSync(oldPathToPrefs, fs.F_OK)){ | |
oldPrefsExists = true; | |
} | |
if(fs.existsSync(newPathToPrefs, fs.F_OK)){ | |
newPrefsExists = true; | |
} | |
var LineByLineReader; | |
if(oldPrefsExists && !newPrefsExists){ | |
LineByLineReader = require('line-by-line'), lr = new LineByLineReader(oldPathToPrefs); | |
} else if(newPrefsExists){ | |
LineByLineReader = require('line-by-line'), lr = new LineByLineReader(newPathToPrefs); | |
} else { | |
quitCantFind(); | |
} | |
lr.on('error', function (err) { | |
quitCantFind(); | |
// 'err' contains error object | |
}); | |
lr.on('line', function (line) { | |
if(line.indexOf(sketchbookPath)>-1){ | |
if(oldPrefsExists && !newPrefsExists){ | |
quitOnlyOld(line.substr(line.indexOf(sketchbookPath) + sketchbookPath.length).trim()); | |
} else if(newPrefsExists){ | |
quitGood(line.substr(line.indexOf(sketchbookPath) + sketchbookPath.length).trim()); | |
} else { | |
quitCantFind(); | |
} | |
} | |
// 'line' contains the current line without the trailing newline character. | |
}); | |
lr.on('end', function () { | |
quitCantFind(); // should never get here - means we reached the end of the file without | |
// finding the sketchbook path | |
// All lines are read, file is closed now. | |
}); | |
} else { | |
quitCantFind(); | |
} | |
function quitOnlyOld(path){ | |
console.log(path); | |
process.exit(2); | |
} | |
function quitCantFind(){ | |
console.log("Can't locate Arduino Sketchbook folder"); | |
process.exit(1); | |
} | |
function quitGood(path){ | |
console.log(path); | |
process.exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment