diff --git a/runners/katacoda/dirUtils.ts b/runners/katacoda/dirUtils.ts index 2c5780a2..78d36078 100644 --- a/runners/katacoda/dirUtils.ts +++ b/runners/katacoda/dirUtils.ts @@ -19,14 +19,14 @@ export class DirUtils{ //saves the remaining path, if currentdir is the prefix of targetDir if(targetDir.substring(0,currentDir.length) == currentDir){ - return path.join(targetDir.replace(currentDir + path.sep, '')).replace("\\", "/"); + return path.join(targetDir.replace(currentDir + path.sep, '')).replace(/\\/g, "/"); } else{ //returns the absolut directory, if the first parent folder is different if(currentPaths[1] != targetPaths[1]){ - return targetDir.replace("\\", "/"); + return targetDir.replace(/\\/g, "/"); } //iterates throught currentPath array to compare parent directories @@ -42,7 +42,7 @@ export class DirUtils{ //slice targetPaths to get the relative path targetPaths = targetPaths.slice(index + 1, targetPaths.length); - return path.join(dirPath, targetPaths.join(path.sep)).replace("\\", "/"); + return path.join(dirPath, targetPaths.join(path.sep)).replace(/\\/g, "/"); } diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index 0ef680ca..ea1c7569 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -138,6 +138,27 @@ export class Katacoda extends Runner { return null; } + runCreateFile(step: Step, command: Command): RunResult{ + + let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw", "workspaces", "main")); + let workspaceDir = path.join("devonfw", "workspaces", "main"); + let filePath = path.join(command.parameters[0].substring(0,path.join(command.parameters[0]).lastIndexOf(path.sep))).replace(/\\/g, "/"); + let fileDir = path.join(workspaceDir, command.parameters[0]).replace(/\\/g, "/"); + let fileName = path.join(command.parameters[0].substring(path.join( command.parameters[0]).lastIndexOf(path.sep) + 1 , command.parameters[0].length )).replace(/\\/g, "/"); + let content = ""; + if(command.parameters.length == 2) { + content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1]), { encoding: "utf-8" }); + } + + this.steps.push({ + "title": "Create a new file", + "text": "step" + this.stepsCount + ".md" + }); + + this.renderTemplate("createFile.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, filePath: filePath, fileDir: fileDir , fileName:fileName , content: content}); + return null; + } + runBuildJava(step: Step, command: Command): RunResult{ let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw", "workspaces", "main", command.parameters[0]));; @@ -159,6 +180,8 @@ export class Katacoda extends Runner { } + + private renderTemplate(name: string, targetPath: string, variables) { let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", name), 'utf8'); let result = ejs.render(template, variables); diff --git a/runners/katacoda/templates/createFile.md b/runners/katacoda/templates/createFile.md new file mode 100644 index 00000000..03b83af4 --- /dev/null +++ b/runners/katacoda/templates/createFile.md @@ -0,0 +1,17 @@ +<%= text; %> + +<%= cdCommand; %> + +If the parent directories aren't already in the project, 'mkdir -p' will create them for you. + +`mkdir -p <%= filePath; %>`{{execute}} + +Switch to the IDE and click 'Copy to Editor'. + +'<%= fileName; %>' will be created automatically inside the newly created folder. + +
+<%= content; %>
+
+ +<%= textAfter; %> \ No newline at end of file diff --git a/spec/runners/katacoda/dirUtilsSpec.ts b/spec/runners/katacoda/dirUtilsSpec.ts index 9e7186b4..dbc864dd 100644 --- a/spec/runners/katacoda/dirUtilsSpec.ts +++ b/spec/runners/katacoda/dirUtilsSpec.ts @@ -9,16 +9,16 @@ describe("DirUtils", () => { expect(target.getCdParam(path.join('/root'), path.join('/root'))).toBe(''); }); it("changes directly to the child folder, because currentDir is the prefix of dir", () => { - expect(target.getCdParam(path.join('/root/devonfw'), path.join('/root/devonfw/setup'))).toBe(path.join('setup').replace("\\", "/")); + expect(target.getCdParam(path.join('/root/devonfw'), path.join('/root/devonfw/setup'))).toBe('setup'); }); it("returns an absolute path, because both dirs don't have matching parent folders", () => { - expect(target.getCdParam(path.join('/setup'), path.join('/root/devonfw/setup'))).toBe(path.join('/root/devonfw/setup').replace("\\", "/")); + expect(target.getCdParam(path.join('/setup'), path.join('/root/devonfw/setup'))).toBe('/root/devonfw/setup'); }); it("changes to parent folder before changing to child folder", () => { - expect(target.getCdParam(path.join('/root/devonfw'), path.join('/root/setup/folder0/folder1'))).toBe(path.join('../setup/folder0/folder1').replace("\\", "/")); + expect(target.getCdParam(path.join('/root/devonfw'), path.join('/root/setup/folder0/folder1'))).toBe('../setup/folder0/folder1'); }); it("changes to parent folder before changing to child folder and one child folder has the same position and name", () => { - expect(target.getCdParam(path.join('/root/devonfw/folder/setup'), path.join('/root/devonfw/setup/setup'))).toBe(path.join('../../setup/setup').replace("\\", "/")); + expect(target.getCdParam(path.join('/root/devonfw/folder/setup'), path.join('/root/devonfw/setup/setup'))).toBe('../../setup/setup'); }); });