in src/targets/go.ts [234:285]
private parseModule(modFile: string): GoModule {
const version = this.extractVersion(path.dirname(modFile));
const majorVersion = Number(version.split('.')[0]);
// extract the module decleration (e.g 'module github.com/aws/constructs-go/constructs/v3')
const match = fs.readFileSync(modFile).toString().match(/module (.*)/);
if (!match || !match[1]) {
throw new Error(`Unable to detected module declaration in ${modFile}`);
}
// e.g 'github.com/aws/constructs-go/constructs/v3'
const cannonicalName = match[1];
// e.g ['github.com', 'aws', 'constructs-go', 'constructs', 'v3']
const cannonicalNameParts = cannonicalName.split('/');
// e.g 'github.com/aws/constructs-go'
const repoURL = cannonicalNameParts.slice(0, 3).join('/');
// e.g 'v3'
const majorVersionSuffix = majorVersion > 1 ? `/v${majorVersion}` : '';
if (!cannonicalName.endsWith(majorVersionSuffix)) {
throw new Error(`Module declaration in '${modFile}' expected to end with '${majorVersionSuffix}' since its major version is larger than 1`);
}
if (!repoURL.startsWith('github.com')) {
throw new Error(`Repository must be hosted on github.com. Found: '${repoURL}' in ${modFile}`);
}
let repoPath = cannonicalNameParts
.slice(3) // e.g ['constructs', 'v3']
.join('/'); // e.g 'constructs/v3'
// we could have something like
// constructs/v3
// or something like
// constructsv3/v3
// we only want to strip the last 'v3'
const split = repoPath.split('/');
if (split[split.length - 1] === `v${majorVersion}`) {
split.pop();
repoPath = split.join('/');
}
// strip '/' if exists (wont exist for top level modules)
repoPath = repoPath.endsWith('/') ? repoPath.substr(0, repoPath.length - 1) : repoPath;
return { modFile, version, cannonicalName, repoPath, repoURL };
}