Skip to content

Commit

Permalink
- Add debug print under debug mode
Browse files Browse the repository at this point in the history
- avoid checking dir item type
  • Loading branch information
t83714 committed Aug 10, 2020
1 parent 226042a commit e46fd31
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import path from "path";
import fs from "fs";
import fse from "fs-extra";
import { execSync } from "child_process";

const isDebugMode = process.env["DEBUG"] === "true" ? true : false;

function extractCfgMapDir(sourceDir: string, targetDir: string) {
let dir: fs.Dir;
try {
dir = fs.opendirSync(sourceDir);
let item: fs.Dirent;
while ((item = dir.readSync())) {
if (!item.isFile()) {
continue;
}
if (
path
.extname(item.name)
.toLowerCase()
.trim() !== ".json"
) {
continue;
}
extractCfgMapJsonFile(targetDir, path.join(sourceDir, item.name));
}
} finally {
if (dir) {
dir.closeSync();
}
if (isDebugMode) {
console.log(`Decoding files in ${sourceDir}...`);
}
const dirItems = fs.readdirSync(sourceDir, { encoding: "utf8" });
dirItems.forEach(dirItem => {
const itemPath = path.join(sourceDir, dirItem);
const isJsonFile =
path
.extname(dirItem)
.toLowerCase()
.trim() === ".json";

if (!isJsonFile) {
return;
}
// we won't check whether the dirItem is a directory here as configMap file comes with 0444 permission
// any attempt to query whther it is file or directory will always return false instead
extractCfgMapJsonFile(targetDir, itemPath);
});
}

function extractCfgMapJsonFile(targetDir: string, filePath: string) {
if (isDebugMode) {
console.log(
`Decoding configMap data file: ${filePath} to ${targetDir}`
);
}

const data = fse.readJSONSync(filePath);
const pathLists = Object.keys(data);
pathLists.forEach(p => {
Expand Down Expand Up @@ -76,11 +81,21 @@ export default function main() {
});
}

if (isDebugMode) {
console.log("List target dir before decode files: ");
console.log(execSync(`ls ${targetDir}`, { encoding: "utf8" }));
}

extractCfgMapDir(defaultCfgMapDir, targetDir);

if (fse.existsSync(extraCfgMapDir)) {
extractCfgMapDir(extraCfgMapDir, targetDir);
}

if (isDebugMode) {
console.log("List target dir after decode all files: ");
console.log(execSync(`ls ${targetDir}`, { encoding: "utf8" }));
}
} catch (e) {
console.error(`Error: ${e}`);
process.exit(-1);
Expand Down

0 comments on commit e46fd31

Please sign in to comment.