diff --git a/src/build/build.ts b/src/build/build.ts index fac7362..40e6c7a 100644 --- a/src/build/build.ts +++ b/src/build/build.ts @@ -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)", diff --git a/src/cli.ts b/src/cli.ts index 4725035..58b93a5 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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); diff --git a/src/create/plugin/plugin.ts b/src/create/plugin/plugin.ts index f13fc54..f4986a1 100644 --- a/src/create/plugin/plugin.ts +++ b/src/create/plugin/plugin.ts @@ -1,9 +1,8 @@ +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"); @@ -11,27 +10,8 @@ const pluginCommand = new Command("plugin"); pluginCommand.description("Create a new tsParticles plugin"); pluginCommand.argument("", "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[] = [ @@ -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 }; diff --git a/src/create/preset/preset.ts b/src/create/preset/preset.ts index 5ded363..db4eaa9 100644 --- a/src/create/preset/preset.ts +++ b/src/create/preset/preset.ts @@ -1,9 +1,8 @@ +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"); @@ -11,27 +10,8 @@ const presetCommand = new Command("preset"); presetCommand.description("Create a new tsParticles preset"); presetCommand.argument("", "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[] = [ @@ -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 }; diff --git a/src/create/shape/shape.ts b/src/create/shape/shape.ts index 4a10320..38ebd49 100644 --- a/src/create/shape/shape.ts +++ b/src/create/shape/shape.ts @@ -1,9 +1,8 @@ +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"); @@ -11,27 +10,8 @@ const shapeCommand = new Command("shape"); shapeCommand.description("Create a new tsParticles shape"); shapeCommand.argument("", "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[] = [ @@ -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 }; diff --git a/src/utils/file-utils.ts b/src/utils/file-utils.ts index 33f9f52..515902d 100644 --- a/src/utils/file-utils.ts +++ b/src/utils/file-utils.ts @@ -1,3 +1,4 @@ +import { execSync } from "child_process"; import fs from "fs-extra"; import path from "path"; @@ -38,3 +39,37 @@ export async function replaceTokensInFiles(options: ReplaceTokensOptions[]): Pro export async function replaceTokensInFile(options: ReplaceTokensOptions): Promise { await replaceTokensInFiles([options]); } + +/** + * + * @param destination - + * @returns the destination directory path + */ +export async function getDestinationDir(destination: string): Promise { + 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 ""; + } +} diff --git a/tests/create-plugin.test.ts b/tests/create-plugin.test.ts index 642ac11..307c7cc 100644 --- a/tests/create-plugin.test.ts +++ b/tests/create-plugin.test.ts @@ -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); @@ -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); diff --git a/tests/create-preset.test.ts b/tests/create-preset.test.ts index bf51ec2..627db38 100644 --- a/tests/create-preset.test.ts +++ b/tests/create-preset.test.ts @@ -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); @@ -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); diff --git a/tests/create-shape.test.ts b/tests/create-shape.test.ts index b7bfe59..7ccc9ff 100644 --- a/tests/create-shape.test.ts +++ b/tests/create-shape.test.ts @@ -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); @@ -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); diff --git a/tests/file-utils.test.ts b/tests/file-utils.test.ts index c9495c9..6fa0e50 100644 --- a/tests/file-utils.test.ts +++ b/tests/file-utils.test.ts @@ -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); @@ -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); + }); });