Skip to content

Commit 326d4c3

Browse files
committed
build: added more tests and more utils functions
1 parent 790a66e commit 326d4c3

File tree

10 files changed

+103
-83
lines changed

10 files changed

+103
-83
lines changed

src/build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import path from "path";
1111

1212
const buildCommand = new Command("build");
1313

14-
buildCommand.description("Build the library using TypeScript");
14+
buildCommand.description("Build the tsParticles library using TypeScript");
1515
buildCommand.option(
1616
"-a, --all",
1717
"Do all build steps (default if no flags are specified) (same as -b -c -d -l -p -t)",

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { createCommand } from "./create/create";
44
import pkgInfo from "../package.json";
55
import { program } from "commander";
66

7-
program.name("tsparticles-cli build");
8-
program.description("tsParticles Builder");
7+
program.name("tsparticles-cli");
8+
program.description("tsParticles CLI");
99
program.version(pkgInfo.version, "-v, --version", "output the current version");
1010
program.addCommand(buildCommand);
1111
program.addCommand(createCommand);

src/create/plugin/plugin.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1+
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
12
import prompts, { type PromptObject } from "prompts";
23
import { Command } from "commander";
34
import { capitalize } from "../../utils/string-utils";
45
import { createPluginTemplate } from "./create-plugin";
5-
import { execSync } from "child_process";
6-
import fs from "fs-extra";
76
import path from "path";
87

98
const pluginCommand = new Command("plugin");
109

1110
pluginCommand.description("Create a new tsParticles plugin");
1211
pluginCommand.argument("<destination>", "Destination folder");
1312
pluginCommand.action(async (destination: string) => {
14-
let repoUrl: string;
15-
16-
const destPath = path.resolve(path.join(process.cwd(), destination)),
17-
destExists = await fs.pathExists(destPath);
18-
19-
if (destExists) {
20-
const destContents = await fs.readdir(destPath),
21-
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
22-
23-
if (destContentsNoGit.length) {
24-
throw new Error("Destination folder already exists and is not empty");
25-
}
26-
}
27-
28-
await fs.ensureDir(destPath);
29-
30-
try {
31-
repoUrl = execSync("git config --get remote.origin.url").toString();
32-
} catch {
33-
repoUrl = "";
34-
}
13+
const destPath = await getDestinationDir(destination),
14+
repoUrl = getRepositoryUrl();
3515

3616
const initialName = destPath.split(path.sep).pop(),
3717
questions: PromptObject[] = [
@@ -59,7 +39,7 @@ pluginCommand.action(async (destination: string) => {
5939

6040
const { name, description, repositoryUrl } = await prompts(questions);
6141

62-
createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42+
await createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
6343
});
6444

6545
export { pluginCommand };

src/create/preset/preset.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1+
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
12
import prompts, { type PromptObject } from "prompts";
23
import { Command } from "commander";
34
import { capitalize } from "../../utils/string-utils";
45
import { createPresetTemplate } from "./create-preset";
5-
import { execSync } from "child_process";
6-
import fs from "fs-extra";
76
import path from "path";
87

98
const presetCommand = new Command("preset");
109

1110
presetCommand.description("Create a new tsParticles preset");
1211
presetCommand.argument("<destination>", "Destination folder");
1312
presetCommand.action(async (destination: string) => {
14-
let repoUrl: string;
15-
16-
const destPath = path.resolve(path.join(process.cwd(), destination)),
17-
destExists = await fs.pathExists(destPath);
18-
19-
if (destExists) {
20-
const destContents = await fs.readdir(destPath),
21-
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
22-
23-
if (destContentsNoGit.length) {
24-
throw new Error("Destination folder already exists and is not empty");
25-
}
26-
}
27-
28-
await fs.ensureDir(destPath);
29-
30-
try {
31-
repoUrl = execSync("git config --get remote.origin.url").toString();
32-
} catch {
33-
repoUrl = "";
34-
}
13+
const destPath = await getDestinationDir(destination),
14+
repoUrl = getRepositoryUrl();
3515

3616
const initialName = destPath.split(path.sep).pop(),
3717
questions: PromptObject[] = [
@@ -59,7 +39,7 @@ presetCommand.action(async (destination: string) => {
5939

6040
const { name, description, repositoryUrl } = await prompts(questions);
6141

62-
createPresetTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42+
await createPresetTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
6343
});
6444

6545
export { presetCommand };

src/create/shape/shape.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1+
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
12
import prompts, { type PromptObject } from "prompts";
23
import { Command } from "commander";
34
import { capitalize } from "../../utils/string-utils";
45
import { createShapeTemplate } from "./create-shape";
5-
import { execSync } from "child_process";
6-
import fs from "fs-extra";
76
import path from "path";
87

98
const shapeCommand = new Command("shape");
109

1110
shapeCommand.description("Create a new tsParticles shape");
1211
shapeCommand.argument("<destination>", "Destination folder");
1312
shapeCommand.action(async (destination: string) => {
14-
let repoUrl: string;
15-
16-
const destPath = path.resolve(path.join(process.cwd(), destination)),
17-
destExists = await fs.pathExists(destPath);
18-
19-
if (destExists) {
20-
const destContents = await fs.readdir(destPath),
21-
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
22-
23-
if (destContentsNoGit.length) {
24-
throw new Error("Destination folder already exists and is not empty");
25-
}
26-
}
27-
28-
await fs.ensureDir(destPath);
29-
30-
try {
31-
repoUrl = execSync("git config --get remote.origin.url").toString();
32-
} catch {
33-
repoUrl = "";
34-
}
13+
const destPath = await getDestinationDir(destination),
14+
repoUrl = getRepositoryUrl();
3515

3616
const initialName = destPath.split(path.sep).pop(),
3717
questions: PromptObject[] = [
@@ -59,7 +39,7 @@ shapeCommand.action(async (destination: string) => {
5939

6040
const { name, description, repositoryUrl } = await prompts(questions);
6141

62-
createShapeTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42+
await createShapeTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
6343
});
6444

6545
export { shapeCommand };

src/utils/file-utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execSync } from "child_process";
12
import fs from "fs-extra";
23
import path from "path";
34

@@ -38,3 +39,37 @@ export async function replaceTokensInFiles(options: ReplaceTokensOptions[]): Pro
3839
export async function replaceTokensInFile(options: ReplaceTokensOptions): Promise<void> {
3940
await replaceTokensInFiles([options]);
4041
}
42+
43+
/**
44+
*
45+
* @param destination -
46+
* @returns the destination directory path
47+
*/
48+
export async function getDestinationDir(destination: string): Promise<string> {
49+
const destPath = path.resolve(path.join(process.cwd(), destination)),
50+
destExists = await fs.pathExists(destPath);
51+
52+
if (destExists) {
53+
const destContents = await fs.readdir(destPath),
54+
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
55+
56+
if (destContentsNoGit.length) {
57+
throw new Error("Destination folder already exists and is not empty");
58+
}
59+
}
60+
61+
await fs.ensureDir(destPath);
62+
63+
return destPath;
64+
}
65+
66+
/**
67+
* @returns the repository URL
68+
*/
69+
export function getRepositoryUrl(): string {
70+
try {
71+
return execSync("git config --get remote.origin.url").toString();
72+
} catch {
73+
return "";
74+
}
75+
}

tests/create-plugin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs-extra";
66

77
describe("create-plugin", async () => {
88
it("should have created the plugin project", async () => {
9-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "foo-plugin"));
9+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-plugin"));
1010

1111
await createPluginTemplate("foo", "Foo", "", destDir);
1212

@@ -18,7 +18,7 @@ describe("create-plugin", async () => {
1818
});
1919

2020
it("should have created the plugin project, w/ repo", async () => {
21-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "bar-plugin"));
21+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-plugin"));
2222

2323
await createPluginTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
2424

tests/create-preset.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs-extra";
66

77
describe("create-plugin", async () => {
88
it("should have created the preset project", async () => {
9-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "foo-preset"));
9+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-preset"));
1010

1111
await createPresetTemplate("foo", "Foo", "", destDir);
1212

@@ -18,7 +18,7 @@ describe("create-plugin", async () => {
1818
});
1919

2020
it("should have created the preset project, w/ repo", async () => {
21-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "bar-preset"));
21+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-preset"));
2222

2323
await createPresetTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
2424

tests/create-shape.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs-extra";
66

77
describe("create-shape", async () => {
88
it("should have created the shape project", async () => {
9-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "foo-shape"));
9+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-shape"));
1010

1111
await createShapeTemplate("foo", "Foo", "", destDir);
1212

@@ -18,7 +18,7 @@ describe("create-shape", async () => {
1818
});
1919

2020
it("should have created the shape project, w/ repo", async () => {
21-
const destDir = path.resolve(path.join(__dirname, "tests", "tmp-files", "bar-shape"));
21+
const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-shape"));
2222

2323
await createShapeTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
2424

tests/file-utils.test.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { describe, it } from "mocha";
22
import { expect } from "chai";
33
import fs from "fs-extra";
44
import path from "path";
5-
import { replaceTokensInFile, replaceTokensInFiles } from "../src/utils/file-utils";
5+
import {
6+
getDestinationDir,
7+
getRepositoryUrl,
8+
replaceTokensInFile,
9+
replaceTokensInFiles,
10+
} from "../src/utils/file-utils";
611

712
describe("file-utils", async () => {
8-
const baseDir = path.join(__dirname, "tests", "tmp-files");
13+
const baseDir = path.join(__dirname, "tmp-files");
914

1015
await fs.ensureDir(baseDir);
1116

@@ -53,4 +58,44 @@ describe("file-utils", async () => {
5358
expect(data).to.be.equal("test1");
5459
});
5560
});
61+
62+
describe("get destination dir", async () => {
63+
const destDir = await getDestinationDir(path.join(baseDir, "baz"));
64+
65+
it("should return the destination dir", () => {
66+
expect(destDir).to.be.equal(path.join(baseDir, "baz"));
67+
});
68+
69+
it("should return the destination dir", async () => {
70+
const destDir2 = await getDestinationDir(path.join(baseDir, "baz"));
71+
72+
expect(destDir2).to.be.equal(path.join(baseDir, "baz"));
73+
});
74+
75+
it("should throw exception", async () => {
76+
await fs.writeFile(path.join(baseDir, "baz", "tmp.txt"), "");
77+
78+
let ex = false;
79+
80+
try {
81+
await getDestinationDir(path.join(baseDir, "baz"));
82+
83+
console.log("never");
84+
} catch {
85+
ex = true;
86+
}
87+
88+
expect(ex).to.be.equal(true);
89+
});
90+
});
91+
92+
describe("get repository url", () => {
93+
it("should return the repository url", () => {
94+
expect(getRepositoryUrl()).to.be.not.equal("");
95+
});
96+
});
97+
98+
after(async () => {
99+
await fs.remove(baseDir);
100+
});
56101
});

0 commit comments

Comments
 (0)