Skip to content

Commit

Permalink
build: added more tests and more utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobruni committed Aug 6, 2023
1 parent 790a66e commit 326d4c3
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import path from "path";

const buildCommand = new Command("build");

buildCommand.description("Build the library using TypeScript");
buildCommand.description("Build the tsParticles library using TypeScript");
buildCommand.option(
"-a, --all",
"Do all build steps (default if no flags are specified) (same as -b -c -d -l -p -t)",
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createCommand } from "./create/create";
import pkgInfo from "../package.json";
import { program } from "commander";

program.name("tsparticles-cli build");
program.description("tsParticles Builder");
program.name("tsparticles-cli");
program.description("tsParticles CLI");
program.version(pkgInfo.version, "-v, --version", "output the current version");
program.addCommand(buildCommand);
program.addCommand(createCommand);
Expand Down
28 changes: 4 additions & 24 deletions src/create/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
import prompts, { type PromptObject } from "prompts";
import { Command } from "commander";
import { capitalize } from "../../utils/string-utils";
import { createPluginTemplate } from "./create-plugin";
import { execSync } from "child_process";
import fs from "fs-extra";
import path from "path";

const pluginCommand = new Command("plugin");

pluginCommand.description("Create a new tsParticles plugin");
pluginCommand.argument("<destination>", "Destination folder");
pluginCommand.action(async (destination: string) => {
let repoUrl: string;

const destPath = path.resolve(path.join(process.cwd(), destination)),
destExists = await fs.pathExists(destPath);

if (destExists) {
const destContents = await fs.readdir(destPath),
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");

if (destContentsNoGit.length) {
throw new Error("Destination folder already exists and is not empty");
}
}

await fs.ensureDir(destPath);

try {
repoUrl = execSync("git config --get remote.origin.url").toString();
} catch {
repoUrl = "";
}
const destPath = await getDestinationDir(destination),
repoUrl = getRepositoryUrl();

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

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

createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
await createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
});

export { pluginCommand };
28 changes: 4 additions & 24 deletions src/create/preset/preset.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
import prompts, { type PromptObject } from "prompts";
import { Command } from "commander";
import { capitalize } from "../../utils/string-utils";
import { createPresetTemplate } from "./create-preset";
import { execSync } from "child_process";
import fs from "fs-extra";
import path from "path";

const presetCommand = new Command("preset");

presetCommand.description("Create a new tsParticles preset");
presetCommand.argument("<destination>", "Destination folder");
presetCommand.action(async (destination: string) => {
let repoUrl: string;

const destPath = path.resolve(path.join(process.cwd(), destination)),
destExists = await fs.pathExists(destPath);

if (destExists) {
const destContents = await fs.readdir(destPath),
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");

if (destContentsNoGit.length) {
throw new Error("Destination folder already exists and is not empty");
}
}

await fs.ensureDir(destPath);

try {
repoUrl = execSync("git config --get remote.origin.url").toString();
} catch {
repoUrl = "";
}
const destPath = await getDestinationDir(destination),
repoUrl = getRepositoryUrl();

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

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

createPresetTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
await createPresetTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
});

export { presetCommand };
28 changes: 4 additions & 24 deletions src/create/shape/shape.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
import prompts, { type PromptObject } from "prompts";
import { Command } from "commander";
import { capitalize } from "../../utils/string-utils";
import { createShapeTemplate } from "./create-shape";
import { execSync } from "child_process";
import fs from "fs-extra";
import path from "path";

const shapeCommand = new Command("shape");

shapeCommand.description("Create a new tsParticles shape");
shapeCommand.argument("<destination>", "Destination folder");
shapeCommand.action(async (destination: string) => {
let repoUrl: string;

const destPath = path.resolve(path.join(process.cwd(), destination)),
destExists = await fs.pathExists(destPath);

if (destExists) {
const destContents = await fs.readdir(destPath),
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");

if (destContentsNoGit.length) {
throw new Error("Destination folder already exists and is not empty");
}
}

await fs.ensureDir(destPath);

try {
repoUrl = execSync("git config --get remote.origin.url").toString();
} catch {
repoUrl = "";
}
const destPath = await getDestinationDir(destination),
repoUrl = getRepositoryUrl();

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

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

createShapeTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
await createShapeTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
});

export { shapeCommand };
35 changes: 35 additions & 0 deletions src/utils/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from "child_process";
import fs from "fs-extra";
import path from "path";

Expand Down Expand Up @@ -38,3 +39,37 @@ export async function replaceTokensInFiles(options: ReplaceTokensOptions[]): Pro
export async function replaceTokensInFile(options: ReplaceTokensOptions): Promise<void> {
await replaceTokensInFiles([options]);
}

/**
*
* @param destination -
* @returns the destination directory path
*/
export async function getDestinationDir(destination: string): Promise<string> {
const destPath = path.resolve(path.join(process.cwd(), destination)),
destExists = await fs.pathExists(destPath);

if (destExists) {
const destContents = await fs.readdir(destPath),
destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");

if (destContentsNoGit.length) {
throw new Error("Destination folder already exists and is not empty");
}
}

await fs.ensureDir(destPath);

return destPath;
}

/**
* @returns the repository URL
*/
export function getRepositoryUrl(): string {
try {
return execSync("git config --get remote.origin.url").toString();
} catch {
return "";
}
}
4 changes: 2 additions & 2 deletions tests/create-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "fs-extra";

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/create-preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "fs-extra";

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/create-shape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "fs-extra";

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

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

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

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

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

Expand Down
49 changes: 47 additions & 2 deletions tests/file-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { describe, it } from "mocha";
import { expect } from "chai";
import fs from "fs-extra";
import path from "path";
import { replaceTokensInFile, replaceTokensInFiles } from "../src/utils/file-utils";
import {
getDestinationDir,
getRepositoryUrl,
replaceTokensInFile,
replaceTokensInFiles,
} from "../src/utils/file-utils";

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

await fs.ensureDir(baseDir);

Expand Down Expand Up @@ -53,4 +58,44 @@ describe("file-utils", async () => {
expect(data).to.be.equal("test1");
});
});

describe("get destination dir", async () => {
const destDir = await getDestinationDir(path.join(baseDir, "baz"));

it("should return the destination dir", () => {
expect(destDir).to.be.equal(path.join(baseDir, "baz"));
});

it("should return the destination dir", async () => {
const destDir2 = await getDestinationDir(path.join(baseDir, "baz"));

expect(destDir2).to.be.equal(path.join(baseDir, "baz"));
});

it("should throw exception", async () => {
await fs.writeFile(path.join(baseDir, "baz", "tmp.txt"), "");

let ex = false;

try {
await getDestinationDir(path.join(baseDir, "baz"));

console.log("never");
} catch {
ex = true;
}

expect(ex).to.be.equal(true);
});
});

describe("get repository url", () => {
it("should return the repository url", () => {
expect(getRepositoryUrl()).to.be.not.equal("");
});
});

after(async () => {
await fs.remove(baseDir);
});
});

0 comments on commit 326d4c3

Please sign in to comment.