@typespec/compiler
Version:
TypeSpec Compiler Preview
42 lines • 1.42 kB
JavaScript
import { joinPaths, resolvePath } from "../core/path-utils.js";
export async function mkTempDir(host, base, prefix) {
const rnd = Math.random() * 0x100000000;
const hex = rnd.toString(16).padStart(8, `0`);
const path = joinPaths(base, ".temp", `${prefix}-${process.pid}-${hex}`);
try {
await host.mkdirp(path);
return path;
}
catch (error) {
if (error.code === `EACCES`) {
throw new Error(`Cannot create temporary folder at ${path}. Permission denied. Please check your permissions.`);
}
else {
throw error;
}
}
}
/**
* List all files in dir recursively
* @returns relative path of the files from the given directory
*/
export async function listAllFilesInDir(host, dir) {
const files = [];
async function readDirs(currentDir) {
const currentDirPath = resolvePath(dir, currentDir);
const fileOrDirs = await host.readDir(currentDirPath);
for (const file of fileOrDirs) {
const fullPath = resolvePath(currentDirPath, file);
const stat = await host.stat(fullPath);
if (stat.isDirectory()) {
await readDirs(resolvePath(currentDir, file));
}
else {
files.push(resolvePath(currentDir, file));
}
}
}
await readDirs("");
return files;
}
//# sourceMappingURL=fs-utils.js.map