Skip to content

Commit 76f29f1

Browse files
committed
better logging and typing
1 parent 20cdead commit 76f29f1

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ program
99
.option("-t, --target-dir <targetDir>", "target directory to build structure in", ".");
1010

1111
program.parse(process.argv);
12-
const { file, targetDir } = program.opts();
12+
const opts = program.opts();
13+
const file = opts.file;
14+
const targetDir = opts.targetDir + "/";
1315

14-
const dirExists = (filepath: string) => {
16+
const pathExists = (filepath: string) => {
1517
try {
1618
fs.accessSync(filepath);
1719
return true;
@@ -49,24 +51,31 @@ try {
4951
const data = fs.readFileSync(file);
5052
const tree = treeStringToJson(data.toString());
5153
const paths = getPaths(tree);
52-
const targetDirExists = dirExists(targetDir);
54+
const targetDirExists = pathExists(targetDir);
5355
if (!targetDirExists) {
56+
console.log("Creating target directory:", targetDir);
5457
fs.mkdirSync(targetDir, { recursive: true });
5558
}
56-
paths.forEach(async ({ filepath, type }: any) => {
59+
paths.forEach(({ filepath, type }: any) => {
60+
const fullPath = targetDir + filepath.replace(/\/+/g, "/");
61+
if (pathExists(fullPath)) {
62+
console.warn("Path already exists:", fullPath);
63+
return;
64+
}
5765
if (type === "dir") {
58-
await fs.mkdirSync(targetDir + "/" + filepath, { recursive: true });
66+
console.log("Creating directory:", fullPath);
67+
fs.mkdirSync(fullPath, { recursive: true });
5968
} else {
60-
const dirname = path.dirname(filepath);
61-
const exist = await dirExists(dirname);
69+
const dirname = path.dirname(fullPath);
70+
const exist = pathExists(dirname);
6271
if (!exist) {
63-
await fs.mkdirSync(targetDir + "/" + dirname, { recursive: true });
72+
console.log("Creating directory:", dirname);
73+
fs.mkdirSync(dirname, { recursive: true });
6474
}
65-
await fs.writeFileSync(targetDir + "/" + filepath, "");
75+
console.log("Creating file:", fullPath);
76+
fs.writeFileSync(fullPath, "", {flag: "wx"});
6677
}
6778
});
68-
console.log(tree);
69-
console.log(paths);
7079
} catch (e: any) {
7180
console.error(e.message);
7281
}

src/tree.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export const getNumberOfTabs = (line: string) => {
33
};
44

55
export const treeStringToJson = (tree: string) => {
6-
const elements: {} = {};
6+
type Element = Record<string, any>;
7+
const elements: { [key: string]: Element } = {};
78
let prevLine = "";
89
const path: Array<string> = [];
910

@@ -36,7 +37,7 @@ export const treeStringToJson = (tree: string) => {
3637
curr = {}
3738
*/
3839
const current: any = path.reduce(
39-
(branch: { [key: string]: {} }, filename: string) => branch[filename],
40+
(branch: { [key: string]: Element }, filename: string) => branch[filename],
4041
elements
4142
);
4243

0 commit comments

Comments
 (0)