Skip to content

Commit

Permalink
feat: #445 - SourceFile, Directory - Add moveToDirectory and copyToDi…
Browse files Browse the repository at this point in the history
…rectory.
  • Loading branch information
dsherret committed Oct 6, 2018
1 parent 1029f75 commit fc806da
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ language: node_js
node_js:
- "6"
before_script:
- npm run lint
- npm run ensure-no-project-compile-errors
- npm run build-declarations
- yarn run lint
- yarn run ensure-no-project-compile-errors
- yarn run build-declarations
script:
- npm run test-coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
- npm run test-ts-versions
- yarn run test-coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
- yarn run test-ts-versions
after_script:
- npm run code-verification
- yarn run code-verification
6 changes: 6 additions & 0 deletions docs/details/source-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ Copy a source file to a new file by specifying a new relative or absolute path:
const newSourceFile = sourceFile.copy("newFileName.ts");
// this won't throw if a file exists at the specified path
const otherSourceFile = sourceFile.copy("other.ts", { overwrite: true });
// or specify directory to copy to
sourceFile.copyToDirectory("/some/dir");
sourceFile.copyToDirectory(someDirectoryObject);
```

Note: If necessary, this will automatically update the module specifiers of the relative import and export declarations
Expand All @@ -99,6 +102,9 @@ Moves a source file to a new file by specifying a new relative or absolute path:
sourceFile.move("newFileName.ts");
// this won't throw if a file exists at the specified path
sourceFile.move("other.ts", { overwrite: true });
// or specify directory to move to
sourceFile.moveToDirectory("/some/dir");
sourceFile.moveToDirectory(someDirectoryObject);
```

Note: If necessary, this will automatically update the module specifiers of the relative import and export declarations
Expand Down
6 changes: 6 additions & 0 deletions docs/navigation/directories.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ directory.move("./newDir");
directory.move("../otherDir", { overwrite: true }); // allows overwriting (otherwise it will throw)
// or specify an absolute path
directory.move("C:\\finalDir");
// or specify the directory to move to
directory.moveToDirectory("some/directory");
directory.moveToDirectory(otherDir);
```

### Moving Immediately
Expand All @@ -182,6 +185,9 @@ directory.copy("../newDir");
directory.copy("../nextDir", { overwrite: true });
// or specify an absolute path
directory.copy("C:\\test");
// or specify the directory to copy to
directory.copyToDirectory("some/directory");
directory.copyToDirectory(otherDir);
```

Note that the directory and source files, in all these cases, won't be created until calling save on the project.
Expand Down
32 changes: 31 additions & 1 deletion lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ export declare class Directory {
}): DirectoryEmitResult;
private _emitInternal;
/**
* Copies a directory to a new directory.
* Copies the directory to a subdirectory of the specified directory.
* @param dirPathOrDirectory Directory path or directory object to copy the directory to.
* @param options Options for copying.
* @returns The new copied directory.
*/
copyToDirectory(dirPathOrDirectory: string | Directory, options?: DirectoryCopyOptions): Directory;
/**
* Copies the directory to a new directory.
* @param relativeOrAbsolutePath - The relative or absolute path to the new directory.
* @param options - Options.
* @returns The directory the copy was made to.
Expand All @@ -200,6 +207,12 @@ export declare class Directory {
* @remarks If includeTrackedFiles is true, then it will execute the pending operations in the current directory.
*/
copyImmediatelySync(relativeOrAbsolutePath: string, options?: DirectoryCopyOptions): Directory;
/**
* Moves the directory to a subdirectory of the specified directory.
* @param dirPathOrDirectory Directory path or directory object to move the directory to.
* @param options Options for moving.
*/
moveToDirectory(dirPathOrDirectory: string | Directory, options?: DirectoryMoveOptions): this;
/**
* Moves the directory to a new path.
* @param relativeOrAbsolutePath - Directory path as an absolute or relative path.
Expand Down Expand Up @@ -7140,6 +7153,15 @@ export declare class SourceFile extends SourceFileBase<ts.SourceFile> {
* @param pos - Position.
*/
getLengthFromLineStartAtPos(pos: number): number;
/**
* Copies this source file to the specified directory.
*
* This will modify the module specifiers in the new file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to copy the file to.
* @param options Options for copying.
* @returns The source file the copy was made to.
*/
copyToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileCopyOptions): SourceFile;
/**
* Copy this source file to a new file.
*
Expand All @@ -7164,6 +7186,14 @@ export declare class SourceFile extends SourceFileBase<ts.SourceFile> {
* @param options - Options for copying.
*/
copyImmediatelySync(filePath: string, options?: SourceFileCopyOptions): SourceFile;
/**
* Moves this source file to the specified directory.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to move the file to.
* @param options Options for moving.
*/
moveToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileMoveOptions): SourceFile;
/**
* Moves this source file to a new file.
*
Expand Down
28 changes: 26 additions & 2 deletions src/compiler/ast/module/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { getTextFromFormattingEdits, replaceNodeText, replaceSourceFileForFilePa
import { getNextMatchingPos, getPreviousMatchingPos } from "../../../manipulation/textSeek";
import { SourceFileStructure } from "../../../structures";
import { Constructor } from "../../../types";
import { LanguageVariant, ScriptTarget, SyntaxKind, ts } from "../../../typescript";
import { LanguageVariant, ScriptTarget, ts } from "../../../typescript";
import { ArrayUtils, EventContainer, FileUtils, ModuleUtils, SourceFileReferenceContainer, StringUtils, TypeGuards } from "../../../utils";
import { Symbol } from "../../symbols";
import { getBodyTextForStructure } from "../base/helpers";
import { TextInsertableNode, ModuledNode } from "../base";
import { callBaseSet } from "../callBaseSet";
Expand Down Expand Up @@ -151,6 +150,19 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
return StringUtils.getLengthFromLineStartAtPos(this.getFullText(), pos);
}

/**
* Copies this source file to the specified directory.
*
* This will modify the module specifiers in the new file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to copy the file to.
* @param options Options for copying.
* @returns The source file the copy was made to.
*/
copyToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileCopyOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.copy(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}

/**
* Copy this source file to a new file.
*
Expand Down Expand Up @@ -233,6 +245,18 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
return newSourceFile;
}

/**
* Moves this source file to the specified directory.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to move the file to.
* @param options Options for moving.
*/
moveToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileMoveOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.move(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}

/**
* Moves this source file to a new file.
*
Expand Down
23 changes: 22 additions & 1 deletion src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,18 @@ export class Directory {
}

/**
* Copies a directory to a new directory.
* Copies the directory to a subdirectory of the specified directory.
* @param dirPathOrDirectory Directory path or directory object to copy the directory to.
* @param options Options for copying.
* @returns The new copied directory.
*/
copyToDirectory(dirPathOrDirectory: string | Directory, options?: DirectoryCopyOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.copy(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}

/**
* Copies the directory to a new directory.
* @param relativeOrAbsolutePath - The relative or absolute path to the new directory.
* @param options - Options.
* @returns The directory the copy was made to.
Expand Down Expand Up @@ -527,6 +538,16 @@ export class Directory {
return this.context.compilerFactory.getDirectoryFromCache(newPath)!;
}

/**
* Moves the directory to a subdirectory of the specified directory.
* @param dirPathOrDirectory Directory path or directory object to move the directory to.
* @param options Options for moving.
*/
moveToDirectory(dirPathOrDirectory: string | Directory, options?: DirectoryMoveOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.move(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}

/**
* Moves the directory to a new path.
* @param relativeOrAbsolutePath - Directory path as an absolute or relative path.
Expand Down
55 changes: 55 additions & 0 deletions src/tests/compiler/module/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ describe(nameof(SourceFile), () => {
});
});

describe(nameof<SourceFile>(n => n.copyToDirectory), () => {
it("should copy when passing in an absolute path", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/File.ts" });
const newSourceFile = sourceFile.copyToDirectory("newDir");
expect(sourceFile.getFilePath()).to.equal("/File.ts");
expect(newSourceFile.getFilePath()).to.equal("/newDir/File.ts");
});

it("should copy when passing in a relative path", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/dir/other/File.ts" });
expect(sourceFile.copyToDirectory("../newDir").getFilePath()).to.equal("/dir/newDir/File.ts");
});

it("should copy when passing in a directory", () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
const dir = project.createDirectory("/newDir");
expect(sourceFile.copyToDirectory(dir).getFilePath()).to.equal("/newDir/File.ts");
});

it("should copy allowing overwrite", () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
const originalSourceFile = project.createSourceFile("/newDir/File.ts");
expect(sourceFile.copyToDirectory("/newDir", { overwrite: true }).getFilePath()).to.equal("/newDir/File.ts");
});
});

describe(nameof<SourceFile>(n => n.copyImmediately), () => {
it("should copy the source file and update the file system", async () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
Expand Down Expand Up @@ -260,6 +286,35 @@ describe(nameof(SourceFile), () => {
});
});

describe(nameof<SourceFile>(n => n.moveToDirectory), () => {
it("should move when passing in an absolute path", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/File.ts" });
sourceFile.moveToDirectory("newDir");
expect(sourceFile.getFilePath()).to.equal("/newDir/File.ts");
});

it("should move when passing in a relative path", () => {
const { sourceFile } = getInfoFromText("", { filePath: "/dir/other/File.ts" });
sourceFile.moveToDirectory("../newDir");
expect(sourceFile.getFilePath()).to.equal("/dir/newDir/File.ts");
});

it("should move when passing in a directory", () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
const dir = project.createDirectory("/newDir");
sourceFile.moveToDirectory(dir);
expect(sourceFile.getFilePath()).to.equal("/newDir/File.ts");
});

it("should move allowing overwrite", () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
const originalSourceFile = project.createSourceFile("/newDir/File.ts");
sourceFile.moveToDirectory("/newDir", { overwrite: true });
expect(sourceFile.getFilePath()).to.equal("/newDir/File.ts");
expect(originalSourceFile.wasForgotten()).to.be.true;
});
});

describe(nameof<SourceFile>(n => n.moveImmediately), () => {
it("should move the source file and update the file system", async () => {
const { sourceFile, project } = getInfoFromText("", { filePath: "/File.ts" });
Expand Down
63 changes: 63 additions & 0 deletions src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,36 @@ describe(nameof(Directory), () => {
});
});

describe(nameof<Directory>(dir => dir.copyToDirectory), () => {
it("should copy when specifying an absolute path", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
expect(dir.copyToDirectory("/otherDir").getPath()).to.equal("/otherDir/temp");
});

it("should copy when specifying a relative path", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
expect(dir.copyToDirectory("../otherDir").getPath()).to.equal("/dir/otherDir/temp");
});

it("should copy when specifying a directory", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
const otherDir = project.createDirectory("otherDir");
expect(dir.copyToDirectory(otherDir).getPath()).to.equal("/otherDir/temp");
});

it("should copy with overwrite", () => {
const project = getProject();
project.createSourceFile("/dir/temp/file.ts");
project.createSourceFile("/otherDir/temp/file.ts");
const dir = project.getDirectoryOrThrow("/dir/temp");
const newDir = dir.copyToDirectory("/otherDir", { overwrite: true });
expect(newDir.getPath()).to.equal("/otherDir/temp");
});
});

describe(nameof<Directory>(dir => dir.move), () => {
it("should move all the files and sub directories to a new directory", () => {
const project = getProject();
Expand Down Expand Up @@ -808,6 +838,39 @@ describe(nameof(Directory), () => {
});
});

describe(nameof<Directory>(dir => dir.moveToDirectory), () => {
it("should move when specifying an absolute path", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
dir.moveToDirectory("/otherDir");
expect(dir.getPath()).to.equal("/otherDir/temp");
});

it("should move when specifying a relative path", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
dir.moveToDirectory("../otherDir");
expect(dir.getPath()).to.equal("/dir/otherDir/temp");
});

it("should move when specifying a directory", () => {
const project = getProject();
const dir = project.createDirectory("dir/temp");
const otherDir = project.createDirectory("otherDir");
dir.moveToDirectory(otherDir);
expect(dir.getPath()).to.equal("/otherDir/temp");
});

it("should move with overwrite", () => {
const project = getProject();
project.createSourceFile("/dir/temp/file.ts");
project.createSourceFile("/otherDir/temp/file.ts");
const dir = project.getDirectoryOrThrow("/dir/temp");
dir.moveToDirectory("/otherDir", { overwrite: true });
expect(dir.getPath()).to.equal("/otherDir/temp");
});
});

describe(nameof<Directory>(d => d.delete), () => {
it("should delete the file and remove all its descendants", () => {
const fileSystem = getFileSystemHostWithFiles([], []);
Expand Down

0 comments on commit fc806da

Please sign in to comment.