Skip to content

Commit 3533c23

Browse files
committed
feat: support wildcards, removed glob
1 parent 3e34a67 commit 3533c23

File tree

3 files changed

+272
-69
lines changed

3 files changed

+272
-69
lines changed

src/commands/fs/automated.js

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
1-
let glob = require("glob");
2-
let fs = require('fs');
1+
const fs = require("fs");
2+
const path = require("path");
33

4-
function globUpdater(er, files) {
4+
function findDirectories(startPath, callback, fileName) {
5+
// Resolve relative paths to absolute paths if needed
6+
const resolvedPath =
7+
startPath.startsWith("./") || startPath.startsWith("../")
8+
? path.resolve(startPath)
9+
: startPath;
510

6-
if (er)
7-
console.log(files, 'glob resolving issue')
8-
else
9-
files.forEach(filename => {
10-
update(filename + 'automated.yml')
11-
})
12-
console.log('Completed')
11+
const segments = resolvedPath.split("/"); // Split path by '/'
12+
let currentPath = "/"; // Start from root
13+
14+
for (let i = 0; i < segments.length; i++) {
15+
const segment = segments[i];
16+
const isWildcard = segment === "*";
17+
18+
if (isWildcard) {
19+
// Get all directories at this level
20+
const directories = fs
21+
.readdirSync(currentPath)
22+
.filter((file) =>
23+
fs.statSync(path.join(currentPath, file)).isDirectory()
24+
);
25+
26+
// Process each directory and continue along the path
27+
directories.forEach((dir) => {
28+
findDirectories(
29+
path.join(currentPath, dir, ...segments.slice(i + 1)),
30+
callback,
31+
fileName
32+
);
33+
});
34+
return; // Stop further processing in the loop for wildcard case
35+
} else {
36+
// Continue to the next part of the path
37+
currentPath = path.join(currentPath, segment);
38+
39+
// If a segment doesn’t exist or isn’t a directory, log an error and stop
40+
if (
41+
!fs.existsSync(currentPath) ||
42+
!fs.statSync(currentPath).isDirectory()
43+
) {
44+
console.log(`Directory not found: ${currentPath}`);
45+
return;
46+
}
47+
}
48+
}
49+
50+
// If we reach the end of the path without wildcards, we have a valid directory
51+
callback(currentPath, fileName);
1352
}
1453

15-
function update(YmlPath) {
16-
let build = `- name: Build
17-
run: yarn build`
54+
function createOrUpdateFile(directoryPath, fileName) {
55+
let buildStep = `- name: Build\n run: yarn build`;
1856

19-
let webpackPath = YmlPath.replace('.github/workflows/automated.yml', 'webpack.config.js')
20-
if (!fs.existsSync(webpackPath))
21-
build = ''
57+
// Check if webpack config exists to include build step
58+
const webpackPath = filePath.replace(fileName, "webpack.config.js");
59+
if (!fs.existsSync(webpackPath)) buildStep = "";
2260

23-
let fileContent = `name: Automated Workflow
61+
// Define file content (e.g., for YAML or other configuration)
62+
const fileContent = `name: Automated Workflow
2463
on:
2564
push:
2665
branches:
@@ -80,30 +119,41 @@ jobs:
80119
run: echo "//registry.npmjs.org/:_authToken=\${{ secrets.NPM_TOKEN }}" > ~/.npmrc
81120
- name: Install dependencies
82121
run: yarn install
83-
${build}
122+
${buildStep}
84123
- name: Set Environment Variables
85124
run: |
86125
echo "organization_id=\${{ secrets.COCREATE_ORGANIZATION_ID }}" >> $GITHUB_ENV
87126
echo "key=\${{ secrets.COCREATE_KEY }}" >> $GITHUB_ENV
88127
echo "host=\${{ secrets.COCREATE_HOST }}" >> $GITHUB_ENV
89128
- name: CoCreate Upload
90129
run: coc upload
91-
92130
`;
93-
if (fs.existsSync(YmlPath))
94-
fs.unlinkSync(YmlPath)
95-
fs.writeFileSync(YmlPath, fileContent)
96131

132+
const filePath = path.join(directoryPath, fileName);
133+
// Create or update the file
134+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
135+
fs.writeFileSync(filePath, fileContent);
97136
}
98137

138+
// Define the directories with wildcards
139+
const directories = [
140+
"../../../../../CoCreate-components/*/",
141+
"../../../../../CoCreate-apps/*/",
142+
"../../../../../CoCreate-plugins/*/",
143+
"../../../../../CoCreateCSS/",
144+
"../../../../../CoCreateJS/",
145+
"../../../../../CoCreateWS/",
146+
"../../../../../YellowOracle/",
147+
"../../../../../CoCreate-website/",
148+
"../../../../../CoCreate-admin/",
149+
"../../../../../CoCreate-website-old/",
150+
"../../../../../CoCreate-superadmin/",
151+
];
152+
const fileName = "automated.yml";
99153

154+
// Execute directory search and create/update file if the directory exists
155+
directories.forEach((directory) => {
156+
findDirectories(directory, createOrUpdateFile, fileName);
157+
});
100158

101-
// glob("/home/cocreate/CoCreate/CoCreate-components/CoCreate-actions/.github/workflows", globUpdater)
102-
glob("/home/cocreate/CoCreate/CoCreate-components/*/.github/workflows/", globUpdater)
103-
// glob("/home/cocreate/CoCreate/CoCreate-apps/*/.github/workflows/", globUpdater)
104-
// glob("/home/cocreate/CoCreate/CoCreate-plugins/*/.github/workflows/", globUpdater)
105-
106-
// glob("/home/cocreate/CoCreate/CoCreate-admin/.github/workflows/", globUpdater)
107-
// glob("/home/cocreate/CoCreate/CoCreateCSS/.github/workflows/", globUpdater)
108-
// glob("/home/cocreate/CoCreate/CoCreateJS/.github/workflows/", globUpdater)
109-
// glob("/home/cocreate/CoCreate/CoCreate-wesite/.github/workflows/", globUpdater)
159+
console.log("Finished");

src/commands/fs/config.js

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,72 @@
1-
let glob = require("glob");
2-
let fs = require('fs');
1+
const fs = require("fs");
32
const path = require("path");
43

5-
function globUpdater(er, files) {
6-
if (er)
7-
console.log(files, 'glob resolving issue');
8-
else
9-
files.forEach(filename => update(filename));
10-
}
4+
function findDirectories(startPath, callback, fileName) {
5+
// Resolve relative paths to absolute paths if needed
6+
const resolvedPath =
7+
startPath.startsWith("./") || startPath.startsWith("../")
8+
? path.resolve(startPath)
9+
: startPath;
1110

11+
const segments = resolvedPath.split("/"); // Split path by '/'
12+
let currentPath = "/"; // Start from root
1213

14+
for (let i = 0; i < segments.length; i++) {
15+
const segment = segments[i];
16+
const isWildcard = segment === "*";
1317

18+
if (isWildcard) {
19+
// Get all directories at this level
20+
const directories = fs
21+
.readdirSync(currentPath)
22+
.filter((file) =>
23+
fs.statSync(path.join(currentPath, file)).isDirectory()
24+
);
1425

15-
function update(MdPath) {
16-
let name = path.basename(path.resolve(path.dirname(MdPath), './')).substring(9);
17-
let object = '';
18-
let replaceContent = fs.readFileSync(MdPath).toString();
26+
// Process each directory and continue along the path
27+
directories.forEach((dir) => {
28+
findDirectories(
29+
path.join(currentPath, dir, ...segments.slice(i + 1)),
30+
callback,
31+
fileName
32+
);
33+
});
34+
return; // Stop further processing in the loop for wildcard case
35+
} else {
36+
// Continue to the next part of the path
37+
currentPath = path.join(currentPath, segment);
1938

39+
// If a segment doesn’t exist or isn’t a directory, log an error and stop
40+
if (
41+
!fs.existsSync(currentPath) ||
42+
!fs.statSync(currentPath).isDirectory()
43+
) {
44+
console.log(`Directory not found: ${currentPath}`);
45+
return;
46+
}
47+
}
48+
}
2049

21-
let content_source = replaceContent.substring(replaceContent.indexOf("sources"));
22-
let content1 = content_source.substring(content_source.indexOf("object"));
23-
let content2 = content1.substring(content1.indexOf(':'));
24-
object = content2.substring(3, content2.indexOf(',') - 4);
50+
// If we reach the end of the path without wildcards, we have a valid directory
51+
callback(currentPath, fileName);
52+
}
2553

54+
function createOrUpdateFile(directoryPath, fileName) {
55+
let name = path
56+
.basename(path.resolve(path.dirname(directoryPath), "./"))
57+
.substring(9);
58+
let object = "";
59+
let replaceContent = fs.readFileSync(directoryPath).toString();
2660

27-
let fileContent = `module.exports = {
61+
// Parse content to extract `object`
62+
let content_source = replaceContent.substring(
63+
replaceContent.indexOf("sources")
64+
);
65+
let content1 = content_source.substring(content_source.indexOf("object"));
66+
let content2 = content1.substring(content1.indexOf(":"));
67+
object = content2.substring(3, content2.indexOf(",") - 4);
68+
69+
let fileContent = `module.exports = {
2870
"config": {
2971
"organization_id": "5ff747727005da1c272740ab",
3072
"key": "2061acef-0451-4545-f754-60cf8160",
@@ -37,7 +79,7 @@ function update(MdPath) {
3779
"object": {
3880
"_id": "${object}",
3981
"name": "index.html",
40-
"path": "/docs/${name}",
82+
"path": "/docs/${name}",
4183
"pathname": "/docs/${name}/index.html",
4284
"src": "{{./docs/index.html}}",
4385
"host": [
@@ -50,28 +92,42 @@ function update(MdPath) {
5092
}
5193
]
5294
}
53-
5495
`;
5596

56-
if (!object.length)
57-
console.log("object Undefined: ", MdPath);
58-
if (object.length != 24 && object.length != 0)
59-
console.log("object not valid! please check your config: ", MdPath);
60-
else {
61-
console.log(MdPath, " -> object : ", object);
62-
if (fs.existsSync(MdPath))
63-
fs.unlinkSync(MdPath);
64-
fs.writeFileSync(MdPath, fileContent);
65-
66-
}
67-
97+
if (!object.length) {
98+
console.log("object Undefined: ", directoryPath);
99+
} else if (object.length !== 24) {
100+
console.log("object not valid! Please check your config: ", directoryPath);
101+
} else {
102+
const filePath = path.join(directoryPath, fileName);
103+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
104+
fs.writeFileSync(filePath, fileContent);
105+
}
106+
const filePath = path.join(directoryPath, fileName);
107+
// Create or update the file
108+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
109+
fs.writeFileSync(filePath, fileContent);
68110
}
69111

70-
// glob("../../CoCreate-components/CoCreate-filter/CoCreate.config.js", globUpdater);
71-
glob("../../CoCreate-components/*/CoCreate.config.js", globUpdater);
72-
glob("../../CoCreate-apps/*/CoCreate.config.js", globUpdater);
73-
glob("../../CoCreate-plugins/*/CoCreate.config.js", globUpdater);
74-
// glob("../CoCreateCSS/CoCreate.config.js", globUpdater);
75-
// glob("../CoCreateJS/CoCreate.config.js", globUpdater);
112+
// Define the directories with wildcards
113+
const directories = [
114+
"../../../../../CoCreate-components/*/",
115+
"../../../../../CoCreate-apps/*/",
116+
"../../../../../CoCreate-plugins/*/",
117+
"../../../../../CoCreateCSS/",
118+
"../../../../../CoCreateJS/",
119+
"../../../../../CoCreateWS/",
120+
"../../../../../YellowOracle/",
121+
"../../../../../CoCreate-website/",
122+
"../../../../../CoCreate-admin/",
123+
"../../../../../CoCreate-website-old/",
124+
"../../../../../CoCreate-superadmin/",
125+
];
126+
const fileName = "CoCreate.config.js";
127+
128+
// Execute directory search and create/update file if the directory exists
129+
directories.forEach((directory) => {
130+
findDirectories(directory, createOrUpdateFile, fileName);
131+
});
76132

77-
console.log('finished');
133+
console.log("Finished");

src/commands/fs/prettier.config.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
function findDirectories(startPath, callback, fileName) {
5+
// Resolve relative paths to absolute paths if needed
6+
const resolvedPath =
7+
startPath.startsWith("./") || startPath.startsWith("../")
8+
? path.resolve(startPath)
9+
: startPath;
10+
11+
const segments = resolvedPath.split("/"); // Split path by '/'
12+
let currentPath = "/"; // Start from root
13+
14+
for (let i = 0; i < segments.length; i++) {
15+
const segment = segments[i];
16+
const isWildcard = segment === "*";
17+
18+
if (isWildcard) {
19+
// Get all directories at this level
20+
const directories = fs
21+
.readdirSync(currentPath)
22+
.filter((file) =>
23+
fs.statSync(path.join(currentPath, file)).isDirectory()
24+
);
25+
26+
// Process each directory and continue along the path
27+
directories.forEach((dir) => {
28+
findDirectories(
29+
path.join(currentPath, dir, ...segments.slice(i + 1)),
30+
callback,
31+
fileName
32+
);
33+
});
34+
return; // Stop further processing in the loop for wildcard case
35+
} else {
36+
// Continue to the next part of the path
37+
currentPath = path.join(currentPath, segment);
38+
39+
// If a segment doesn’t exist or isn’t a directory, log an error and stop
40+
if (
41+
!fs.existsSync(currentPath) ||
42+
!fs.statSync(currentPath).isDirectory()
43+
) {
44+
console.log(`Directory not found: ${currentPath}`);
45+
return;
46+
}
47+
}
48+
}
49+
50+
// If we reach the end of the path without wildcards, we have a valid directory
51+
callback(currentPath, fileName);
52+
}
53+
54+
function createOrUpdateFile(directoryPath, fileName) {
55+
const fileContent = `module.exports = {
56+
tabWidth: 4,
57+
semi: true,
58+
bracketSameLine: true,
59+
overrides: [
60+
{
61+
files: ["*.json", "*.yml", "*.yaml"],
62+
options: {
63+
tabWidth: 2,
64+
},
65+
}
66+
],
67+
};
68+
`;
69+
70+
const filePath = path.join(directoryPath, fileName);
71+
// Create or update the file
72+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
73+
fs.writeFileSync(filePath, fileContent);
74+
}
75+
76+
// Define the directories with wildcards
77+
const directories = [
78+
"../../../../../CoCreate-components/*/",
79+
"../../../../../CoCreate-apps/*/",
80+
"../../../../../CoCreate-plugins/*/",
81+
"../../../../../CoCreateCSS/",
82+
"../../../../../CoCreateJS/",
83+
"../../../../../CoCreateWS/",
84+
"../../../../../YellowOracle/",
85+
"../../../../../CoCreate-website/",
86+
"../../../../../CoCreate-admin/",
87+
"../../../../../CoCreate-website-old/",
88+
"../../../../../CoCreate-superadmin/",
89+
];
90+
const fileName = "prettier.config.js";
91+
92+
// Execute directory search and create/update file if the directory exists
93+
directories.forEach((directory) => {
94+
findDirectories(directory, createOrUpdateFile, fileName);
95+
});
96+
97+
console.log("Finished");

0 commit comments

Comments
 (0)