Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coolassassin committed Jan 1, 2024
1 parent 450e002 commit cd5db2d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.5.1]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions src/generateFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';

import { getTemplate } from './getTemplate';
import { getRelativePath, processPath, processObjectName, mapNameToCase } from './helpers';
import { getRelativePath, processPath, processObjectName, mapNameToCase, getIsItemExists } from './helpers';
import { ComponentFileList, Config, isTypingCase, Project, templatePlaceholdersData } from './types';

type Properties = {
Expand Down Expand Up @@ -40,7 +40,7 @@ export const generateFiles = async ({
processObjectName({ name: componentName, isFolder: true, processFileAndFolderName })
);

if (!fs.existsSync(folder)) {
if (!(await getIsItemExists(folder))) {
await fs.promises.mkdir(folder);
}

Expand Down
6 changes: 6 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,9 @@ export const getFileIndexForTemplate = (files: string, template: string): number
}
return parseInt((/\d+/.exec(names[elementIndex]) ?? ['0'])[0], 10);
};

export const getIsItemExists = (item: string) =>
fs.promises.access(item).then(
() => true,
() => false
);
8 changes: 4 additions & 4 deletions src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path';

import { CONFIG_FILE_NAME, OLD_CONFIG_FILE_NAME } from './constants';
import { getQuestionsSettings } from './getQuestionsSettings';
import { writeToConsole } from './helpers';
import { getIsItemExists, writeToConsole } from './helpers';
import { CommandLineFlags } from './types';

type Properties = {
Expand All @@ -17,12 +17,12 @@ type Properties = {

export const initialize = async ({ root, moduleRoot, commandLineFlags }: Properties) => {
const localConfigPath = path.resolve(root, CONFIG_FILE_NAME);
if (fs.existsSync(localConfigPath)) {
if (await getIsItemExists(localConfigPath)) {
return;
}

const oldLocalConfig = path.resolve(root, OLD_CONFIG_FILE_NAME);
if (fs.existsSync(oldLocalConfig)) {
if (await getIsItemExists(oldLocalConfig)) {
writeToConsole(`Please rename file ${kleur.yellow(OLD_CONFIG_FILE_NAME)} to ${kleur.yellow(CONFIG_FILE_NAME)}`);
process.exit();
return;
Expand Down Expand Up @@ -87,7 +87,7 @@ export const initialize = async ({ root, moduleRoot, commandLineFlags }: Propert

if (templatesAgreement) {
const templateFolderPath = path.resolve(root, templateFolderName);
if (!fs.existsSync(templateFolderPath)) {
if (!(await getIsItemExists(templateFolderPath))) {
await fs.promises.mkdir(templateFolderPath);
}
const defaultTempleFolder = path.resolve(moduleRoot, 'templates');
Expand Down
10 changes: 9 additions & 1 deletion src/setPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'path';

import { getQuestionsSettings } from './getQuestionsSettings';
import {
getIsItemExists,
isDirectory,
makePathShort,
processObjectName,
Expand Down Expand Up @@ -60,7 +61,14 @@ export const setPath = async ({
}: Properties): Promise<Output> => {
const { dest, update, skipSearch } = commandLineFlags;
const potentialFolders = typeof folderPath === 'string' ? [folderPath] : folderPath;
const availableFolders = potentialFolders.filter((folder) => fs.existsSync(path.resolve(root, project, folder)));
const availableFolders = [];

for (const folder of potentialFolders) {
if (!await getIsItemExists(path.resolve(root, project, folder))) {
continue;
}
availableFolders.push(folder);
}

let projectRootPath = projectRootPathInput ?? dest;

Expand Down
20 changes: 11 additions & 9 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,18 @@ describe('helpers', () => {
['[name].tsx', true],
['[name].module.css', false]
])('getIsFileAlreadyExists: is file exists "%s" ? %s', (fileNameTemplate, expected) => {
const result = getIsFileAlreadyExists({
root,
project,
fileNameTemplate,
objectName: 'TestComponent',
processFileAndFolderName: config.processFileAndFolderName,
resultPath,
projectRootPath
});

expect(
getIsFileAlreadyExists({
root,
project,
fileNameTemplate,
objectName: 'TestComponent',
processFileAndFolderName: config.processFileAndFolderName,
resultPath,
projectRootPath
})
result
).toEqual(expected);
});

Expand Down
3 changes: 2 additions & 1 deletion tests/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as consts from '../src/constants';
import { CommandLineFlags } from '../src/types';

import { mockProcess } from './testUtils';
import {getIsItemExists} from "../src/helpers";

describe('initialize', () => {
const props: Parameters<typeof initialize>[0] = {
Expand Down Expand Up @@ -67,7 +68,7 @@ describe('initialize', () => {
prompts.inject([true, true, newTemplatesFolder]);
await initialize(props);
expect(mkdirSpy).toBeCalledTimes(1);
expect(fs.existsSync(path.resolve(__dirname, '../', newTemplatesFolder))).toBe(true);
expect(await getIsItemExists(path.resolve(__dirname, '../', newTemplatesFolder))).toBe(true);
});

it('config not exists, disagree about folder for templates', async () => {
Expand Down

0 comments on commit cd5db2d

Please sign in to comment.