chore: create script for sqlite migration

This commit is contained in:
Christian Capeans 2024-01-04 18:36:41 +01:00
parent cb6f5d7891
commit 30eebd0bcc
4 changed files with 79 additions and 1 deletions

View File

@ -0,0 +1,43 @@
import type { StringLiteral, Transform } from 'jscodeshift';
const transform: Transform = (file, api) => {
const { j } = api;
// Parse the file content
const root = j(file.source);
if (!file.path.includes('config/database.js')) {
return file.source;
}
const targetProperties = new Set([
'sqlite3',
'vscode/sqlite3',
'sqlite-legacy',
'better-sqlite3',
]);
return root
.find(j.ObjectExpression)
.forEach((path) => {
j(path)
.find(j.Property)
.forEach((propertyPath) => {
// Check if the property name is one of the targets
const key = propertyPath.node.key;
const propertyName = key.type === 'Identifier' ? key.name : (key as StringLiteral).value;
if (targetProperties.has(propertyName)) {
// Rename the property to 'sqlite'
if (key.type === 'Identifier') {
key.name = 'sqlite';
} else {
// For Literal types
(key as StringLiteral).value = 'sqlite';
}
}
});
})
.toSource({ quote: 'single' });
};
export default transform;

View File

@ -0,0 +1,29 @@
import path from 'node:path';
import type { modules } from '../../../dist';
const transform: modules.runner.json.JSONTransform = (file, params) => {
const { cwd, json } = params;
const rootPackageJsonPath = path.join(cwd, 'package.json');
if (file.path !== rootPackageJsonPath) {
return file.json;
}
const j = json(file.json);
const targetProperties = ['sqlite3', 'vscode/sqlite3', 'sqlite-legacy'];
targetProperties.forEach((targetProperty) => {
const oldSqliteDependency = `dependencies.${targetProperty}`;
if (j.has(oldSqliteDependency)) {
j.remove(oldSqliteDependency);
j.set('dependencies.better-sqlite3', '9.0.0');
}
});
return j.root();
};
export default transform;

View File

@ -1,4 +1,4 @@
import { cloneDeep, get, has, set, merge } from 'lodash/fp';
import { cloneDeep, get, has, set, merge, omit } from 'lodash/fp';
import type { Utils } from '@strapi/types';
@ -31,6 +31,11 @@ export class JSONTransformAPI implements JSONTransformAPIInterface {
return this;
}
remove(path: string) {
this.json = omit(path, this.json);
return this;
}
root(): Utils.JSONObject {
return cloneDeep(this.json);
}

View File

@ -8,4 +8,5 @@ export interface JSONTransformAPI {
set(path: string, value: Utils.JSONValue): this;
merge(other: Utils.JSONObject): this;
root(): Utils.JSONObject;
remove(path: string): this;
}