Skip to content

Commit

Permalink
Fix remove command and other small improvements
Browse files Browse the repository at this point in the history
* Remove command should now remove all services, as expected

* Update vscode extension

* Fix Aqua CLI downloading

* A comment about our recommendations is now not added to .gitignore if there is nothing to add

* Added new lines to the end of the files
  • Loading branch information
shamsartem authored Jun 3, 2022
1 parent da14ac8 commit 3f3e713
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ oclif.manifest.json
/fluence.yaml
.fluence
/src/aqua
.idea
.DS_Store
**/node_modules
Cargo.lock
**/target/
.repl_history
4 changes: 2 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"editorconfig.editorconfig",
"streetsidesoftware.code-spell-checker",
"redhat.vscode-yaml",
"FluenceLabs.aqua-syntax-highlight"
"FluenceLabs.aqua"
]
}
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"/home/shams/.fluence/aqua",
"/home/shams/Projects/work/fluence-cli/artifacts"
]
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"scripts": {
"build": "shx rm -rf dist && tsc -b",
"lint": "eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --cache --fix",
"postpack": "shx rm -f oclif.manifest.json",
"prepack": "npm run build && oclif manifest && oclif readme",
"mocha-test": "mocha --forbid-only \"test/**/*.test.ts\"",
"test": "npm run mocha-test",
"posttest": "npm run lint",
"prepare": "husky install",
"check": "npm run build && npm run mocha-test",
"check": "npm run build && npm run lint-fix && npm run mocha-test",
"pre-commit": "oclif readme && git add README.md"
},
"dependencies": {
Expand Down Expand Up @@ -224,8 +225,7 @@
],
"lint-staged": {
"*.ts": [
"prettier --write",
"eslint --cache --fix"
"prettier --write"
]
},
"directories": {
Expand Down
12 changes: 6 additions & 6 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import path from "node:path";

import { Command, Flags } from "@oclif/core";

import { AquaCLI } from "../lib/aquaCli";
import { AquaCLI, initAquaCli } from "../lib/aquaCli";
import {
DeployedServiceConfig,
initAppConfig,
Expand Down Expand Up @@ -107,7 +107,7 @@ type DeployServiceOptions = Readonly<{
artifactsPath: string;
addr: string;
secretKey: string;
aquaCli: Readonly<AquaCLI>;
aquaCli: AquaCLI;
timeout: string | undefined;
}>;

Expand All @@ -127,7 +127,7 @@ const deployService = async ({
}: DeployServiceOptions): Promise<Error | DeployedServiceConfig> => {
let result: string;
try {
result = await aquaCli.run(
result = await aquaCli(
{
command: "remote deploy_service",
flags: {
Expand Down Expand Up @@ -193,7 +193,7 @@ const deployServices = async ({
let result: string;
try {
// eslint-disable-next-line no-await-in-loop
result = await aquaCli.run(
result = await aquaCli(
{
command: "remote create_service",
flags: {
Expand Down Expand Up @@ -249,8 +249,6 @@ const deploy = async ({
commandObj: CommandObj;
}): Promise<void> => {
const artifactsPath = getArtifactsPath();
const aquaCli = new AquaCLI(commandObj);
const successfullyDeployedServices: DeployedServiceConfig[] = [];
const cwd = process.cwd();
const peerId = (await confirm({
message: "Do you want to enter peerId to deploy on?",
Expand All @@ -260,6 +258,8 @@ const deploy = async ({
: getRandomRelayId();
const addr = getRelayAddr(peerId);

const aquaCli = await initAquaCli(commandObj);
const successfullyDeployedServices: DeployedServiceConfig[] = [];
for (const { name, count = 1 } of fluenceConfig.services) {
process.chdir(path.join(artifactsPath, name));
// eslint-disable-next-line no-await-in-loop
Expand Down
15 changes: 9 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const extensionsJsonSchema: JSONSchemaType<ExtensionsJson> = {
};
const validateExtensionsJson = ajv.compile(extensionsJsonSchema);
const extensionsConfig: ExtensionsJson = {
recommendations: ["redhat.vscode-yaml", "FluenceLabs.aqua-syntax-highlight"],
recommendations: ["redhat.vscode-yaml", "FluenceLabs.aqua"],
};

const ensureRecommendedExtensions = async (
Expand All @@ -129,7 +129,7 @@ const ensureRecommendedExtensions = async (
} catch {
await fsPromises.writeFile(
extensionsJsonPath,
JSON.stringify(extensionsConfig, null, 2),
JSON.stringify(extensionsConfig, null, 2) + "\n",
FS_OPTIONS
);
return;
Expand All @@ -150,7 +150,7 @@ const ensureRecommendedExtensions = async (
}
await fsPromises.writeFile(
extensionsJsonPath,
JSON.stringify(parsedFileContent, null, 2),
JSON.stringify(parsedFileContent, null, 2) + "\n",
FS_OPTIONS
);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ const ensureRecommendedSettings = async (
} catch {
await fsPromises.writeFile(
settingsJsonPath,
JSON.stringify(await getSettingsConfig(commandObj), null, 2),
JSON.stringify(await getSettingsConfig(commandObj), null, 2) + "\n",
FS_OPTIONS
);
return;
Expand All @@ -216,7 +216,7 @@ const ensureRecommendedSettings = async (
}
await fsPromises.writeFile(
settingsJsonPath,
JSON.stringify(parsedFileContent, null, 2),
JSON.stringify(parsedFileContent, null, 2) + "\n",
FS_OPTIONS
);
}
Expand All @@ -234,7 +234,10 @@ const ensureGitIgnore = async (projectPath: string): Promise<void> => {
const missingGitIgnoreEntries = GIT_IGNORE_CONTENT.split("\n")
.filter((entry): boolean => !currentGitIgnoreEntries.has(entry))
.join("\n");
gitIgnoreContent = `${currentGitIgnore}\n${missingGitIgnoreEntries}`;
gitIgnoreContent =
missingGitIgnoreEntries === ""
? currentGitIgnore
: `${currentGitIgnore}\n# recommended by Fluence Labs:\n${missingGitIgnoreEntries}\n`;
} catch {
gitIgnoreContent = GIT_IGNORE_CONTENT;
}
Expand Down
14 changes: 6 additions & 8 deletions src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fsPromises from "node:fs/promises";

import { Command, Flags } from "@oclif/core";

import { AquaCLI } from "../lib/aquaCli";
import { initAquaCli } from "../lib/aquaCli";
import {
AppConfig,
DeployedServiceConfig,
Expand Down Expand Up @@ -70,7 +70,8 @@ export default class Remove extends Command {
/**
* Gets key-pair for stuff that user selected for removal
* removes each service from the config
* removes each successfully removed service from the config and commits it to disk
* if all services removed successfully - it deletes app.yaml
* otherwise it commits not removed services and throws an error
* @param param0 { name: string; commandObj: CommandObj; timeout: string | undefined; deployedConfig: DeployedConfig;}
* @returns Promise<void>
*/
Expand All @@ -83,8 +84,6 @@ export const removeApp = async ({
timeout: string | undefined;
appConfig: AppConfig;
}>): Promise<void> => {
const aquaCli = new AquaCLI(commandObj);

const { keyPairName, timestamp, services } = appConfig;
const keyPair = await getKeyPair({ commandObj, keyPairName });

Expand All @@ -97,15 +96,15 @@ export const removeApp = async ({
return;
}

const aquaCli = await initAquaCli(commandObj);
const notRemovedServices: DeployedServiceConfig[] = [];

for (const [index, service] of services.entries()) {
for (const service of services) {
const { serviceId, peerId, name, blueprintId } = service;
const addr = getRelayAddr(peerId);

try {
// eslint-disable-next-line no-await-in-loop
await aquaCli.run(
await aquaCli(
{
command: "remote remove_service",
flags: {
Expand All @@ -126,7 +125,6 @@ export const removeApp = async ({
"deployed at": timestamp,
}
);
appConfig.services.splice(index, 1);
} catch (error) {
commandObj.warn(`When removing service\n${String(error)}`);
notRemovedServices.push(service);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { JSONSchemaType } from "ajv";

import { ajv } from "../lib/ajv";
import { ensureAppServicesAquaFile } from "../lib/aqua/ensureAppServicesAquaFile";
import { AquaCLI } from "../lib/aquaCli";
import { initAquaCli } from "../lib/aquaCli";
import { initReadonlyAppConfig } from "../lib/configs/project/app";
import { CommandObj, FS_OPTIONS } from "../lib/const";
import { usage } from "../lib/helpers/usage";
Expand Down Expand Up @@ -92,15 +92,15 @@ export default class Run extends Command {

const relay = flags.relay ?? getRelayAddr(on);

const aquaCli = new AquaCLI(this);
const data = await getRunData(flags, this);
const imports = [
flags.import,
await ensureAppServicesAquaFile(this),
await getMaybeArtifactsPath(),
];

const result = await aquaCli.run(
const aquaCli = await initAquaCli(this);
const result = await aquaCli(
{
command: "run",
flags: {
Expand Down
31 changes: 15 additions & 16 deletions src/lib/aquaCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ export type AquaCliInput =
OptionalFlags<"on" | "timeout" | "data" | "import">;
};

export class AquaCLI {
#aquaCliPathPromise: Promise<string>;

constructor(commandObj: CommandObj) {
this.#aquaCliPathPromise = ensureNpmDependency(
DEPENDENCIES.aqua,
commandObj,
"Downloading the latest version of Aqua CLI, may take a while"
);
}

async run(
export type AquaCLI = {
(
aquaCliInput: AquaCliInput,
message: string,
keyValuePairs?: Record<string, string>
): Promise<string> {
const aquaCliPath = await this.#aquaCliPathPromise;
): Promise<string>;
};

export const initAquaCli = async (commandObj: CommandObj): Promise<AquaCLI> => {
const aquaCliPath = await ensureNpmDependency(
DEPENDENCIES.aqua,
commandObj,
"Downloading Aqua CLI, may take a while"
);

return async (aquaCliInput, message, keyValuePairs): Promise<string> => {
const { command, flags } = aquaCliInput;

const timeoutNumber = Number(flags.timeout);
Expand All @@ -71,5 +70,5 @@ export class AquaCLI {
getMessageWithKeyValuePairs(message, keyValuePairs),
Number.isNaN(timeoutNumber) ? undefined : timeoutNumber
);
}
}
};
};
20 changes: 16 additions & 4 deletions src/lib/configs/initConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ensureSchema = async (
await fsPromises.mkdir(schemaDir, { recursive: true });
await fsPromises.writeFile(
path.join(schemaDir, schemaFileName),
JSON.stringify(schema, null, 2),
JSON.stringify(schema, null, 2) + "\n",
FS_OPTIONS
);
};
Expand Down Expand Up @@ -71,7 +71,11 @@ async function getConfigString<LatestConfig extends BaseConfig>(
{},
await getDefaultConfig(commandObj)
);
await fsPromises.writeFile(configPath, defaultConfigString, FS_OPTIONS);
await fsPromises.writeFile(
configPath,
defaultConfigString + "\n",
FS_OPTIONS
);
return defaultConfigString;
}
}
Expand Down Expand Up @@ -131,7 +135,11 @@ const migrateConfig = async <
);
}
if (configString !== migratedConfigString) {
await fsPromises.writeFile(configPath, migratedConfigString, FS_OPTIONS);
await fsPromises.writeFile(
configPath,
migratedConfigString + "\n",
FS_OPTIONS
);
}
return {
latestConfig: latestConfig,
Expand Down Expand Up @@ -417,7 +425,11 @@ export function initConfig<

if (configString !== newConfigString) {
configString = newConfigString;
await fsPromises.writeFile(configPath, configString, FS_OPTIONS);
await fsPromises.writeFile(
configPath,
configString + "\n",
FS_OPTIONS
);
}
},
$getConfigString(): string {
Expand Down
8 changes: 3 additions & 5 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,13 @@ export const DEPENDENCIES: Record<"aqua", Dependency> = {

export type CommandObj = Readonly<InstanceType<typeof Command>>;

export const GIT_IGNORE_CONTENT = `# recommended by Fluence Labs:
.idea
export const GIT_IGNORE_CONTENT = `.idea
.DS_Store
.vscode
.fluence
/schemas
**/node_modules
Cargo.lock
**/target/
**/artifacts/*.wasm
.repl_history`;

export const APP_SERVICES_AQUA = `data DeployedService:
Expand All @@ -104,4 +101,5 @@ export const APP_SERVICES_AQUA = `data DeployedService:
blueprintId: string
name: string
alias App : []DeployedService`;
alias App : []DeployedService
`;

0 comments on commit 3f3e713

Please sign in to comment.