@@ -2,13 +2,14 @@ import $RefParser, { JSONSchema } from '@apidevtools/json-schema-ref-parser'
22import cleanDeep , { CleanOptions } from 'clean-deep'
33import { createHash } from 'crypto'
44import { existsSync , readFileSync } from 'fs'
5- import { readdir , readFile , writeFile } from 'fs/promises'
5+ import { access , mkdir , readdir , readFile , writeFile } from 'fs/promises'
66import { glob } from 'glob'
77import walk from 'ignore-walk'
88import { dump , load } from 'js-yaml'
99import { omit } from 'lodash'
1010import { dirname , join , resolve } from 'path'
1111import { $ , ProcessOutput , within } from 'zx'
12+ import { operatorEnv } from '../operator/validators'
1213import { terminal } from './debug'
1314import { env } from './envalid'
1415
@@ -205,21 +206,23 @@ export const getSchemaSecretsPaths = async (teams: string[]): Promise<string[]>
205206 return cleanSecretPaths
206207}
207208
208- async function ensureKeepFile ( keepFilePath : string , deps = { writeFile } ) : Promise < void > {
209- const dirPath = dirname ( keepFilePath )
210- if ( ! existsSync ( dirPath ) ) {
211- await $ `mkdir -p ${ dirname ( keepFilePath ) } `
209+ export async function ensureManifestDirectories ( ) : Promise < void > {
210+ await ensureDirectoryWithGitkeepAsync ( join ( env . ENV_DIR , operatorEnv . GITOPS_NS_MANIFESTS_RELATIVE_PATH ) )
211+ await ensureDirectoryWithGitkeepAsync ( join ( env . ENV_DIR , operatorEnv . GITOPS_GLOBAL_MANIFESTS_RELATIVE_PATH ) )
212+ }
213+
214+ async function ensureDirectoryWithGitkeepAsync ( dirPath : string , deps = { access, writeFile, mkdir } ) : Promise < void > {
215+ await deps . mkdir ( dirPath , { recursive : true } )
216+ const gitkeepPath = join ( dirPath , '.gitkeep' )
217+
218+ try {
219+ await deps . access ( gitkeepPath )
220+ } catch {
221+ await deps . writeFile ( gitkeepPath , '' )
212222 }
213- if ( existsSync ( keepFilePath ) ) return
214- // create the .keep file
215- await deps . writeFile ( keepFilePath , '' )
216223}
217224
218- export async function ensureTeamGitOpsDirectories (
219- envDir : string ,
220- values : Record < string , any > ,
221- deps = { writeFile, glob } ,
222- ) {
225+ export async function ensureTeamGitOpsDirectories ( envDir : string , values : Record < string , any > , deps = { glob } ) {
223226 const dirs = await deps . glob ( `${ envDir } /env/teams/*` )
224227 const baseGitOpsDirs = [ 'sealedsecrets' , 'workloadValues' ]
225228 const aiGitOpsDirs = [ 'databases' , 'knowledgebases' , 'agents' ]
@@ -236,10 +239,7 @@ export async function ensureTeamGitOpsDirectories(
236239
237240 await Promise . allSettled (
238241 keepFilePaths . map ( async ( keepFilePath ) => {
239- await ensureKeepFile ( keepFilePath , deps )
240- if ( ! existsSync ( dirname ( keepFilePath ) ) ) {
241- await $ `mkdir -p ${ dirname ( keepFilePath ) } `
242- }
242+ await ensureDirectoryWithGitkeepAsync ( dirname ( keepFilePath ) )
243243 } ) ,
244244 )
245245 return keepFilePaths
0 commit comments