Skip to content

Commit 8cd30be

Browse files
committed
fixed deletion bug and removed empty generator constraint
1 parent d74d5c8 commit 8cd30be

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codingap/steve",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"exports": "./index.ts",
55
"description": "STEVE is a configurable template engine that allows JavaScript to run in files to create files. It has a plugin system to allow for custom generation and a built-in site generator.",
66
"tasks": {

src/plugin/site-generator.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import { copySync, existsSync, walkSync } from '@std/fs';
12-
import { join, resolve } from '@std/path';
12+
import { join, relative } from '@std/path';
1313
import { STEVE, STEVEPlugin } from '../../index.ts';
1414

1515
/**
@@ -66,12 +66,12 @@ class SingleRoute {
6666

6767
constructor(options: SingleRouteOptions) {
6868
if (!options.render) {
69-
throw new Error("please provide 'render'!");
69+
throw new Error('please provide \'render\'!');
7070
}
7171
this.#render = options.render;
7272

7373
if (!options.data) {
74-
throw new Error("please provide 'data'!");
74+
throw new Error('please provide \'data\'!');
7575
}
7676
this.#data = options.data;
7777

@@ -97,17 +97,17 @@ class GeneratorRoute {
9797

9898
constructor(options: GeneratorRouteOptions) {
9999
if (!options.render) {
100-
throw new Error("please provide 'render'!");
100+
throw new Error('please provide \'render\'!');
101101
}
102102
this.#render = options.render;
103103

104104
if (!options.data) {
105-
throw new Error("please provide 'data'!");
105+
throw new Error('please provide \'data\'!');
106106
}
107107
this.#data = options.data;
108108

109-
if (!options.generator || options.generator.length == 0) {
110-
throw new Error('please provide a non-empty generator!');
109+
if (!options.generator) {
110+
throw new Error('please provide \'generator\'!');
111111
}
112112
this.#generator = options.generator;
113113

@@ -146,7 +146,7 @@ class SiteGenerator extends STEVEPlugin {
146146
this.#ignoredFiles = options.ignoredFiles ?? [];
147147
if (!options.outputDirectory) {
148148
throw new Error(
149-
"please provide an 'outputDirectory' for the files to go to!",
149+
'please provide an \'outputDirectory\' for the files to go to!',
150150
);
151151
}
152152
this.#outputDirectory = options.outputDirectory;
@@ -159,9 +159,10 @@ class SiteGenerator extends STEVEPlugin {
159159
// clean up the output directory by removing non-ignored files and directories
160160
if (existsSync(this.#outputDirectory)) {
161161
const files = [...walkSync(this.#outputDirectory)].filter(file => {
162-
return this.#ignoredFiles?.filter((filename) =>
163-
resolve(file.path).includes(filename)
164-
).length == 0 && !file.name.startsWith('.') && resolve(this.#outputDirectory) !== file.path
162+
const filename = relative(this.#outputDirectory, file.path);
163+
return this.#ignoredFiles?.filter((f) =>
164+
filename.includes(f)
165+
).length == 0 && !filename.startsWith('.') && filename != ''
165166
});
166167

167168
for (const file of files) {
@@ -280,7 +281,7 @@ class SiteGenerator extends STEVEPlugin {
280281

281282
if (!this.staticDirectory) {
282283
throw new Error(
283-
"there is no 'staticDirectory' defined to get static file!",
284+
'there is no \'staticDirectory\' defined to get static file!',
284285
);
285286
}
286287

test/site-generator.test.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { assertEquals, assertThrows } from '@std/assert';
1111
import { ensureDir, exists } from 'jsr:@std/fs';
1212
import { GeneratorRoute, SingleRoute, SiteGenerator, STEVE } from '../index.ts';
13+
import { join } from '@std/path/join';
1314

1415
// ~~~~~ SingleRoute testing ~~~~~
1516
Deno.test('SingleRoute: generates a single route with render content', () => {
@@ -102,20 +103,6 @@ Deno.test("GeneratorRoute: throws error when 'render' is empty", () => {
102103
);
103104
});
104105

105-
Deno.test("GeneratorRoute: throws error when 'generator' is empty", () => {
106-
assertThrows(
107-
() =>
108-
new GeneratorRoute({
109-
render: 'test string',
110-
data: {},
111-
isFile: false,
112-
generator: [],
113-
}),
114-
Error,
115-
'please provide a non-empty generator!',
116-
);
117-
});
118-
119106
Deno.test('GeneratorRoute: reads template from file when isFile is true', async () => {
120107
const tempDir = await Deno.makeTempDir();
121108
const templateFile = `${tempDir}/template.html`;
@@ -278,6 +265,11 @@ Deno.test('SiteGenerator: handles ignored files correctly', async () => {
278265
`${outputDir}/.system`,
279266
'This file should not be deleted.',
280267
);
268+
await ensureDir(join(outputDir, '.git'));
269+
await Deno.writeTextFile(
270+
join(outputDir, '.git', 'HEAD'),
271+
'This file should not be deleted.',
272+
);
281273

282274
// Create SiteGenerator instance
283275
const generator = new SiteGenerator({
@@ -294,6 +286,7 @@ Deno.test('SiteGenerator: handles ignored files correctly', async () => {
294286
// Validate that ignored files remain
295287
assertEquals(await exists(`${outputDir}/ignore.txt`), true);
296288
assertEquals(await exists(`${outputDir}/.system`), true);
289+
assertEquals(await exists(`${outputDir}/.git/HEAD`), true);
297290

298291
// Validate that non-ignored files are deleted
299292
assertEquals(await exists(`${outputDir}/delete.txt`), false);

0 commit comments

Comments
 (0)