Skip to content

Commit

Permalink
Added File replace and replaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
Rod Johnson committed Oct 2, 2017
1 parent b1a1b85 commit 6805e55
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@atomist/automation-client",
"version": "0.1.36",
"version": "0.1.37",
"description": "Atomist automation client for running command and event handlers",
"author": "Atomist, Inc.",
"license": "GPL-3.0",
Expand Down
4 changes: 4 additions & 0 deletions src/project/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface FileAsync extends FileCore {

getContent(): Promise<string>;

replace(re: RegExp, replacement: string): Promise<this>;

replaceAll(oldLiteral: string, newLiteral: string): Promise<this>;

setPath(path: string): Promise<this>;

}
Expand Down
16 changes: 15 additions & 1 deletion src/project/support/AbstractFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,22 @@ export abstract class AbstractFile extends AbstractScriptedFlushable<File> imple
return this.recordAction(f => f.setPath(path));
}

public replace(re: RegExp, replacement: string): Promise<this> {
return this.getContent()
.then(content =>
this.setContent(content.replace(re, replacement)),
);
}

public replaceAll(oldLiteral: string, newLiteral: string): Promise<this> {
return this.getContent()
.then(content =>
this.setContent(content.split(oldLiteral).join(newLiteral)),
);
}

public recordReplace(re: RegExp, replacement: string): this {
// TODO
// TODO use replace
return this.recordSetContent(this.getContentSync().replace(re, replacement));
}

Expand Down
2 changes: 1 addition & 1 deletion test/project/git/GitProjectTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("GitProject", () => {
done();
})
.catch(done);
});
}).timeout(6000);

it("add a file, then PR push to remote repo", function(done) {
this.retries(5);
Expand Down
22 changes: 22 additions & 0 deletions test/project/util/projectUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,26 @@ describe("projectUtils", () => {
}).catch(done);
});

it("replaces literals across project", () => {
const p = tempProject();
p.addFileSync("Thing", "A");
p.addFileSync("config/Thing", "B");
doWithFiles(p, "**/Thing", f => f.replaceAll("A", "alpha"))
.run()
.then(_ => {
assert(p.findFileSync("Thing").getContentSync() === "alpha");
});
});

it("replaces regex across project", () => {
const p = tempProject();
p.addFileSync("Thing", "A");
p.addFileSync("config/Thing", "B");
doWithFiles(p, "**/Thing", f => f.replace(/A-Z/, "alpha"))
.run()
.then(_ => {
assert(p.findFileSync("Thing").getContentSync() === "alpha");
});
});

});

0 comments on commit 6805e55

Please sign in to comment.