Skip to content

Commit 9630d39

Browse files
committed
Add generate tree file option
1 parent 76f29f1 commit 9630d39

File tree

7 files changed

+233
-116
lines changed

7 files changed

+233
-116
lines changed

package-lock.json

Lines changed: 30 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "cli",
33
"version": "1.0.0",
44
"description": "cli tool for generating directory structure from a .tree file",
5-
"main": "src/index.ts",
5+
"bin": {
6+
"treez": "out/index.js"
7+
},
68
"scripts": {
79
"lint": "eslint . --ext .ts,.tsx",
810
"build": "tsc",
@@ -22,8 +24,12 @@
2224
"@types/node": "^16.11.6",
2325
"@typescript-eslint/eslint-plugin": "^5.2.0",
2426
"@typescript-eslint/parser": "^5.2.0",
25-
"commander": "^8.3.0",
2627
"eslint": "^8.1.0",
2728
"typescript": "^4.4.4"
29+
},
30+
"dependencies": {
31+
"@structure-codes/is-nondot": "^1.1.2",
32+
"commander": "^8.3.0",
33+
"glob": "^7.2.0"
2834
}
2935
}

src/buildStructure.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as fs from "fs";
2+
import path from "path";
3+
import { treeStringToJson } from "./tree";
4+
import isNondot from "@structure-codes/is-nondot";
5+
6+
const pathExists = (filepath: string) => {
7+
try {
8+
fs.accessSync(filepath);
9+
return true;
10+
} catch {
11+
return false;
12+
}
13+
};
14+
15+
const getPaths = (tree: any) => {
16+
type Path = {
17+
filepath: string;
18+
type: "file" | "dir";
19+
};
20+
const paths: Array<Path> = [];
21+
const searchTree = (tree: any, filepath: Array<string>) => {
22+
const branches = Object.keys(tree);
23+
if (branches.length === 0) {
24+
const leafName = filepath[filepath.length - 1];
25+
const type = leafName.includes(".") || isNondot(leafName)
26+
? "file"
27+
: "dir";
28+
const currentPath = filepath.join("/");
29+
30+
return paths.push({ filepath: currentPath, type });
31+
}
32+
branches.forEach((branch) => {
33+
const newPath = [...filepath];
34+
newPath.push(branch);
35+
searchTree(tree[branch], newPath);
36+
});
37+
};
38+
searchTree(tree, []);
39+
return paths;
40+
};
41+
42+
export const buildStructure = (file, outputDir) => {
43+
try {
44+
const data = fs.readFileSync(file);
45+
const tree = treeStringToJson(data.toString());
46+
const paths = getPaths(tree);
47+
const outputDirExists = pathExists(outputDir);
48+
if (!outputDirExists) {
49+
console.log("Creating target directory:", outputDir);
50+
fs.mkdirSync(outputDir, { recursive: true });
51+
}
52+
paths.forEach(({ filepath, type }: any) => {
53+
const fullPath = outputDir + "/" + filepath.replace(/\/+/g, "/");
54+
if (pathExists(fullPath)) {
55+
console.warn("Path already exists:", fullPath);
56+
return;
57+
}
58+
if (type === "dir") {
59+
console.log("Creating directory:", fullPath);
60+
fs.mkdirSync(fullPath, { recursive: true });
61+
} else {
62+
const dirname = path.dirname(fullPath);
63+
const exist = pathExists(dirname);
64+
if (!exist) {
65+
console.log("Creating directory:", dirname);
66+
fs.mkdirSync(dirname, { recursive: true });
67+
}
68+
console.log("Creating file:", fullPath);
69+
fs.writeFileSync(fullPath, "", { flag: "wx" });
70+
}
71+
});
72+
} catch (e: any) {
73+
console.error(e.message);
74+
}
75+
};

0 commit comments

Comments
 (0)