Last active
July 4, 2017 23:13
-
-
Save uchcode/5f7a154e124288c9bde755f015d12df0 to your computer and use it in GitHub Desktop.
ChocoShell.js
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
| ChocoFileManager = { | |
| pwd() { | |
| return $.NSFileManager.defaultManager.currentDirectoryPath.js | |
| }, | |
| cd(path='~') { | |
| let p = $(path).stringByStandardizingPath.js | |
| let r = $.NSFileManager.defaultManager.changeCurrentDirectoryPath(p) | |
| if (!r) { | |
| throw new Error(`cd(): No such file or directory: "${path}"`) | |
| } | |
| return r | |
| }, | |
| mkfile(path) { | |
| if ($.NSFileManager.defaultManager.fileExistsAtPath($(path).stringByStandardizingPath.js)) | |
| throw new Error(`mkfile(): Path is exists: "${path}"`) | |
| let p = $(path).stringByStandardizingPath.js | |
| let c = $.NSData.data //zero data | |
| let a = $() | |
| let r = $.NSFileManager.defaultManager.createFileAtPathContentsAttributes(p, c, a) | |
| if (!r) { | |
| throw new Error(`mkfile(): Create file failed: "${path}"`) | |
| } | |
| return r | |
| }, | |
| mkdir(path, createIntermediatesFlag=1) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let i = createIntermediatesFlag ? 1 : 0 | |
| let a = $() | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(p, i, a, e) | |
| if (!e.isNil()) { | |
| let s1 = 'mkdir(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| rm(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.removeItemAtPathError(p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'rm(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| mv(at, to) { | |
| let a = $(at).stringByStandardizingPath.js | |
| let t = $(to).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.moveItemAtPathToPathError(a, t, e) | |
| if (!e.isNil()) { | |
| let s1 = 'mv(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| cp(at, to) { | |
| let a = $(at).stringByStandardizingPath.js | |
| let t = $(to).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.copyItemAtPathToPathError(a, t, e) | |
| if (!e.isNil()) { | |
| let s1 = 'cp(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| link(at, to) { | |
| let a = $(at).stringByStandardizingPath.js | |
| let t = $(to).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.linkItemAtPathToPathError(a, t, e) | |
| if (!e.isNil()) { | |
| let s1 = 'link(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| ln(at, to) { | |
| let a = $(to).stringByStandardizingPath.js | |
| let w = $(at).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.createSymbolicLinkAtPathWithDestinationPathError(a, w, e) | |
| if (!e.isNil()) { | |
| let s1 = 'ln(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| chmod(value, path) { | |
| let a = $({'NSFilePosixPermissions':value}) | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.setAttributesOfItemAtPathError(a, p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'chmod(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| chown(user, path) { | |
| let u = user.split(':') | |
| let d = {NSFileOwnerAccountName:u[0]} | |
| if (u[1]) d.NSFileGroupOwnerAccountName = u[1] | |
| let a = $(d) | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.setAttributesOfItemAtPathError(a,p,e) | |
| if (!e.isNil()) { | |
| let s1 = 'chown(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| chgrp(group, path) { | |
| let a = $({NSFileGroupOwnerAccountName:group}) | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.setAttributesOfItemAtPathError(a,p,e) | |
| if (!e.isNil()) { | |
| let s1 = 'chgrp(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r | |
| }, | |
| readlink(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let r = $.NSFileManager.defaultManager.destinationOfSymbolicLinkAtPathError(p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'readlink(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return r.isNil() ? '' : r.js | |
| }, | |
| isExists(path) { | |
| return $.NSFileManager.defaultManager.fileExistsAtPath($(path).stringByStandardizingPath.js) | |
| }, | |
| isReadable(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| if (!this.isExists(p)) { | |
| throw new Error(`isReadable(): No such file or directory: "${path}"`) | |
| } | |
| return $.NSFileManager.defaultManager.isReadableFileAtPath(p) | |
| }, | |
| isWritable(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| if (!this.isExists(p)) { | |
| throw new Error(`isWritable(): No such file or directory: "${path}"`) | |
| } | |
| return $.NSFileManager.defaultManager.isWritableFileAtPath(p) | |
| }, | |
| isDeletable(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| if (!this.isExists(p)) { | |
| throw new Error(`isDeletable(): No such file or directory: "${path}"`) | |
| } | |
| return $.NSFileManager.defaultManager.isDeletableFileAtPath(p) | |
| }, | |
| isExecutable(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| if (!this.isExists(p)) { | |
| throw new Error(`isExecutable(): No such file or directory: "${path}"`) | |
| } | |
| return $.NSFileManager.defaultManager.isExecutableFileAtPath(p) | |
| }, | |
| isDir(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let a = $.NSFileManager.defaultManager.attributesOfItemAtPathError(p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'isDir(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js | |
| }, | |
| isFile(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let a = $.NSFileManager.defaultManager.attributesOfItemAtPathError(p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'isFile(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return !(a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js) | |
| }, | |
| isSymbolicLink(path) { | |
| let p = $(path).stringByStandardizingPath.js | |
| let e = $() | |
| let a = $.NSFileManager.defaultManager.attributesOfItemAtPathError(p, e) | |
| if (!e.isNil()) { | |
| let s1 = 'isSymbolicLink(): ' | |
| let s2 = e.localizedDescription.js | |
| let s3 = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : '' | |
| throw new Error(s1+s2+s3) | |
| } | |
| return a.objectForKey($.NSFileType).js === $.NSFileTypeSymbolicLink.js | |
| }, | |
| } | |
| class PathItemObject { | |
| constructor(path) { | |
| if (path==='') throw new Error('PathItemObject: Argument value is empty') | |
| if (!path) throw new Error('PathItemObject: Argument is required.') | |
| if (Object.prototype.toString.call(path).slice(8,-1).toLowerCase()!=='string') throw new Error('PathItemObject: Argument must be a string') | |
| // | |
| this.pathString = path | |
| let $p = $(path).stringByStandardizingPath.js | |
| if (/^\//.test($p)) { | |
| this.path = $($p).stringByStandardizingPath.js | |
| } else { | |
| this.path = $($.NSFileManager.defaultManager.currentDirectoryPath.js+'/'+$p).stringByStandardizingPath.js | |
| } | |
| } | |
| // Computed property | |
| get isExists() { | |
| if (!this.pathString) { | |
| $.NSLog('isExists: Invalid value: %@', JSON.stringify(this)) | |
| return false | |
| } | |
| return ChocoFileManager.isExists(this.path) | |
| } | |
| get isReadable() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isReadable(this.path) | |
| } | |
| get isWritable() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isWritable(this.path) | |
| } | |
| get isDeletable() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isDeletable(this.path) | |
| } | |
| get isExecutable() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isExecutable(this.path) | |
| } | |
| get isDir() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isDir(this.path) | |
| } | |
| get isFile() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isFile(this.path) | |
| } | |
| get isSymbolicLink() { | |
| if (!this.isExists) return false | |
| return ChocoFileManager.isSymbolicLink(this.path) | |
| } | |
| get destinationOfSymbolicLink() { | |
| return ChocoFileManager.readlink(this.path) | |
| } | |
| // Update | |
| mkfile() { | |
| try { | |
| return ChocoFileManager.mkfile(this.path) ? this : undefined | |
| } catch(e) { | |
| throw new Error('mkfile(): ' + e.toString()) | |
| } | |
| } | |
| mkdir() { | |
| try { | |
| return ChocoFileManager.mkdir(this.path) ? this : undefined | |
| } catch(e) { | |
| throw new Error('mkdir(): ' + e.toString()) | |
| } | |
| } | |
| rm() { | |
| try { | |
| return ChocoFileManager.rm(this.path) ? this : undefined | |
| } catch(e) { | |
| throw new Error('rm(): ' + e.toString()) | |
| } | |
| } | |
| mv(to) { | |
| try { | |
| if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase()==='function') { | |
| return this.mv(to(this)) | |
| } | |
| let $to = new PathItemObject(to) | |
| let a = this.path | |
| if ($to.isDir) { | |
| let l = $(a).lastPathComponent.js | |
| var t = $to.path + '/' + l | |
| var p = to.replace(/\/$/,'') + '/' + l | |
| } else { | |
| var t = $to.path | |
| var p = to | |
| } | |
| if (!ChocoFileManager.mv(a,t)) throw new Error('Unknown error') | |
| this.path = t | |
| this.pathString = p | |
| return this | |
| } catch(e) { | |
| throw new Error('mv(): ' + e.toString()) | |
| } | |
| } | |
| cp(to) { | |
| try { | |
| if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase()==='function') { | |
| return this.cp(to(this)) | |
| } | |
| let $to = new PathItemObject(to) | |
| let a = this.path | |
| if ($to.isDir) { | |
| let l = $(a).lastPathComponent.js | |
| var t = $to.path + '/' + l | |
| var p = to.replace(/\/$/,'') + '/' + l | |
| } else { | |
| var t = $to.path | |
| var p = to | |
| } | |
| return ChocoFileManager.cp(a,t) ? new PathItemObject(p) : undefined | |
| } catch(e) { | |
| throw new Error('cp(): ' + e.toString()) | |
| } | |
| } | |
| link(to) { | |
| try { | |
| if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase()==='function') { | |
| return this.link(to(this)) | |
| } | |
| let $to = new PathItemObject(to) | |
| let a = this.path | |
| if ($to.isDir) { | |
| let l = $(a).lastPathComponent.js | |
| var t = $to.path + '/' + l | |
| var p = to.replace(/\/$/,'') + '/' + l | |
| } else { | |
| var t = $to.path | |
| var p = to | |
| } | |
| return ChocoFileManager.link(a,t) ? new PathItemObject(p) : undefined | |
| } catch(e) { | |
| throw new Error('link(): ' + e.toString()) | |
| } | |
| } | |
| ln(to) { | |
| try { | |
| if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase()==='function') { | |
| return this.ln(to(this)) | |
| } | |
| let $to = new PathItemObject(to) | |
| let a = this.path | |
| if ($to.isDir) { | |
| let l = $(a).lastPathComponent.js | |
| var t = $to.path + '/' + l | |
| var p = to.replace(/\/$/,'') + '/' + l | |
| } else { | |
| var t = $to.path | |
| var p = to | |
| } | |
| return ChocoFileManager.ln(a,t) ? new PathItemObject(p) : undefined | |
| } catch(e) { | |
| throw new Error('ln(): ' + e.toString()) | |
| } | |
| } | |
| chmod(value) { | |
| try { | |
| if (Object.prototype.toString.call(value).slice(8,-1).toLowerCase()==='function') { | |
| return this.chmod(value(this)) | |
| } | |
| return ChocoFileManager.chmod(value, this.path) | |
| } catch(e) { | |
| throw new Error('chmod(): ' + e.toString()) | |
| } | |
| } | |
| chown(user) { | |
| try { | |
| if (Object.prototype.toString.call(user).slice(8,-1).toLowerCase()==='function') { | |
| return this.chown(user(this)) | |
| } | |
| return ChocoFileManager.chown(user, this.path) | |
| } catch(e) { | |
| throw new Error('chown(): ' + e.toString()) | |
| } | |
| } | |
| chgrp(group) { | |
| try { | |
| if (Object.prototype.toString.call(group).slice(8,-1).toLowerCase()==='function') { | |
| return this.chgrp(group(this)) | |
| } | |
| return ChocoFileManager.chgrp(group, this.path) | |
| } catch(e) { | |
| throw new Error('chgrp(): ' + e.toString()) | |
| } | |
| } | |
| // | |
| toString() { | |
| return JSON.stringify(this) | |
| } | |
| } | |
| class PathItemArray extends Array { | |
| constructor(...path) { | |
| super() | |
| for (let p of path) this.push(new PathItemObject(p)) | |
| } | |
| // Update | |
| mkfile() { | |
| for (let o of this) o.mkfile() | |
| return this | |
| } | |
| mkdir() { | |
| for (let o of this) o.mkdir() | |
| return this | |
| } | |
| rm() { | |
| for (let o of this) o.rm() | |
| return this | |
| } | |
| mv(to) { | |
| for (let o of this) o.mv(to) | |
| return this | |
| } | |
| cp(to) { | |
| let a = new PathItemArray() | |
| for (let o of this) a.push(o.cp(to)) | |
| return a | |
| } | |
| link(to) { | |
| let a = new PathItemArray() | |
| for (let o of this) a.push(o.link(to)) | |
| return a | |
| } | |
| ln(to) { | |
| let a = new PathItemArray() | |
| for (let o of this) a.push(o.ln(to)) | |
| return a | |
| } | |
| chmod(value) { | |
| for (let o of this) o.chmod(value) | |
| return this | |
| } | |
| chown(user) { | |
| for (let o of this) o.chown(user) | |
| return this | |
| } | |
| chgrp(group) { | |
| for (let o of this) o.chgrp(group) | |
| return this | |
| } | |
| // | |
| toString() { | |
| return JSON.stringify(this) | |
| } | |
| } | |
| ChocoShell = ((PathItemObject, PathItemArray)=>{ | |
| let Applet = Application.currentApplication() | |
| Applet.includeStandardAdditions = true | |
| function sh(script, opt={}) { | |
| return Applet.doShellScript(script, { | |
| administratorPrivileges: !!opt.withPrompt, | |
| withPrompt: opt.withPrompt ? opt.withPrompt : '', | |
| alteringLineEndings: opt.alteringLineEndings ? opt.alteringLineEndings : false | |
| }).replace(/\n$/,'') | |
| } | |
| return { | |
| PathItemObject: PathItemObject, | |
| PathItemArray: PathItemArray, | |
| sh: sh, | |
| cd: ChocoFileManager.cd, | |
| pwd: ChocoFileManager.pwd, | |
| ls(arg='') { | |
| return new PathItemArray(...sh(`ls ${arg}`).split('\n')) | |
| }, | |
| find(arg='.') { | |
| return new PathItemArray(...sh(`find ${arg}`).split('\n')) | |
| }, | |
| mdfind(arg='') { | |
| return new PathItemArray(...sh(`mdfind ${arg}`).split('\n')) | |
| }, | |
| } | |
| })(PathItemObject, PathItemArray) | |
| $p = (p) => { return new ChocoShell.PathItemObject(p) } | |
| $a = (...p) => { return new ChocoShell.PathItemArray(...p) } | |
| ls = ChocoShell.ls | |
| $p('foo.txt').cp('bar.txt') | |
| $a('foo.txt').cp('buz.txt') | |
| ls('*.txt').cp('./bar/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment