-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: barebones dependency conversion
- Loading branch information
1 parent
191c8c2
commit b64f2c8
Showing
8 changed files
with
17,878 additions
and
465 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { chunk } from 'lodash'; | ||
import fse from 'fs-extra'; | ||
|
||
export const parseGitmodulesDependencies = (): { [key: string]: string } => { | ||
let iniAsStr; | ||
try { | ||
iniAsStr = fse.readFileSync('./.gitmodules', { encoding: 'utf8' }); | ||
} catch { | ||
return {}; | ||
} | ||
const matches = [...iniAsStr.matchAll(/(?:path|url) = (.*)/g)].map(regexMatch => regexMatch[1]); | ||
if (matches.length == 0) return {}; | ||
|
||
// this assumes .gitmodules to be well formed, same amount of paths and urls | ||
const dependencyPairs = chunk(matches, 2); | ||
|
||
// and path is always before url | ||
const dependencyObject = dependencyPairs.reduce<{ [key: string]: string }>((acc, curr) => { | ||
acc[curr[0].replace('lib/', '')] = curr[1]; | ||
return acc; | ||
}, {}); | ||
return dependencyObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect } from 'chai'; | ||
import mock from 'mock-fs'; | ||
import { parseGitmodulesDependencies } from '../../src/parseGitmodulesDependencies'; | ||
|
||
describe('parseGitmodulesDependencies', () => { | ||
before(() => { | ||
mock.restore(); | ||
}); | ||
after(() => { | ||
mock.restore(); | ||
}); | ||
describe('with no gitmodules file', function () { | ||
before(() => { | ||
mock({ | ||
'.gitmodules': '', | ||
}); | ||
}); | ||
it('should return an empty dependency list', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies).to.deep.eq({}); | ||
}); | ||
}); | ||
describe('with an empty file', function () { | ||
before(() => { | ||
mock({ | ||
'.gitmodules': '', | ||
}); | ||
}); | ||
it('should return an empty dependency list', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies).to.deep.eq({}); | ||
}); | ||
}); | ||
describe('with a file with several dependencies', function () { | ||
before(() => { | ||
mock({ | ||
'.gitmodules': ` | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/openzeppelin-contracts"] | ||
path = lib/openzeppelin-contracts | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
`, | ||
}); | ||
}); | ||
it('should be able to parse them', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies['forge-std']).to.eq('https://github.com/foundry-rs/forge-std'); | ||
expect(parsedDependencies['openzeppelin-contracts']).to.eq( | ||
'https://github.com/OpenZeppelin/openzeppelin-contracts', | ||
); | ||
expect(Object.keys(parsedDependencies)).to.have.length(2); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.