Skip to content

Commit

Permalink
Add aqua compilation, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shamsartem committed Aug 4, 2022
1 parent 68287e1 commit b17d375
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 25 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Pull request and release process:
# Commands

<!-- commands -->
* [`fluence aqua`](#fluence-aqua)
* [`fluence autocomplete [SHELL]`](#fluence-autocomplete-shell)
* [`fluence dependency [NAME]`](#fluence-dependency-name)
* [`fluence deploy`](#fluence-deploy)
Expand All @@ -120,6 +121,40 @@ Pull request and release process:
* [`fluence service remove [NAME | PATH | URL]`](#fluence-service-remove-name--path--url)
* [`fluence service repl [NAME | PATH | URL]`](#fluence-service-repl-name--path--url)

## `fluence aqua`

Compile aqua file or directory that contains your .aqua files

```
USAGE
$ fluence aqua [-i <value>] [-o <value>] [--import <value>] [--air | --js] [--log-level <value>]
[--const <value>] [--no-relay] [--no-xor] [--dry] [--scheduled] [-w] [--no-input]

FLAGS
-i, --input=<path> Path to an aqua file or an input directory that contains your .aqua files
-o, --output=<path> Path to the output directory. Will be created if it doesn't exists
-w, --watch Watch aqua file or folder for changes and recompile
--air Generate .air file instead of .ts
--const=<NAME=value> Set log level
--dry Checks if compilation is succeeded, without output
--import=<path>... Path to a directory to import from. May be used several times
--js Generate .js file instead of .ts
--log-level=<level> Set log level
--no-input Don't interactively ask for any input from the user
--no-relay Do not generate a pass through the relay node
--no-xor Do not generate a wrapper that catches and displays errors
--scheduled Generate air code for script storage. Without error handling wrappers and hops on relay. Will
ignore other options

DESCRIPTION
Compile aqua file or directory that contains your .aqua files

EXAMPLES
$ fluence aqua
```
_See code: [dist/commands/aqua.ts](https://github.com/fluencelabs/fluence-cli/blob/v0.0.0/dist/commands/aqua.ts)_
## `fluence autocomplete [SHELL]`
display autocomplete installation instructions
Expand Down
31 changes: 22 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@oclif/plugin-not-found": "^2.3.1",
"ajv": "^8.11.0",
"camelcase": "^5.2.0",
"chokidar": "^3.5.3",
"decompress": "^4.2.1",
"filenamify": "^4",
"inquirer": "^8.2.4",
Expand All @@ -59,6 +60,7 @@
"@tsconfig/node16-strictest": "^1.0.1",
"@types/camelcase": "^5.2.0",
"@types/chai": "^4",
"@types/chokidar": "^2.1.3",
"@types/decompress": "^4.2.4",
"@types/iarna__toml": "^2.0.2",
"@types/inquirer": "^8.2.1",
Expand Down
150 changes: 150 additions & 0 deletions src/commands/aqua.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright 2022 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import { initAquaCli } from "../lib/aquaCli";
import { NO_INPUT_FLAG } from "../lib/const";
import { getIsInteractive } from "../lib/helpers/getIsInteractive";
import { ensureFluenceAquaDir } from "../lib/paths";
import { input } from "../lib/prompt";

export default class Aqua extends Command {
static override description =
"Compile aqua file or directory that contains your .aqua files";
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
input: Flags.string({
description:
"Path to an aqua file or an input directory that contains your .aqua files",
helpValue: "<path>",
char: "i",
}),
output: Flags.string({
description:
"Path to the output directory. Will be created if it doesn't exists",
helpValue: "<path>",
char: "o",
}),
import: Flags.string({
description:
"Path to a directory to import from. May be used several times",
helpValue: "<path>",
multiple: true,
}),
air: Flags.boolean({
description: "Generate .air file instead of .ts",
exclusive: ["js"],
}),
js: Flags.boolean({
description: "Generate .js file instead of .ts",
exclusive: ["air"],
}),
"log-level": Flags.string({
description: "Set log level",
helpValue: "<level>",
}),
const: Flags.string({
description: "Set log level",
helpValue: "<NAME=value>",
}),
"no-relay": Flags.boolean({
description: "Do not generate a pass through the relay node",
}),
"no-xor": Flags.boolean({
description: "Do not generate a wrapper that catches and displays errors",
}),
dry: Flags.boolean({
description: "Checks if compilation is succeeded, without output",
}),
scheduled: Flags.boolean({
description:
"Generate air code for script storage. Without error handling wrappers and hops on relay. Will ignore other options",
}),
watch: Flags.boolean({
description: "Watch aqua file or folder for changes and recompile",
char: "w",
}),
...NO_INPUT_FLAG,
};
async run(): Promise<void> {
const { flags } = await this.parse(Aqua);
const isInteractive = getIsInteractive(flags);

const {
watch,
input: inputFromFlags = await input({
isInteractive,
message:
"Enter path to an aqua file or an input directory that contains your .aqua files",
flagName: "input",
}),
output = await input({
isInteractive,
message:
"Enter path to the output directory. Will be created if it doesn't exists",
flagName: "input",
}),
import: importsFromFlags,
...aquaCliOptionalFlags
} = flags;

const aquaCliFlags = {
input: inputFromFlags,
output,
import: [...(importsFromFlags ?? []), await ensureFluenceAquaDir()],
...aquaCliOptionalFlags,
};

const aquaCli = await initAquaCli(this);

const compile = (): Promise<string> =>
aquaCli({ flags: aquaCliFlags }, "Compiling");

if (!watch) {
return this.log(await compile());
}

const watchingNotification = (): void =>
this.log(
`Watching for changes at ${color.yellow(aquaCliFlags.input)}...`
);

watchingNotification();

chokidar
.watch(aquaCliFlags.input, {
followSymlinks: false,
usePolling: false,
interval: 100,
binaryInterval: 300,
ignoreInitial: true,
})
.on("all", (): void => {
compile()
.then((output): void => {
this.log(output);
watchingNotification();
})
.catch((error): void => {
this.log(error);
return watchingNotification();
});
});
}
}
22 changes: 15 additions & 7 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { initNewReadonlyFluenceConfig } from "../lib/configs/project/fluence";
import {
CommandObj,
FS_OPTIONS,
RECOMMENDED_GIT_IGNORE_CONTENT,
RECOMMENDED_GITIGNORE_CONTENT,
NO_INPUT_FLAG,
} from "../lib/const";
import { getIsInteractive } from "../lib/helpers/getIsInteractive";
Expand Down Expand Up @@ -101,8 +101,12 @@ const ensureRecommendedExtensions = async (): Promise<void> => {
try {
fileContent = await fsPromises.readFile(extensionsJsonPath, FS_OPTIONS);
} catch {
fileContent = JSON.stringify({});
await fsPromises.writeFile(extensionsJsonPath, fileContent, FS_OPTIONS);
await fsPromises.writeFile(
extensionsJsonPath,
JSON.stringify(extensionsConfig, null, 2),
FS_OPTIONS
);

return;
}

Expand Down Expand Up @@ -162,8 +166,12 @@ const ensureRecommendedSettings = async (): Promise<void> => {
try {
fileContent = await fsPromises.readFile(settingsJsonPath, FS_OPTIONS);
} catch {
fileContent = JSON.stringify({});
await fsPromises.writeFile(settingsJsonPath, fileContent, FS_OPTIONS);
await fsPromises.writeFile(
settingsJsonPath,
JSON.stringify(await initSettingsConfig(), null, 2),
FS_OPTIONS
);

return;
}

Expand Down Expand Up @@ -205,7 +213,7 @@ const ensureGitIgnore = async (): Promise<void> => {
currentGitIgnoreContent.split("\n")
);

const missingGitIgnoreEntries = RECOMMENDED_GIT_IGNORE_CONTENT.split("\n")
const missingGitIgnoreEntries = RECOMMENDED_GITIGNORE_CONTENT.split("\n")
.filter((entry): boolean => !currentGitIgnoreEntries.has(entry))
.join("\n");

Expand All @@ -214,7 +222,7 @@ const ensureGitIgnore = async (): Promise<void> => {
? currentGitIgnoreContent
: `${currentGitIgnoreContent}\n# recommended by Fluence Labs:\n${missingGitIgnoreEntries}\n`;
} catch {
newGitIgnoreContent = RECOMMENDED_GIT_IGNORE_CONTENT;
newGitIgnoreContent = RECOMMENDED_GITIGNORE_CONTENT;
}

return fsPromises.writeFile(gitIgnorePath, newGitIgnoreContent, FS_OPTIONS);
Expand Down
Loading

0 comments on commit b17d375

Please sign in to comment.