Skip to content

Commit

Permalink
Allow entering path as argument to the init
Browse files Browse the repository at this point in the history
  • Loading branch information
shamsartem authored Jun 13, 2022
1 parent 9fdf756 commit 56388fa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 89 deletions.
9 changes: 1 addition & 8 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import assert from "node:assert";
import path from "node:path";

import color from "@oclif/color";
Expand All @@ -34,7 +33,6 @@ import {
FLUENCE_CONFIG_FILE_NAME,
FORCE_FLAG_NAME,
KEY_PAIR_FLAG,
NAME_ARG,
NO_INPUT_FLAG,
TIMEOUT_FLAG,
} from "../lib/const";
Expand Down Expand Up @@ -68,13 +66,10 @@ export default class Deploy extends Command {
...KEY_PAIR_FLAG,
...NO_INPUT_FLAG,
};
static override args = [
{ name: NAME_ARG, description: "Deployment config name" },
];
static override usage: string = usage(this);

async run(): Promise<void> {
const { flags, args } = await this.parse(Deploy);
const { flags } = await this.parse(Deploy);
const isInteractive = getIsInteractive(flags);
await ensureProjectFluenceDirPath(this, isInteractive);

Expand Down Expand Up @@ -108,8 +103,6 @@ export default class Deploy extends Command {
if (keyPair instanceof Error) {
this.error(keyPair.message);
}
const nameArg: unknown = args[NAME_ARG];
assert(nameArg === undefined || typeof nameArg === "string");

await deploy({
commandObj: this,
Expand Down
101 changes: 36 additions & 65 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
VSCODE_DIR_NAME,
GITIGNORE_FILE_NAME,
GIT_IGNORE_CONTENT,
NAME_ARG,
AQUA_DIR_NAME,
SETTINGS_JSON_FILE_NAME,
DEFAULT_SRC_AQUA_FILE_NAME,
Expand All @@ -47,72 +46,34 @@ import { getArtifactsPath } from "../lib/pathsGetters/getArtifactsPath";
import { getSrcAquaDirPath } from "../lib/pathsGetters/getSrcAquaDirPath";
import { input } from "../lib/prompt";

export const NAME_OR_PATH = "NAME-OR-PATH";

export default class Init extends Command {
static override description =
"Initialize fluence project in the current directory";
static override description = "Initialize fluence project";
static override examples = ["<%= config.bin %> <%= command.id %>"];
// TODO DXJ-31: add "--path" optional flag to set path of the project
static override flags = {
...NO_INPUT_FLAG,
};
static override args = [{ name: NAME_ARG, description: "Project name" }];
static override args = [
{
name: NAME_OR_PATH,
description: "New project directory name or path",
},
];
static override usage: string = usage(this);
async run(): Promise<void> {
const { args, flags } = await this.parse(Init);
const isInteractive = getIsInteractive(flags);
const projectName: unknown = args[NAME_ARG];
assert(projectName === undefined || typeof projectName === "string");
await init(this, isInteractive, projectName);
const nameOrPath: unknown = args[NAME_OR_PATH];
assert(nameOrPath === undefined || typeof nameOrPath === "string");
await init({
commandObj: this,
isInteractive,
nameOrPath,
});
}
}

const validateProjectName = async (name: string): Promise<string | true> => {
const projectPath = path.join(process.cwd(), name);
try {
await fsPromises.access(projectPath);
return `file or directory ${color.yellow(name)} already exists`;
} catch {
return true;
}
};

const ensureNameIsValid = async (
name: string,
commandObj: CommandObj,
isInteractive: boolean
): Promise<string> => {
const validOrWarning = await validateProjectName(name);
if (validOrWarning === true) {
return name;
}

if (!isInteractive) {
commandObj.error(validOrWarning);
}
commandObj.warn(validOrWarning);

const projectName = await input({
message: "Enter a valid project name:",
validate: validateProjectName,
isInteractive,
});

return projectName;
};

const getProjectPath = async (
name: string | undefined,
commandObj: CommandObj,
isInteractive: boolean
): Promise<string> => {
if (name === undefined) {
return process.cwd();
}

const validName = await ensureNameIsValid(name, commandObj, isInteractive);
return path.join(process.cwd(), validName);
};

type ExtensionsJson = {
recommendations: Array<string>;
};
Expand Down Expand Up @@ -260,16 +221,26 @@ const ensureGitIgnore = async (projectPath: string): Promise<void> => {
return fsPromises.writeFile(gitIgnorePath, gitIgnoreContent, FS_OPTIONS);
};

export const init = async (
commandObj: CommandObj,
isInteractive: boolean,
projectName?: string
): Promise<void> => {
const projectPath = await getProjectPath(
projectName,
commandObj,
isInteractive
);
type InitOptions = {
commandObj: CommandObj;
isInteractive: boolean;
nameOrPath?: string | undefined;
};

export const init = async (options: InitOptions): Promise<void> => {
const { commandObj, isInteractive, nameOrPath } = options;

const projectPath =
nameOrPath === undefined && !isInteractive
? process.cwd()
: path.resolve(
nameOrPath ??
(await input({
message:
"Enter project name or path or press enter to init in the current directory:",
isInteractive,
}))
);

try {
const fluenceDirPath = path.join(projectPath, FLUENCE_DIR_NAME);
Expand Down
15 changes: 2 additions & 13 deletions src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import assert from "node:assert";
import fsPromises from "node:fs/promises";

import color from "@oclif/color";
Expand All @@ -26,12 +25,7 @@ import {
DeployedServiceConfig,
initAppConfig,
} from "../lib/configs/project/app";
import {
CommandObj,
NAME_ARG,
NO_INPUT_FLAG,
TIMEOUT_FLAG,
} from "../lib/const";
import { CommandObj, NO_INPUT_FLAG, TIMEOUT_FLAG } from "../lib/const";
import { getIsInteractive } from "../lib/helpers/getIsInteractive";
import { getMessageWithKeyValuePairs } from "../lib/helpers/getMessageWithKeyValuePairs";
import { usage } from "../lib/helpers/usage";
Expand All @@ -47,16 +41,11 @@ export default class Remove extends Command {
...TIMEOUT_FLAG,
...NO_INPUT_FLAG,
};
static override args = [
{ name: NAME_ARG, description: "Deployment config name" },
];
static override usage: string = usage(this);
async run(): Promise<void> {
const { flags, args } = await this.parse(Remove);
const { flags } = await this.parse(Remove);
const isInteractive = getIsInteractive(flags);
await ensureProjectFluenceDirPath(this, isInteractive);
const nameFromArgs: unknown = args[NAME_ARG];
assert(nameFromArgs === undefined || typeof nameFromArgs === "string");

const appConfig = await initAppConfig(this);

Expand Down
2 changes: 0 additions & 2 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ export const TIMEOUT_FLAG = {

export const FORCE_FLAG_NAME = "force";

export const NAME_ARG = "NAME";

export type Dependency = {
name: string;
version: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pathsGetters/getProjectFluenceDirPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const ensureProjectFluenceDirPath = async (
);
}

await init(commandObj, isInteractive);
await init({ commandObj, isInteractive });

return getProjectFluenceDirPath();
};

0 comments on commit 56388fa

Please sign in to comment.