-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli] Resolve correct submodule key inside monorepos (#5)
* [cli] Detach utils related to filesystem * [cli] Resolve correct submodule key in monorepos * [cli] Fix submodule paths
- Loading branch information
Showing
3 changed files
with
68 additions
and
45 deletions.
There are no files selected for viewing
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,21 @@ | ||
import path from 'path'; | ||
import * as zx from 'zx'; | ||
|
||
// NOTE: Assumes that current platform is case-sensitive | ||
export const haveSamePath = (haystack: string[], needle: string) => | ||
haystack.some((hay) => path.resolve(hay) === path.resolve(needle)); | ||
|
||
export const insideDir = async ( | ||
path: string, | ||
callback: () => Promise<void>, | ||
) => { | ||
const pwd = (await zx.$`pwd`).stdout; | ||
zx.cd(path); | ||
await callback(); | ||
|
||
try { | ||
zx.cd(pwd); | ||
} catch { | ||
/* ignored */ | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,49 +1,62 @@ | ||
import path from 'path'; | ||
import NodePath from 'path'; | ||
import * as zx from 'zx'; | ||
|
||
import { haveSamePath, insideDir } from './filesystem.js'; | ||
|
||
export type Submodule = { | ||
commitHash: string; | ||
path: string; | ||
url: string; | ||
gitModulePath: string; | ||
}; | ||
|
||
// NOTE: Assumes that current platform is case-sensitive | ||
const haveSamePath = (haystack: string[], needle: string) => | ||
haystack.some((hay) => path.resolve(hay) === path.resolve(needle)); | ||
|
||
type FetchSubmodulesOptions = { | ||
paths: string[] | null; | ||
}; | ||
export const fetchSubmodules = async ( | ||
options: FetchSubmodulesOptions, | ||
): Promise<Submodule[]> => { | ||
const output = await zx.$`git submodule status --recursive`; | ||
|
||
const submodules = await Promise.all( | ||
output.stdout | ||
.split('\n') | ||
.flatMap((rawLine) => { | ||
const line = rawLine.trim(); | ||
if (line.length > 0) { | ||
let [commitHash, path] = line.split(' '); | ||
if (!!options.paths && !haveSamePath(options.paths, path)) { | ||
return []; | ||
} | ||
if (commitHash.startsWith('-')) { | ||
commitHash = commitHash.slice(1); | ||
} | ||
return { commitHash, path }; | ||
} | ||
const topLevel = (await zx.$`git rev-parse --show-toplevel`).stdout.trim(); | ||
let output: string = ''; | ||
await insideDir(topLevel, async () => { | ||
output = (await zx.$`git submodule status --recursive`).stdout; | ||
}); | ||
|
||
let submodules: Submodule[] = []; | ||
|
||
const submoduleRefs = output.split('\n').flatMap((rawLine) => { | ||
const line = rawLine.trim(); | ||
if (line.length > 0) { | ||
let [commitHash, path] = line.split(' '); | ||
if (!!options.paths && !haveSamePath(options.paths, path)) { | ||
return []; | ||
}) | ||
.map(async ({ commitHash, path }) => { | ||
const url = | ||
await zx.$`git config --file .gitmodules --get submodule.${path}.url`.then( | ||
(output) => output.stdout.trim(), | ||
); | ||
return { commitHash, path, url }; | ||
}), | ||
); | ||
} | ||
if (commitHash.startsWith('-')) { | ||
commitHash = commitHash.slice(1); | ||
} | ||
return { | ||
commitHash, | ||
path: NodePath.join(topLevel, path), | ||
gitModulePath: path, | ||
}; | ||
} | ||
return []; | ||
}); | ||
|
||
for (const { commitHash, path, gitModulePath } of submoduleRefs) { | ||
await insideDir(topLevel, async () => { | ||
let pathName = path.endsWith('/') | ||
? path.substring(0, path.lastIndexOf('/')) | ||
: path; | ||
pathName = pathName.split('/').slice(-1)[0]; | ||
|
||
const url = | ||
await zx.$`git config --file .gitmodules --get submodule.${gitModulePath}.url`.then( | ||
(output) => output.stdout.trim(), | ||
); | ||
submodules.push({ commitHash, path, url, gitModulePath }); | ||
}); | ||
} | ||
|
||
return submodules; | ||
}; |