Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 41789ba

Browse files
Make fs.copyFile sync
1 parent c037777 commit 41789ba

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed

declarations.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ interface IFileSystem {
245245
*/
246246
writeJson(filename: string, data: any, space?: string, encoding?: string): void;
247247

248-
copyFile(sourceFileName: string, destinationFileName: string): IFuture<void>;
248+
/**
249+
* Copies a file.
250+
* @param {string} sourceFileName The original file that has to be copied.
251+
* @param {string} destinationFileName The filepath where the file should be copied.
252+
* @returns {void}
253+
*/
254+
copyFile(sourceFileName: string, destinationFileName: string): void;
249255

250256
/**
251257
* Returns unique file name based on the passed name by checkin if it exists and adding numbers to the passed name until a non-existent file is found.

file-system.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -207,35 +207,20 @@ export class FileSystem implements IFileSystem {
207207
return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
208208
}
209209

210-
public copyFile(sourceFileName: string, destinationFileName: string): IFuture<void> {
210+
public copyFile(sourceFileName: string, destinationFileName: string): void {
211211
if (path.resolve(sourceFileName) === path.resolve(destinationFileName)) {
212-
return Future.fromResult();
212+
return;
213213
}
214214

215-
let res = new Future<void>();
216-
217215
this.createDirectory(path.dirname(destinationFileName));
218-
let source = this.createReadStream(sourceFileName);
219-
let target = this.createWriteStream(destinationFileName);
220216

221-
source.on("error", (e: Error) => {
222-
if (!res.isResolved()) {
223-
res.throw(e);
224-
}
225-
});
226-
target.on("finish", () => {
227-
if (!res.isResolved()) {
228-
res.return();
229-
}
230-
})
231-
.on("error", (e: Error) => {
232-
if (!res.isResolved()) {
233-
res.throw(e);
234-
}
235-
});
217+
shelljs.cp("-f", sourceFileName, destinationFileName);
236218

237-
source.pipe(target);
238-
return res;
219+
const err = shelljs.error();
220+
221+
if (err) {
222+
throw new Error(err);
223+
}
239224
}
240225

241226
public createReadStream(path: string, options?: {

services/auto-completion-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ export class AutoCompletionService implements IAutoCompletionService {
111111
}
112112
}
113113

114-
// TODO: Remove IFuture, reason: appendFile
115114
public enableAutoCompletion(): IFuture<void> {
116115
return (() => {
117116
this.updateCLIShellScript().wait();

test/unit-tests/file-system.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe("FileSystem", () => {
213213
});
214214

215215
it("correctly copies file to the same directory", () => {
216-
fs.copyFile(testFileName, newFileName).wait();
216+
fs.copyFile(testFileName, newFileName);
217217
assert.isTrue(fs.exists(newFileName), "Renamed file should exists.");
218218
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
219219
assert.deepEqual(fs.getFsStats(testFileName).size, fs.getFsStats(testFileName).size, "Original file and copied file must have the same size.");
@@ -222,15 +222,15 @@ describe("FileSystem", () => {
222222
it("copies file to non-existent directory", () => {
223223
let newFileNameInSubDir = path.join(tempDir, "subDir", "newfilename");
224224
assert.isFalse(fs.exists(newFileNameInSubDir));
225-
fs.copyFile(testFileName, newFileNameInSubDir).wait();
225+
fs.copyFile(testFileName, newFileNameInSubDir);
226226
assert.isTrue(fs.exists(newFileNameInSubDir), "Renamed file should exists.");
227227
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
228228
assert.deepEqual(fs.getFsStats(testFileName).size, fs.getFsStats(testFileName).size, "Original file and copied file must have the same size.");
229229
});
230230

231231
it("produces correct file when source and target file are the same", () => {
232232
let originalSize = fs.getFsStats(testFileName).size;
233-
fs.copyFile(testFileName, testFileName).wait();
233+
fs.copyFile(testFileName, testFileName);
234234
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
235235
assert.deepEqual(fs.getFsStats(testFileName).size, originalSize, "Original file and copied file must have the same size.");
236236
assert.deepEqual(fs.readText(testFileName), fileContent, "File content should not be changed.");

0 commit comments

Comments
 (0)