Skip to content

Commit

Permalink
Support Windows paths for path mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
stringham committed Mar 30, 2018
1 parent 01d0478 commit 493b459
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Moves TypeScript files and folders containing TypeScript and updates their relat

## Release Notes

## 1.11.3

Add support for path mapping for Windows users.

## 1.11.2

Add support for path mapping when mapping to multiple paths.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "move-ts",
"displayName": "Move TS - Move TypeScript files and update relative imports",
"description": "extension for moving typescript files and folders and updating relative imports in your workspace",
"version": "1.11.2",
"version": "1.11.3",
"publisher": "stringham",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function warn(importer: ReferenceIndexer): Thenable<boolean> {
function warnThenMove(importer:ReferenceIndexer, item:FileItem):Thenable<any> {
return warn(importer).then((success: boolean): any => {
if(success) {
return vscode.workspace.saveAll(false).then(() => {
return vscode.workspace.saveAll(false).then((): any => {
importer.startNewMove(item.sourcePath, item.targetPath);
let move = item.move(importer)
move.catch(e => {
Expand Down
2 changes: 1 addition & 1 deletion src/fileitem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class FileItem {
});
}
})
.then(() => {
.then((): any => {
return this;
}).catch(e => {
console.log('error in move', e);
Expand Down
4 changes: 2 additions & 2 deletions src/index/referenceindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export class ReferenceIndex {

for(let p in this.referencedBy) {
if(whiteList.size > 0) {
const relative = path.relative(directory, p).split('/')[0];
const relative = path.relative(directory, p).split(path.sep)[0];
if(whiteList.has(relative)) {
this.referencedBy[p].forEach(reference => {
if(added.has(reference.path)) return;
let relative2 = path.relative(directory, reference.path).split('/')[0];
let relative2 = path.relative(directory, reference.path).split(path.sep)[0];
if(!whiteList.has(relative2)) {
result.push(reference);
added.add(reference.path);
Expand Down
27 changes: 14 additions & 13 deletions src/index/referenceindexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function isInDir(dir:string, p:string) {
return !isPathToAnotherDir(relative);
}

export function asUnix(fsPath: string) {
return fsPath.replace(/\\/g, '/');
}

export class ReferenceIndexer {
changeDocumentEvent: vscode.Disposable;
Expand Down Expand Up @@ -309,7 +312,7 @@ export class ReferenceIndexer {
return vscode.workspace.findFiles(relative + '/**',undefined,100000).then(files => {
let promises = files.filter(file => {
if(whiteList.size > 0) {
return minimatch(file.fsPath, glob) && whiteList.has(path.relative(to, file.fsPath).split('/')[0]);
return minimatch(file.fsPath, glob) && whiteList.has(path.relative(to, file.fsPath).split(path.sep)[0]);
}
return minimatch(file.fsPath, glob);
}).map(file => {
Expand All @@ -319,7 +322,7 @@ export class ReferenceIndexer {
let change = references.filter(p => {
let abs = this.resolveRelativeReference(originalPath, p);
if(whiteList.size > 0) {
const name = path.relative(from, abs).split('/')[0];
const name = path.relative(from, abs).split(path.sep)[0];
if(whiteList.has(name)) {
return false;
}
Expand Down Expand Up @@ -352,7 +355,7 @@ export class ReferenceIndexer {
let change = imports.filter(p => {
let abs = this.resolveRelativeReference(reference.path, p);
if(fileNames.length > 0) {
const name = path.relative(from, abs).split('/')[0];
const name = path.relative(from, abs).split(path.sep)[0];
if(whiteList.has(name)) {
return true;
}
Expand Down Expand Up @@ -501,7 +504,7 @@ export class ReferenceIndexer {
const mapped = paths[i].slice(0,-1);
const mappedDir = path.resolve(baseUrl, mapped);
if(isInDir(mappedDir, to)) {
return p.slice(0,-1) + path.relative(mappedDir, to);
return asUnix(p.slice(0,-1) + path.relative(mappedDir, to));
}
}
}
Expand All @@ -510,22 +513,21 @@ export class ReferenceIndexer {
for(let packageName in this.packageNames) {
let packagePath = this.packageNames[packageName];
if(isInDir(packagePath, to) && !isInDir(packagePath, from)) {
return packageName + '/' + path.relative(packagePath, to);
return asUnix(path.join(packageName, path.relative(packagePath, to)));
}
}
let relativeToTsConfig = this.conf('relativeToTsconfig', false);
if(relativeToTsConfig && configInfo) {
let configDir = path.dirname(configInfo.configPath);
if(isInDir(configDir, from) && isInDir(configDir, to)) {
return path.relative(configDir, to);
return asUnix(path.relative(configDir, to));
}
}
let relative = path.relative(path.dirname(from), to);
relative = relative.replace(/\\/g, "/");
if(!relative.startsWith('.')) {
relative = './' + relative;
}
return relative;
return asUnix(relative);
}

private resolveRelativeReference(fsPath:string, reference:string):string {
Expand All @@ -551,15 +553,15 @@ export class ReferenceIndexer {
for(let i=0; i<paths.length; i++) {
const mapped = paths[i].slice(0,-1);
const mappedDir = path.resolve(baseUrl, mapped);
const potential = mappedDir + '/' + reference.substr(p.slice(0,-1).length);
const potential = path.join(mappedDir, reference.substr(p.slice(0,-1).length));
if(this.doesFileExist(potential)) {
return potential;
}
}
if(config.compilerOptions.paths[p].length == 1) {
let mapped = config.compilerOptions.paths[p][0].slice(0,-1);
let mappedDir = path.resolve(path.dirname(configPath), mapped);
return mappedDir + '/' + reference.substr(p.slice(0,-1).length);
return path.join(mappedDir, reference.substr(p.slice(0,-1).length));
}
}
}
Expand All @@ -578,7 +580,7 @@ export class ReferenceIndexer {
let prevDir = filePath;
let dir = path.dirname(filePath);
while (dir != prevDir) {
let tsConfigPath = dir + '/tsconfig.json';
let tsConfigPath = path.join(dir, 'tsconfig.json');
if (this.tsconfigs.hasOwnProperty(tsConfigPath)) {
return {config:this.tsconfigs[tsConfigPath], configPath: tsConfigPath};
}
Expand Down Expand Up @@ -641,9 +643,8 @@ export class ReferenceIndexer {
if(deleteByFile) {
this.index.deleteByPath(filePath);
}
let fsPath = filePath.replace(/[\/\\]/g, "/");

fsPath = this.removeExtension(fsPath);
let fsPath = this.removeExtension(filePath);

let references = this.getRelativeImportSpecifiers(data, fsPath);

Expand Down

0 comments on commit 493b459

Please sign in to comment.