Skip to content

Commit

Permalink
Update code around file path splitters
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxStalker committed Apr 23, 2021
1 parent 1ab27be commit d08d118
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/javascript/src/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import Handlebars from "handlebars";
import { resolve } from "path";
import { getFilesList, trimAndSplit, underscoreToCamelCase } from "utils";
import { clearPath, generateExports, readFile, writeFile } from 'utils/file'
import {getFilesList, getSplitCharacter, trimAndSplit, underscoreToCamelCase} from "../utils";
import { clearPath, generateExports, readFile, writeFile } from '../utils/file'

// Load compiled templates
import "./templates";

const processFolder = async (input, output) => {
const fullBasePath = `${resolve(input)}\\`;
const splitCharacter = getSplitCharacter(input);
const fullBasePath = `${resolve(input)}${splitCharacter}`;
const fileList = await getFilesList(input);

for (let i = 0; i < fileList.length; i++) {
const path = fileList[i]

const packages = trimAndSplit(path, fullBasePath, "\\");
const packages = trimAndSplit(path, fullBasePath);
const pathPackages = packages.slice(0, -1);
const file = packages.slice(-1)[0];

const code = readFile(path, "utf8");
const code = readFile(path);
const name = underscoreToCamelCase(file.replace(".cdc", ""));

const data = Handlebars.templates.asset({ code, name, assetName: name });
Expand All @@ -41,4 +41,5 @@ const main = async () => {

console.log("✅ Done. You can find newly generated code in src/generated folder")
};

main();
2 changes: 1 addition & 1 deletion lib/javascript/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { hashInput } from "./hash";
export { getFilesList, readFile, writeFile } from "./file";
export { underscoreToCamelCase, trimAndSplit } from "./strings";
export { underscoreToCamelCase, trimAndSplit, getSplitCharacter } from "./strings";
export { reportMissingImports, report } from "./generator";
18 changes: 17 additions & 1 deletion lib/javascript/src/utils/strings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ export const underscoreToCamelCase = (text) => {
};

export const trimAndSplit = (input, trimWith, splitBy) => {
return input.replace(trimWith, "").split(splitBy);
if (splitBy){
return input.replace(trimWith, "").split(splitBy);
}
return input.replace(trimWith, "").split(getSplitCharacter(input));
};

export const getSplitCharacter = (input) => {
switch (true){
case input.indexOf("//") >= 0:
return "//"
case input.indexOf("/") >= 0:
return "/"
case input.indexOf("\\") >= 0:
return "\\"
default:
return ""
}
}

0 comments on commit d08d118

Please sign in to comment.