@tsparticles/cli 1.11.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { execSync } from "child_process";
2
2
  import fs from "fs-extra";
3
3
  import path from "path";
4
+ import { replaceTokensInFile } from "./file-utils";
4
5
 
5
6
  /**
6
7
  * Updates the package.json file
@@ -17,22 +18,35 @@ export async function updatePackageFile(
17
18
  fileName: string,
18
19
  repoUrl: string,
19
20
  ): Promise<void> {
20
- const packagePath = path.resolve(destPath, "package.json"),
21
- packageContents = await fs.readFile(packagePath, "utf-8"),
22
- descriptionRegex = /"tsParticles empty template"/g,
23
- replacedDescriptionText = packageContents.replace(descriptionRegex, `"${description}"`),
24
- fileRegex = /"tsparticles.empty.template.min.js"/g,
25
- replacedFileText = replacedDescriptionText.replace(fileRegex, fileName),
26
- privateRegex = /\s{4}"private": true,\r?\n?/g,
27
- replacedPrivateText = replacedFileText.replace(privateRegex, ""),
28
- nameRegex = /"@tsparticles\/empty-template"/g,
29
- nameReplacedText = replacedPrivateText.replace(nameRegex, packageName),
30
- repoUrlRegex = /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
31
- repoUrlReplacedText = nameReplacedText.replace(repoUrlRegex, `"url": "git+${repoUrl}"`),
32
- issuesUrlRegex = /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
33
- replacedText = repoUrlReplacedText.replace(issuesUrlRegex, `"url": "${repoUrl.replace(".git", "/issues")}"`);
34
-
35
- await fs.writeFile(packagePath, replacedText);
21
+ await replaceTokensInFile({
22
+ path: path.resolve(destPath, "package.json"),
23
+ tokens: [
24
+ {
25
+ from: /"tsParticles empty template"/g,
26
+ to: `"${description}"`,
27
+ },
28
+ {
29
+ from: /"tsparticles.empty.template.min.js"/g,
30
+ to: `"${fileName}"`,
31
+ },
32
+ {
33
+ from: /\s{4}"private": true,\r?\n?/g,
34
+ to: "",
35
+ },
36
+ {
37
+ from: /"@tsparticles\/empty-template"/g,
38
+ to: `"${packageName}"`,
39
+ },
40
+ {
41
+ from: /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
42
+ to: `"url": "git+${repoUrl}"`,
43
+ },
44
+ {
45
+ from: /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
46
+ to: `"url": "${repoUrl.replace(".git", "/issues")}"`,
47
+ },
48
+ ],
49
+ });
36
50
  }
37
51
 
38
52
  /**
@@ -50,22 +64,35 @@ export async function updatePackageDistFile(
50
64
  fileName: string,
51
65
  repoUrl: string,
52
66
  ): Promise<void> {
53
- const packagePath = path.resolve(destPath, "package.dist.json"),
54
- packageContents = await fs.readFile(packagePath, "utf-8"),
55
- descriptionRegex = /"tsParticles empty template"/g,
56
- replacedDescriptionText = packageContents.replace(descriptionRegex, `"${description}"`),
57
- fileRegex = /"tsparticles.empty.template.min.js"/g,
58
- replacedFileText = replacedDescriptionText.replace(fileRegex, fileName),
59
- privateRegex = /\s{4}"private": true,\r?\n?/g,
60
- replacedPrivateText = replacedFileText.replace(privateRegex, ""),
61
- nameRegex = /"@tsparticles\/empty-template"/g,
62
- nameReplacedText = replacedPrivateText.replace(nameRegex, packageName),
63
- repoUrlRegex = /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
64
- repoUrlReplacedText = nameReplacedText.replace(repoUrlRegex, `"url": "git+${repoUrl}"`),
65
- issuesUrlRegex = /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
66
- replacedText = repoUrlReplacedText.replace(issuesUrlRegex, `"url": "${repoUrl.replace(".git", "/issues")}"`);
67
-
68
- await fs.writeFile(packagePath, replacedText);
67
+ await replaceTokensInFile({
68
+ path: path.resolve(destPath, "package.dist.json"),
69
+ tokens: [
70
+ {
71
+ from: /"tsParticles empty template"/g,
72
+ to: `"${description}"`,
73
+ },
74
+ {
75
+ from: /"tsparticles.empty.template.min.js"/g,
76
+ to: `"${fileName}"`,
77
+ },
78
+ {
79
+ from: /\s{4}"private": true,\r?\n?/g,
80
+ to: "",
81
+ },
82
+ {
83
+ from: /"@tsparticles\/empty-template"/g,
84
+ to: `"${packageName}"`,
85
+ },
86
+ {
87
+ from: /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
88
+ to: `"url": "git+${repoUrl}"`,
89
+ },
90
+ {
91
+ from: /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
92
+ to: `"url": "${repoUrl.replace(".git", "/issues")}"`,
93
+ },
94
+ ],
95
+ });
69
96
  }
70
97
 
71
98
  /**
@@ -81,16 +108,23 @@ export async function updateWebpackFile(
81
108
  description: string,
82
109
  fnName: string,
83
110
  ): Promise<void> {
84
- const webpackPath = path.resolve(destPath, "webpack.config.js"),
85
- webpack = await fs.readFile(webpackPath, "utf-8"),
86
- webpackDescriptionRegex = /tsParticles Empty Template/g,
87
- replacedDescriptionText = webpack.replace(webpackDescriptionRegex, description),
88
- webpackEntryRegex = /"template(\.bundle)?"/g,
89
- replacedNameText = replacedDescriptionText.replace(webpackEntryRegex, `"${name}$1"`),
90
- webpackFunctionNameRegex = /loadParticlesTemplate/g,
91
- replacedFunctionNameText = replacedNameText.replace(webpackFunctionNameRegex, fnName);
92
-
93
- await fs.writeFile(webpackPath, replacedFunctionNameText);
111
+ await replaceTokensInFile({
112
+ path: path.resolve(destPath, "webpack.config.js"),
113
+ tokens: [
114
+ {
115
+ from: /"Empty"/g,
116
+ to: `"${description}"`,
117
+ },
118
+ {
119
+ from: /"empty"/g,
120
+ to: `"${name}"`,
121
+ },
122
+ {
123
+ from: /loadParticlesTemplate/g,
124
+ to: fnName,
125
+ },
126
+ ],
127
+ });
94
128
  }
95
129
 
96
130
  /**
@@ -98,9 +132,7 @@ export async function updateWebpackFile(
98
132
  * @param destPath - The path where the project will be created
99
133
  */
100
134
  export async function copyEmptyTemplateFiles(destPath: string): Promise<void> {
101
- const emptyPath = path.resolve(__dirname, "..", "..", "files", "empty-project");
102
-
103
- await fs.copy(emptyPath, destPath, {
135
+ await fs.copy(path.resolve(__dirname, "..", "..", "files", "empty-project"), destPath, {
104
136
  overwrite: true,
105
137
  filter: copyFilter,
106
138
  });
@@ -0,0 +1,31 @@
1
+ import { describe, it } from "mocha";
2
+ import { expect } from "chai";
3
+ import { createPluginTemplate } from "../src/create/plugin/create-plugin";
4
+ import path from "path";
5
+ import fs from "fs-extra";
6
+
7
+ describe("create-plugin", async () => {
8
+ it("should have created the plugin project", async () => {
9
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-plugin"));
10
+
11
+ await createPluginTemplate("foo", "Foo", "", destDir);
12
+
13
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
14
+
15
+ expect(pkgInfo.name).to.be.equal("tsparticles-plugin-foo");
16
+
17
+ await fs.remove(destDir);
18
+ });
19
+
20
+ it("should have created the plugin project, w/ repo", async () => {
21
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-plugin"));
22
+
23
+ await createPluginTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
24
+
25
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
26
+
27
+ expect(pkgInfo.name).to.be.equal("tsparticles-plugin-bar");
28
+
29
+ await fs.remove(destDir);
30
+ });
31
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, it } from "mocha";
2
+ import { expect } from "chai";
3
+ import { createPresetTemplate } from "../src/create/preset/create-preset";
4
+ import path from "path";
5
+ import fs from "fs-extra";
6
+
7
+ describe("create-plugin", async () => {
8
+ it("should have created the preset project", async () => {
9
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-preset"));
10
+
11
+ await createPresetTemplate("foo", "Foo", "", destDir);
12
+
13
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
14
+
15
+ expect(pkgInfo.name).to.be.equal("tsparticles-preset-foo");
16
+
17
+ await fs.remove(destDir);
18
+ });
19
+
20
+ it("should have created the preset project, w/ repo", async () => {
21
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-preset"));
22
+
23
+ await createPresetTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
24
+
25
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
26
+
27
+ expect(pkgInfo.name).to.be.equal("tsparticles-preset-bar");
28
+
29
+ await fs.remove(destDir);
30
+ });
31
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, it } from "mocha";
2
+ import { expect } from "chai";
3
+ import { createShapeTemplate } from "../src/create/shape/create-shape";
4
+ import path from "path";
5
+ import fs from "fs-extra";
6
+
7
+ describe("create-shape", async () => {
8
+ it("should have created the shape project", async () => {
9
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "foo-shape"));
10
+
11
+ await createShapeTemplate("foo", "Foo", "", destDir);
12
+
13
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
14
+
15
+ expect(pkgInfo.name).to.be.equal("tsparticles-shape-foo");
16
+
17
+ await fs.remove(destDir);
18
+ });
19
+
20
+ it("should have created the shape project, w/ repo", async () => {
21
+ const destDir = path.resolve(path.join(__dirname, "tmp-files", "bar-shape"));
22
+
23
+ await createShapeTemplate("bar", "Bar", "https://github.com/matteobruni/tsparticles", destDir);
24
+
25
+ const pkgInfo = await fs.readJSON(path.join(destDir, "package.json"));
26
+
27
+ expect(pkgInfo.name).to.be.equal("tsparticles-shape-bar");
28
+
29
+ await fs.remove(destDir);
30
+ });
31
+ });
@@ -0,0 +1,101 @@
1
+ import { describe, it } from "mocha";
2
+ import { expect } from "chai";
3
+ import fs from "fs-extra";
4
+ import path from "path";
5
+ import {
6
+ getDestinationDir,
7
+ getRepositoryUrl,
8
+ replaceTokensInFile,
9
+ replaceTokensInFiles,
10
+ } from "../src/utils/file-utils";
11
+
12
+ describe("file-utils", async () => {
13
+ const baseDir = path.join(__dirname, "tmp-files");
14
+
15
+ await fs.ensureDir(baseDir);
16
+
17
+ describe("replace tokens in files", async () => {
18
+ fs.writeFileSync(path.join(baseDir, "files1.txt"), "test");
19
+ fs.writeFileSync(path.join(baseDir, "files2.txt"), "test");
20
+
21
+ await replaceTokensInFiles([{
22
+ path: path.join(baseDir, "files1.txt"),
23
+ tokens: [{
24
+ from: "test",
25
+ to: "test1",
26
+ }],
27
+ }, {
28
+ path: path.join(baseDir, "files2.txt"),
29
+ tokens: [{
30
+ from: "test",
31
+ to: "test2",
32
+ }],
33
+ }]);
34
+
35
+ it("should replace tokens in files", async () => {
36
+ const data1 = await fs.readFile(path.join(baseDir, "files1.txt"), "utf8"),
37
+ data2 = await fs.readFile(path.join(baseDir, "files2.txt"), "utf8");
38
+
39
+ expect(data1).to.be.equal("test1");
40
+ expect(data2).to.be.equal("test2");
41
+ });
42
+ });
43
+
44
+ describe("replace tokens in file", async () => {
45
+ fs.writeFileSync(path.join(baseDir, "file1.txt"), "test");
46
+
47
+ await replaceTokensInFile({
48
+ path: path.join(baseDir, "file1.txt"),
49
+ tokens: [{
50
+ from: "test",
51
+ to: "test1",
52
+ }],
53
+ });
54
+
55
+ it("should replace tokens in files", async () => {
56
+ const data = await fs.readFile(path.join(baseDir, "file1.txt"), "utf8");
57
+
58
+ expect(data).to.be.equal("test1");
59
+ });
60
+ });
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
+ });
101
+ });
@@ -0,0 +1,125 @@
1
+ import { describe, it } from "mocha";
2
+ import { expect } from "chai";
3
+ import { camelize, capitalize, dash } from "../src/utils/string-utils";
4
+
5
+ describe("capitalize", () => {
6
+ describe("empty string", () => {
7
+ expect(capitalize("")).to.be.equal("");
8
+ });
9
+
10
+ describe("lowercase string", () => {
11
+ it("should return capitalized string", () => {
12
+ expect(capitalize("test")).to.be.equal("Test");
13
+ });
14
+ });
15
+
16
+ describe("capitalized string", () => {
17
+ it("should return capitalized string", () => {
18
+ expect(capitalize("Test")).to.be.equal("Test");
19
+ });
20
+ });
21
+
22
+ describe("multiple lowercase words string", () => {
23
+ it("should return capitalized string, no split", () => {
24
+ expect(capitalize("test test")).to.be.equal("Test test");
25
+ });
26
+
27
+ it("should return capitalized string, split", () => {
28
+ expect(capitalize("test test", " ")).to.be.equal("TestTest");
29
+ });
30
+
31
+ it("should return capitalized string, wrong split", () => {
32
+ expect(capitalize("test test", ";")).to.be.equal("Test test");
33
+ });
34
+ });
35
+
36
+ describe("multiple uppercase words string", () => {
37
+ it("should return capitalized string, no split", () => {
38
+ expect(capitalize("Test Test")).to.be.equal("Test Test");
39
+ });
40
+
41
+ it("should return capitalized string, split", () => {
42
+ expect(capitalize("Test Test", " ")).to.be.equal("TestTest");
43
+ });
44
+
45
+ it("should return capitalized string, wrong split", () => {
46
+ expect(capitalize("Test Test", ";")).to.be.equal("Test Test");
47
+ });
48
+ });
49
+ });
50
+
51
+ describe("camelize", () => {
52
+ describe("empty string", () => {
53
+ expect(camelize("")).to.be.equal("");
54
+ });
55
+
56
+ describe("lowercase string", () => {
57
+ it("should return camelized string", () => {
58
+ expect(camelize("test")).to.be.equal("test");
59
+ });
60
+ });
61
+
62
+ describe("uppercase string", () => {
63
+ it("should return camelized string", () => {
64
+ expect(camelize("Test")).to.be.equal("test");
65
+ });
66
+ });
67
+
68
+ describe("multiple lowercase words string", () => {
69
+ it("should return camelized string, no split", () => {
70
+ expect(camelize("test test")).to.be.equal("test test");
71
+ });
72
+
73
+ it("should return camelized string, split", () => {
74
+ expect(camelize("test test", " ")).to.be.equal("testTest");
75
+ });
76
+
77
+ it("should return camelized string, wrong split", () => {
78
+ expect(camelize("test test", ";")).to.be.equal("test test");
79
+ });
80
+ });
81
+
82
+ describe("multiple uppercase words string", () => {
83
+ it("should return camelized string, no split", () => {
84
+ expect(camelize("Test Test")).to.be.equal("test Test");
85
+ });
86
+
87
+ it("should return camelized string, split", () => {
88
+ expect(camelize("Test Test", " ")).to.be.equal("testTest");
89
+ });
90
+
91
+ it("should return camelized string, wrong split", () => {
92
+ expect(camelize("Test Test", ";")).to.be.equal("test Test");
93
+ });
94
+ });
95
+ });
96
+
97
+ describe("dash", () => {
98
+ describe("empty string", () => {
99
+ expect(dash("")).to.be.equal("");
100
+ });
101
+
102
+ describe("lowercase string", () => {
103
+ it("should return dashed string", () => {
104
+ expect(dash("test")).to.be.equal("test");
105
+ });
106
+ });
107
+
108
+ describe("uppercase string", () => {
109
+ it("should return dashed string", () => {
110
+ expect(dash("Test")).to.be.equal("test");
111
+ });
112
+ });
113
+
114
+ describe("capitalized word string", () => {
115
+ it("should return dashed string", () => {
116
+ expect(dash("TestTest")).to.be.equal("test-test");
117
+ });
118
+ });
119
+
120
+ describe("camelized word string", () => {
121
+ it("should return dashed string", () => {
122
+ expect(dash("testTest")).to.be.equal("test-test");
123
+ });
124
+ });
125
+ });
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2021",
4
+ "module": "commonjs",
5
+ "lib": ["ESNext", "ES2022", "ES2021", "ES2020", "ES2019", "ES2018", "ES2017", "ES2016", "ES2015", "DOM"],
6
+ "types": ["jsdom", "mocha", "chai", "node"],
7
+ "allowJs": true,
8
+ "rootDir": ".",
9
+ "declaration": false,
10
+ "removeComments": true,
11
+ "importHelpers": false,
12
+ "strict": true,
13
+ "noImplicitAny": true,
14
+ "strictNullChecks": true,
15
+ "alwaysStrict": true,
16
+ "noFallthroughCasesInSwitch": true,
17
+ "moduleResolution": "node",
18
+ "allowSyntheticDefaultImports": true,
19
+ "esModuleInterop": true,
20
+ "experimentalDecorators": true,
21
+ "forceConsistentCasingInFileNames": true
22
+ },
23
+ "references": [{ "path": "../src" }],
24
+ }