Skip to content

Commit

Permalink
feat: barebones dependency conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
0xteddybear committed Nov 20, 2024
1 parent 191c8c2 commit b64f2c8
Show file tree
Hide file tree
Showing 8 changed files with 17,878 additions and 465 deletions.
17,880 changes: 17,574 additions & 306 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions dist/licenses.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"@commitlint/cli": "17.8.0",
"@commitlint/config-conventional": "17.8.0",
"fs-extra": "11.1.1",
"husky": "8.0.3"
"husky": "8.0.3",
"lodash": "4.17.21"
},
"devDependencies": {
"@types/chai": "4.3.5",
"@types/fs-extra": "11.0.1",
"@types/glob": "8.1.0",
"@types/lodash": "4.17.13",
"@types/mocha": "10.0.1",
"@types/mock-fs": "4.13.1",
"@types/node": "20.8.6",
Expand Down
3 changes: 3 additions & 0 deletions src/createPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { execSync } from 'child_process';
import { copySolidityFiles } from './copySolidityFiles';
import { PackageJson } from './types';
import { createReadmeAndLicense } from './createReadmeAndLicense';
import { parseGitmodulesDependencies } from './parseGitmodulesDependencies';
import { ExportType } from './constants';

export const createPackage = (
Expand All @@ -25,12 +26,14 @@ export const createPackage = (
// Read and copy the input package.json
const inputPackageJson = fse.readJsonSync('./package.json');
if (!inputPackageJson) throw new Error('package.json not found');
const gitDependencies = parseGitmodulesDependencies();
// Create custom package.json in the export directory
const packageJson: PackageJson = {
name: packageFullName,
version: inputPackageJson.version,
dependencies: {
...inputPackageJson.dependencies,
...gitDependencies,
},
};
fse.writeJsonSync(`${destinationDir}/package.json`, packageJson, { spaces: 4 });
Expand Down
23 changes: 23 additions & 0 deletions src/parseGitmodulesDependencies.ts
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;
};
56 changes: 56 additions & 0 deletions test/unit/parseGitmodulesDependencies.spec.ts
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);
});
});
});
Loading

0 comments on commit b64f2c8

Please sign in to comment.