MCPcopy
hub / github.com/dmno-dev/varlock / commandFn

Function commandFn

packages/varlock/src/cli/commands/init.command.ts:54–407  ·  view source on GitHub ↗
(ctx)

Source from the content-addressed store, hash-verified

52});
53
54export const commandFn: TypedGunshiCommandFn<typeof commandSpec> = async (ctx) => {
55 const agentMode = Boolean(ctx.values.agent);
56 const jsPackageManager = detectJsPackageManager();
57
58 console.log('🧙 Hello and welcome to Varlock 🔒🔥✨');
59 if (agentMode) {
60 logLines([ansis.dim('Agent mode enabled: running init non-interactively with deterministic defaults.')]);
61 }
62
63 // scan for all .env files within current directory
64 const envFilePaths = await findEnvFiles();
65 const parsedEnvFiles: Record<string, DetectedEnvFile> = {};
66 for (const filePath of envFilePaths) {
67 const fileContents = await fs.readFile(filePath, 'utf-8');
68 const fileName = path.basename(filePath);
69 const parsedFile = await tryCatch(async () => parseEnvSpecDotEnvFile(fileContents), () => {
70 logLines([
71 '',
72 `Unable to parse ${fmt.filePath(filePath)}`,
73 'This file will be skipped.',
74 ]);
75 });
76 if (!parsedFile) {
77 continue;
78 }
79 parsedEnvFiles[fileName] = {
80 fileName,
81 fullPath: filePath,
82 parsedFile,
83 };
84 }
85
86 const existingSchemaFile = parsedEnvFiles['.env.schema'];
87
88 // * SET UP SCHEMA ---------------------------------------------
89 if (existingSchemaFile) {
90 // for now - we don't do anything if they already have a schema set up
91 // in the future, we may want to add more tools for projects that are already set up
92 logLines([
93 `It looks like you already have a ${fmt.fileName('.env.schema')} file 🎉`,
94 'This init helper is meant to help you get a new project set up.',
95 'If you need to make changes to your schema or values, you can update your files directly.',
96 'See more docs at https://varlock.dev/guides/schema',
97 ]);
98 } else {
99 const allExampleFileNames = Object.keys(parsedEnvFiles).filter((fileName) => {
100 return fileName.startsWith('.env.example') || fileName.startsWith('.env.sample');
101 });
102 let exampleFileToConvert: typeof parsedEnvFiles[keyof typeof parsedEnvFiles] | undefined;
103
104 if (allExampleFileNames.length === 1) {
105 exampleFileToConvert = parsedEnvFiles[allExampleFileNames[0]];
106 } else if (allExampleFileNames.length > 1) {
107 if (agentMode) {
108 const sortedFileNames = [...allExampleFileNames].sort((a, b) => a.localeCompare(b));
109 const preferredFileName = sortedFileNames.find((name) => name === '.env.example')
110 ?? sortedFileNames.find((name) => name.startsWith('.env.example.'))
111 ?? sortedFileNames.find((name) => name === '.env.sample')

Callers

nothing calls this directly

Calls 15

detectJsPackageManagerFunction · 0.90
logLinesFunction · 0.90
findEnvFilesFunction · 0.90
tryCatchFunction · 0.90
parseEnvSpecDotEnvFileFunction · 0.90
selectFunction · 0.90
scanCodeForEnvVarsFunction · 0.90
inferSchemaUpdatesFunction · 0.90
ensureAllItemsExistFunction · 0.90
isWellKnownEnvKeyFunction · 0.90
inferItemDecoratorsFunction · 0.90
checkIsFileGitIgnoredFunction · 0.90

Tested by

no test coverage detected