Skip to content

Commit

Permalink
Merge pull request #220 from mate-academy/vite-service
Browse files Browse the repository at this point in the history
add ViteService for React tasks
  • Loading branch information
mgrinko authored Jul 14, 2024
2 parents 85876d3 + 970ceeb commit 1b4e275
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mate-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mate-academy/scripts",
"version": "1.8.1",
"version": "1.8.2",
"description": "Scripts to init, run, test, deploy Mate academy homework projects",
"main": "bin/mateScripts.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion mate-scripts/src/commands/Build.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import fs from 'fs-extra';
import { DESTINATION_DIR } from '../constants';
import { ParcelService, ReactScriptsService } from '../services';
import { Command } from './Command';
import { NodeJsVersions } from '../typedefs';
import { ViteService } from '../services/Vite.service';

export interface BuildOptions {
shouldShowInternalLogs: boolean;
Expand All @@ -11,7 +13,10 @@ export interface BuildOptions {
export class BuildCommand extends Command {
private readonly parcel = new ParcelService(this.rootDir);

private reactScripts = new ReactScriptsService(this.rootDir);
private readonly reactScripts =
this.config.nodejsMajorVersion === NodeJsVersions.v20
? new ViteService(this.rootDir)
: new ReactScriptsService(this.rootDir);

protected common(): void {
// do nothing
Expand Down
7 changes: 6 additions & 1 deletion mate-scripts/src/commands/Start.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { JestService, ParcelService, ReactScriptsService } from '../services';
import { Command } from './Command';
import { ExecResult } from '../tools';
import { NodeJsVersions } from '../typedefs';
import { ViteService } from '../services/Vite.service';

export interface StartOptions {
shouldShowInternalLogs: boolean;
Expand All @@ -13,7 +15,10 @@ export class StartCommand extends Command {

private readonly jest = new JestService();

private readonly reactScripts = new ReactScriptsService(this.rootDir);
private readonly reactScripts =
this.config.nodejsMajorVersion === NodeJsVersions.v20
? new ViteService(this.rootDir)
: new ReactScriptsService(this.rootDir);

protected common(): void {
// do nothing
Expand Down
61 changes: 61 additions & 0 deletions mate-scripts/src/services/Vite.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import path from 'path';
import {
execBashCodeControlled, execBashCodeSync, ExecResult,
} from '../tools';

interface StartOptions {
port?: number;
open?: boolean;
showLogs?: boolean;
}

export class ViteService {
private readonly binDir = path.join(this.rootDir, 'node_modules/.bin/');

constructor(private readonly rootDir: string) {
}

start<Async extends boolean, Result = ExecResult<Async>>(
options: StartOptions = {},
async: Async = false as Async,
): Result {
const {
port,
open = true,
showLogs = false,
} = options;

const execFn = async
? execBashCodeControlled
: execBashCodeSync;

const PORT = port
? ` --port=${port}`
: '';

const OPEN = open
? ' --open'
: '';

return execFn(
`${this.binDir}vite dev${PORT}${OPEN}`,
showLogs,
) as any;
}

build(
buildPath?: string,
showLogs = true,
) {
const BUILD_PATH = buildPath
? ` --out-dir ${buildPath}`
: '';
const command = `cross-env ${this.binDir}vite build${BUILD_PATH}`;

if (showLogs) {
console.log(`Execute command: ${command}`);
}

return execBashCodeSync(command);
}
}
3 changes: 2 additions & 1 deletion mate-scripts/src/tools/getDefaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { defaultLintersConfig, defaultTestsConfig } from '../constants';
import {
Config, Linters, ProjectTypes, Tests,
Config, Linters, NodeJsVersions, ProjectTypes, Tests,
} from '../typedefs';

export const getDefaultConfig = (
projectType = ProjectTypes.None,
): Config => ({
projectType,
nodejsMajorVersion: NodeJsVersions.v14,
linters: {
...getDefaultLintersConfig(projectType),
},
Expand Down
6 changes: 6 additions & 0 deletions mate-scripts/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export enum ProjectTypes {
NodeJs = 'nodeJs',
}

export enum NodeJsVersions {
v14 = '14',
v20 = '20',
}

export interface Linters {
html: boolean;
bem: boolean;
Expand All @@ -27,6 +32,7 @@ export interface Tests {

export interface Config {
projectType: ProjectTypes;
nodejsMajorVersion: NodeJsVersions;
linters: Linters;
tests: Tests;
}

0 comments on commit 1b4e275

Please sign in to comment.