MCPcopy Index your code
hub / github.com/devcontainers/cli / getProjectName

Function getProjectName

src/spec-node/dockerCompose.ts:638–682  ·  view source on GitHub ↗
(params: DockerCLIParameters | DockerResolverParameters, workspace: Workspace, composeFiles: string[], composeConfig: any)

Source from the content-addressed store, hash-verified

636}
637
638export async function getProjectName(params: DockerCLIParameters | DockerResolverParameters, workspace: Workspace, composeFiles: string[], composeConfig: any) {
639 const { cliHost } = 'cliHost' in params ? params : params.common;
640 const newProjectName = await useNewProjectName(params);
641 const envName = toProjectName(cliHost.env.COMPOSE_PROJECT_NAME || '', newProjectName);
642 if (envName) {
643 return envName;
644 }
645 try {
646 const envPath = cliHost.path.join(cliHost.cwd, '.env');
647 const buffer = await cliHost.readFile(envPath);
648 const match = /^COMPOSE_PROJECT_NAME=(.+)$/m.exec(buffer.toString());
649 const value = match && match[1].trim();
650 const envFileName = toProjectName(value || '', newProjectName);
651 if (envFileName) {
652 return envFileName;
653 }
654 } catch (err) {
655 if (!(err && (err.code === 'ENOENT' || err.code === 'EISDIR'))) {
656 throw err;
657 }
658 }
659 if (composeConfig?.name) {
660 if (composeConfig.name !== 'devcontainer') {
661 return toProjectName(composeConfig.name, newProjectName);
662 }
663 // Check if 'devcontainer' is from a compose file or just the default.
664 for (let i = composeFiles.length - 1; i >= 0; i--) {
665 try {
666 const fragment = yaml.load((await cliHost.readFile(composeFiles[i])).toString()) || {} as any;
667 if (fragment.name) {
668 // Use composeConfig.name ('devcontainer') because fragment.name could include environment variables.
669 return toProjectName(composeConfig.name, newProjectName);
670 }
671 } catch (error) {
672 // Ignore when parsing fails due to custom yaml tags (e.g., !reset)
673 }
674 }
675 }
676 const configDir = workspace.configFolderPath;
677 const workingDir = composeFiles[0] ? cliHost.path.dirname(composeFiles[0]) : cliHost.cwd; // From https://github.com/docker/compose/blob/79557e3d3ab67c3697641d9af91866d7e400cfeb/compose/config/config.py#L290
678 if (equalPaths(cliHost.platform, workingDir, cliHost.path.join(configDir, '.devcontainer'))) {
679 return toProjectName(`${cliHost.path.basename(configDir)}_devcontainer`, newProjectName);
680 }
681 return toProjectName(cliHost.path.basename(workingDir), newProjectName);
682}
683
684function toProjectName(basename: string, newProjectName: boolean) {
685 // From https://github.com/docker/compose/blob/79557e3d3ab67c3697641d9af91866d7e400cfeb/compose/cli/command.py#L152

Callers 2

doBuildFunction · 0.90

Calls 5

equalPathsFunction · 0.90
useNewProjectNameFunction · 0.85
toProjectNameFunction · 0.85
readFileMethod · 0.65
execMethod · 0.65

Tested by

no test coverage detected