Skip to content

Commit

Permalink
Address PR concerns: support any path protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Apr 23, 2021
1 parent 14c5145 commit 50a1555
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1265,15 +1265,15 @@ export class Program {
//this file has been added in-memory, from a plugin, for example
filePathObj = {
//add an interpolated src path (since it doesn't actually exist in memory)
src: `bsc:/${util.removePkgProtocol(file.pkgPath)}`,
src: `bsc-in-memory:/${util.removeProtocol(file.pkgPath)}`,
dest: file.pkgPath
};
}

//prep the output path
let outputPath = filePathObj.dest
//replace any leading pkg:/
.replace(/^pkg:\//, '')
//replace any leading protocol
.replace(/^[-a-z_]+:\//, '')
//change any .bs file extension to .brs
.replace(/\.bs$/gi, '.brs');

Expand Down
2 changes: 1 addition & 1 deletion src/files/tests/imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('import statements', () => {
let files = program.getFiles().filter(x => !!x).map(x => {
return {
src: x.srcPath,
dest: util.removePkgProtocol(x.pkgPath)
dest: util.removeProtocol(x.pkgPath)
};
});
await program.transpile(files, stagingFolderPath);
Expand Down
31 changes: 31 additions & 0 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,35 @@ describe('util', () => {
)).to.be.true;
});
});

describe('removeProtocol', () => {
it('removes pkg:/', () => {
expect(
util.removeProtocol('pkg:/some/path')
).to.eql(
'some/path'
);
});
it('removes libpkg:/', () => {
expect(
util.removeProtocol('libpkg:/some/path')
).to.eql(
'some/path'
);
});
it('removes file:/', () => {
expect(
util.removeProtocol('file:/some/path')
).to.eql(
'some/path'
);
});
it('does nothing when protocol is missing', () => {
expect(
util.removeProtocol('some/path')
).to.eql(
'some/path'
);
});
});
});
28 changes: 20 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ export class Util {
*/
public sanitizePkgPath(pkgPath: string) {
pkgPath = pkgPath.replace(/\\/g, '/');
if (!pkgPath.startsWith('pkg:/')) {
//if there's no protocol, assume it's supposed to start with `pkg:/`
if (!this.startsWithProtocol(pkgPath)) {
pkgPath = 'pkg:/' + pkgPath;
}
return pkgPath;
}

/**
* Determine if the given path starts with a protocol
*/
public startsWithProtocol(path: string) {
return !!/^[a-z]+:\//.exec(path);
}

/**
* Given a path to a file/directory, replace all path separators with the current system's version.
* @param filePath
Expand Down Expand Up @@ -377,12 +386,14 @@ export class Util {
* @param targetPath a full pkgPath, or a path relative to the containing file
*/
public getPkgPathFromTarget(sourcePkgPath: string, targetPath: string) {
//handle some edge cases
if (targetPath === 'pkg:' || targetPath === 'pkg:/') {
const [protocol] = /^[-a-z0-9_]+:\/?/i.exec(targetPath) ?? [];

//if the target path is only a file protocol (with or without the trailing slash such as `pkg:` or `pkg:/`), nothing more can be done
if (targetPath?.length === protocol?.length) {
return null;
}
//if the target starts with 'pkg:', return as-is
if (targetPath.startsWith('pkg:/')) {
if (protocol) {
return targetPath;
}

Expand Down Expand Up @@ -1093,11 +1104,12 @@ export class Util {
}

/**
* Given a pkgPath, remove the `pkg:/` portion of the path.
* Remove leading simple protocols from a path (if present)
*/
public removePkgProtocol(pkgPath: string) {
if (pkgPath.startsWith('pkg:/')) {
return pkgPath.substring(5);
public removeProtocol(pkgPath: string) {
let match = /^[-a-z_]+:\//.exec(pkgPath);
if (match) {
return pkgPath.substring(match[0].length);
} else {
return pkgPath;
}
Expand Down

0 comments on commit 50a1555

Please sign in to comment.