@ikas/storefront-cmd 4.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ /**
5
+ * Divides the given filePath string into directory part and fileName part
6
+ * @param path Path for the file
7
+ */
8
+ export function getFilePathParts(path: string) {
9
+ const componentFilePathParts = path.split("/");
10
+
11
+ if (componentFilePathParts.length > 1) {
12
+ const dirParts = componentFilePathParts.slice(
13
+ 0,
14
+ componentFilePathParts.length - 1
15
+ );
16
+ const filePart = componentFilePathParts[componentFilePathParts.length - 1];
17
+
18
+ return {
19
+ dirParts,
20
+ filePart,
21
+ };
22
+ }
23
+
24
+ return {
25
+ filePart: path,
26
+ };
27
+ }
28
+
29
+ /**
30
+ * Deletes the contents of the given directory, exluding the ignoreFiles
31
+ * @param dirPath Path for the directory
32
+ * @param ignoreFiles List of files to keep in directory. Does not accept recursive paths, only first level files
33
+ *
34
+ * @returns true for successfull operation, false otherwise
35
+ */
36
+ export async function deleteDirContent(
37
+ dirPath: string,
38
+ ignoreFiles?: string[]
39
+ ) {
40
+ try {
41
+ // const fs = await import("fs-extra");
42
+ const files = fs.readdirSync(dirPath);
43
+
44
+ files.forEach((file) => {
45
+ if (!ignoreFiles?.includes(file)) {
46
+ fs.unlinkSync(path.join(dirPath, file));
47
+ }
48
+ });
49
+
50
+ return true;
51
+ } catch (err) {
52
+ return false;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Creates the given file and directories for the filePath
58
+ * @param basePath Base directory path for the file
59
+ * @param fileSubPath Filename or the sub directory path for the file to write
60
+ * @param fileStr File contents
61
+ *
62
+ * @returns true for successfull operation, false otherwise
63
+ */
64
+ export async function createFile(
65
+ basePath: string,
66
+ fileSubPath: string,
67
+ fileStr: string
68
+ ) {
69
+ try {
70
+ // const fs = await import("fs-extra");
71
+ let generatedFolderPath = basePath;
72
+
73
+ const pageFilePathParts = getFilePathParts(fileSubPath);
74
+ if (pageFilePathParts.dirParts?.length) {
75
+ generatedFolderPath = path.join(
76
+ generatedFolderPath,
77
+ ...pageFilePathParts.dirParts
78
+ );
79
+ }
80
+
81
+ if (!fs.existsSync(generatedFolderPath)) {
82
+ fs.mkdirSync(generatedFolderPath, { recursive: true });
83
+ }
84
+
85
+ const filePath = path.join(generatedFolderPath, pageFilePathParts.filePart);
86
+ fs.writeFileSync(filePath, fileStr);
87
+
88
+ return true;
89
+ } catch (err) {
90
+ console.error(err);
91
+ return false;
92
+ }
93
+ }
@@ -0,0 +1,20 @@
1
+ export function sortObject(obj: Record<string, any>) {
2
+ return Object.keys(obj)
3
+ .sort()
4
+ .reduce((sortedObj: Record<string, any>, key) => {
5
+ sortedObj[key] = obj[key];
6
+ return sortedObj;
7
+ }, {});
8
+ }
9
+
10
+ export function findAllByKey(obj: Record<string, any>, keyToFind: string): any {
11
+ return Object.entries(obj || {}).reduce(
12
+ (acc, [key, value]) =>
13
+ key === keyToFind
14
+ ? acc.concat(value)
15
+ : typeof value === "object"
16
+ ? acc.concat(findAllByKey(value, keyToFind))
17
+ : acc,
18
+ []
19
+ );
20
+ }