-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 configuration initialisation process (rebased) (#10)
## Why? An initial revision of the configuration initialisation processes.⚠️ Depends on https://github.com/fleek-platform/cli/tree/chore/formatter ## How? - Create static type for Fleek configuration formats - Asserts configuration file saved in the file system - Improve error message handling ## Tickets? - [PLAT-1096](https://linear.app/fleekxyz/issue/PLAT-1096/configuration-initialisation-revision) ## Contribution checklist? - [x] The commit messages are detailed - [x] The `build` command runs locally - [ ] Assets or static content are linked and stored in the project - [x] You have manually tested - [ ] You have provided tests ## Security checklist? - [ ] Sensitive data has been identified and is being protected properly - [ ] Injection has been prevented (parameterized queries, no eval or system calls) ## Preview? N/A
Showing
17 changed files
with
657 additions
and
36 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
24 changes: 16 additions & 8 deletions
24
src/commands/sites/prompts/selectConfigurationFormatPrompt.ts
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,15 +1,23 @@ | ||
import { selectPrompt } from '../../../prompts/selectPrompt'; | ||
import { t } from '../../../utils/translation'; | ||
import { getConfigFileByTypeValue } from '../../../utils/configuration'; | ||
|
||
export const selectConfigurationFormatPrompt = async () => { | ||
const choices = [ | ||
{ title: 'Typescript (fleek.config.ts)', value: 'ts' } as const, | ||
{ title: 'Javascript (fleek.config.js)', value: 'js' } as const, | ||
{ title: 'JSON (fleek.config.json)', value: 'json' } as const, | ||
]; | ||
import { FleekSiteConfigFormats } from '../../../utils/configuration/types'; | ||
|
||
return selectPrompt<(typeof choices)[number]['value']>({ | ||
const choices = Object.keys(FleekSiteConfigFormats).map((name) => { | ||
const value = | ||
FleekSiteConfigFormats[name as keyof typeof FleekSiteConfigFormats]; | ||
|
||
const configFile = getConfigFileByTypeValue(value); | ||
|
||
return { | ||
title: `${name} (${configFile})`, | ||
value, | ||
}; | ||
}); | ||
|
||
export const selectConfigurationFormatPrompt = async () => | ||
selectPrompt<(typeof choices)[number]['value']>({ | ||
message: `${t('selectFormatForSiteConf')}:`, | ||
choices, | ||
}); | ||
}; |
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,56 @@ | ||
import type { Site } from '@fleek-platform/sdk'; | ||
|
||
import { saveConfiguration } from '../../../utils/configuration/saveConfiguration'; | ||
import type { FleekRootConfig } from '../../../utils/configuration/types'; | ||
import { t } from '../../../utils/translation'; | ||
import { enterDirectoryPathPrompt } from '../prompts/enterDirectoryPathPrompt'; | ||
import { selectConfigurationFormatPrompt } from '../prompts/selectConfigurationFormatPrompt'; | ||
import { selectBuildCommandOrSkip } from './selectBuildCommandOrSkip'; | ||
import { isValidFleekConfigFormat } from '../../../utils/formats'; | ||
import { fileExists } from '../../../utils/fs'; | ||
|
||
type InitConfigurationArgs = { | ||
site: Site; | ||
onUnexpectedFormatError: (format: string) => void; | ||
onSaveConfigurationError: () => void; | ||
}; | ||
|
||
export const initConfiguration = async ({ | ||
site, | ||
onUnexpectedFormatError, | ||
onSaveConfigurationError, | ||
}: InitConfigurationArgs) => { | ||
const distDir = await enterDirectoryPathPrompt({ | ||
message: t('specifyDistDirToSiteUpl'), | ||
}); | ||
|
||
const buildCommand = await selectBuildCommandOrSkip(); | ||
|
||
const config: FleekRootConfig = { | ||
sites: [{ slug: site.slug, distDir, buildCommand }], | ||
}; | ||
|
||
const format = await selectConfigurationFormatPrompt(); | ||
|
||
if (!isValidFleekConfigFormat(format)) { | ||
onUnexpectedFormatError(format); | ||
} | ||
|
||
const configFile = await saveConfiguration({ config, format }); | ||
|
||
if (!configFile) { | ||
onSaveConfigurationError(); | ||
|
||
return; | ||
} | ||
|
||
const isFile = await fileExists(configFile); | ||
|
||
if (!isFile) { | ||
onSaveConfigurationError(); | ||
|
||
return; | ||
} | ||
|
||
return config; | ||
}; |
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
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,2 @@ | ||
/** @type {import('@fleek-platform/cli').FleekConfig} */ | ||
module.exports = $jsonContent; |
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,3 @@ | ||
import { FleekConfig } from '@fleek-platform/cli'; | ||
|
||
export default $jsonContent satisfies FleekConfig; |
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,64 @@ | ||
import { constants, promises as fs } from 'node:fs'; | ||
import { join as joinPath } from 'node:path'; | ||
import { FleekConfigMissingFileError } from '@fleek-platform/errors'; | ||
import { | ||
type FleekSiteConfigFormatValue, | ||
FleekSiteConfigFormats, | ||
} from './types'; | ||
|
||
type GetConfigurationPathArgs = { | ||
predefinedConfigPath?: string; | ||
}; | ||
|
||
export const getConfigurationPath = async ({ | ||
predefinedConfigPath, | ||
}: GetConfigurationPathArgs) => { | ||
if (predefinedConfigPath) { | ||
const absolutePath = joinPath(process.cwd(), predefinedConfigPath); | ||
|
||
return fs | ||
.access(absolutePath, constants.R_OK) | ||
.then(() => absolutePath) | ||
.catch(() => | ||
Promise.reject( | ||
new FleekConfigMissingFileError({ configPath: predefinedConfigPath }), | ||
), | ||
); | ||
} | ||
|
||
// Sorted by priority, we return only the first match | ||
const supposedFilenames = [ | ||
'fleek.config.ts', | ||
'fleek.config.js', | ||
'fleek.config.json', | ||
]; | ||
|
||
for (const supposedFilename of supposedFilenames) { | ||
const absolutePath = joinPath(process.cwd(), supposedFilename); | ||
|
||
const isSupposedFileAccessible = await fs | ||
.access(absolutePath, constants.R_OK) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
if (isSupposedFileAccessible) { | ||
return absolutePath; | ||
} | ||
} | ||
|
||
throw new FleekConfigMissingFileError({}); | ||
}; | ||
|
||
const FLEEK_CONFIG_BASENAME = 'fleek.config'; | ||
export const FLEEK_CONFIG_TMPL_JSON_PLACEHOLDER = '$jsonContent'; | ||
|
||
export const getConfigFileByTypeName = ( | ||
name: keyof typeof FleekSiteConfigFormats, | ||
) => `${FLEEK_CONFIG_BASENAME}.${FleekSiteConfigFormats[name]}`; | ||
|
||
export const getConfigFileByTypeValue = (val: FleekSiteConfigFormatValue) => | ||
`${FLEEK_CONFIG_BASENAME}.${val}`; | ||
|
||
export const getConfigTemplateByTypeName = ( | ||
name: keyof typeof FleekSiteConfigFormats, | ||
) => `${getConfigFileByTypeName(name)}.tmpl`; |
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,16 @@ | ||
export { | ||
getConfigurationPath, | ||
getConfigFileByTypeName, | ||
getConfigFileByTypeValue, | ||
getConfigTemplateByTypeName, | ||
FLEEK_CONFIG_TMPL_JSON_PLACEHOLDER, | ||
} from './getConfiguration'; | ||
export { loadConfiguration } from './loadConfiguration'; | ||
export { readConfigurationFile } from './readConfigurationFile'; | ||
export { | ||
type FleekSiteConfigFormatValue, | ||
type FleekConfig, | ||
type FleekRootConfig, | ||
type FleekSiteConfig, | ||
FleekSiteConfigFormats, | ||
} from './types'; |
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
Oops, something went wrong.