Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### 5.62.1 04/03/2025
fix: _tpl_testing_ ignore split out into copy and render ignore patterns

### 5.62.0 04/03/2025
feat: _tpl_testing_ added to the default ignore allowing a tpl to hold own test files

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generate-it",
"version": "5.62.0",
"version": "5.62.1",
"description": "Generate-it, will generate servers, clients, web-socket and anything else you can template with nunjucks from yml files (openapi/asyncapi)",
"author": "Acrontum GmbH & Liffery Ltd",
"license": "MIT",
Expand Down
10 changes: 8 additions & 2 deletions src/lib/FileIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ConfigExtendedBase } from '@/interfaces/ConfigExtendedBase';
import FileTypeCheck from '@/lib/FileTypeCheck';
import generateFile from '@/lib/generate/generateFile';
import GenerateInterfaceFiles from '@/lib/generate/GenerateInterfaceFiles';
import isFileToIgnore from '@/lib/helpers/isFileToIgnore';
import shouldCopyOrRenderFile from '@/lib/helpers/shouldCopyOrRenderFile';
import GenerateOperation from '@/lib/generate/GenerateOperation';
import { GenerateOperationFileConfig } from '@/interfaces/GenerateOperationFileConfig';
import TemplateRenderer from '@/lib/template/TemplateRenderer';
Expand All @@ -32,9 +32,15 @@ class FileWalker {
if (err) {
throw err;
}
if (isFileToIgnore(fullpath, dirent.name, providedConfig.nodegenRc)) {
if (!shouldCopyOrRenderFile({
ignoreForWhichAction: 'render',
directoryPathContainingFilename: fullpath,
filenameBeingProcessed: dirent.name,
nodegenRc: providedConfig.nodegenRc
})) {
return Promise.resolve(false);
}

if (dirent.isDirectory()) {
return;
}
Expand Down
12 changes: 7 additions & 5 deletions src/lib/generate/generateBaseStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs-extra';
import path from 'path';
import { mergePackageJsonFiles } from '@/lib/helpers/mergePackageJsonFiles';
import { ConfigExtendedBase } from '@/interfaces';
import isFileToIgnore from '@/lib/helpers/isFileToIgnore';
import shouldCopyOrRenderFile from '@/lib/helpers/shouldCopyOrRenderFile';

/**
* Creates the base structure
Expand Down Expand Up @@ -33,11 +33,13 @@ export default (targetDir: string, templatesDir: string, config: ConfigExtendedB
}
const dir = src.substring(0, src.lastIndexOf(path.sep));
const file = src.substr(src.lastIndexOf(path.sep) + 1);
if (isFileToIgnore(dir, file, config.nodegenRc)) {
return false;
}

return true;
return shouldCopyOrRenderFile({
directoryPathContainingFilename: dir,
filenameBeingProcessed: file,
ignoreForWhichAction: 'copy',
nodegenRc: config.nodegenRc
});
},
});

Expand Down
61 changes: 0 additions & 61 deletions src/lib/helpers/__tests__/isFileToIgnore.ts

This file was deleted.

114 changes: 114 additions & 0 deletions src/lib/helpers/__tests__/shouldCopyOrRenderFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import shouldCopyOrRenderFile from '@/lib/helpers/shouldCopyOrRenderFile';
import { NodegenRc } from '@/interfaces';

describe('File Ignore Functionality', () => {
it('should skip .git, node_modules, and editor files', () => {
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/.git',
filenameBeingProcessed: 'config'
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/.idea',
filenameBeingProcessed: 'workspace'
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/.vscode',
filenameBeingProcessed: 'workspace'
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/node_modules',
filenameBeingProcessed: 'workspace'
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/_tpl_testing_/something',
filenameBeingProcessed: 'workspace'
})).toBe(false);
});
//
it('should render _tpl_testing_ but not copy them over', () => {
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'render',
directoryPathContainingFilename: 'som/dir/_tpl_testing_/something',
filenameBeingProcessed: 'workspace'
})).toBe(true);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/_tpl_testing_/something',
filenameBeingProcessed: 'workspace'
})).toBe(false);
});

it('should match exactly', () => {
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/.gitignore',
filenameBeingProcessed: 'config'
})).toBe(true);
});

it('should use configuration from NodegenRc', () => {
const nodegenRc: NodegenRc = {
nodegenDir: '',
nodegenType: '',
ignoreFiles: ['gradle']
};

expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'gradle/dir/test',
filenameBeingProcessed: 'gradle.jar',
nodegenRc
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: '.gradle/dir/test',
filenameBeingProcessed: 'file.png',
nodegenRc
})).toBe(false);
});

it('should match files using regex', () => {
const nodegenRc: NodegenRc = {
nodegenDir: '',
nodegenType: '',
ignoreFiles: ['\\.\\w{3}$'] // Ignores any file whose extension is 3 chars
};

expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'src/main/test',
filenameBeingProcessed: 'gradle.jar',
nodegenRc
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'gradle/dir/test',
filenameBeingProcessed: 'gradle.jar',
nodegenRc
})).toBe(false);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: '.gradle/dir/test',
filenameBeingProcessed: 'file.png',
nodegenRc
})).toBe(false);
});

it('should allow http paths and .njk files', () => {
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/http',
filenameBeingProcessed: 'config'
})).toBe(true);
expect(shouldCopyOrRenderFile({
ignoreForWhichAction: 'copy',
directoryPathContainingFilename: 'som/dir/http',
filenameBeingProcessed: '___op.njk'
})).toBe(true);
});
});
25 changes: 0 additions & 25 deletions src/lib/helpers/isFileToIgnore.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/lib/helpers/shouldCopyOrRenderFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NodegenRc } from '@/interfaces';
import path from 'path';

const defaultCopyIgnoreList = [`(\\.idea|\\.git|\\.vscode|node_modules|build|dist|_tpl_testing_)\\b`];
const defaultRenderIgnoreList = [`(\\.idea|\\.git|\\.vscode|node_modules|build|dist)\\b`];
let ignoreList: string;
let fullRegex: RegExp;

export default (input: {
ignoreForWhichAction: 'copy' | 'render'
directoryPathContainingFilename: string,
filenameBeingProcessed: string,
nodegenRc?: NodegenRc
}) => {

let filesToIgnore = input.nodegenRc?.ignoreFiles ??
(input.ignoreForWhichAction === 'copy' ? defaultCopyIgnoreList :
input.ignoreForWhichAction === 'render' ? defaultRenderIgnoreList : undefined);

console.log(filesToIgnore);

filesToIgnore = Array.isArray(filesToIgnore) ? filesToIgnore : [filesToIgnore];
filesToIgnore = filesToIgnore.join('|');

if (!fullRegex || filesToIgnore !== ignoreList) {
ignoreList = filesToIgnore;
fullRegex = new RegExp(filesToIgnore);
}

return !fullRegex.test(
path.join(input.directoryPathContainingFilename, input.filenameBeingProcessed)
);
};