@commercetools-frontend/codemod 0.0.0-canary-20220428154636

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 commercetools GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @commercetools-frontend/codemod
2
+
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/@commercetools-frontend/codemod"><img src="https://badgen.net/npm/v/@commercetools-frontend/codemod" alt="Latest release (latest dist-tag)" /></a> <a href="https://www.npmjs.com/package/@commercetools-frontend/codemod"><img src="https://badgen.net/npm/v/@commercetools-frontend/codemod/next" alt="Latest release (next dist-tag)" /></a> <a href="https://bundlephobia.com/result?p=@commercetools-frontend/codemod"><img src="https://badgen.net/bundlephobia/minzip/@commercetools-frontend/codemod" alt="Minified + GZipped size" /></a> <a href="https://github.com/commercetools/merchant-center-application-kit/blob/main/LICENSE"><img src="https://badgen.net/github/license/commercetools/merchant-center-application-kit" alt="GitHub license" /></a>
5
+ </p>
6
+
7
+ Codemod transformations for Custom Applications.
8
+
9
+ ## Usage
10
+
11
+ ```bash
12
+ $ npx @commercetools-frontend/codemod <transform> <glob_pattern>
13
+ ```
14
+
15
+ ## Transforms
16
+
17
+ ### `rename-js-to-jsx`
18
+
19
+ Rename `.js` files using React JSX syntax to `.jsx`.
20
+
21
+ ```
22
+ $ npx @commercetools-frontend/codemod rename-js-to-jsx 'src/**/*.js'
23
+ ```
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../').run();
package/build/cli.js ADDED
@@ -0,0 +1,63 @@
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.run = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const mri_1 = __importDefault(require("mri"));
9
+ const glob_1 = __importDefault(require("glob"));
10
+ // @ts-ignore internal module
11
+ const Runner_1 = __importDefault(require("jscodeshift/src/Runner"));
12
+ const run = () => {
13
+ const flags = (0, mri_1.default)(process.argv.slice(2), {
14
+ alias: { help: ['h'] },
15
+ boolean: ['dry-run'],
16
+ });
17
+ const commands = flags._;
18
+ if (commands.length === 0 || (flags.help && commands.length === 0)) {
19
+ console.log(`
20
+ Usage: mc-codemod [global-options] [transform] [glob-pattern]
21
+
22
+ Global options:
23
+
24
+ --dry-run (optional) Executes the command but does not send any mutation request.
25
+
26
+ Transforms:
27
+
28
+ rename-js-to-jsx Rename ".js" files using React JSX syntax to ".jsx".
29
+ `);
30
+ process.exit(0);
31
+ }
32
+ const [transform, globPattern] = commands;
33
+ const files = glob_1.default.sync(globPattern);
34
+ const runJscodeshift = async (transformPath, filePaths, options) => {
35
+ await Runner_1.default.run(transformPath, filePaths, options);
36
+ };
37
+ const execute = async () => {
38
+ switch (transform) {
39
+ case 'rename-js-to-jsx': {
40
+ const transformPath = path_1.default.join(__dirname, `transforms/${transform}.js`);
41
+ await runJscodeshift(transformPath, files, {
42
+ extensions: 'tsx,ts,jsx,js',
43
+ ignorePattern: [
44
+ '**/node_modules/**',
45
+ '**/public/**',
46
+ '**/dist/**',
47
+ '**/build/**',
48
+ ],
49
+ parser: 'tsx',
50
+ dry: flags['dry-run'],
51
+ });
52
+ break;
53
+ }
54
+ default:
55
+ throw new Error(`Unknown transform ${transform}.`);
56
+ }
57
+ };
58
+ execute().catch((error) => {
59
+ console.error(error.stack || error.message || error);
60
+ process.exit(1);
61
+ });
62
+ };
63
+ exports.run = run;
@@ -0,0 +1,25 @@
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
+ const fs_1 = __importDefault(require("fs"));
7
+ function renameJsToJsx(file, api, options) {
8
+ const j = api.jscodeshift;
9
+ const root = j(file.source);
10
+ if (file.path.endsWith('.js') && !file.path.endsWith('.spec.js')) {
11
+ const hasJSXElements = root.findJSXElements().length > 0;
12
+ if (hasJSXElements) {
13
+ if (options.dry) {
14
+ api.stats(file.path);
15
+ }
16
+ else {
17
+ const renamedFilePath = file.path.replace('.js', '.jsx');
18
+ fs_1.default.renameSync(file.path, renamedFilePath);
19
+ api.report(`renamed to ${renamedFilePath}`);
20
+ }
21
+ }
22
+ }
23
+ return root.toSource(options);
24
+ }
25
+ exports.default = renameJsToJsx;
package/build/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@commercetools-frontend/codemod",
3
+ "version": "0.0.0-canary-20220428154636",
4
+ "description": "Codemod transformations for Custom Applications",
5
+ "bugs": "https://github.com/commercetools/merchant-center-application-kit/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/commercetools/merchant-center-application-kit.git",
9
+ "directory": "packages/codemod"
10
+ },
11
+ "homepage": "https://docs.commercetools.com/custom-applications",
12
+ "keywords": [
13
+ "javascript",
14
+ "frontend",
15
+ "codemod",
16
+ "toolkit"
17
+ ],
18
+ "license": "MIT",
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "main": "./build/cli.js",
23
+ "files": [
24
+ "bin",
25
+ "build",
26
+ "package.json",
27
+ "LICENSE",
28
+ "README.md"
29
+ ],
30
+ "bin": {
31
+ "mc-codemod": "./bin/mc-codemod.js"
32
+ },
33
+ "scripts": {
34
+ "build": "rimraf build && tsc",
35
+ "build:bundles:watch": "yarn build -w"
36
+ },
37
+ "dependencies": {
38
+ "glob": "8.0.1",
39
+ "jscodeshift": "0.13.1",
40
+ "mri": "1.2.0"
41
+ },
42
+ "devDependencies": {
43
+ "@tsconfig/node14": "^1.0.1",
44
+ "@types/jscodeshift": "0.11.0",
45
+ "rimraf": "3.0.2",
46
+ "typescript": "4.6.3"
47
+ },
48
+ "engines": {
49
+ "node": ">=14"
50
+ }
51
+ }