-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: barebones dependency conversion #61
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 () { | ||
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 () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth also having a case where the gitmodules exist but is not in the format we expect it |
||
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); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If is not, shouldn't it throw an error describing the issue?
I see this as a NTH tho