// Compiled by ClojureScript 1.10.520 {:target :nodejs}
goog.provide('weathergen.filesystem');
goog.require('cljs.core');
goog.require('clojure.string');
goog.require('taoensso.timbre');
goog.require('weathergen.interop.node.path');
goog.require('weathergen.util');
weathergen.filesystem.fs_lib = require("fs");
weathergen.filesystem.os_lib = require("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__27388 = existing;
var G__27388__$1 = (((G__27388 == null))?null:weathergen.filesystem.fs_lib.readdirSync(G__27388));
var G__27388__$2 = (((G__27388__$1 == null))?null:cljs.core.filter.call(null,((function (G__27388,G__27388__$1){
return (function (p1__27387_SHARP_){
return cljs.core._EQ_.call(null,clojure.string.lower_case.call(null,p1__27387_SHARP_),clojure.string.lower_case.call(null,child));
});})(G__27388,G__27388__$1))
,G__27388__$1));
var G__27388__$3 = (((G__27388__$2 == null))?null:cljs.core.first.call(null,G__27388__$2));
if((G__27388__$3 == null)){
return null;
} else {
return weathergen.interop.node.path.join.call(null,existing,G__27388__$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__5720__auto__ = weathergen.filesystem.case_desensitize.call(null,weathergen.filesystem.normalize.call(null,path));
if(cljs.core.truth_(temp__5720__auto__)){
var n = temp__5720__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 G__27393 = arguments.length;
switch (G__27393) {
case 2:
return weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
var args_arr__4751__auto__ = [];
var len__4730__auto___27395 = arguments.length;
var i__4731__auto___27396 = (0);
while(true){
if((i__4731__auto___27396 < len__4730__auto___27395)){
args_arr__4751__auto__.push((arguments[i__4731__auto___27396]));

var G__27397 = (i__4731__auto___27396 + (1));
i__4731__auto___27396 = G__27397;
continue;
} else {
}
break;
}

var argseq__4752__auto__ = (new cljs.core.IndexedSeq(args_arr__4751__auto__.slice((2)),(0),null));
return weathergen.filesystem.path_combine.cljs$core$IFn$_invoke$arity$variadic((arguments[(0)]),(arguments[(1)]),argseq__4752__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);
});

/** @this {Function} */
weathergen.filesystem.path_combine.cljs$lang$applyTo = (function (seq27390){
var G__27391 = cljs.core.first.call(null,seq27390);
var seq27390__$1 = cljs.core.next.call(null,seq27390);
var G__27392 = cljs.core.first.call(null,seq27390__$1);
var seq27390__$2 = cljs.core.next.call(null,seq27390__$1);
var self__4717__auto__ = this;
return self__4717__auto__.cljs$core$IFn$_invoke$arity$variadic(G__27391,G__27392,seq27390__$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 G__27399 = arguments.length;
switch (G__27399) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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__27400 = opts;
var map__27400__$1 = (((((!((map__27400 == null))))?(((((map__27400.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__27400.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__27400):map__27400);
var omit_extension_QMARK_ = cljs.core.get.call(null,map__27400__$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;

/**
 * Given a path, return the file extension without the leading dot.
 */
weathergen.filesystem.extension = (function weathergen$filesystem$extension(path){
return cljs.core.subs.call(null,weathergen.interop.node.path.extname.call(null,path),(1));
});
/**
 * 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 (e27403){var x = e27403;
taoensso.timbre._log_BANG_.call(null,taoensso.timbre._STAR_config_STAR_,new cljs.core.Keyword(null,"error","error",-978969032),"weathergen.filesystem",null,102,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,792867656);

throw cljs.core.ex_info.call(null,["Failed to open file ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(path)].join(''),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__4120__auto__ = weathergen.filesystem.exists_QMARK_.call(null,a);
if(cljs.core.truth_(and__4120__auto__)){
var and__4120__auto____$1 = weathergen.filesystem.exists_QMARK_.call(null,b);
if(cljs.core.truth_(and__4120__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__4120__auto____$1;
}
} else {
return and__4120__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__4120__auto__ = parent;
if(cljs.core.truth_(and__4120__auto__)){
var and__4120__auto____$1 = descendant__$1;
if(cljs.core.truth_(and__4120__auto____$1)){
var and__4120__auto____$2 = weathergen.filesystem.exists_QMARK_.call(null,parent);
if(cljs.core.truth_(and__4120__auto____$2)){
return weathergen.filesystem.exists_QMARK_.call(null,descendant__$1);
} else {
return and__4120__auto____$2;
}
} else {
return and__4120__auto____$1;
}
} else {
return and__4120__auto__;
}
})())){
var or__4131__auto__ = weathergen.filesystem.identical_QMARK_.call(null,parent,ancestor__$1);
if(cljs.core.truth_(or__4131__auto__)){
return or__4131__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__4120__auto__ = make_parents_QMARK_;
if(cljs.core.truth_(and__4120__auto__)){
return cljs.core.not.call(null,weathergen.filesystem.exists_QMARK_.call(null,par));
} else {
return and__4120__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);
});
weathergen.filesystem.save_data_async = (function weathergen$filesystem$save_data_async(path,data){
weathergen.filesystem.mkdir.call(null,weathergen.filesystem.parent.call(null,path),true);

return weathergen.filesystem.fs_lib.writeFile(path,data,(function (err){
if(cljs.core.truth_(err)){
return console.error("save-data-async error",path,err);
} else {
return null;
}
}));
});
/**
 * 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);
});
/**
 * Returns a seq of files in `dir`.
 */
weathergen.filesystem.ls = (function weathergen$filesystem$ls(var_args){
var args__4736__auto__ = [];
var len__4730__auto___27410 = arguments.length;
var i__4731__auto___27411 = (0);
while(true){
if((i__4731__auto___27411 < len__4730__auto___27410)){
args__4736__auto__.push((arguments[i__4731__auto___27411]));

var G__27412 = (i__4731__auto___27411 + (1));
i__4731__auto___27411 = G__27412;
continue;
} else {
}
break;
}

var argseq__4737__auto__ = ((((1) < args__4736__auto__.length))?(new cljs.core.IndexedSeq(args__4736__auto__.slice((1)),(0),null)):null);
return weathergen.filesystem.ls.cljs$core$IFn$_invoke$arity$variadic((arguments[(0)]),argseq__4737__auto__);
});

weathergen.filesystem.ls.cljs$core$IFn$_invoke$arity$variadic = (function (dir,p__27406){
var vec__27407 = p__27406;
var options = cljs.core.nth.call(null,vec__27407,(0),null);
return weathergen.filesystem.fs_lib.readdirSync(dir);
});

weathergen.filesystem.ls.cljs$lang$maxFixedArity = (1);

/** @this {Function} */
weathergen.filesystem.ls.cljs$lang$applyTo = (function (seq27404){
var G__27405 = cljs.core.first.call(null,seq27404);
var seq27404__$1 = cljs.core.next.call(null,seq27404);
var self__4717__auto__ = this;
return self__4717__auto__.cljs$core$IFn$_invoke$arity$variadic(G__27405,seq27404__$1);
});

