Skip to content

Commit

Permalink
chore: Update function params as interfaces for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 16, 2024
1 parent b66325c commit 1979f77
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/commands/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const setupCommand = () => {
'will not be touched by default. Use --override to re-create them.',
);
log.info('Creating initial files for DEMS...');
createPath(cliConfig.root);
createPath({ path: cliConfig.root });
createFile({ file: cliConfig.file, content: JSON.stringify(cliConfig) });
createFile({
file: cliConfig.currentProjectFile,
Expand Down Expand Up @@ -131,13 +131,13 @@ export const setupCommand = () => {
}

fs.writeFileSync(cliConfig.currentProjectFile, currentProject);
createPath(`${projectRootPath}`);
createPath({ path: projectRootPath });
createFile({
file: `${projectRootPath}/config.json`,
content: JSON.stringify(config, null, 2),
override: confirmOverride,
});
createPath(dataPath);
createPath({ path: dataPath });
dotEnv.generate(dotEnvFile, config);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import log from '../utils/log.js';
import log from '../utils/log';
import { flattenObject } from '../utils/object';
import { type DEMSProjectConfig } from './dems';

Expand Down
7 changes: 2 additions & 5 deletions src/utils/compose.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import fs from 'node:fs';
import path from 'path';
import type { ComposeFilesParams } from './interfaces';

export const composeFiles = ({
prefix = 'compose',
filesDir = 'src/compose',
dockerDir = '.docker',
}: {
prefix?: string;
filesDir?: string;
dockerDir?: string;
} = {}): string => {
}: ComposeFilesParams): string => {
let composeFileString = '';

const readFilesRecursively = (currentDir: string) => {
Expand Down
43 changes: 19 additions & 24 deletions src/utils/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fs from 'node:fs';
import { confirm } from '@inquirer/prompts';
import type {
FileModificationOperation,
PathModificationOperation,
SourceTargetOperation,
} from './interfaces';
import log from './log';

export const isFile = (path: string): boolean => {
Expand All @@ -10,39 +15,32 @@ export const isDirectory = (path: string): boolean => {
return fs.existsSync(path) && fs.lstatSync(path).isDirectory();
};

export const copyFile = (
source: string,
target: string,
output = true,
): void => {
export const copyFile = ({
source,
target,
verbose = true,
}: SourceTargetOperation) => {
if (isFile(target)) {
if (output) log.warning(`Path: ${target} already exists.`);
if (verbose) log.warning(`Path: ${target} already exists.`);
return;
}

if (isFile(source)) {
fs.copyFileSync(source, target, 0);
if (output)
if (verbose)
log.success(`File: ${source} successfully copied to ${target}.`);
} else {
if (output) log.error('Source is not a valid file.');
if (verbose) log.error('Source is not a valid file.');
process.exit(1);
}
};

interface FileModificiationInput {
file: string;
content: string;
verbose?: boolean;
override?: boolean;
}

export const createFile = ({
file,
content,
verbose = true,
override = false,
}: FileModificiationInput): void => {
}: FileModificationOperation) => {
if (!isFile(file) || override) {
if (verbose) log.info(`Creating file: ${file}...`);
fs.writeFileSync(file, content, 'utf8');
Expand All @@ -52,7 +50,10 @@ export const createFile = ({
}
};

export const createPath = (path: string, verbose = true): void => {
export const createPath = ({
path,
verbose = true,
}: PathModificationOperation) => {
if (!fs.existsSync(path)) {
if (verbose) log.info(`Creating path: ${path}...`);
fs.mkdirSync(path, { recursive: true });
Expand All @@ -62,17 +63,11 @@ export const createPath = (path: string, verbose = true): void => {
}
};

interface PathDeletionParams {
path: string;
force?: boolean;
verbose?: boolean;
}

export const deletePath = async ({
path,
force = false,
verbose = true,
}: PathDeletionParams) => {
}: PathModificationOperation) => {
if (isDirectory(path)) {
if (
force ||
Expand Down
19 changes: 5 additions & 14 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import fs from 'node:fs';
import { createPath } from './file-system.js';
import log from './log.js';

type GitParams = {
workingDir: string;
repo: string;
ref: string;
};
import { createPath, isDirectory } from './file-system';
import type { GitParams } from './interfaces';
import log from './log';

export default class Git {
repoUrl: string;
Expand All @@ -21,7 +15,7 @@ export default class Git {

clone({ workingDir, repo, ref }: GitParams) {
this.remoteRepoExists(repo);
createPath(workingDir, false);
createPath({ path: workingDir, verbose: false });
if (this.localRepoExists(this.repoPath)) {
log.warning(`Repo ${repo} already cloned.`);
} else {
Expand All @@ -44,10 +38,7 @@ export default class Git {
}

localRepoExists(path: string) {
return (
fs.existsSync(`${path}/.git`) &&
fs.lstatSync(`${path}/.git`).isDirectory()
);
return isDirectory(`${path}/.git`);
}

remoteRepoExists(repo: string = this.repoUrl, verbose = false) {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface SourceTargetOperation {
source: string;
target: string;
verbose?: boolean;
}

export interface FileModificationOperation {
file: string;
content: string;
verbose?: boolean;
override?: boolean;
}

export interface PathModificationOperation {
path: string;
force?: boolean;
verbose?: boolean;
}

export interface GitParams {
workingDir: string;
repo: string;
ref: string;
}

export interface ComposeFilesParams {
prefix?: string;
filesDir?: string;
dockerDir?: string;
}
8 changes: 3 additions & 5 deletions src/utils/object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs';
import { isFile } from './file-system.js';
import log from './log.js';
import { isFile } from './file-system';
import log from './log';

export const flattenObject = (
// biome-ignore lint/suspicious/noExplicitAny:
Expand Down Expand Up @@ -79,9 +79,7 @@ export const replaceKeysInFile = (
}
fs.writeFileSync(filePath, updatedContent);
if (verbose)
log.success(
`File ${filePath} updated successfully with replacements.`,
);
log.success(`File ${filePath} updated successfully with replacements.`);
} catch (error) {
if (verbose)
log.error(`Error updating file ${filePath}. See below for more info:`);
Expand Down
2 changes: 1 addition & 1 deletion test/utils/file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Utils: file-system', () => {
// Create and copy file with createFile() and copyFile()
createFile({ file: file, content: content, verbose: false });
expect(isFile(file)).toBeTrue();
copyFile(file, copiedFile, false);
copyFile({ source: file, target: copiedFile, verbose: false });
expect(isFile(copiedFile)).toBeTrue();

// Ensure files are deleted
Expand Down

0 comments on commit 1979f77

Please sign in to comment.