sensai
Version:
Because even AI needs a master
56 lines (55 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
hasFile: function() {
return hasFile;
},
walk: function() {
return walk;
},
write: function() {
return write;
}
});
const _promises = require("node:fs/promises");
const _nodepath = require("node:path");
const walk = async function*(dir) {
const entries = await (0, _promises.readdir)(dir, {
withFileTypes: true
});
for (const entry of entries){
const fullPath = (0, _nodepath.join)(dir, entry.name);
if (entry.isDirectory()) {
yield* walk(fullPath);
} else {
yield fullPath;
}
}
};
const write = async (path, content)=>{
const match = content.match(/^[ \t]*(?=\S)/m);
if (match) {
// Find the first non-empty line and get its indentation
const indent = match[0].length;
return await (0, _promises.writeFile)(path, content.split("\n").map((line)=>line.substring(indent)) // Remove the common indentation
.join("\n").trim());
} else {
return await (0, _promises.writeFile)(path, content.trim());
}
};
const hasFile = async (filePath)=>{
try {
await (0, _promises.access)(filePath, _promises.constants.F_OK);
return true;
} catch (error) {
return false;
}
};