@tsparticles/cli 1.12.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.
@@ -44,6 +44,7 @@ jobs:
44
44
  ${{ runner.os }}-pnpm-store-
45
45
  - run: pnpm install --no-frozen-lockfile
46
46
  - run: npx pnpm run build:ci
47
+ - run: npx pnpm test
47
48
  pr:
48
49
  runs-on: ubuntu-latest
49
50
  if: ${{ github.event_name == 'pull_request' }}
@@ -81,3 +82,4 @@ jobs:
81
82
  ${{ runner.os }}-pnpm-store-
82
83
  - run: pnpm install --no-frozen-lockfile
83
84
  - run: npx pnpm run build:ci
85
+ - run: npx pnpm test
package/.mocharc.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extension": [
3
+ "ts"
4
+ ],
5
+ "spec": "tests/**/*.ts",
6
+ "require": [
7
+ "ts-node/register",
8
+ "source-map-support/register"
9
+ ],
10
+ "recursive": true
11
+ }
@@ -16,7 +16,7 @@ const build_eslint_1 = require("./build-eslint");
16
16
  const path_1 = __importDefault(require("path"));
17
17
  const buildCommand = new commander_1.Command("build");
18
18
  exports.buildCommand = buildCommand;
19
- buildCommand.description("Build the library using TypeScript");
19
+ buildCommand.description("Build the tsParticles library using TypeScript");
20
20
  buildCommand.option("-a, --all", "Do all build steps (default if no flags are specified) (same as -b -c -d -l -p -t)", true);
21
21
  buildCommand.option("-b, --bundle", "Bundle the library using Webpack", false);
22
22
  buildCommand.option("-c, --clean", "Clean the dist folder", false);
package/dist/cli.js CHANGED
@@ -8,8 +8,8 @@ const build_1 = require("./build/build");
8
8
  const create_1 = require("./create/create");
9
9
  const package_json_1 = __importDefault(require("../package.json"));
10
10
  const commander_1 = require("commander");
11
- commander_1.program.name("tsparticles-cli build");
12
- commander_1.program.description("tsParticles Builder");
11
+ commander_1.program.name("tsparticles-cli");
12
+ commander_1.program.description("tsParticles CLI");
13
13
  commander_1.program.version(package_json_1.default.version, "-v, --version", "output the current version");
14
14
  commander_1.program.addCommand(build_1.buildCommand);
15
15
  commander_1.program.addCommand(create_1.createCommand);
@@ -8,14 +8,27 @@ const string_utils_1 = require("../../utils/string-utils");
8
8
  const template_utils_1 = require("../../utils/template-utils");
9
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
10
10
  const path_1 = __importDefault(require("path"));
11
+ const file_utils_1 = require("../../utils/file-utils");
11
12
  /**
12
13
  * Updates the index file with the correct function name
13
14
  * @param destPath - The path where the project is located
14
15
  * @param name - The name of the project
15
16
  */
16
17
  async function updateIndexFile(destPath, name) {
17
- const indexPath = path_1.default.resolve(destPath, "src", "index.ts"), index = await fs_extra_1.default.readFile(indexPath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), indexFunctionRegex = /loadTemplatePlugin/g, replacedFuncText = index.replace(indexFunctionRegex, `load${capitalizedName}Plugin`), indexNameRegex = /"#template#"/g, replacedNameText = replacedFuncText.replace(indexNameRegex, `"${camelizedName}"`);
18
- await fs_extra_1.default.writeFile(indexPath, replacedNameText);
18
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName);
19
+ await (0, file_utils_1.replaceTokensInFile)({
20
+ path: path_1.default.resolve(destPath, "src", "index.ts"),
21
+ tokens: [
22
+ {
23
+ from: /loadTemplatePlugin/g,
24
+ to: `load${capitalizedName}Plugin`,
25
+ },
26
+ {
27
+ from: /"#template#"/g,
28
+ to: `"${camelizedName}"`,
29
+ },
30
+ ],
31
+ });
19
32
  }
20
33
  /**
21
34
  * Updates the plugin package file
@@ -26,7 +39,7 @@ async function updateIndexFile(destPath, name) {
26
39
  */
27
40
  async function updatePluginPackageFile(destPath, name, description, repoUrl) {
28
41
  const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
29
- await (0, template_utils_1.updatePackageFile)(destPath, `"tsparticles-plugin-${dashedName}"`, description, `"tsparticles.plugin.${camelizedName}.min.js"`, repoUrl);
42
+ await (0, template_utils_1.updatePackageFile)(destPath, `tsparticles-plugin-${dashedName}`, description, `tsparticles.plugin.${camelizedName}.min.js`, repoUrl);
30
43
  }
31
44
  /**
32
45
  * Updates the plugin package dist file
@@ -37,7 +50,7 @@ async function updatePluginPackageFile(destPath, name, description, repoUrl) {
37
50
  */
38
51
  async function updatePluginPackageDistFile(destPath, name, description, repoUrl) {
39
52
  const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
40
- (0, template_utils_1.updatePackageDistFile)(destPath, `"tsparticles-plugin-${dashedName}"`, description, `"tsparticles.plugin.${camelizedName}.min.js"`, repoUrl);
53
+ await (0, template_utils_1.updatePackageDistFile)(destPath, `tsparticles-plugin-${dashedName}`, description, `tsparticles.plugin.${camelizedName}.min.js`, repoUrl);
41
54
  }
42
55
  /**
43
56
  * Updates the plugin readme file
@@ -47,10 +60,42 @@ async function updatePluginPackageDistFile(destPath, name, description, repoUrl)
47
60
  * @param repoUrl - The repository url
48
61
  */
49
62
  async function updateReadmeFile(destPath, name, description, repoUrl) {
50
- const readmePath = path_1.default.resolve(destPath, "README.md"), readme = await fs_extra_1.default.readFile(readmePath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), readmeDescriptionRegex = /tsParticles Template Plugin/g, replacedDescriptionText = readme.replace(readmeDescriptionRegex, `tsParticles ${description} Plugin`), readmePackageNameRegex = /tsparticles-plugin-template/g, replacedPackageNameText = replacedDescriptionText.replace(readmePackageNameRegex, `tsparticles-plugin-${dashedName}`), readmeFileNameRegex = /tsparticles\.plugin\.template(\.bundle)?\.min\.js/g, replacedFileNameText = replacedPackageNameText.replace(readmeFileNameRegex, `tsparticles.plugin.${camelizedName}$1.min.js`), readmeFunctionNameRegex = /loadTemplatePlugin/g, replacedFunctionNameText = replacedFileNameText.replace(readmeFunctionNameRegex, `load${capitalizedName}Plugin`), readmeMiniDescriptionRegex = /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template plugin\./g, replacedMiniDescriptionText = replacedFunctionNameText.replace(readmeMiniDescriptionRegex, `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} plugin.`), readmeUsageRegex = /plugin\.type: "template"/g, replacedUsageText = replacedMiniDescriptionText.replace(readmeUsageRegex, `plugin.type: "${camelizedName}`), sampleImageRegex = /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/plugin-template\/main\/images\/sample.png\)/g, repoPath = repoUrl.includes("github.com")
63
+ const readmePath = path_1.default.resolve(destPath, "README.md"), capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), repoPath = repoUrl.includes("github.com")
51
64
  ? repoUrl.substring(repoUrl.indexOf("github.com/") + 11, repoUrl.indexOf(".git"))
52
- : "tsparticles/plugin-template", replacedText = replacedUsageText.replace(sampleImageRegex, `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`);
53
- await fs_extra_1.default.writeFile(readmePath, replacedText);
65
+ : "tsparticles/plugin-template";
66
+ await (0, file_utils_1.replaceTokensInFile)({
67
+ path: readmePath,
68
+ tokens: [
69
+ {
70
+ from: /tsParticles Template Plugin/g,
71
+ to: `tsParticles ${description} Plugin`,
72
+ },
73
+ {
74
+ from: /tsparticles-plugin-template/g,
75
+ to: `tsparticles-plugin-${dashedName}`,
76
+ },
77
+ {
78
+ from: /tsparticles\.plugin\.template(\.bundle)?\.min\.js/g,
79
+ to: `tsparticles.plugin.${camelizedName}$1.min.js`,
80
+ },
81
+ {
82
+ from: /loadTemplatePlugin/g,
83
+ to: `load${capitalizedName}Plugin`,
84
+ },
85
+ {
86
+ from: /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template plugin\./g,
87
+ to: `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} plugin.`,
88
+ },
89
+ {
90
+ from: /plugin\.type: "template"/g,
91
+ to: `plugin.type: "${camelizedName}"`,
92
+ },
93
+ {
94
+ from: /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/plugin-template\/main\/images\/sample.png\)/g,
95
+ to: `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
96
+ },
97
+ ],
98
+ });
54
99
  }
55
100
  /**
56
101
  * Updates the plugin webpack file
@@ -59,7 +104,7 @@ async function updateReadmeFile(destPath, name, description, repoUrl) {
59
104
  * @param description - The description of the project
60
105
  */
61
106
  async function updatePluginWebpackFile(destPath, name, description) {
62
- await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " ")), description, "loadParticlesPlugin");
107
+ await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)(name, "-", " ")), description, "loadParticlesPlugin");
63
108
  }
64
109
  /**
65
110
  * Creates the plugin project
@@ -4,33 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.pluginCommand = void 0;
7
+ const file_utils_1 = require("../../utils/file-utils");
7
8
  const prompts_1 = __importDefault(require("prompts"));
8
9
  const commander_1 = require("commander");
9
10
  const string_utils_1 = require("../../utils/string-utils");
10
11
  const create_plugin_1 = require("./create-plugin");
11
- const child_process_1 = require("child_process");
12
- const fs_extra_1 = __importDefault(require("fs-extra"));
13
12
  const path_1 = __importDefault(require("path"));
14
13
  const pluginCommand = new commander_1.Command("plugin");
15
14
  exports.pluginCommand = pluginCommand;
16
15
  pluginCommand.description("Create a new tsParticles plugin");
17
16
  pluginCommand.argument("<destination>", "Destination folder");
18
17
  pluginCommand.action(async (destination) => {
19
- let repoUrl;
20
- const destPath = path_1.default.resolve(path_1.default.join(process.cwd(), destination)), destExists = await fs_extra_1.default.pathExists(destPath);
21
- if (destExists) {
22
- const destContents = await fs_extra_1.default.readdir(destPath), destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
23
- if (destContentsNoGit.length) {
24
- throw new Error("Destination folder already exists and is not empty");
25
- }
26
- }
27
- await fs_extra_1.default.ensureDir(destPath);
28
- try {
29
- repoUrl = (0, child_process_1.execSync)("git config --get remote.origin.url").toString();
30
- }
31
- catch {
32
- repoUrl = "";
33
- }
18
+ const destPath = await (0, file_utils_1.getDestinationDir)(destination), repoUrl = (0, file_utils_1.getRepositoryUrl)();
34
19
  const initialName = destPath.split(path_1.default.sep).pop(), questions = [
35
20
  {
36
21
  type: "text",
@@ -54,5 +39,5 @@ pluginCommand.action(async (destination) => {
54
39
  },
55
40
  ];
56
41
  const { name, description, repositoryUrl } = await (0, prompts_1.default)(questions);
57
- (0, create_plugin_1.createPluginTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42
+ await (0, create_plugin_1.createPluginTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
58
43
  });
@@ -8,14 +8,23 @@ const string_utils_1 = require("../../utils/string-utils");
8
8
  const template_utils_1 = require("../../utils/template-utils");
9
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
10
10
  const path_1 = __importDefault(require("path"));
11
+ const file_utils_1 = require("../../utils/file-utils");
11
12
  /**
12
13
  * Updates the bundle file with the correct function name
13
14
  * @param destPath - The path where the project is located
14
15
  * @param name - The name of the project
15
16
  */
16
17
  async function updateBundleFile(destPath, name) {
17
- const bundlePath = path_1.default.resolve(destPath, "src", "bundle.ts"), bundle = await fs_extra_1.default.readFile(bundlePath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), bundleRegex = /loadTemplatePreset/g, replacedText = bundle.replace(bundleRegex, `load${capitalizedName}Preset`);
18
- await fs_extra_1.default.writeFile(bundlePath, replacedText);
18
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " ");
19
+ await (0, file_utils_1.replaceTokensInFile)({
20
+ path: path_1.default.resolve(destPath, "src", "bundle.ts"),
21
+ tokens: [
22
+ {
23
+ from: /loadTemplatePreset/g,
24
+ to: `load${capitalizedName}Preset`,
25
+ },
26
+ ],
27
+ });
19
28
  }
20
29
  /**
21
30
  * Updates the index file with the correct function name
@@ -23,8 +32,20 @@ async function updateBundleFile(destPath, name) {
23
32
  * @param name - The name of the project
24
33
  */
25
34
  async function updateIndexFile(destPath, name) {
26
- const indexPath = path_1.default.resolve(destPath, "src", "index.ts"), index = await fs_extra_1.default.readFile(indexPath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), indexFunctionRegex = /loadTemplatePreset/g, replacedFuncText = index.replace(indexFunctionRegex, `load${capitalizedName}Preset`), indexNameRegex = /"#template#"/g, replacedNameText = replacedFuncText.replace(indexNameRegex, `"${camelizedName}"`);
27
- await fs_extra_1.default.writeFile(indexPath, replacedNameText);
35
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName);
36
+ await (0, file_utils_1.replaceTokensInFile)({
37
+ path: path_1.default.resolve(destPath, "src", "index.ts"),
38
+ tokens: [
39
+ {
40
+ from: /loadTemplatePreset/g,
41
+ to: `load${capitalizedName}Preset`,
42
+ },
43
+ {
44
+ from: /"#template#"/g,
45
+ to: `"${camelizedName}"`,
46
+ },
47
+ ],
48
+ });
28
49
  }
29
50
  /**
30
51
  * Updates the preset package file
@@ -34,8 +55,8 @@ async function updateIndexFile(destPath, name) {
34
55
  * @param repoUrl - The repository url
35
56
  */
36
57
  async function updatePresetPackageFile(destPath, name, description, repoUrl) {
37
- const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
38
- (0, template_utils_1.updatePackageFile)(destPath, `"tsparticles-preset-${dashedName}"`, description, `"tsparticles.preset.${camelizedName}.min.js"`, repoUrl);
58
+ const camelizedName = (0, string_utils_1.camelize)(name, "-", " "), dashedName = (0, string_utils_1.dash)(camelizedName);
59
+ await (0, template_utils_1.updatePackageFile)(destPath, `tsparticles-preset-${dashedName}`, description, `tsparticles.preset.${camelizedName}.min.js`, repoUrl);
39
60
  }
40
61
  /**
41
62
  * Updates the preset package dist file
@@ -45,8 +66,8 @@ async function updatePresetPackageFile(destPath, name, description, repoUrl) {
45
66
  * @param repoUrl - The repository url
46
67
  */
47
68
  async function updatePresetPackageDistFile(destPath, name, description, repoUrl) {
48
- const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
49
- await (0, template_utils_1.updatePackageDistFile)(destPath, `"tsparticles-preset-${dashedName}"`, description, `"tsparticles.preset.${camelizedName}.min.js"`, repoUrl);
69
+ const camelizedName = (0, string_utils_1.camelize)(name, "-", " "), dashedName = (0, string_utils_1.dash)(camelizedName);
70
+ await (0, template_utils_1.updatePackageDistFile)(destPath, `tsparticles-preset-${dashedName}`, description, `tsparticles.preset.${camelizedName}.min.js`, repoUrl);
50
71
  }
51
72
  /**
52
73
  * Updates the preset readme file
@@ -56,10 +77,42 @@ async function updatePresetPackageDistFile(destPath, name, description, repoUrl)
56
77
  * @param repoUrl - The repository url
57
78
  */
58
79
  async function updateReadmeFile(destPath, name, description, repoUrl) {
59
- const readmePath = path_1.default.resolve(destPath, "README.md"), readme = await fs_extra_1.default.readFile(readmePath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), readmeDescriptionRegex = /tsParticles Template Preset/g, replacedDescriptionText = readme.replace(readmeDescriptionRegex, `tsParticles ${description} Preset`), readmePackageNameRegex = /tsparticles-preset-template/g, replacedPackageNameText = replacedDescriptionText.replace(readmePackageNameRegex, `tsparticles-preset-${dashedName}`), readmeFileNameRegex = /tsparticles\.preset\.template(\.bundle)?\.min\.js/g, replacedFileNameText = replacedPackageNameText.replace(readmeFileNameRegex, `tsparticles.preset.${camelizedName}$1.min.js`), readmeFunctionNameRegex = /loadTemplatePreset/g, replacedFunctionNameText = replacedFileNameText.replace(readmeFunctionNameRegex, `load${capitalizedName}Preset`), readmeMiniDescriptionRegex = /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) preset template\./g, replacedMiniDescriptionText = replacedFunctionNameText.replace(readmeMiniDescriptionRegex, `[tsParticles](https://github.com/matteobruni/tsparticles) preset ${name}.`), readmeUsageRegex = /preset: "template"/g, replacedUsageText = replacedMiniDescriptionText.replace(readmeUsageRegex, `preset: "${camelizedName}`), sampleImageRegex = /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/preset-template\/main\/images\/sample.png\)/g, repoPath = repoUrl.includes("github.com")
80
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), repoPath = repoUrl.includes("github.com")
60
81
  ? repoUrl.substring(repoUrl.indexOf("github.com/") + 11, repoUrl.indexOf(".git"))
61
- : "tsparticles/preset-template", replacedText = replacedUsageText.replace(sampleImageRegex, `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`);
62
- await fs_extra_1.default.writeFile(readmePath, replacedText);
82
+ : "tsparticles/preset-template";
83
+ await (0, file_utils_1.replaceTokensInFile)({
84
+ path: path_1.default.resolve(destPath, "README.md"),
85
+ tokens: [
86
+ {
87
+ from: /tsParticles Template Preset/g,
88
+ to: `tsParticles ${description} Preset`,
89
+ },
90
+ {
91
+ from: /tsparticles-preset-template/g,
92
+ to: `tsparticles-preset-${dashedName}`,
93
+ },
94
+ {
95
+ from: /tsparticles\.preset\.template(\.bundle)?\.min\.js/g,
96
+ to: `tsparticles.preset.${camelizedName}$1.min.js`,
97
+ },
98
+ {
99
+ from: /loadTemplatePreset/g,
100
+ to: `load${capitalizedName}Preset`,
101
+ },
102
+ {
103
+ from: /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) preset template\./g,
104
+ to: `[tsParticles](https://github.com/matteobruni/tsparticles) preset ${name}.`,
105
+ },
106
+ {
107
+ from: /preset: "template"/g,
108
+ to: `preset: "${camelizedName}`,
109
+ },
110
+ {
111
+ from: /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/preset-template\/main\/images\/sample.png\)/g,
112
+ to: `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
113
+ },
114
+ ],
115
+ });
63
116
  }
64
117
  /**
65
118
  * Updates the preset webpack file
@@ -68,7 +121,7 @@ async function updateReadmeFile(destPath, name, description, repoUrl) {
68
121
  * @param description - The description of the project
69
122
  */
70
123
  async function updatePresetWebpackFile(destPath, name, description) {
71
- await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " ")), description, "loadParticlesPreset");
124
+ await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)(name, "-", " ")), description, "loadParticlesPreset");
72
125
  }
73
126
  /**
74
127
  * Creates the preset project
@@ -4,33 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.presetCommand = void 0;
7
+ const file_utils_1 = require("../../utils/file-utils");
7
8
  const prompts_1 = __importDefault(require("prompts"));
8
9
  const commander_1 = require("commander");
9
10
  const string_utils_1 = require("../../utils/string-utils");
10
11
  const create_preset_1 = require("./create-preset");
11
- const child_process_1 = require("child_process");
12
- const fs_extra_1 = __importDefault(require("fs-extra"));
13
12
  const path_1 = __importDefault(require("path"));
14
13
  const presetCommand = new commander_1.Command("preset");
15
14
  exports.presetCommand = presetCommand;
16
15
  presetCommand.description("Create a new tsParticles preset");
17
16
  presetCommand.argument("<destination>", "Destination folder");
18
17
  presetCommand.action(async (destination) => {
19
- let repoUrl;
20
- const destPath = path_1.default.resolve(path_1.default.join(process.cwd(), destination)), destExists = await fs_extra_1.default.pathExists(destPath);
21
- if (destExists) {
22
- const destContents = await fs_extra_1.default.readdir(destPath), destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
23
- if (destContentsNoGit.length) {
24
- throw new Error("Destination folder already exists and is not empty");
25
- }
26
- }
27
- await fs_extra_1.default.ensureDir(destPath);
28
- try {
29
- repoUrl = (0, child_process_1.execSync)("git config --get remote.origin.url").toString();
30
- }
31
- catch {
32
- repoUrl = "";
33
- }
18
+ const destPath = await (0, file_utils_1.getDestinationDir)(destination), repoUrl = (0, file_utils_1.getRepositoryUrl)();
34
19
  const initialName = destPath.split(path_1.default.sep).pop(), questions = [
35
20
  {
36
21
  type: "text",
@@ -54,5 +39,5 @@ presetCommand.action(async (destination) => {
54
39
  },
55
40
  ];
56
41
  const { name, description, repositoryUrl } = await (0, prompts_1.default)(questions);
57
- (0, create_preset_1.createPresetTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42
+ await (0, create_preset_1.createPresetTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
58
43
  });
@@ -8,14 +8,27 @@ const string_utils_1 = require("../../utils/string-utils");
8
8
  const template_utils_1 = require("../../utils/template-utils");
9
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
10
10
  const path_1 = __importDefault(require("path"));
11
+ const file_utils_1 = require("../../utils/file-utils");
11
12
  /**
12
13
  * Updates the index file with the correct function name
13
14
  * @param destPath - The path where the project is located
14
15
  * @param name - The name of the project
15
16
  */
16
17
  async function updateIndexFile(destPath, name) {
17
- const indexPath = path_1.default.resolve(destPath, "src", "index.ts"), index = await fs_extra_1.default.readFile(indexPath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), indexFunctionRegex = /loadTemplateShape/g, replacedFuncText = index.replace(indexFunctionRegex, `load${capitalizedName}Shape`), indexNameRegex = /"#template#"/g, replacedNameText = replacedFuncText.replace(indexNameRegex, `"${camelizedName}"`);
18
- await fs_extra_1.default.writeFile(indexPath, replacedNameText);
18
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName);
19
+ await (0, file_utils_1.replaceTokensInFile)({
20
+ path: path_1.default.resolve(destPath, "src", "index.ts"),
21
+ tokens: [
22
+ {
23
+ from: /loadTemplateShape/g,
24
+ to: `load${capitalizedName}Shape`,
25
+ },
26
+ {
27
+ from: /"#template#"/g,
28
+ to: `"${camelizedName}"`,
29
+ },
30
+ ],
31
+ });
19
32
  }
20
33
  /**
21
34
  * Updates the shape package file
@@ -26,7 +39,7 @@ async function updateIndexFile(destPath, name) {
26
39
  */
27
40
  async function updateShapePackageFile(destPath, name, description, repoUrl) {
28
41
  const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
29
- (0, template_utils_1.updatePackageFile)(destPath, `"tsparticles-shape-${dashedName}"`, description, `"tsparticles.shape.${camelizedName}.min.js"`, repoUrl);
42
+ await (0, template_utils_1.updatePackageFile)(destPath, `tsparticles-shape-${dashedName}`, description, `tsparticles.shape.${camelizedName}.min.js`, repoUrl);
30
43
  }
31
44
  /**
32
45
  * Updates the shape package dist file
@@ -37,7 +50,7 @@ async function updateShapePackageFile(destPath, name, description, repoUrl) {
37
50
  */
38
51
  async function updateShapePackageDistFile(destPath, name, description, repoUrl) {
39
52
  const camelizedName = (0, string_utils_1.camelize)((0, string_utils_1.camelize)(name, "-"), " "), dashedName = (0, string_utils_1.dash)(camelizedName);
40
- await (0, template_utils_1.updatePackageDistFile)(destPath, `"tsparticles-shape-${dashedName}"`, description, `"tsparticles.shape.${camelizedName}.min.js"`, repoUrl);
53
+ await (0, template_utils_1.updatePackageDistFile)(destPath, `tsparticles-shape-${dashedName}`, description, `tsparticles.shape.${camelizedName}.min.js`, repoUrl);
41
54
  }
42
55
  /**
43
56
  * Updates the shape readme file
@@ -47,10 +60,42 @@ async function updateShapePackageDistFile(destPath, name, description, repoUrl)
47
60
  * @param repoUrl - The repository url
48
61
  */
49
62
  async function updateReadmeFile(destPath, name, description, repoUrl) {
50
- const readmePath = path_1.default.resolve(destPath, "README.md"), readme = await fs_extra_1.default.readFile(readmePath, "utf-8"), capitalizedName = (0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), readmeDescriptionRegex = /tsParticles Template Shape/g, replacedDescriptionText = readme.replace(readmeDescriptionRegex, `tsParticles ${description} Shape`), readmePackageNameRegex = /tsparticles-shape-template/g, replacedPackageNameText = replacedDescriptionText.replace(readmePackageNameRegex, `tsparticles-shape-${dashedName}`), readmeFileNameRegex = /tsparticles\.shape\.template(\.bundle)?\.min\.js/g, replacedFileNameText = replacedPackageNameText.replace(readmeFileNameRegex, `tsparticles.shape.${camelizedName}$1.min.js`), readmeFunctionNameRegex = /loadTemplateShape/g, replacedFunctionNameText = replacedFileNameText.replace(readmeFunctionNameRegex, `load${capitalizedName}Shape`), readmeMiniDescriptionRegex = /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template shape\./g, replacedMiniDescriptionText = replacedFunctionNameText.replace(readmeMiniDescriptionRegex, `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} shape.`), readmeUsageRegex = /shape\.type: "template"/g, replacedUsageText = replacedMiniDescriptionText.replace(readmeUsageRegex, `shape.type: "${camelizedName}`), sampleImageRegex = /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/shape-template\/main\/images\/sample.png\)/g, repoPath = repoUrl.includes("github.com")
63
+ const capitalizedName = (0, string_utils_1.capitalize)(name, "-", " "), camelizedName = (0, string_utils_1.camelize)(capitalizedName), dashedName = (0, string_utils_1.dash)(camelizedName), repoPath = repoUrl.includes("github.com")
51
64
  ? repoUrl.substring(repoUrl.indexOf("github.com/") + 11, repoUrl.indexOf(".git"))
52
- : "tsparticles/shape-template", replacedText = replacedUsageText.replace(sampleImageRegex, `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`);
53
- await fs_extra_1.default.writeFile(readmePath, replacedText);
65
+ : "tsparticles/shape-template";
66
+ await (0, file_utils_1.replaceTokensInFile)({
67
+ path: path_1.default.resolve(destPath, "README.md"),
68
+ tokens: [
69
+ {
70
+ from: /tsParticles Template Shape/g,
71
+ to: `tsParticles ${description} Shape`,
72
+ },
73
+ {
74
+ from: /tsparticles-shape-template/g,
75
+ to: `tsparticles-shape-${dashedName}`,
76
+ },
77
+ {
78
+ from: /tsparticles\.shape\.template(\.bundle)?\.min\.js/g,
79
+ to: `tsparticles.shape.${camelizedName}$1.min.js`,
80
+ },
81
+ {
82
+ from: /loadTemplateShape/g,
83
+ to: `load${capitalizedName}Shape`,
84
+ },
85
+ {
86
+ from: /\[tsParticles]\(https:\/\/github.com\/matteobruni\/tsparticles\) additional template shape\./g,
87
+ to: `[tsParticles](https://github.com/matteobruni/tsparticles) additional ${name} shape.`,
88
+ },
89
+ {
90
+ from: /shape\.type: "template"/g,
91
+ to: `shape.type: "${camelizedName}`,
92
+ },
93
+ {
94
+ from: /!\[demo]\(https:\/\/raw.githubusercontent.com\/tsparticles\/shape-template\/main\/images\/sample.png\)/g,
95
+ to: `![demo](https://raw.githubusercontent.com/${repoPath}/main/images/sample.png)`,
96
+ },
97
+ ],
98
+ });
54
99
  }
55
100
  /**
56
101
  * Updates the shape webpack file
@@ -59,7 +104,7 @@ async function updateReadmeFile(destPath, name, description, repoUrl) {
59
104
  * @param description - The description of the project
60
105
  */
61
106
  async function updateShapeWebpackFile(destPath, name, description) {
62
- await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)((0, string_utils_1.capitalize)(name, "-"), " ")), description, "loadParticlesShape");
107
+ await (0, template_utils_1.updateWebpackFile)(destPath, (0, string_utils_1.camelize)((0, string_utils_1.capitalize)(name, "-", " ")), description, "loadParticlesShape");
63
108
  }
64
109
  /**
65
110
  * Creates the shape project
@@ -4,33 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.shapeCommand = void 0;
7
+ const file_utils_1 = require("../../utils/file-utils");
7
8
  const prompts_1 = __importDefault(require("prompts"));
8
9
  const commander_1 = require("commander");
9
10
  const string_utils_1 = require("../../utils/string-utils");
10
11
  const create_shape_1 = require("./create-shape");
11
- const child_process_1 = require("child_process");
12
- const fs_extra_1 = __importDefault(require("fs-extra"));
13
12
  const path_1 = __importDefault(require("path"));
14
13
  const shapeCommand = new commander_1.Command("shape");
15
14
  exports.shapeCommand = shapeCommand;
16
15
  shapeCommand.description("Create a new tsParticles shape");
17
16
  shapeCommand.argument("<destination>", "Destination folder");
18
17
  shapeCommand.action(async (destination) => {
19
- let repoUrl;
20
- const destPath = path_1.default.resolve(path_1.default.join(process.cwd(), destination)), destExists = await fs_extra_1.default.pathExists(destPath);
21
- if (destExists) {
22
- const destContents = await fs_extra_1.default.readdir(destPath), destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
23
- if (destContentsNoGit.length) {
24
- throw new Error("Destination folder already exists and is not empty");
25
- }
26
- }
27
- await fs_extra_1.default.ensureDir(destPath);
28
- try {
29
- repoUrl = (0, child_process_1.execSync)("git config --get remote.origin.url").toString();
30
- }
31
- catch {
32
- repoUrl = "";
33
- }
18
+ const destPath = await (0, file_utils_1.getDestinationDir)(destination), repoUrl = (0, file_utils_1.getRepositoryUrl)();
34
19
  const initialName = destPath.split(path_1.default.sep).pop(), questions = [
35
20
  {
36
21
  type: "text",
@@ -54,5 +39,5 @@ shapeCommand.action(async (destination) => {
54
39
  },
55
40
  ];
56
41
  const { name, description, repositoryUrl } = await (0, prompts_1.default)(questions);
57
- (0, create_shape_1.createShapeTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
42
+ await (0, create_shape_1.createShapeTemplate)(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
58
43
  });
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getRepositoryUrl = exports.getDestinationDir = exports.replaceTokensInFile = exports.replaceTokensInFiles = void 0;
7
+ const child_process_1 = require("child_process");
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const path_1 = __importDefault(require("path"));
10
+ /**
11
+ *
12
+ * @param options -
13
+ */
14
+ async function replaceTokensInFiles(options) {
15
+ for (const item of options) {
16
+ const filePath = path_1.default.resolve(item.path);
17
+ let data = await fs_extra_1.default.readFile(filePath, "utf-8");
18
+ for (const token of item.tokens) {
19
+ const regex = new RegExp(token.from, "g");
20
+ data = data.replace(regex, token.to);
21
+ }
22
+ await fs_extra_1.default.writeFile(filePath, data);
23
+ }
24
+ }
25
+ exports.replaceTokensInFiles = replaceTokensInFiles;
26
+ /**
27
+ *
28
+ * @param options -
29
+ */
30
+ async function replaceTokensInFile(options) {
31
+ await replaceTokensInFiles([options]);
32
+ }
33
+ exports.replaceTokensInFile = replaceTokensInFile;
34
+ /**
35
+ *
36
+ * @param destination -
37
+ * @returns the destination directory path
38
+ */
39
+ async function getDestinationDir(destination) {
40
+ const destPath = path_1.default.resolve(path_1.default.join(process.cwd(), destination)), destExists = await fs_extra_1.default.pathExists(destPath);
41
+ if (destExists) {
42
+ const destContents = await fs_extra_1.default.readdir(destPath), destContentsNoGit = destContents.filter(t => t !== ".git" && t !== ".gitignore");
43
+ if (destContentsNoGit.length) {
44
+ throw new Error("Destination folder already exists and is not empty");
45
+ }
46
+ }
47
+ await fs_extra_1.default.ensureDir(destPath);
48
+ return destPath;
49
+ }
50
+ exports.getDestinationDir = getDestinationDir;
51
+ /**
52
+ * @returns the repository URL
53
+ */
54
+ function getRepositoryUrl() {
55
+ try {
56
+ return (0, child_process_1.execSync)("git config --get remote.origin.url").toString();
57
+ }
58
+ catch {
59
+ return "";
60
+ }
61
+ }
62
+ exports.getRepositoryUrl = getRepositoryUrl;
@@ -4,22 +4,28 @@ exports.dash = exports.camelize = exports.capitalize = void 0;
4
4
  /**
5
5
  * This function is used to capitalize a string.
6
6
  * @param str - the string to capitalize (e.g. "my-string" -\> "MyString")
7
- * @param split - the character used to split the string, if not provided the string will be considered a single word
7
+ * @param splits - the characters used to split the string, if not provided the string will be considered a single word
8
8
  * @returns the capitalized string
9
9
  */
10
- function capitalize(str, split) {
11
- const words = split ? str.split(split) : [str];
12
- return words.map(w => w.replace(/./, c => c.toUpperCase())).join("");
10
+ function capitalize(str, ...splits) {
11
+ let res = str.replace(/./, c => c.toUpperCase());
12
+ for (const split of splits) {
13
+ res = res
14
+ .split(split)
15
+ .map(w => w.replace(/./, c => c.toUpperCase()))
16
+ .join("");
17
+ }
18
+ return res;
13
19
  }
14
20
  exports.capitalize = capitalize;
15
21
  /**
16
22
  * This function is used to camelcase a string.
17
23
  * @param str - the string to camelcase (e.g. "my-string" -\> "myString")
18
- * @param split - the character used to split the string, if not provided the string will be considered a single word
24
+ * @param splits - the characters used to split the string, if not provided the string will be considered a single word
19
25
  * @returns the camelized string
20
26
  */
21
- function camelize(str, split) {
22
- return capitalize(str, split).replace(/./, c => c.toLowerCase());
27
+ function camelize(str, ...splits) {
28
+ return capitalize(str, ...splits).replace(/./, c => c.toLowerCase());
23
29
  }
24
30
  exports.camelize = camelize;
25
31
  /**