Git Product home page Git Product logo

codemods's Introduction

@strapi/codemods

@strapi/codemods

CLI to help you migrate your Strapi applications & plugins from v3 to v4.

Features

  • Migrate a Strapi application to v4
  • Migrate a Strapi plugin to v4
  • Migrate a Strapi application or a plugin's dependecies to v4

Getting started

🖐 Requirements

Before running any commands, be sure you have initialized a git repository, the working tree is clean, you've pushed your code to GitHub, and you are on a new branch.

🕹 Usage

Migrate

Usage with prompt

npx @strapi/codemods migrate

The prompt will ask you:

  • What do you want to migrate?
    • Application (migrate folder structure + dependencies)
    • Plugin (migrate folder structure + dependencies)
    • Dependencies (on migrate dependencies)
  • Where is the project located? (default: ./).
  • (plugin only) Where do you want to create the v4 plugin

Bypass the prompt

To bypass the prompts use one of the following commands:

  • Application migration
npx @strapi/codemods migrate:application <path>
  • Plugin migration
npx @strapi/codemods migrate:plugin <path> [pathForV4Plugin]

Note: if no pathForV4Plugin is provided it will be created at <path>-v4

  • Dependencies migration
npx @strapi/codemods migrate:dependencies <path>

Transform

⚠️ This command will modify your source code. Be sure you have initialized a git repository, the working tree is clean, you've pushed your code to GitHub, and you are on a new branch.

npx @strapi/codemods transform

The prompt will ask two questions:

  • What kind of transformation you want to perform:

    • find -> findMany: Change find method to findMany

    • strapi-some-package -> @strapi/some-package: Update strapi scoped imports

    • .models -> .contentTypes: Change model getters to content types

    • strapi.plugins['some-plugin'] -> strapi.plugin('some-plugin'): Update top level plugin getters

    • strapi.plugin('some-plugin').controllers['some-controller'] -> strapi.plugin('some-plugin').controller('some-controller'): Use plugin getters

    • Add arrow function for service export

    • Add strapi to bootstrap function params

  • Where is the file(s) or folder to transform

Enjoy 🎉

codemods's People

Contributors

alexandrebodin avatar benjenkyn avatar bglidwell avatar dependabot[bot] avatar derrickmehaffy avatar echatzief avatar maccomaccomaccomacco avatar markkaylor avatar remidej avatar stun3r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codemods's Issues

Script not working in monorepo: `Not a git repository: backend/.git`

We originally used the strapi-starter-gatsby-corporate template to set up our website. This sets up two folders in a single Git repo: backend (Strapi) and frontend (Gatsby). Now that I'm trying to upgrade/migrate it to v4, I'm running into the error below, as the script is looking for a .git folder in the backend folder, while we only have a .git folder in the root folder of our project.

Could you please add an option to ignore this check? I can make sure myself that my Git working tree is clean 😉 thanks!

% npx @strapi/codemods migrate:application backend
npx: installed 568 in 40.029s
error: Command failed with exit code 128: git --git-dir=backend/.git --work-tree=backend status --porcelain
fatal: not a git repository: 'backend/.git'

Component field relations dissappearing during the migration process

Bug report

Hello and thanks for the hard work with the migration scripts! This is my first PR so let me know in case there is something unclear. Also steps 5. and 8. are something that could raise another issue, but I think that the most critical and valuable thing to solve is the described issue.

Required System information

  • Node.js version: 14 on v3, 16 on v4
  • NPM version: 7.20.3
  • Source Strapi version: 4.3.8
  • Target Strapi version: 3.5.1
  • Source Database: sqlite
  • Target Database: sqlite
  • Operating system: osx
  • Which script are you running: v3-sql-v4-sql

Describe the bug

Component field relations are dissappearing during the migration process

Steps to reproduce the behavior

  1. npx [email protected] reproductible-relation-error
  2. Create following things in the admin panel:
  • Collection Tools
    • name (string)
  • Component Toolbox with fields
    • tools (relation with tools)
      • one toolbox can have multiple tools
  • Collection Carpenter
    • name (string)
    • Toolbox (repeatable component)
  1. Add some carpenters with toolboxes that contain tools
  2. Do migration to strapi v4
  3. Remove fields from strapi_permission in the DB
  4. Run the v3-sql-v4-sql migration script
  5. Encounter Cannot read property 'fields' of null error
  6. On table admin_permissions change the null fields to {} in the column properties
  7. Everything runs nicely
  8. Witness that the component relations have disappeared from v4 DB

Expected behavior

Component relations should migrate to v4

Screenshots

To visualize the collections:

Screenshot 2022-09-16 at 15 58 28

Screenshot 2022-09-16 at 15 58 23

V3 API returns:

Screenshot 2022-09-16 at 15 57 48

V4 API returns:

image

V3 database structure seems to show the components_element_stores__tools

Screenshot 2022-09-16 at 16 44 25

but the V4 seems to have discarded it

image

Code snippets

Additional context

I'm happy to provide the v3 and v4 databases if needed

Error: ER_TABLE_EXISTS_ERROR: Table 'mapcareers' already exists

Bug report

Required System information

create table mapCareers (id int unsigned not null auto_increment primary key, title varchar(255) null, description longtext null) - ER_TABLE_EXISTS_ERROR: Table 'mapcareers' already exists
Error: ER_TABLE_EXISTS_ERROR: Table 'mapcareers' already exists
at Query.Sequence._packetToError (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\Connection.js:88:28)
at Socket. (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (node:events:394:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
--------------------
at Protocol._enqueue (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\Hussain\Desktop\backend-clone\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\dialects\mysql\index.js:126:18
at new Promise ()
at Client_MySQL._query (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\dialects\mysql\index.js:120:12)
at executeQuery (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\internal\query-executioner.js:37:17)
at Client_MySQL.query (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\client.js:134:12)
at C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\transaction.js:363:24
at new Promise ()
at Client_MySQL.trxClient.query (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\transaction.js:358:12)
at Runner.query (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\run at Runner.queryArray (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\runner.js:224:21)
at ensureConnectionCallback (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\internal\ensure-connection-callback.js:11:19)
at Runner.ensureConnection (C:\Users\Hussain\Desktop\backend-clone\node_modules@strapi\database\node_modules\knex\lib\execution\runner.js:272:20)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
error Command failed with exit code 1.

  • Node.js version: 16.9.1
  • Strapi version: 4.1.2
  • Database: mysql
  • Operating system: windows

Describe the bug

I am migrating my strapi version from v3 to v4. I have followed the documentation and used codemods to change the folder structure and to update the schemas. Once I change the dependencies and build and run the project with my database I am getting this error. I am not understanding why it is creating the tables again.

Why does codemods create a `v3` folder?

I have a folder called v3, which was generated by codemods. It seems that this folder contains the old code, but does it contain everything? What is it supposed to contain? In my case, it contains all main folders (so admin, api, config, extensions and plugins), except public. I suppose this is just created only as a backup and can be removed if we don't need to keep anything from our v3 app. Is that right?

Btw, in my case, the plugins folder was completely ignored (apparently). I suppose this is because the tool cannot automatically migrate plugins in general.

error Command "@strapi/codemods" not found.

Hi Strapi Team,

Whenever I do:
yarn @strapi/codemods migrate:plugin ./plugins/pluginname-v3/ ./plugins/pluginname-v4/
(as documentation and the README.md of this repo state)

I get:

yarn run v1.22.18
error Command "@strapi/codemods" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

But I did
yarn add @strapi/codemods
before successfully.

I'm doing something obviously stupid right? :D
Thank you kind stranger for your time and effort!

Update from v3 to v4 ( database migration )

Hey,

I'm trying to use the codemods plugin for the migration of the v3 to v4. However, the codemods plugin doesn't work properly and i will do the migration by hand. Is there a way to transfer the database in that case?

Thanks in advance

[feature request] New codemod to migrate controllers, routes and services files to v4 version

The idea is to create a codemod that loops on all the folder of the new src/api folder. And edits the files in the folders controllers/<route-name>.js, routes/<route-name>.js andservices/<route-name>.js with the new methods createCoreController, createCoreRouter, createCoreService

v3 Router

module.exports = [
  {
    method: 'GET',
    path: '/actualites',
    handler: 'actualite.find',
    config: { policies: [] }
  },
  {
    method: 'GET',
    path: '/actualites/:id',
    handler: 'actualite.findOne',
    config: { policies: [] }
  },
  {
    method: 'POST',
    path: '/actualites',
    handler: 'actualite.create',
    config: { policies: [] }
  },
  {
    method: 'PUT',
    path: '/actualites/:id',
    handler: 'actualite.update',
    config: { policies: [] }
  },
  {
    method: 'DELETE',
    path: '/actualites/:id',
    handler: 'actualite.delete',
    config: { policies: [] }
  }
]

v4 Router

'use strict';

/**
 * actualite router.
 */

const { createCoreRouter } = require('@strapi/strapi').factories;

module.exports = createCoreRouter('api::actualite.actualite');

Error migrating application with multiple `database.js` files using `env` folder

While migrating my application I got the error:

error: Cannot find module '/[...]/strapi-project/config/database.js'
Require stack:
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/lib/v4/migration-helpers/update-application-folder-structure.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/lib/v4/migration-helpers/index.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/lib/v4/index.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/lib/index.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/bin/commands/migrate.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/bin/commands/default-commands.js
- /home/<user>/.npm/_npx/636cd3a3cca06392/node_modules/@strapi/codemods/bin/cli.js

I do not have a config/database.js, because I use environments using the env folder: https://docs-v3.strapi.io/developer-docs/latest/setup-deployment-guides/configurations.html#formats

It would be nice, if codemods supported this case gracefully.

Tested using @strapi/[email protected]

"Cannot set property 'node' of undefined" - solution

Hi guys, I just wanted to mention an issue I ran into while using codemods script. After running the npx @strapi/codemods migrate command I received an error Cannot set property 'node' of undefined.

It turned out my package.json file was missing the "engines": {} part and in the codemods update-package-dependencies.js these lines were causing it:

image

I hope it's going to help someone.

Latest(Oct2023) script version wants node > 18 < 20

Looks like script just set up latest strapi version, but it caused a problem with node version. I guess for migration purpose would be nice to freeze strapi version with something that used similar environment.

yarn

yarn install v1.22.5
info No lockfile found.
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/5] Validating package.json...
[2/5] Resolving packages...
warning knex > [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (debug-js/debug#797)
warning knex > [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
warning knex > liftoff > findup-sync > micromatch > snapdragon > [email protected]: See https://github.com/lydell/source-map-resolve#deprecated
warning knex > liftoff > findup-sync > micromatch > snapdragon > source-map-resolve > [email protected]: Please see https://github.com/lydell/urix#deprecated
warning knex > liftoff > findup-sync > micromatch > snapdragon > source-map-resolve > [email protected]: See https://github.com/lydell/source-map-url#deprecated
warning knex > liftoff > findup-sync > micromatch > snapdragon > source-map-resolve > [email protected]: https://github.com/lydell/resolve-url#deprecated
warning @strapi/strapi > @koa/[email protected]: IMPORTANT 10x+ PERFORMANCE UPGRADE: Please upgrade to v12.0.1+ as we have fixed an issue with debuglog causing 10x slower router benchmark performance, see koajs/router#173
warning @strapi/strapi > @strapi/types > @koa/[email protected]: IMPORTANT 10x+ PERFORMANCE UPGRADE: Please upgrade to v12.0.1+ as we have fixed an issue with debuglog causing 10x slower router benchmark performance, see koajs/router#173
warning @strapi/strapi > koa-body > [email protected]: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
warning @strapi/strapi > @strapi/admin > fork-ts-checker-webpack-plugin > [email protected]: this will be v4
warning @strapi/strapi > @strapi/admin > webpack-dev-server > webpack-dev-middleware > [email protected]: this will be v4
warning @strapi/strapi > @strapi/plugin-email > @strapi/provider-email-sendmail > sendmail > [email protected]: This project is unmaintained
warning @strapi/strapi > @strapi/plugin-email > @strapi/provider-email-sendmail > sendmail > mailcomposer > [email protected]: This project is unmaintained
[3/5] Fetching packages...
error @strapi/[email protected]: The engine "node" is incompatible with this module. Expected version ">=18.0.0 <=20.x.x". Got "16.20.2"
error Found incompatible module.

migrate plugin does not recognize Strapi plugin dir as Strapi

When I do:

PS C:\Users\username\Desktop\git\mystrapi> npx @strapi/codemods
? What would you like to do? Migrate
? What do you want to migrate? Plugin
? Enter the path to your Strapi plugin C:\Users\username\Desktop\git\mystrapi\plugins\myplugin\
? Where would you like to create your v4 plugin?
C:\Users\username\Desktop\git\mystrapi\plugins\myplugin-v4

on Powershell / CMD.

I get:
error: The specified path is not a Strapi project. Please check the path and try again.

But it is :)
I can run it, have an admin user, can access admin panel etc. etc.

I checked out codemods and searched for the string:

grafik

But there are a lot of things I´m not sure of.
For example: is path my whole C:\Users\username\Desktop\git\mystrapi\plugins\myplugin\ because there wouldn´t be a package.json intentionally - or is it already preprocessed?

I tried to debug it but I don´t know how to get it executable.
I guess I´d have to build it somehow?
But there is only:

"test": "jest --verbose",
"lint": "eslint .",
"lint:fix": "eslint --fix ."

in the scripts section of package.json

Maybe someone can shed a little light for me :)
I will analyze the problem myself and post a PR if this is welcome but I would highly appreciate some pointers how to build / run it.

[feature-request]: Update components

I think it would help a lot, if the script also adapts following stuff.

add the displayName to the info object because it causes some issues in the UI and babel config if there is no displayName.

 "info": {
    "name": "Link",
    "displayName": <NAME>,
  },

and change the model file to from

    "picture": {
      "model": "file",
      "via": "related",
      "plugin": "upload",
      "required": false
    },

to

    "picture": {
      "type": "media",
      "required": false
    },

migrate:plugin gives error due to missing functions directory

Using Windows. Migrating from 3.6.10.

C:\Code\XXX\strapi>npx @strapi/codemods migrate:plugin v3\plugins\my-plugin src\plugins\my-plugin
npx: installed 570 in 55.462s
info: copied v3 plugin to C:\Code\XXX\strapi\src\plugins\my-plugin
info: created C:\Code\XXX\strapi\src\plugins\my-plugin\strapi-admin.js
info: created C:\Code\XXX\strapi\src\plugins\my-plugin\strapi-server.js
info: moved controllers to C:\Code\XXX\strapi\src\plugins\my-plugin\server\controllers
info: moved services to C:\Code\XXX\strapi\src\plugins\my-plugin\server\services
error: ENOENT: no such file or directory, scandir 'C:\Code\XXX\strapi\src\plugins\my-plugin\config\functions'

ENOENT: no such file or directory, scandir ...

I'm attempting to migrate strapi-plugin-react-editorjs over to version 4. At this time, the owner is unable to work on the migration, so I'm trying to figure out how to do this.

I keep getting an error saying "error: ENOENT: no such file or directory, scandir '/Volumes/Work/Web/strapicodemods/strapi-plugin-react-editorjs-v4/config/functions'", and I just can't figure out if this is a problem with the original code or if it's a problem with the codemods plugin migrate tool.

Any help or guidance with this would be greatly appreciated!

give option to avoid reformatting code or at least respect prettier rc in project root

There are coding conventions that teams follow that this mod is assuming as THE standard. However, it should be to the engineer what to transform. So give them the option to disable reformatting of code by both disabling prettier completely and/or using prettier .prettierrc.js file... in project root.

I had to fork and comment out the line that formats the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.