mazey 4.1.0 → 4.1.1

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.
package/lib/index.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Mazey v4.1.0 https://github.com/mazeyqian/mazey
2
+ * Mazey v4.1.1 https://github.com/mazeyqian/mazey
3
3
  * (c) 2018-2024 Cheng
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Mazey v4.1.0 https://github.com/mazeyqian/mazey
2
+ * Mazey v4.1.1 https://github.com/mazeyqian/mazey
3
3
  * (c) 2018-2024 Cheng
4
4
  * Released under the MIT License.
5
5
  */
package/lib/mazey.min.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Mazey v4.1.0 https://github.com/mazeyqian/mazey
2
+ * Mazey v4.1.1 https://github.com/mazeyqian/mazey
3
3
  * (c) 2018-2024 Cheng
4
4
  * Released under the MIT License.
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mazey",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "A functional library for daily front-end work.",
5
5
  "main": "lib/index.cjs.js",
6
6
  "module": "lib/index.esm.js",
@@ -0,0 +1,38 @@
1
+ # scripts
2
+
3
+ ## release
4
+
5
+ Release this project with version by `process.env.VERSION` or `package.json`.
6
+
7
+ Usage:
8
+
9
+ ```javascript
10
+ const pkgVersion = process.env.VERSION || require('../package.json').version;
11
+ release(pkgVersion);
12
+ ```
13
+
14
+ It will be more straightforward if you use the development dependence CrossEnv.
15
+
16
+ ```shell
17
+ # Install
18
+ npm i cross-env -D
19
+
20
+ # scripts
21
+ cross-env SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version node ./scripts/release.js
22
+ ```
23
+
24
+ ```javascript
25
+ // release.js
26
+ release();
27
+ ```
28
+
29
+ <!-- @param {string} ver Version
30
+ @returns {void} -->
31
+
32
+ ## generateToc
33
+
34
+ Generate table of contents for the markdown file.
35
+
36
+ <!-- @param {string} path Path of markdown file.
37
+ @param {array} options.hiddenHeadings Hidden headings.
38
+ @returns {void} -->
@@ -0,0 +1,76 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ /* eslint-disable no-undef */
3
+ const path = require("path");
4
+ const _resolve = (_path) => path.resolve(__dirname, _path);
5
+ const format = require("date-fns/format");
6
+ const toc = require("markdown-toc");
7
+ const fs = require("fs");
8
+ // Generate version: 20210308.072903
9
+ const genVersion = () => {
10
+ const d = new Date();
11
+ const version = format(d, "yyyyMMdd.HHmmss");
12
+ return version;
13
+ };
14
+ const version = genVersion();
15
+ const defaultHiddenHeadings = [
16
+ "mazey",
17
+ "Install",
18
+ "Usage",
19
+ "Contributing",
20
+ "License",
21
+ "API Examples",
22
+ "Development Environment",
23
+ "Returns",
24
+ "Scripts",
25
+ "Node.js",
26
+ "TypeScript",
27
+ "Table of Contents",
28
+ ];
29
+
30
+ /**
31
+ * Generate table of contents for the markdown file.
32
+ *
33
+ * @param {string} path Path of markdown file.
34
+ * @param {array} options.hiddenHeadings Hidden headings.
35
+ * @returns {void}
36
+ */
37
+ const generateToc = (path = "../README.md", options = {
38
+ hiddenHeadings: defaultHiddenHeadings,
39
+ }) => {
40
+ console.log("Generating Table of Contents...");
41
+ options = Object.assign({
42
+ hiddenHeadings: defaultHiddenHeadings,
43
+ }, options);
44
+ const RMFilePath = _resolve(path);
45
+ // https://melvingeorge.me/blog/get-all-the-contents-from-file-as-string-nodejs
46
+ console.log("running...");
47
+ const buffer = fs.readFileSync(RMFilePath);
48
+ const fileContent = buffer.toString();
49
+ // https://github.com/jonschlinkert/markdown-toc
50
+ const removeUnnecessaryHeadings = (str) => {
51
+ return !options.hiddenHeadings.some(v => {
52
+ return str.includes(v);
53
+ });
54
+ };
55
+ let tocContent = toc(fileContent, {
56
+ filter: removeUnnecessaryHeadings,
57
+ bullets: [ "-", "-" ],
58
+ }).content;
59
+ tocContent = tocContent.replace(/[ ]{4}/gm, "");
60
+ tocContent = "<!-- toc - begin -->\n" + "- Generated with ❤️\n" + tocContent;
61
+ tocContent = tocContent + "\n<!-- toc - end -->";
62
+ // Insert to file.
63
+ // https://stackoverflow.com/questions/14177087/replace-a-string-in-a-file-with-nodejs
64
+ let newFileContent = fileContent;
65
+ // https://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines
66
+ newFileContent = newFileContent.replace(/<!-- toc - begin -->[\s\S]*<!-- toc - end -->/gm, tocContent);
67
+ console.log("running...");
68
+ fs.writeFileSync(RMFilePath, newFileContent);
69
+ console.log("Generating Table of Contents is done.");
70
+ };
71
+
72
+ module.exports = {
73
+ _resolve,
74
+ version,
75
+ generateToc,
76
+ };
package/scripts/dev.js ADDED
@@ -0,0 +1,23 @@
1
+ /* eslint-disable no-undef */
2
+ /* eslint-disable @typescript-eslint/no-var-requires */
3
+ // const execa = require('execa');
4
+ const { generateToc } = require("./build-helper");
5
+
6
+ (async () => {
7
+ // const { stdout: pushMsg = '' } = await execa('git', ['push'], { stdio: 'pipe' })
8
+ // .catch(error => {
9
+ // console.log('error:', error.message);
10
+ // });
11
+ // try {
12
+ // await execa('git', ['push'], { stdio: 'pipe' });
13
+ // } catch (error) {
14
+ // console.log('error:', error.message);
15
+ // const { stdout: currentBranch } = await execa('git', ['branch', '--show-current'], { stdio: 'pipe' });
16
+ // console.log('currentBranch:', currentBranch); // currentBranch: fix/rollup_dependences_mon_oct_31st_2022
17
+ // await execa('git', ['push', '--set-upstream', 'origin', currentBranch]);
18
+ // }
19
+ // console.log('pushMsg:', pushMsg);
20
+ // const cb = await getGitCurrentBranch('fix returns value');
21
+ // console.log('bbb', cb);
22
+ generateToc();
23
+ })();
@@ -0,0 +1,163 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ /* eslint-disable no-undef */
3
+ // Git Helper
4
+ const execa = require("execa");
5
+
6
+ /**
7
+ * Release this project with version by `process.env.VERSION` or `package.json`.
8
+ *
9
+ * Usage:
10
+ *
11
+ * ```javascript
12
+ * const pkgVersion = process.env.VERSION || require('../package.json').version;
13
+ * release(pkgVersion);
14
+ * ```
15
+ *
16
+ * It will be more straightforward if you use the development dependence CrossEnv.
17
+ *
18
+ * ```shell
19
+ * # Install
20
+ * npm i cross-env -D
21
+ *
22
+ * # scripts
23
+ * cross-env SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version node ./scripts/release.js
24
+ * ```
25
+ *
26
+ * ```javascript
27
+ * // release.js
28
+ * release();
29
+ * ```
30
+ *
31
+ * @param {string} ver Version
32
+ * @returns {void}
33
+ */
34
+ async function release (ver, { canGenerateToc = false, defaultBranch = "main" } = {}) {
35
+ if (!ver) {
36
+ ver = process.env.SCRIPTS_NPM_PACKAGE_VERSION;
37
+ }
38
+ if (!ver) {
39
+ console.error("Fail to get the current version.");
40
+ return;
41
+ }
42
+ const { generateToc } = require("./build-helper");
43
+ const releaseVersion = `v${ver}`;
44
+ const { stdout: releaseStdout } = await execa("echo", [ `Start release ${releaseVersion}...` ]);
45
+ console.log(releaseStdout);
46
+ // Generating Table of Contents
47
+ if (canGenerateToc) {
48
+ generateToc();
49
+ }
50
+ // Commit
51
+ await gitCommit(`${releaseVersion} stage`);
52
+ // Marge
53
+ await gitMergeMain2Release(defaultBranch);
54
+ // Build
55
+ await execa("npm", [ "run", "preview" ]);
56
+ await execa("npm", [ "publish" ]);
57
+ // Commit Again
58
+ await gitCommit(releaseVersion);
59
+ // Push
60
+ console.log("Pushing to the remote Git...");
61
+ // await execa('git', ['tag', '-a', `${releaseVersion}`, '-m', `Release ${releaseVersion}`]);
62
+ // await execa('git', ['push', 'origin', `refs/tags/${releaseVersion}`]);
63
+ await gitTagPush(releaseVersion);
64
+ await gitPush();
65
+ console.log("All done.");
66
+ }
67
+
68
+ /**
69
+ * Push git tag.
70
+ *
71
+ * @example
72
+ * ```
73
+ * await gitTagPush('v1.0.0');
74
+ * ```
75
+ *
76
+ * @param {string} ver Release version
77
+ * @returns {boolean} Is success
78
+ */
79
+ async function gitTagPush (ver = "") {
80
+ if (!ver) {
81
+ console.error("Fail to get the current version.");
82
+ return false;
83
+ }
84
+ if (!ver.startsWith("v")) {
85
+ ver = `v${ver}`;
86
+ }
87
+ await execa("git", [ "tag", "-a", `${ver}`, "-m", `Release ${ver}` ]);
88
+ await execa("git", [ "push", "origin", `refs/tags/${ver}` ]);
89
+ return true;
90
+ }
91
+
92
+ /**
93
+ * Push code.
94
+ */
95
+ async function gitPush () {
96
+ try {
97
+ await execa("git", [ "push" ], { stdio: "pipe" });
98
+ } catch (error) {
99
+ console.log("Error:", error.message);
100
+ const { stdout: currentBranch } = await execa("git", [ "branch", "--show-current" ], { stdio: "pipe" });
101
+ console.log(`Current Branch: ${currentBranch}`);
102
+ await execa("git", [ "push", "--set-upstream", "origin", currentBranch ]);
103
+ }
104
+ return true;
105
+ }
106
+
107
+ /**
108
+ * Merge main to current branch.
109
+ */
110
+ async function gitMergeMain2Release (defaultBranch = "main") {
111
+ const currentBranch = await getGitCurrentBranch();
112
+ await execa("git", [ "checkout", defaultBranch ]);
113
+ await execa("git", [ "pull" ]);
114
+ await execa("git", [ "checkout", currentBranch ]);
115
+ await execa("git", [ "merge", defaultBranch ]);
116
+ return true;
117
+ }
118
+
119
+ /**
120
+ * Alias of `gitMergeMain2Release`.
121
+ */
122
+ async function gitMergeMaster2Release (defaultBranch = "main") {
123
+ return await gitMergeMain2Release(defaultBranch);
124
+ }
125
+
126
+ /**
127
+ * Get git current branch.
128
+ */
129
+ async function getGitCurrentBranch () {
130
+ let currentBranch = "";
131
+ try {
132
+ ({ stdout: currentBranch } = await execa("git", [ "branch", "--show-current" ], { stdio: "pipe" }));
133
+ } catch (error) {
134
+ ({ stdout: currentBranch } = await execa("git", [ "rev-parse", "--abbrev-ref", "HEAD" ], { stdio: "pipe" }));
135
+ }
136
+ console.log(`Current Branch: ${currentBranch}`);
137
+ return currentBranch;
138
+ }
139
+
140
+ /**
141
+ * Commit current code.
142
+ */
143
+ async function gitCommit (releaseVersion = 0) {
144
+ const { stdout: diffStdout } = await execa("git", [ "diff" ], { stdio: "pipe" });
145
+ if (diffStdout) {
146
+ console.log("Committing changes...");
147
+ await execa("git", [ "add", "-A" ]);
148
+ await execa("git", [ "commit", "-m", `release: ${releaseVersion}` ]);
149
+ } else {
150
+ console.log("No changes to commit.");
151
+ }
152
+ return true;
153
+ }
154
+
155
+ module.exports = {
156
+ release,
157
+ gitPush,
158
+ gitMergeMaster2Release, // Deprecated
159
+ gitMergeMain2Release,
160
+ getGitCurrentBranch,
161
+ gitCommit,
162
+ gitTagPush,
163
+ };
@@ -0,0 +1,9 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ /* eslint-disable no-undef */
3
+ // release
4
+ const { release } = require("./git-helper");
5
+ const pkgVersion = process.env.SCRIPTS_NPM_PACKAGE_VERSION || process.env.VERSION || require("../package.json").version;
6
+
7
+ console.log("release: SCRIPTS_NPM_PACKAGE_VERSION", process.env.SCRIPTS_NPM_PACKAGE_VERSION);
8
+
9
+ release(pkgVersion, { canGenerateToc: true });
@@ -0,0 +1,121 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ /* eslint-disable no-undef */
3
+ import { babel } from "@rollup/plugin-babel";
4
+ import commonjs from "@rollup/plugin-commonjs";
5
+ import rollupTypescript from "rollup-plugin-typescript2";
6
+ import { DEFAULT_EXTENSIONS } from "@babel/core";
7
+ import cleaner from "rollup-plugin-cleaner";
8
+ import terser from "@rollup/plugin-terser";
9
+ import { dts } from "rollup-plugin-dts";
10
+ import path from "path";
11
+ import { fileURLToPath } from "url";
12
+ import { dirname } from "path";
13
+
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = dirname(__filename);
16
+ const _resolve = (_path) => path.resolve(__dirname, _path);
17
+ const pkgVersion = process.env.SCRIPTS_NPM_PACKAGE_VERSION || process.env.VERSION || "unknown";
18
+ const debugMode = process.env.SCRIPTS_NPM_PACKAGE_DEBUG;
19
+ const banner =
20
+ "/*!\n" +
21
+ ` * Mazey v${pkgVersion} https://github.com/mazeyqian/mazey\n` +
22
+ ` * (c) 2018-${new Date().getFullYear()} Cheng\n` +
23
+ " * Released under the MIT License.\n" +
24
+ " */";
25
+ const plugins = [
26
+ rollupTypescript(),
27
+ commonjs({
28
+ include: /node_modules/,
29
+ }),
30
+ babel({
31
+ babelHelpers: "runtime",
32
+ // Just convert the source code, don't run external dependencies.
33
+ exclude: "**/node_modules/**",
34
+ // Babel does not support TypeScript by default; it needs to be manually added.
35
+ extensions: [
36
+ ...DEFAULT_EXTENSIONS,
37
+ ".ts",
38
+ ],
39
+ }),
40
+ ];
41
+ const iifePlugins = [];
42
+ const dTsConf = {
43
+ input: _resolve("../src/typing.d.ts"),
44
+ // https://rollupjs.org/guide/en/#outputformat
45
+ output: [
46
+ {
47
+ file: _resolve("../lib/typing.d.ts"),
48
+ format: "es",
49
+ },
50
+ ],
51
+ plugins: [
52
+ dts(),
53
+ ],
54
+ external: [],
55
+ };
56
+ const gTsConf = {
57
+ input: _resolve("../global.d.ts"),
58
+ output: [
59
+ {
60
+ file: _resolve("../lib/global.d.ts"),
61
+ format: "es",
62
+ },
63
+ ],
64
+ plugins: [
65
+ dts(),
66
+ ],
67
+ external: [],
68
+ };
69
+
70
+ if (debugMode !== "open") {
71
+ iifePlugins.push(
72
+ // Add minification.
73
+ // https://github.com/TrySound/rollup-plugin-terser
74
+ terser({ // https://github.com/terser/terser
75
+ format: {
76
+ // https://github.com/terser/terser#format-options
77
+ comments: /^!\n\s\*\sMazey/,
78
+ },
79
+ }),
80
+ );
81
+ }
82
+
83
+ // https://rollupjs.org/guide/en/
84
+ export default [
85
+ {
86
+ input: _resolve("../src/index.ts"),
87
+ // https://rollupjs.org/guide/en/#outputformat
88
+ output: [
89
+ {
90
+ file: _resolve("../lib/index.cjs.js"),
91
+ format: "cjs",
92
+ banner,
93
+ plugins: iifePlugins,
94
+ },
95
+ {
96
+ file: _resolve("../lib/index.esm.js"),
97
+ format: "esm",
98
+ banner,
99
+ plugins: iifePlugins,
100
+ },
101
+ {
102
+ file: _resolve("../lib/mazey.min.js"),
103
+ format: "iife",
104
+ name: "mazey",
105
+ banner,
106
+ plugins: iifePlugins,
107
+ },
108
+ ],
109
+ plugins: [
110
+ ...plugins,
111
+ cleaner({
112
+ targets: [
113
+ _resolve("../lib/"),
114
+ ],
115
+ }),
116
+ ],
117
+ external: [],
118
+ },
119
+ dTsConf,
120
+ gTsConf,
121
+ ];