@tsparticles/cli 1.12.0 → 2.0.0-beta.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.
@@ -7,6 +7,7 @@ exports.runBuild = exports.runInstall = exports.copyFilter = exports.copyEmptyTe
7
7
  const child_process_1 = require("child_process");
8
8
  const fs_extra_1 = __importDefault(require("fs-extra"));
9
9
  const path_1 = __importDefault(require("path"));
10
+ const file_utils_1 = require("./file-utils");
10
11
  /**
11
12
  * Updates the package.json file
12
13
  * @param destPath - The path where the package.json file is located
@@ -16,8 +17,35 @@ const path_1 = __importDefault(require("path"));
16
17
  * @param repoUrl - The repository URL
17
18
  */
18
19
  async function updatePackageFile(destPath, packageName, description, fileName, repoUrl) {
19
- const packagePath = path_1.default.resolve(destPath, "package.json"), packageContents = await fs_extra_1.default.readFile(packagePath, "utf-8"), descriptionRegex = /"tsParticles empty template"/g, replacedDescriptionText = packageContents.replace(descriptionRegex, `"${description}"`), fileRegex = /"tsparticles.empty.template.min.js"/g, replacedFileText = replacedDescriptionText.replace(fileRegex, fileName), privateRegex = /\s{4}"private": true,\r?\n?/g, replacedPrivateText = replacedFileText.replace(privateRegex, ""), nameRegex = /"@tsparticles\/empty-template"/g, nameReplacedText = replacedPrivateText.replace(nameRegex, packageName), repoUrlRegex = /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g, repoUrlReplacedText = nameReplacedText.replace(repoUrlRegex, `"url": "git+${repoUrl}"`), issuesUrlRegex = /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g, replacedText = repoUrlReplacedText.replace(issuesUrlRegex, `"url": "${repoUrl.replace(".git", "/issues")}"`);
20
- await fs_extra_1.default.writeFile(packagePath, replacedText);
20
+ await (0, file_utils_1.replaceTokensInFile)({
21
+ path: path_1.default.resolve(destPath, "package.json"),
22
+ tokens: [
23
+ {
24
+ from: /"tsParticles empty template"/g,
25
+ to: `"${description}"`,
26
+ },
27
+ {
28
+ from: /"tsparticles.empty.template.min.js"/g,
29
+ to: `"${fileName}"`,
30
+ },
31
+ {
32
+ from: /\s{4}"private": true,\r?\n?/g,
33
+ to: "",
34
+ },
35
+ {
36
+ from: /"@tsparticles\/empty-template"/g,
37
+ to: `"${packageName}"`,
38
+ },
39
+ {
40
+ from: /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
41
+ to: `"url": "git+${repoUrl}"`,
42
+ },
43
+ {
44
+ from: /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
45
+ to: `"url": "${repoUrl.replace(".git", "/issues")}"`,
46
+ },
47
+ ],
48
+ });
21
49
  }
22
50
  exports.updatePackageFile = updatePackageFile;
23
51
  /**
@@ -29,8 +57,35 @@ exports.updatePackageFile = updatePackageFile;
29
57
  * @param repoUrl - The url of the repository
30
58
  */
31
59
  async function updatePackageDistFile(destPath, packageName, description, fileName, repoUrl) {
32
- const packagePath = path_1.default.resolve(destPath, "package.dist.json"), packageContents = await fs_extra_1.default.readFile(packagePath, "utf-8"), descriptionRegex = /"tsParticles empty template"/g, replacedDescriptionText = packageContents.replace(descriptionRegex, `"${description}"`), fileRegex = /"tsparticles.empty.template.min.js"/g, replacedFileText = replacedDescriptionText.replace(fileRegex, fileName), privateRegex = /\s{4}"private": true,\r?\n?/g, replacedPrivateText = replacedFileText.replace(privateRegex, ""), nameRegex = /"@tsparticles\/empty-template"/g, nameReplacedText = replacedPrivateText.replace(nameRegex, packageName), repoUrlRegex = /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g, repoUrlReplacedText = nameReplacedText.replace(repoUrlRegex, `"url": "git+${repoUrl}"`), issuesUrlRegex = /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g, replacedText = repoUrlReplacedText.replace(issuesUrlRegex, `"url": "${repoUrl.replace(".git", "/issues")}"`);
33
- await fs_extra_1.default.writeFile(packagePath, replacedText);
60
+ await (0, file_utils_1.replaceTokensInFile)({
61
+ path: path_1.default.resolve(destPath, "package.dist.json"),
62
+ tokens: [
63
+ {
64
+ from: /"tsParticles empty template"/g,
65
+ to: `"${description}"`,
66
+ },
67
+ {
68
+ from: /"tsparticles.empty.template.min.js"/g,
69
+ to: `"${fileName}"`,
70
+ },
71
+ {
72
+ from: /\s{4}"private": true,\r?\n?/g,
73
+ to: "",
74
+ },
75
+ {
76
+ from: /"@tsparticles\/empty-template"/g,
77
+ to: `"${packageName}"`,
78
+ },
79
+ {
80
+ from: /"url": "git\+https:\/\/github\.com\/tsparticles\/empty-template\.git"/g,
81
+ to: `"url": "git+${repoUrl}"`,
82
+ },
83
+ {
84
+ from: /"url": "https:\/\/github\.com\/tsparticles\/empty-template\/issues"/g,
85
+ to: `"url": "${repoUrl.replace(".git", "/issues")}"`,
86
+ },
87
+ ],
88
+ });
34
89
  }
35
90
  exports.updatePackageDistFile = updatePackageDistFile;
36
91
  /**
@@ -41,8 +96,23 @@ exports.updatePackageDistFile = updatePackageDistFile;
41
96
  * @param fnName - The name of the function to load the template
42
97
  */
43
98
  async function updateWebpackFile(destPath, name, description, fnName) {
44
- const webpackPath = path_1.default.resolve(destPath, "webpack.config.js"), webpack = await fs_extra_1.default.readFile(webpackPath, "utf-8"), webpackDescriptionRegex = /"Empty"/g, replacedDescriptionText = webpack.replace(webpackDescriptionRegex, `"${description}"`), webpackEntryRegex = /"empty"/g, replacedNameText = replacedDescriptionText.replace(webpackEntryRegex, `"${name}"`), webpackFunctionNameRegex = /loadParticlesTemplate/g, replacedFunctionNameText = replacedNameText.replace(webpackFunctionNameRegex, fnName);
45
- await fs_extra_1.default.writeFile(webpackPath, replacedFunctionNameText);
99
+ await (0, file_utils_1.replaceTokensInFile)({
100
+ path: path_1.default.resolve(destPath, "webpack.config.js"),
101
+ tokens: [
102
+ {
103
+ from: /"Empty"/g,
104
+ to: `"${description}"`,
105
+ },
106
+ {
107
+ from: /"empty"/g,
108
+ to: `"${name}"`,
109
+ },
110
+ {
111
+ from: /loadParticlesTemplate/g,
112
+ to: fnName,
113
+ },
114
+ ],
115
+ });
46
116
  }
47
117
  exports.updateWebpackFile = updateWebpackFile;
48
118
  /**
@@ -50,8 +120,7 @@ exports.updateWebpackFile = updateWebpackFile;
50
120
  * @param destPath - The path where the project will be created
51
121
  */
52
122
  async function copyEmptyTemplateFiles(destPath) {
53
- const emptyPath = path_1.default.resolve(__dirname, "..", "..", "files", "empty-project");
54
- await fs_extra_1.default.copy(emptyPath, destPath, {
123
+ await fs_extra_1.default.copy(path_1.default.resolve(__dirname, "..", "..", "files", "empty-project"), destPath, {
55
124
  overwrite: true,
56
125
  filter: copyFilter,
57
126
  });
@@ -83,7 +83,7 @@
83
83
  "prettier": "@tsparticles/prettier-config",
84
84
  "devDependencies": {
85
85
  "@babel/core": "^7.22.9",
86
- "@tsparticles/cli": "^1.12.0",
86
+ "@tsparticles/cli": "^2.0.0-beta.0",
87
87
  "@tsparticles/eslint-config": "^1.19.0",
88
88
  "@tsparticles/prettier-config": "^1.12.0",
89
89
  "@tsparticles/tsconfig": "^1.14.0",
@@ -95,7 +95,7 @@
95
95
  "browserslist": "^4.21.10",
96
96
  "copyfiles": "^2.4.1",
97
97
  "eslint": "^8.46.0",
98
- "eslint-config-prettier": "^8.10.0",
98
+ "eslint-config-prettier": "^9.0.0",
99
99
  "prettier": "^3.0.1",
100
100
  "rimraf": "^5.0.1",
101
101
  "terser-webpack-plugin": "^5.3.9",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/cli",
3
- "version": "1.12.0",
3
+ "version": "2.0.0-beta.0",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "tsparticles-cli": "dist/cli.js"
@@ -10,32 +10,38 @@
10
10
  },
11
11
  "prettier": "@tsparticles/prettier-config",
12
12
  "dependencies": {
13
- "@tsparticles/eslint-config": "^1.19.0",
13
+ "@tsparticles/eslint-config": "^1.20.0",
14
14
  "@tsparticles/prettier-config": "^1.12.0",
15
15
  "@tsparticles/tsconfig": "^1.14.0",
16
- "@tsparticles/webpack-plugin": "^1.23.0",
17
- "@typescript-eslint/eslint-plugin": "^6.1.0",
18
- "@typescript-eslint/parser": "^6.1.0",
16
+ "@tsparticles/webpack-plugin": "^1.24.0",
17
+ "@typescript-eslint/eslint-plugin": "^6.3.0",
18
+ "@typescript-eslint/parser": "^6.3.0",
19
19
  "commander": "^11.0.0",
20
- "eslint": "^8.45.0",
21
- "eslint-config-prettier": "^8.8.0",
22
- "eslint-plugin-jsdoc": "^46.4.4",
20
+ "eslint": "^8.46.0",
21
+ "eslint-config-prettier": "^9.0.0",
22
+ "eslint-plugin-jsdoc": "^46.4.6",
23
23
  "eslint-plugin-tsdoc": "^0.2.17",
24
24
  "fs-extra": "^11.1.1",
25
25
  "klaw": "^4.1.0",
26
26
  "path-scurry": "^1.10.1",
27
- "prettier": "^3.0.0",
27
+ "prettier": "^3.0.1",
28
28
  "prompts": "^2.4.2",
29
29
  "rimraf": "^5.0.1",
30
30
  "typescript": "^5.1.6",
31
31
  "webpack": "^5.88.2"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/eslint": "^8.44.0",
34
+ "@types/chai": "^4.3.5",
35
35
  "@types/fs-extra": "^11.0.1",
36
36
  "@types/klaw": "^3.0.3",
37
- "@types/node": "^20.4.4",
38
- "@types/prompts": "^2.4.4"
37
+ "@types/mocha": "^10.0.1",
38
+ "@types/node": "^20.4.8",
39
+ "@types/prompts": "^2.4.4",
40
+ "chai": "^4.3.7",
41
+ "cross-env": "^7.0.3",
42
+ "mocha": "^10.2.0",
43
+ "nyc": "^15.1.0",
44
+ "ts-node": "^10.9.1"
39
45
  },
40
46
  "description": "tsParticles CLI",
41
47
  "main": ".eslintrc.js",
@@ -51,6 +57,7 @@
51
57
  "compile:ci": "pnpm run build:ts",
52
58
  "build:ts": "pnpm run build:ts:cjs",
53
59
  "build:ts:cjs": "tsc -p src",
60
+ "test": "cross-env TS_NODE_PROJECT='./tests/tsconfig.json' nyc mocha --timeout 300000",
54
61
  "build": "pnpm run clear:dist && pnpm run prettify:src && pnpm run lint && pnpm run compile && pnpm run prettify:readme && chmod +x dist/cli.js && chmod +x dist/build/build.js && chmod +x dist/create/create.js && chmod +x dist/create/preset/preset.js",
55
62
  "build:ci": "pnpm run clear:dist && pnpm run prettify:ci:src && pnpm run lint:ci && pnpm run compile && pnpm run prettify:ci:readme",
56
63
  "clear:dist": "rimraf ./dist",
@@ -71,7 +71,7 @@ export async function buildDistFiles(basePath: string): Promise<boolean> {
71
71
  await fs.writeFile(file.path, contents.replaceAll("__VERSION__", `"${pkgInfo.version}"`), "utf8");
72
72
  }
73
73
 
74
- /*for await (const file of klaw(path.join(distPath, "cjs"))) {
74
+ for await (const file of klaw(path.join(distPath, "cjs"))) {
75
75
  await fs.rename(file.path, file.path.replace(/\.js$/, ".cjs"));
76
76
  }
77
77
 
@@ -80,7 +80,7 @@ export async function buildDistFiles(basePath: string): Promise<boolean> {
80
80
  }
81
81
 
82
82
  await fs.writeFile(path.join(distPath, "cjs", "package.json"), `{ "type": "commonjs" }`);
83
- await fs.writeFile(path.join(distPath, "esm", "package.json"), `{ "type": "module" }`);*/
83
+ await fs.writeFile(path.join(distPath, "esm", "package.json"), `{ "type": "module" }`);
84
84
 
85
85
  res = true;
86
86
  } catch (e) {
@@ -11,7 +11,7 @@ import path from "path";
11
11
 
12
12
  const buildCommand = new Command("build");
13
13
 
14
- buildCommand.description("Build the library using TypeScript");
14
+ buildCommand.description("Build the tsParticles library using TypeScript");
15
15
  buildCommand.option(
16
16
  "-a, --all",
17
17
  "Do all build steps (default if no flags are specified) (same as -b -c -d -l -p -t)",
package/src/cli.ts CHANGED
@@ -4,8 +4,8 @@ import { createCommand } from "./create/create";
4
4
  import pkgInfo from "../package.json";
5
5
  import { program } from "commander";
6
6
 
7
- program.name("tsparticles-cli build");
8
- program.description("tsParticles Builder");
7
+ program.name("tsparticles-cli");
8
+ program.description("tsParticles CLI");
9
9
  program.version(pkgInfo.version, "-v, --version", "output the current version");
10
10
  program.addCommand(buildCommand);
11
11
  program.addCommand(createCommand);
@@ -10,6 +10,7 @@ import {
10
10
  } from "../../utils/template-utils";
11
11
  import fs from "fs-extra";
12
12
  import path from "path";
13
+ import { replaceTokensInFile } from "../../utils/file-utils";
13
14
 
14
15
  /**
15
16
  * Updates the index file with the correct function name
@@ -17,16 +18,22 @@ import path from "path";
17
18
  * @param name - The name of the project
18
19
  */
19
20
  async function updateIndexFile(destPath: string, name: string): Promise<void> {
20
- const indexPath = path.resolve(destPath, "src", "index.ts"),
21
- index = await fs.readFile(indexPath, "utf-8"),
22
- capitalizedName = capitalize(capitalize(name, "-"), " "),
23
- camelizedName = camelize(capitalizedName),
24
- indexFunctionRegex = /loadTemplatePlugin/g,
25
- replacedFuncText = index.replace(indexFunctionRegex, `load${capitalizedName}Plugin`),
26
- indexNameRegex = /"#template#"/g,
27
- replacedNameText = replacedFuncText.replace(indexNameRegex, `"${camelizedName}"`);
21
+ const capitalizedName = capitalize(name, "-", " "),
22
+ camelizedName = camelize(capitalizedName);
28
23
 
29
- await fs.writeFile(indexPath, replacedNameText);
24
+ await replaceTokensInFile({
25
+ path: path.resolve(destPath, "src", "index.ts"),
26
+ tokens: [
27
+ {
28
+ from: /loadTemplatePlugin/g,
29
+ to: `load${capitalizedName}Plugin`,
30
+ },
31
+ {
32
+ from: /"#template#"/g,
33
+ to: `"${camelizedName}"`,
34
+ },
35
+ ],
36
+ });
30
37
  }
31
38
 
32
39
  /**
@@ -47,9 +54,9 @@ async function updatePluginPackageFile(
47
54
 
48
55
  await updatePackageFile(
49
56
  destPath,
50
- `"tsparticles-plugin-${dashedName}"`,
57
+ `tsparticles-plugin-${dashedName}`,
51
58
  description,
52
- `"tsparticles.plugin.${camelizedName}.min.js"`,
59
+ `tsparticles.plugin.${camelizedName}.min.js`,
53
60
  repoUrl,
54
61
  );
55
62
  }
@@ -70,11 +77,11 @@ async function updatePluginPackageDistFile(
70
77
  const camelizedName = camelize(camelize(name, "-"), " "),
71
78
  dashedName = dash(camelizedName);
72
79
 
73
- updatePackageDistFile(
80
+ await updatePackageDistFile(
74
81
  destPath,
75
- `"tsparticles-plugin-${dashedName}"`,
82
+ `tsparticles-plugin-${dashedName}`,
76
83
  description,
77
- `"tsparticles.plugin.${camelizedName}.min.js"`,
84
+ `tsparticles.plugin.${camelizedName}.min.js`,
78
85
  repoUrl,
79
86
  );
80
87
  }
@@ -88,46 +95,46 @@ async function updatePluginPackageDistFile(
88
95
  */
89
96
  async function updateReadmeFile(destPath: string, name: string, description: string, repoUrl: string): Promise<void> {
90
97
  const readmePath = path.resolve(destPath, "README.md"),
91
- readme = await fs.readFile(readmePath, "utf-8"),
92
- capitalizedName = capitalize(capitalize(name, "-"), " "),
98
+ capitalizedName = capitalize(name, "-", " "),
93
99
  camelizedName = camelize(capitalizedName),
94
100
  dashedName = dash(camelizedName),
95
- readmeDescriptionRegex = /tsParticles Template Plugin/g,
96
- replacedDescriptionText = readme.replace(readmeDescriptionRegex, `tsParticles ${description} Plugin`),
97
- readmePackageNameRegex = /tsparticles-plugin-template/g,
98
- replacedPackageNameText = replacedDescriptionText.replace(
99
- readmePackageNameRegex,
100
- `tsparticles-plugin-${dashedName}`,
101
- ),
102
- readmeFileNameRegex = /tsparticles\.plugin\.template(\.bundle)?\.min\.js/g,
103
- replacedFileNameText = replacedPackageNameText.replace(
104
- readmeFileNameRegex,
105
- `tsparticles.plugin.${camelizedName}$1.min.js`,
106
- ),
107
- readmeFunctionNameRegex = /loadTemplatePlugin/g,
108
- replacedFunctionNameText = replacedFileNameText.replace(
109
- readmeFunctionNameRegex,
110
- `load${capitalizedName}Plugin`,
111
- ),
112
- readmeMiniDescriptionRegex =
113
- /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template plugin\./g,
114
- replacedMiniDescriptionText = replacedFunctionNameText.replace(
115
- readmeMiniDescriptionRegex,
116
- `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} plugin.`,
117
- ),
118
- readmeUsageRegex = /plugin\.type: "template"/g,
119
- replacedUsageText = replacedMiniDescriptionText.replace(readmeUsageRegex, `plugin.type: "${camelizedName}`),
120
- sampleImageRegex =
121
- /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/plugin-template\/main\/images\/sample.png\)/g,
122
101
  repoPath = repoUrl.includes("github.com")
123
102
  ? repoUrl.substring(repoUrl.indexOf("github.com/") + 11, repoUrl.indexOf(".git"))
124
- : "tsparticles/plugin-template",
125
- replacedText = replacedUsageText.replace(
126
- sampleImageRegex,
127
- `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
128
- );
103
+ : "tsparticles/plugin-template";
129
104
 
130
- await fs.writeFile(readmePath, replacedText);
105
+ await replaceTokensInFile({
106
+ path: readmePath,
107
+ tokens: [
108
+ {
109
+ from: /tsParticles Template Plugin/g,
110
+ to: `tsParticles ${description} Plugin`,
111
+ },
112
+ {
113
+ from: /tsparticles-plugin-template/g,
114
+ to: `tsparticles-plugin-${dashedName}`,
115
+ },
116
+ {
117
+ from: /tsparticles\.plugin\.template(\.bundle)?\.min\.js/g,
118
+ to: `tsparticles.plugin.${camelizedName}$1.min.js`,
119
+ },
120
+ {
121
+ from: /loadTemplatePlugin/g,
122
+ to: `load${capitalizedName}Plugin`,
123
+ },
124
+ {
125
+ from: /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template plugin\./g,
126
+ to: `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} plugin.`,
127
+ },
128
+ {
129
+ from: /plugin\.type: "template"/g,
130
+ to: `plugin.type: "${camelizedName}"`,
131
+ },
132
+ {
133
+ from: /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/plugin-template\/main\/images\/sample.png\)/g,
134
+ to: `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
135
+ },
136
+ ],
137
+ });
131
138
  }
132
139
 
133
140
  /**
@@ -137,12 +144,7 @@ async function updateReadmeFile(destPath: string, name: string, description: str
137
144
  * @param description - The description of the project
138
145
  */
139
146
  async function updatePluginWebpackFile(destPath: string, name: string, description: string): Promise<void> {
140
- await updateWebpackFile(
141
- destPath,
142
- camelize(capitalize(capitalize(name, "-"), " ")),
143
- description,
144
- "loadParticlesPlugin",
145
- );
147
+ await updateWebpackFile(destPath, camelize(capitalize(name, "-", " ")), description, "loadParticlesPlugin");
146
148
  }
147
149
 
148
150
  /**
@@ -1,9 +1,8 @@
1
+ import { getDestinationDir, getRepositoryUrl } from "../../utils/file-utils";
1
2
  import prompts, { type PromptObject } from "prompts";
2
3
  import { Command } from "commander";
3
4
  import { capitalize } from "../../utils/string-utils";
4
5
  import { createPluginTemplate } from "./create-plugin";
5
- import { execSync } from "child_process";
6
- import fs from "fs-extra";
7
6
  import path from "path";
8
7
 
9
8
  const pluginCommand = new Command("plugin");
@@ -11,27 +10,8 @@ const pluginCommand = new Command("plugin");
11
10
  pluginCommand.description("Create a new tsParticles plugin");
12
11
  pluginCommand.argument("<destination>", "Destination folder");
13
12
  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();
35
15
 
36
16
  const initialName = destPath.split(path.sep).pop(),
37
17
  questions: PromptObject[] = [
@@ -59,7 +39,7 @@ pluginCommand.action(async (destination: string) => {
59
39
 
60
40
  const { name, description, repositoryUrl } = await prompts(questions);
61
41
 
62
- createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42
+ await createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
63
43
  });
64
44
 
65
45
  export { pluginCommand };
@@ -10,6 +10,7 @@ import {
10
10
  } from "../../utils/template-utils";
11
11
  import fs from "fs-extra";
12
12
  import path from "path";
13
+ import { replaceTokensInFile } from "../../utils/file-utils";
13
14
 
14
15
  /**
15
16
  * Updates the bundle file with the correct function name
@@ -17,13 +18,17 @@ import path from "path";
17
18
  * @param name - The name of the project
18
19
  */
19
20
  async function updateBundleFile(destPath: string, name: string): Promise<void> {
20
- const bundlePath = path.resolve(destPath, "src", "bundle.ts"),
21
- bundle = await fs.readFile(bundlePath, "utf-8"),
22
- capitalizedName = capitalize(capitalize(name, "-"), " "),
23
- bundleRegex = /loadTemplatePreset/g,
24
- replacedText = bundle.replace(bundleRegex, `load${capitalizedName}Preset`);
25
-
26
- await fs.writeFile(bundlePath, replacedText);
21
+ const capitalizedName = capitalize(name, "-", " ");
22
+
23
+ await replaceTokensInFile({
24
+ path: path.resolve(destPath, "src", "bundle.ts"),
25
+ tokens: [
26
+ {
27
+ from: /loadTemplatePreset/g,
28
+ to: `load${capitalizedName}Preset`,
29
+ },
30
+ ],
31
+ });
27
32
  }
28
33
 
29
34
  /**
@@ -32,16 +37,22 @@ async function updateBundleFile(destPath: string, name: string): Promise<void> {
32
37
  * @param name - The name of the project
33
38
  */
34
39
  async function updateIndexFile(destPath: string, name: string): Promise<void> {
35
- const indexPath = path.resolve(destPath, "src", "index.ts"),
36
- index = await fs.readFile(indexPath, "utf-8"),
37
- capitalizedName = capitalize(capitalize(name, "-"), " "),
38
- camelizedName = camelize(capitalizedName),
39
- indexFunctionRegex = /loadTemplatePreset/g,
40
- replacedFuncText = index.replace(indexFunctionRegex, `load${capitalizedName}Preset`),
41
- indexNameRegex = /"#template#"/g,
42
- replacedNameText = replacedFuncText.replace(indexNameRegex, `"${camelizedName}"`);
43
-
44
- await fs.writeFile(indexPath, replacedNameText);
40
+ const capitalizedName = capitalize(name, "-", " "),
41
+ camelizedName = camelize(capitalizedName);
42
+
43
+ await replaceTokensInFile({
44
+ path: path.resolve(destPath, "src", "index.ts"),
45
+ tokens: [
46
+ {
47
+ from: /loadTemplatePreset/g,
48
+ to: `load${capitalizedName}Preset`,
49
+ },
50
+ {
51
+ from: /"#template#"/g,
52
+ to: `"${camelizedName}"`,
53
+ },
54
+ ],
55
+ });
45
56
  }
46
57
 
47
58
  /**
@@ -57,14 +68,14 @@ async function updatePresetPackageFile(
57
68
  description: string,
58
69
  repoUrl: string,
59
70
  ): Promise<void> {
60
- const camelizedName = camelize(camelize(name, "-"), " "),
71
+ const camelizedName = camelize(name, "-", " "),
61
72
  dashedName = dash(camelizedName);
62
73
 
63
- updatePackageFile(
74
+ await updatePackageFile(
64
75
  destPath,
65
- `"tsparticles-preset-${dashedName}"`,
76
+ `tsparticles-preset-${dashedName}`,
66
77
  description,
67
- `"tsparticles.preset.${camelizedName}.min.js"`,
78
+ `tsparticles.preset.${camelizedName}.min.js`,
68
79
  repoUrl,
69
80
  );
70
81
  }
@@ -82,14 +93,14 @@ async function updatePresetPackageDistFile(
82
93
  description: string,
83
94
  repoUrl: string,
84
95
  ): Promise<void> {
85
- const camelizedName = camelize(camelize(name, "-"), " "),
96
+ const camelizedName = camelize(name, "-", " "),
86
97
  dashedName = dash(camelizedName);
87
98
 
88
99
  await updatePackageDistFile(
89
100
  destPath,
90
- `"tsparticles-preset-${dashedName}"`,
101
+ `tsparticles-preset-${dashedName}`,
91
102
  description,
92
- `"tsparticles.preset.${camelizedName}.min.js"`,
103
+ `tsparticles.preset.${camelizedName}.min.js`,
93
104
  repoUrl,
94
105
  );
95
106
  }
@@ -102,47 +113,46 @@ async function updatePresetPackageDistFile(
102
113
  * @param repoUrl - The repository url
103
114
  */
104
115
  async function updateReadmeFile(destPath: string, name: string, description: string, repoUrl: string): Promise<void> {
105
- const readmePath = path.resolve(destPath, "README.md"),
106
- readme = await fs.readFile(readmePath, "utf-8"),
107
- capitalizedName = capitalize(capitalize(name, "-"), " "),
116
+ const capitalizedName = capitalize(name, "-", " "),
108
117
  camelizedName = camelize(capitalizedName),
109
118
  dashedName = dash(camelizedName),
110
- readmeDescriptionRegex = /tsParticles Template Preset/g,
111
- replacedDescriptionText = readme.replace(readmeDescriptionRegex, `tsParticles ${description} Preset`),
112
- readmePackageNameRegex = /tsparticles-preset-template/g,
113
- replacedPackageNameText = replacedDescriptionText.replace(
114
- readmePackageNameRegex,
115
- `tsparticles-preset-${dashedName}`,
116
- ),
117
- readmeFileNameRegex = /tsparticles\.preset\.template(\.bundle)?\.min\.js/g,
118
- replacedFileNameText = replacedPackageNameText.replace(
119
- readmeFileNameRegex,
120
- `tsparticles.preset.${camelizedName}$1.min.js`,
121
- ),
122
- readmeFunctionNameRegex = /loadTemplatePreset/g,
123
- replacedFunctionNameText = replacedFileNameText.replace(
124
- readmeFunctionNameRegex,
125
- `load${capitalizedName}Preset`,
126
- ),
127
- readmeMiniDescriptionRegex =
128
- /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) preset template\./g,
129
- replacedMiniDescriptionText = replacedFunctionNameText.replace(
130
- readmeMiniDescriptionRegex,
131
- `[tsParticles](https://github.com/matteobruni/tsparticles) preset ${name}.`,
132
- ),
133
- readmeUsageRegex = /preset: "template"/g,
134
- replacedUsageText = replacedMiniDescriptionText.replace(readmeUsageRegex, `preset: "${camelizedName}`),
135
- sampleImageRegex =
136
- /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/preset-template\/main\/images\/sample.png\)/g,
137
119
  repoPath = repoUrl.includes("github.com")
138
120
  ? repoUrl.substring(repoUrl.indexOf("github.com/") + 11, repoUrl.indexOf(".git"))
139
- : "tsparticles/preset-template",
140
- replacedText = replacedUsageText.replace(
141
- sampleImageRegex,
142
- `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
143
- );
144
-
145
- await fs.writeFile(readmePath, replacedText);
121
+ : "tsparticles/preset-template";
122
+
123
+ await replaceTokensInFile({
124
+ path: path.resolve(destPath, "README.md"),
125
+ tokens: [
126
+ {
127
+ from: /tsParticles Template Preset/g,
128
+ to: `tsParticles ${description} Preset`,
129
+ },
130
+ {
131
+ from: /tsparticles-preset-template/g,
132
+ to: `tsparticles-preset-${dashedName}`,
133
+ },
134
+ {
135
+ from: /tsparticles\.preset\.template(\.bundle)?\.min\.js/g,
136
+ to: `tsparticles.preset.${camelizedName}$1.min.js`,
137
+ },
138
+ {
139
+ from: /loadTemplatePreset/g,
140
+ to: `load${capitalizedName}Preset`,
141
+ },
142
+ {
143
+ from: /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) preset template\./g,
144
+ to: `[tsParticles](https://github.com/matteobruni/tsparticles) preset ${name}.`,
145
+ },
146
+ {
147
+ from: /preset: "template"/g,
148
+ to: `preset: "${camelizedName}`,
149
+ },
150
+ {
151
+ from: /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/preset-template\/main\/images\/sample.png\)/g,
152
+ to: `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
153
+ },
154
+ ],
155
+ });
146
156
  }
147
157
 
148
158
  /**
@@ -152,12 +162,7 @@ async function updateReadmeFile(destPath: string, name: string, description: str
152
162
  * @param description - The description of the project
153
163
  */
154
164
  async function updatePresetWebpackFile(destPath: string, name: string, description: string): Promise<void> {
155
- await updateWebpackFile(
156
- destPath,
157
- camelize(capitalize(capitalize(name, "-"), " ")),
158
- description,
159
- "loadParticlesPreset",
160
- );
165
+ await updateWebpackFile(destPath, camelize(capitalize(name, "-", " ")), description, "loadParticlesPreset");
161
166
  }
162
167
 
163
168
  /**