Skip to content

Commit

Permalink
feat: add config formatting (#486)
Browse files Browse the repository at this point in the history
* feat: add config formatting

* improve

* unify new line

* improve

* fix

* fix: npm not being found when installed with yarn [fixes DXJ-479] (#484)

* fix: npm not being found when installed with yarn [fixes DXJ-479]

* display both errors

* improve error

* check local npm exists

* improve formatting

* improve error message

* chore(deps): update dependency @fluencelabs/aqua-lib to v0.7.7 (#488)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(main): release fluence-cli 0.8.8 (#495)

* chore(main): release fluence-cli 0.8.8

* Apply automatic changes

---------

Co-authored-by: fluencebot <[email protected]>
Co-authored-by: shamsartem <[email protected]>

* rename

* simplify

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: fluencebot <[email protected]>
Co-authored-by: fluencebot <[email protected]>
  • Loading branch information
4 people authored Sep 26, 2023
1 parent 71acd94 commit 4767406
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 43 deletions.
62 changes: 54 additions & 8 deletions src/lib/configs/initConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const migrateConfig = async <
}

if (configString !== migratedConfigString) {
await writeFile(configPath, migratedConfigString + "\n", FS_OPTIONS);
await saveConfig(configPath, migratedConfigString);
}

return {
Expand Down Expand Up @@ -356,7 +356,7 @@ export function getReadonlyConfigInitFunction<
: `${schemaPathComment}\n${fileContent.trim()}\n`;

if (configString !== fileContent) {
await writeFile(configPath, configString, FS_OPTIONS);
await saveConfig(configPath, configString);
}
} catch {
if (getDefaultConfig === undefined) {
Expand All @@ -380,14 +380,14 @@ export function getReadonlyConfigInitFunction<
const defConf = await getDefaultConfig();

configString = docsInConfigs
? `${schemaPathComment}\n\n${documentationLinkComment}\n\n${defConf}`
? `${schemaPathComment}\n\n${documentationLinkComment}\n${defConf}`
: yamlDiffPatch(
`${schemaPathComment}${description}\n\n${documentationLinkComment}\n\n`,
`${schemaPathComment}\n${description}\n\n${documentationLinkComment}\n`,
{},
parse(defConf),
);

await writeFile(configPath, `${configString.trim()}\n`, FS_OPTIONS);
await saveConfig(configPath, configString);
}

const config: unknown = parse(configString);
Expand Down Expand Up @@ -438,6 +438,54 @@ export function getReadonlyConfigInitFunction<

const initializedConfigs = new Set<string>();

function formatConfig(configString: string) {
const formattedConfig = configString
.trim()
.split("\n")
.flatMap((line, i, ar) => {
// If it's an empty string - it was a newline before split - remove it
if (line.trim() === "") {
return [];
}

const maybePreviousLine = ar[i - 1];
const isComment = line.startsWith("#");
const isPreviousLineComment = maybePreviousLine?.startsWith("#") ?? false;

const addNewLineBeforeBlockOfComments =
isComment && !isPreviousLineComment;

if (addNewLineBeforeBlockOfComments) {
return ["", line];
}

const isFirstLine = maybePreviousLine === undefined;
const isIndentedCode = line.startsWith(" ");

const doNotAddNewLine =
isFirstLine || isIndentedCode || isComment || isPreviousLineComment;

if (doNotAddNewLine) {
return [line];
}

// If it's top level property - separate it with a new line ("" -> "\n" when joined)
return ["", line];
})
.join("\n");

return `${formattedConfig.trim()}\n`;
}

async function saveConfig(
configPath: string,
migratedConfigString: string,
): Promise<string> {
const configToSave = formatConfig(migratedConfigString);
await writeFile(configPath, configToSave, FS_OPTIONS);
return configToSave;
}

export function getConfigInitFunction<
Config extends BaseConfig,
LatestConfig extends BaseConfig,
Expand Down Expand Up @@ -524,9 +572,7 @@ export function getConfigInitFunction<
).trim()}\n`;

if (configString !== newConfigString) {
configString = newConfigString;

await writeFile(configPath, configString, FS_OPTIONS);
configString = await saveConfig(configPath, newConfigString);
}
},
$getConfigString(): string {
Expand Down
27 changes: 11 additions & 16 deletions src/lib/configs/project/fluence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,35 +498,30 @@ const getDefaultConfig = async (): Promise<string> => {
# Most importantly - what exactly you want to deploy and how
# You can use \`fluence init\` command to generate a template for new Fluence project
# config version
version: 2
# nox multiaddresses that will be used by cli to connect to the Fluence network.
# can be a list of multiaddresses or a name of the network.
relays: ${jsonStringify(DEFAULT_RELAYS_FOR_TEMPLATE)} # default: kras
# Path to the aqua file or directory with aqua files that you want to compile by default.
# Must be relative to the project root dir
aquaInputPath: ${path.relative(projectRootDir, await ensureSrcAquaMainPath())}
# A map with worker names as keys and worker configs as values
workers:
# # worker name
${DEFAULT_WORKER_NAME}:
services: [] # list of service names to be deployed to this worker
spells: [] # list of spell names to be deployed to this worker
# A map with worker names as keys and deals as values
deals:
# # worker name
${DEFAULT_WORKER_NAME}:
minWorkers: ${MIN_WORKERS} # required amount of workers to activate the deal
targetWorkers: ${TARGET_WORKERS} # max amount of workers in the deal
# Path to the aqua file or directory with aqua files that you want to compile by default.
# Must be relative to the project root dir
aquaInputPath: ${path.relative(projectRootDir, await ensureSrcAquaMainPath())}
# nox multiaddresses that will be used by cli to connect to the Fluence network.
# can be a list of multiaddresses or a name of the network.
relays: ${jsonStringify(DEFAULT_RELAYS_FOR_TEMPLATE)} # default: kras
# config version
version: 2
# # A map with service names as keys and service configs as values.
# # Service names must start with a lowercase letter and contain only letters numbers and underscores.
# # You can use \`fluence service new\` or \`fluence service add\` command to add a service
Expand Down
7 changes: 4 additions & 3 deletions src/lib/configs/project/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,17 @@ const getDefault: (name: string) => GetDefaultConfig = (
): GetDefaultConfig => {
return () => {
return `# Module type "rust" is for the source code written in rust which can be compiled into a Marine module
# config versions
version: 0
# Module type "compiled" is for the precompiled modules.
type: ${MODULE_TYPE_RUST} # default: "compiled"
# "name" property from the Cargo.toml (for module type "rust")
# or name of the precompiled .wasm file (for module type "compiled")
name: ${name}
# config versions
version: 0
# # environment variables accessible by a particular module
# # with standard Rust env API like this: std::env::var(IPFS_ADDR_ENV_NAME)
# # Module environment variables could be examined with repl
Expand Down
6 changes: 3 additions & 3 deletions src/lib/configs/project/projectSecrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const getDefault: GetDefaultConfig = () => {
return `# Defines project's secret keys that are used only in the scope of this particular Fluence project.
# You can manage project's keys using commands from \`fluence key\` group of commands
# config version
version: 0
# Key Pairs available for your fluence project
keyPairs:
[
Expand All @@ -86,9 +89,6 @@ keyPairs:
# Key pair with this name will be used for the deployment by default.
# You can override it with flags or by using keyPair properties in fluence.yaml
# defaultKeyPairName: myKeyPair
# config version
version: 0
`;
};

Expand Down
7 changes: 3 additions & 4 deletions src/lib/configs/project/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ const getDefault: (
# most importantly the modules that the service consists of.
# You can use \`fluence service new\` command to generate a template for new service
# config version
version: 0
# Service name.
# Currently it is used for the service name only when you add service to fluence.yaml using "add" command.
# But this name can be overridden to any other with the --name flag or manually in fluence.yaml
Expand All @@ -208,7 +211,6 @@ name: ${name}
# A map of modules that the service consists of.
# Service must have a facade module. Each module properties can be overridden
modules:
# # module name
facade:
# Either path to the module directory or
# URL to the tar.gz archive which contains the content of the module directory
Expand Down Expand Up @@ -248,9 +250,6 @@ modules:
# # Aliases should be used in Marine module development because it's hard to know the full path to a file
# volumes:
# alias: "some/alias/path"
# config version
version: 0
`;
};
};
Expand Down
6 changes: 3 additions & 3 deletions src/lib/configs/project/spell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ export const initReadonlySpellConfig = async (
const getDefault = (): string => {
return `# Defines a spell. You can use \`fluence spell new\` command to generate a template for new spell
# config version
version: 0
# Path to Aqua file which contains an Aqua function that you want to use as a spell
aquaFilePath: "./spell.aqua"
Expand Down Expand Up @@ -331,9 +334,6 @@ clock:
# # If this property or \`endDelaySec\` not specified, periodic execution will never end.
# # If it is in the past at the moment of spell creation on Rust peer - the spell will never be executed
# endTimestamp: '2023-07-06T23:59:59Z'
# config version
version: 0
`;
};

Expand Down
6 changes: 3 additions & 3 deletions src/lib/configs/user/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ const configSchemaV0: JSONSchemaType<ConfigV0> = {
const getDefault = () => {
return `# Defines global config for Fluence CLI
# Weather you consent to send usage data to Countly
countlyConsent: false
# config version
version: 0
# Weather you consent to send usage data to Countly
countlyConsent: false
# Whether to include commented-out documented config examples in the configs generated with the CLI
docsInConfigs: false
Expand Down
6 changes: 3 additions & 3 deletions src/lib/configs/user/userSecrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ const getDefault: GetDefaultConfig = async () => {
return `# Defines user's secret keys that can be used across different Fluence projects.
# You can manage user's keys using commands from \`fluence key\` group of commands with \`--user\` flag
# config version
version: 0
# user's key pairs
keyPairs:
- name: ${name}
secretKey: ${secretKey}
# Key pair with this name will be used for the deployment by default.
defaultKeyPairName: ${name}
# config version
version: 0
`;
};

Expand Down

0 comments on commit 4767406

Please sign in to comment.