Skip to content

Commit

Permalink
Merge pull request #18995 from strapi/upgrade-tool/refactor-v5
Browse files Browse the repository at this point in the history
  • Loading branch information
Convly committed Dec 8, 2023
2 parents 24dabd2 + 997b349 commit fdc903e
Show file tree
Hide file tree
Showing 109 changed files with 3,634 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -121,7 +121,7 @@
"eslint-plugin-testing-library": "6.0.2",
"execa": "5.1.1",
"find-up": "5.0.0",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"get-port": "5.1.1",
"glob": "7.2.3",
"husky": "8.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/create-strapi-starter/package.json
Expand Up @@ -49,7 +49,7 @@
"ci-info": "3.8.0",
"commander": "8.3.0",
"execa": "5.1.1",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"inquirer": "8.2.5",
"ora": "5.4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/admin/package.json
Expand Up @@ -103,7 +103,7 @@
"fork-ts-checker-webpack-plugin": "8.0.0",
"formik": "2.4.0",
"fractional-indexing": "3.2.0",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"highlight.js": "^10.4.1",
"history": "^4.9.0",
"html-webpack-plugin": "5.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/content-type-builder/package.json
Expand Up @@ -64,7 +64,7 @@
"@strapi/helper-plugin": "4.15.5",
"@strapi/icons": "1.13.2",
"@strapi/utils": "4.15.5",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"immer": "9.0.19",
"koa-bodyparser": "4.4.1",
"lodash": "4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/data-transfer/package.json
Expand Up @@ -48,7 +48,7 @@
"chalk": "4.1.2",
"cli-table3": "0.6.2",
"commander": "8.3.0",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"inquirer": "8.2.5",
"lodash": "4.17.21",
"ora": "5.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/package.json
Expand Up @@ -43,7 +43,7 @@
"@strapi/utils": "4.15.5",
"date-fns": "2.30.0",
"debug": "4.3.4",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"knex": "3.0.1",
"lodash": "4.17.21",
"semver": "7.5.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/strapi/package.json
Expand Up @@ -133,8 +133,8 @@
"delegates": "1.0.0",
"dotenv": "14.2.0",
"execa": "5.1.1",
"fs-extra": "10.0.0",
"glob": "7.2.3",
"fs-extra": "10.1.0",
"glob": "10.3.10",
"http-errors": "1.8.1",
"inquirer": "8.2.5",
"is-docker": "2.2.1",
Expand Down
15 changes: 0 additions & 15 deletions packages/core/strapi/src/load/glob.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/strapi/src/load/load-files.ts
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import fse from 'fs-extra';

import { importDefault } from '@strapi/utils';
import glob from './glob';
import { glob } from 'glob';
import filePathToPath from './filepath-to-prop-path';

/**
Expand Down
11 changes: 2 additions & 9 deletions packages/core/types/src/types/core/attributes/json.ts
@@ -1,3 +1,4 @@
import type * as Utils from '../../utils';
import type { Attribute } from '..';

export type JSON = Attribute.OfType<'json'> &
Expand All @@ -9,14 +10,6 @@ export type JSON = Attribute.OfType<'json'> &
Attribute.VisibleOption &
Attribute.DefaultOption<JsonValue>;

type JSONValue = string | number | boolean | null | JSONObject | JSONArray;

type JSONArray = Array<JSONValue>;

export interface JSONObject {
[key: string]: JSONValue;
}

export type JsonValue<T extends JSONValue = JSONValue> = T;
export type JsonValue<T extends Utils.JSONValue = Utils.JSONValue> = T;

export type GetJsonValue<T extends Attribute.Attribute> = T extends JSON ? JsonValue : never;
2 changes: 2 additions & 0 deletions packages/core/types/src/types/utils/index.ts
Expand Up @@ -6,6 +6,8 @@ export * as Function from './function';
export * as Tuple from './tuple';
export * as Expression from './expression';

export * from './json';

/**
* Get the type of a specific key `TKey` in `TValue`
*
Expand Down
7 changes: 7 additions & 0 deletions packages/core/types/src/types/utils/json.ts
@@ -0,0 +1,7 @@
export type JSONValue = string | number | boolean | null | JSONObject | JSONArray;

export type JSONArray = Array<JSONValue>;

export interface JSONObject {
[key: string]: JSONValue;
}
2 changes: 1 addition & 1 deletion packages/core/upload/package.json
Expand Up @@ -53,7 +53,7 @@
"cropperjs": "1.6.0",
"date-fns": "2.30.0",
"formik": "2.4.0",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"immer": "9.0.19",
"koa-range": "0.3.0",
"koa-static": "5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/app/package.json
Expand Up @@ -48,7 +48,7 @@
"@sentry/node": "6.19.7",
"chalk": "^4.1.2",
"execa": "5.1.1",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"inquirer": "8.2.5",
"lodash": "4.17.21",
"node-machine-id": "^1.1.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/generators/package.json
Expand Up @@ -51,7 +51,7 @@
"@strapi/utils": "4.15.5",
"chalk": "4.1.2",
"copyfiles": "2.4.1",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"node-plop": "0.26.3",
"plop": "2.7.6",
"pluralize": "8.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/documentation/package.json
Expand Up @@ -53,7 +53,7 @@
"bcryptjs": "2.4.3",
"cheerio": "^1.0.0-rc.12",
"formik": "2.4.0",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"immer": "9.0.19",
"koa-static": "^5.0.0",
"lodash": "4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/upload-local/package.json
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@strapi/utils": "4.15.5",
"fs-extra": "10.0.0"
"fs-extra": "10.1.0"
},
"devDependencies": {
"@strapi/pack-up": "4.15.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/typescript/package.json
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"chalk": "4.1.2",
"cli-table3": "0.6.2",
"fs-extra": "10.0.0",
"fs-extra": "10.1.0",
"lodash": "4.17.21",
"prettier": "2.8.4",
"typescript": "5.3.2"
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/upgrade/.eslintignore
@@ -0,0 +1,6 @@
bin
coverage
dist
# Prevent linting error (import/no-extraneous-dependencies)
# "'@strapi/pack-up' should be listed in the project's dependencies, not devDependencies"
packup.config.ts
7 changes: 7 additions & 0 deletions packages/utils/upgrade/.eslintrc.js
@@ -0,0 +1,7 @@
module.exports = {
root: true,
extends: ['custom/back/typescript'],
parserOptions: {
project: ['./tsconfig.eslint.json'],
},
};
22 changes: 22 additions & 0 deletions packages/utils/upgrade/LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2015-present Strapi Solutions SAS

Portions of the Strapi software are licensed as follows:

- All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".

- All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.

MIT Expat License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
157 changes: 157 additions & 0 deletions packages/utils/upgrade/README.md
@@ -0,0 +1,157 @@
# Strapi Upgrade Tool

## Description

The Strapi Upgrade Tool is the CLI for facilitating upgrades between Strapi versions.

The exact details of what level of upgrade will be run (patch, minor, major) based on the current version and what cli options will be available are still in discussion and this document will be updated once they are finalized.

From now on, all breaking changes, and ideally also deprecations, must have an accompanying code transform written to be accepted.

### Types of Transforms

The upgrade tool provides two types of transforms:

- `json`: for updating a project's .json files, primarily intended for the `package.json`
- `code`: codemods; for updating a project's .js and .ts files

### What's a codemod?

Codemods are a scripted way to refactor code. Here we are providing and running these scripts for users for any changes necessary in user code between Strapi versions.

For example, if we need to rename a package used by Strapi projects, instead of instructing users to change the import, we provide a script that searches through the user's project and does the replacement for them.

### Data Migrations

Data migrations are not handled by the upgrade tool.

For Strapi v4, no data migrations will be allowed and no support is planned (except in extenuating circumstances eg, a critical security issue somehow relating to the database shape)

For Strapi v5, automated data migrations can be added in the `packages/core/database` package of the `v5/main` branch of this repo.

## Usage

This package is not yet released, so currently it can be run on a project in the monorepo /examples directory with the following command:

`../../packages/utils/upgrade/bin/upgrade`

Run the command with the `--help` option to see all the available options.

[Coming Soon] The Strapi Upgrade tool will be available using `npx @strapi/upgrade` and an alias for that within a project using `strapi upgrade`

## Writing a code transforms

To begin your code transform script, create a file `upgrade/resources/transforms/{X.X.X}/{short-description-of-action}.{code|json}.ts` where `X.X.X` is the target version of Strapi the codemod will be run for.

For example, all breaking changes for the initial release of Strapi v5 will go in upgrade/resources/transforms/5.0.0

Note that "short-description-of-action" will be converted to text displayed to the user with hyphens converted to spaces, for example: "short description of action"

### 'json' transforms

Your transform will be called for every json file in a user's project, and you must return the json object (modified or not) at the end to be passed to the next transform.

Here is an example JSON Transform script:

```typescript
import path from 'node:path';

import type { JSONTransform } from '../../..';

const transform: JSONTransform = (file, params) => {
// Extract the json api and the cwd so we can target specific files
const { cwd, json } = params;

// To target only a root level package.json file:
const rootPackageJsonPath = path.join(cwd, 'package.json');
if (file.path !== rootPackageJsonPath) {
// Return the json object unmodified to pass it to the next transform
return file.json;
}

// Use json() to get useful helpers for performing your transform
const j = json(file.json);

const strapiDepAddress = 'dependencies.@strapi/strapi';

// if this file contains a value at dependencies.@strapi/strapi
if (j.has(strapiDepAddress)) {
// we set the value to 5.0.0
j.set(strapiDepAddress, '5.0.0');
}

// at the end we must return the modified json object
return j.root();
};

export default transform;
```

For reference, these are the types for the relevant objects, which can be found in `packages/utils/upgrade/src/core/runner/json.ts`:

```typescript
export interface JSONTransformParams {
cwd: string;
json: (object: Utils.JSONObject) => JSONTransformAPI;
}

export interface JSONTransformAPI {
get<T extends Utils.JSONValue>(path?: string, defaultValue?: T): T | undefined;
has(path: string): boolean;
set(path: string, value: Utils.JSONValue): this;
merge(other: Utils.JSONObject): this;
root(): Utils.JSONObject;
}

export type JSONTransform = (file: JSONSourceFile, params: JSONTransformParams) => Utils.JSONObject;
```

The methods available from `json()` are wrappers for the lodash methods of the same name:

- **get(path, default)**: get path or default if not found
- **set(path, value)**: set path (such as 'engines.node', 'dependencies', 'author.name') to value
- **has(path)**: checks if path exists
- **merge(obj)**: merges two json objects
- **root()**: returns the whole json object

### 'code' codemod transforms

Codemod transforms use the [`jscodeshift`](https://github.com/facebook/jscodeshift) library to modify code passed in. Please see their documentation for advanced details.

The `file` and `api` parameters come directly from the [jsoncodeshift arguments of the same name](https://github.com/facebook/jscodeshift#arguments).

```typescript
import type { Transform } from 'jscodeshift';

const transform: Transform = (file, api) => {
// Extract the jscodeshift API
const { j } = api;
// Parse the file content
const root = j(file.source);

root
// Find console.log calls expressions
.find(j.CallExpression, {
callee: { object: { name: 'console' }, property: { name: 'log' } },
})
// For each call expression
.forEach((path) => {
const { callee } = path.node;

if (
// Make sure the callee is a member expression (object/property)
j.MemberExpression.check(callee) &&
// Make sure the property is an actual identifier (contains a name property)
j.Identifier.check(callee.property)
) {
// Update the property's identifier name
callee.property.name = 'info';
}
});

// Return the updated file content
return root.toSource();
};

export default transform;
```
2 changes: 2 additions & 0 deletions packages/utils/upgrade/bin/upgrade.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../dist/cli.js');
5 changes: 5 additions & 0 deletions packages/utils/upgrade/jest.config.js
@@ -0,0 +1,5 @@
module.exports = {
preset: '../../../jest-preset.unit.js',
displayName: 'Upgrade',
collectCoverageFrom: ['src/**/*.ts'],
};

0 comments on commit fdc903e

Please sign in to comment.