// Compiled by ClojureScript 1.9.562 {}
goog.provide('weathergen.filesystem');
goog.require('cljs.core');
goog.require('cljs.nodejs');
goog.require('clojure.string');
goog.require('taoensso.timbre');
goog.require('weathergen.interop.node.path');
goog.require('weathergen.util');
weathergen.filesystem.fs_lib = cljs.nodejs.require.call(null,"fs");
weathergen.filesystem.os_lib = cljs.nodejs.require.call(null,"os");
weathergen.filesystem.win32_QMARK_ = cljs.core._EQ_.call(null,weathergen.filesystem.os_lib.platform(),"win32");
/**
 * If on a case-sensitive filesystem, try to convert to an existing
 *   path, ignoring case.
 */
weathergen.filesystem.case_desensitize = (function weathergen$filesystem$case_desensitize(path){
if(weathergen.filesystem.win32_QMARK_){
return path;
} else {
return cljs.core.reduce.call(null,(function (existing,child){
var G__34674 = existing;
var G__34674__$1 = (((G__34674 == null))?null:weathergen.filesystem.fs_lib.readdirSync(G__34674));
var G__34674__$2 = (((G__34674__$1 == null))?null:cljs.core.filter.call(null,((function (G__34674,G__34674__$1){
return (function (p1__34672_SHARP_){
return cljs.core._EQ_.call(null,clojure.string.lower_case.call(null,p1__34672_SHARP_),clojure.string.lower_case.call(null,child));
});})(G__34674,G__34674__$1))
,G__34674__$1));
var G__34674__$3 = (((G__34674__$2 == null))?null:cljs.core.first.call(null,G__34674__$2));
if((G__34674__$3 == null)){
return null;
} else {
return weathergen.interop.node.path.join.call(null,existing,G__34674__$3);
}
}),"/",cljs.core.remove.call(null,cljs.core.empty_QMARK_,clojure.string.split.call(null,path,/\//)));
}
});
/**
 * Deal with the fact that Windows likes backslashes and everyone else
 *   is sane.
 */
weathergen.filesystem.normalize = (function weathergen$filesystem$normalize(path){
return clojure.string.replace.call(null,path,"\\","/");
});
/**
 * Returns true if the specified file exists.
 */
weathergen.filesystem.exists_QMARK_ = (function weathergen$filesystem$exists_QMARK_(path){
var temp__5278__auto__ = weathergen.filesystem.case_desensitize.call(null,weathergen.filesystem.normalize.call(null,path));
if(cljs.core.truth_(temp__5278__auto__)){
var n = temp__5278__auto__;
return weathergen.filesystem.fs_lib.existsSync(n);
} else {
return null;
}
});
/**
 * Given two path components, combine them. If the second is absolute, return it.
 */
weathergen.filesystem.path_combine = (function weathergen$filesystem$path_combine(var_args){
var args34675 = [];
var len__24559__auto___34681 = arguments.length;
var i__24560__auto___34682 = (0);
while(true){
if((i__24560__auto___34682 < len__24559__auto___34681)){
args34675.push((arguments[i__24560__auto___34682]));

var G__34683 = (i__24560__auto___34682 + (1));
i__24560__auto___34682 = G__34683;
continue;
} else {
}
break;
}

var G__34680 = args34675.length;
switch (G__34680) {
case 2:
return weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
var argseq__24582__auto__ = (new cljs.core.IndexedSeq(args34675.slice((2)),(0),null));
return weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$variadic((arguments[(0)]),(arguments[(1)]),argseq__24582__auto__);

}
});

weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$2 = (function (path1,path2){
if(cljs.core.truth_(weathergen.interop.node.path.isAbsolute.call(null,weathergen.filesystem.normalize.call(null,path2)))){
return weathergen.filesystem.normalize.call(null,path2);
} else {
return weathergen.interop.node.path.join.call(null,weathergen.filesystem.normalize.call(null,path1),weathergen.filesystem.normalize.call(null,path2));
}
});

weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$variadic = (function (path1,path2,paths){
return cljs.core.apply.call(null,weathergen.filesystem.path_combine,weathergen.filesystem.path_combine.call(null,path1,path2),paths);
});

weathergen.filesystem.path_combine.cljs$lang$applyTo = (function (seq34676){
var G__34677 = cljs.core.first.call(null,seq34676);
var seq34676__$1 = cljs.core.next.call(null,seq34676);
var G__34678 = cljs.core.first.call(null,seq34676__$1);
var seq34676__$2 = cljs.core.next.call(null,seq34676__$1);
return weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$variadic(G__34677,G__34678,seq34676__$2);
});

weathergen.filesystem.path_combine.cljs$lang$maxFixedArity = (2);

/**
 * Given a path, return the directory that contains it. Returns null
 *   if at the root.
 */
weathergen.filesystem.parent = (function weathergen$filesystem$parent(path){
var p = weathergen.interop.node.path.dirname.call(null,weathergen.filesystem.normalize.call(null,path));
if(cljs.core._EQ_.call(null,p,path)){
return null;
} else {
return p;
}
});
/**
 * Given a path, return its basename. `opts` includes
 *   `:omit-extension?`, which, if true, will return the path without the
 *   file extension.
 */
weathergen.filesystem.basename = (function weathergen$filesystem$basename(var_args){
var args34685 = [];
var len__24559__auto___34690 = arguments.length;
var i__24560__auto___34691 = (0);
while(true){
if((i__24560__auto___34691 < len__24559__auto___34690)){
args34685.push((arguments[i__24560__auto___34691]));

var G__34692 = (i__24560__auto___34691 + (1));
i__24560__auto___34691 = G__34692;
continue;
} else {
}
break;
}

var G__34687 = args34685.length;
switch (G__34687) {
case 1:
return weathergen.filesystem.basename.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));

break;
case 2:
return weathergen.filesystem.basename.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args34685.length)].join('')));

}
});

weathergen.filesystem.basename.cljs$core$IFn$_invoke$arity$1 = (function (path){
return weathergen.filesystem.basename.call(null,path,cljs.core.PersistentArrayMap.EMPTY);
});

weathergen.filesystem.basename.cljs$core$IFn$_invoke$arity$2 = (function (path,opts){
var map__34688 = opts;
var map__34688__$1 = ((((!((map__34688 == null)))?((((map__34688.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__34688.cljs$core$ISeq$)))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__34688):map__34688);
var omit_extension_QMARK_ = cljs.core.get.call(null,map__34688__$1,new cljs.core.Keyword(null,"omit-extension?","omit-extension?",-1855127967));
var b = weathergen.interop.node.path.basename.call(null,path);
if(cljs.core.not.call(null,omit_extension_QMARK_)){
return b;
} else {
var e = weathergen.interop.node.path.extname.call(null,b);
return cljs.core.subs.call(null,b,(0),(cljs.core.count.call(null,b) - cljs.core.count.call(null,e)));
}
});

weathergen.filesystem.basename.cljs$lang$maxFixedArity = 2;

/**
 * Returns a DataView wrappingx the contents of the file at `path`.
 */
weathergen.filesystem.file_buf = (function weathergen$filesystem$file_buf(path){
try{var n = weathergen.filesystem.case_desensitize.call(null,weathergen.filesystem.normalize.call(null,path));
var buf = weathergen.filesystem.fs_lib.readFileSync(n);
var ab = buf.buffer;
var dv = (new DataView(ab));
return (new DataView(ab,buf.byteOffset,buf.byteLength));
}catch (e34695){var x = e34695;
taoensso.timbre._log_BANG_.call(null,taoensso.timbre._STAR_config_STAR_,new cljs.core.Keyword(null,"error","error",-978969032),"weathergen.filesystem",null,98,new cljs.core.Keyword(null,"p","p",151049309),new cljs.core.Keyword(null,"auto","auto",-566279492),(new cljs.core.Delay(((function (x){
return (function (){
return new cljs.core.PersistentVector(null, 4, 5, cljs.core.PersistentVector.EMPTY_NODE, [x,"Failed to open file",new cljs.core.Keyword(null,"path","path",-188191168),path], null);
});})(x))
,null)),null,-537237246);

throw cljs.core.ex_info.call(null,"Failed to open file",new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"reason","reason",-2070751759),new cljs.core.Keyword(null,"file-open-failed","file-open-failed",-571385612),new cljs.core.Keyword(null,"path","path",-188191168),path], null));
}});
/**
 * Reads the contents of file at `path` as a Node Buffer.
 */
weathergen.filesystem.file_buffer = (function weathergen$filesystem$file_buffer(path){
return weathergen.filesystem.fs_lib.readFileSync(weathergen.filesystem.case_desensitize.call(null,weathergen.filesystem.normalize.call(null,path)));
});
/**
 * Returns a string with the contents of the file at `path`.
 */
weathergen.filesystem.file_text = (function weathergen$filesystem$file_text(path){
var buf = weathergen.filesystem.fs_lib.readFileSync(weathergen.filesystem.case_desensitize.call(null,weathergen.filesystem.normalize.call(null,path)));
return buf.toString();
});
/**
 * Returns true if `a` and `b` are the same file.
 */
weathergen.filesystem.identical_QMARK_ = (function weathergen$filesystem$identical_QMARK_(a,b){
var and__23326__auto__ = weathergen.filesystem.exists_QMARK_.call(null,a);
if(cljs.core.truth_(and__23326__auto__)){
var and__23326__auto____$1 = weathergen.filesystem.exists_QMARK_.call(null,b);
if(cljs.core.truth_(and__23326__auto____$1)){
var stats_a = weathergen.filesystem.fs_lib.statSync(weathergen.filesystem.normalize.call(null,a));
var stats_b = weathergen.filesystem.fs_lib.statSync(weathergen.filesystem.normalize.call(null,b));
return cljs.core._EQ_.call(null,stats_a.ino,stats_b.ino);
} else {
return and__23326__auto____$1;
}
} else {
return and__23326__auto__;
}
});
/**
 * Returns true if `descendant` is a descendant file of `ancestor`.
 */
weathergen.filesystem.ancestor_QMARK_ = (function weathergen$filesystem$ancestor_QMARK_(ancestor,descendant){
if(!(weathergen.filesystem.win32_QMARK_)){
return clojure.string.starts_with_QMARK_.call(null,clojure.string.lower_case.call(null,descendant),clojure.string.lower_case.call(null,ancestor));
} else {
var ancestor__$1 = weathergen.filesystem.normalize.call(null,ancestor);
var descendant__$1 = weathergen.filesystem.normalize.call(null,descendant);
var parent = weathergen.filesystem.parent.call(null,descendant__$1);
if(cljs.core.truth_((function (){var and__23326__auto__ = parent;
if(cljs.core.truth_(and__23326__auto__)){
var and__23326__auto____$1 = descendant__$1;
if(cljs.core.truth_(and__23326__auto____$1)){
var and__23326__auto____$2 = weathergen.filesystem.exists_QMARK_.call(null,parent);
if(cljs.core.truth_(and__23326__auto____$2)){
return weathergen.filesystem.exists_QMARK_.call(null,descendant__$1);
} else {
return and__23326__auto____$2;
}
} else {
return and__23326__auto____$1;
}
} else {
return and__23326__auto__;
}
})())){
var or__23338__auto__ = weathergen.filesystem.identical_QMARK_.call(null,parent,ancestor__$1);
if(cljs.core.truth_(or__23338__auto__)){
return or__23338__auto__;
} else {
return weathergen.filesystem.ancestor_QMARK_.call(null,ancestor__$1,parent);
}
} else {
return null;
}
}
});
/**
 * Creates a directory. Recursively creates the parents if
 *   `make-parents?` is true.
 */
weathergen.filesystem.mkdir = (function weathergen$filesystem$mkdir(path,make_parents_QMARK_){
var par = weathergen.filesystem.parent.call(null,path);
if(cljs.core.truth_((function (){var and__23326__auto__ = make_parents_QMARK_;
if(cljs.core.truth_(and__23326__auto__)){
return cljs.core.not.call(null,weathergen.filesystem.exists_QMARK_.call(null,par));
} else {
return and__23326__auto__;
}
})())){
weathergen.filesystem.mkdir.call(null,par,true);
} else {
}

if(cljs.core.not.call(null,weathergen.filesystem.exists_QMARK_.call(null,path))){
return weathergen.filesystem.fs_lib.mkdirSync(path);
} else {
return null;
}
});
/**
 * Saves `data` to the filesystem at `path`. Creates the directory if
 *   necessary.
 */
weathergen.filesystem.save_data = (function weathergen$filesystem$save_data(path,data){
weathergen.filesystem.mkdir.call(null,weathergen.filesystem.parent.call(null,path),true);

return weathergen.filesystem.fs_lib.writeFileSync(path,data);
});
/**
 * Returns stats for file as map with keys `:size` and `:modified`.
 */
weathergen.filesystem.stat = (function weathergen$filesystem$stat(path){
var stats = weathergen.filesystem.fs_lib.statSync(path);
return new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"size","size",1098693007),stats.size,new cljs.core.Keyword(null,"modified","modified",-2134587826),stats.mtime.valueOf()], null);
});
