Skip to content

Commit

Permalink
addded feature to add options in config.json file and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bippan1407 committed Feb 7, 2024
1 parent e04e6c2 commit 2758c3d
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 85 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

migration.config.json

.vscode
30 changes: 30 additions & 0 deletions src/configOptionsService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let configInitialised = false;
let configOptions = {
projectLocation: null,
transformedFolderLocation: null,
dryRun: true,
emptyTransformFolder: false,
saveErrorLogs: false,
commentAxios: true,
commentOtherCode: true,
};

function configOptionsService() {
return {
get() {
return configOptions;
},
initialise(options) {
if (configInitialised) {
return;
}
configInitialised = true;
configOptions = {
...configOptions,
...options,
};
},
};
}

module.exports = configOptionsService;
7 changes: 7 additions & 0 deletions src/constants/emitSyntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const newEmitSyntaxMapping = {
input: "update:modelValue",
};

module.exports = {
newEmitSyntaxMapping,
};
7 changes: 7 additions & 0 deletions src/constants/propSyntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const newPropSyntaxMapping = {
value: "modelValue",
};

module.exports = {
newPropSyntaxMapping,
};
57 changes: 25 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,24 @@ const {
} = require("./utility");
const path = require("path");
const chalk = require("chalk");
const configOptionsService = require("./configOptionsService");

const { argv } = yargs;

const projectLocation = argv["projectLocation"];
const transformedFolderLocation = argv["transformFolder"];
let dryRun = argv["dryRun"];
let emptyTransformFolder = argv["emptyTransformFolder"];
let saveErrorLogs = argv["saveErrorLogs"];

if (dryRun === "false") {
dryRun = false;
} else {
dryRun = true;
}

if (emptyTransformFolder === "true") {
emptyTransformFolder = true;
let configOptions = {};
const configLocation = argv["configLocation"];
if (configLocation) {
configOptionsService().initialise(require(configLocation));
configOptions = configOptionsService().get();
} else {
emptyTransformFolder = false;
}

if (saveErrorLogs === "true") {
saveErrorLogs = true;
} else {
saveErrorLogs = false;
configOptions.projectLocation = argv["projectLocation"];
configOptions.transformedFolderLocation = argv["transformFolder"];
configOptions.dryRun = Boolean(argv["configOptions.dryRun"]);
configOptions.emptyTransformFolder = Boolean(
argv["configOptions.emptyTransformFolder"]
);
configOptions.saveErrorLogs = Boolean(argv["configOptions.saveErrorLogs"]);
configOptionsService().initialise(configOptions);
configOptions = configOptionsService().get();
}

const filesToMigrateManually = [];
Expand All @@ -51,8 +44,8 @@ const main = (file) => {
let codemodTransform = new Codemod();

codemodTransform.initialiseFile(file, {
transformedFolderLocation,
shouldTransformMainFile: !dryRun,
transformedFolderLocation: configOptions.transformedFolderLocation,
shouldTransformMainFile: !configOptions.dryRun,
});
console.log(chalk.greenBright("completed processing file - ", fileName));
filesMigrated++;
Expand All @@ -68,7 +61,7 @@ const main = (file) => {
// console.log(chalk.redBright(error.message));
// console.log(chalk.redBright(error.stack));

if (saveErrorLogs) {
if (configOptions.saveErrorLogs) {
filesToMigrateManually.push({
fileName: file,
errorMessage: error.message,
Expand All @@ -81,25 +74,25 @@ const main = (file) => {
};

function startTransformation(baseFolderLocation) {
if (!projectLocation) {
if (!configOptions.projectLocation) {
console.log(
chalk.redBright("Please provide absolute path to project location")
);
return;
}
console.log(chalk.greenBright("Process initiated\n"));
if (transformedFolderLocation) {
const folder = fs.readdirSync(transformedFolderLocation);
if (configOptions.transformedFolderLocation) {
const folder = fs.readdirSync(configOptions.transformedFolderLocation);
const subdirectoriesAndFiles = folder.filter((file) => {
const fullPath = `${transformedFolderLocation}/${file}`;
const fullPath = `${configOptions.transformedFolderLocation}/${file}`;
return (
fs.statSync(fullPath).isDirectory() || fs.statSync(fullPath).isFile()
);
});
if (subdirectoriesAndFiles.length !== 0) {
if (emptyTransformFolder) {
if (configOptions.emptyTransformFolder) {
console.log(chalk.green("Deleting all files in transform folder"));
deleteFilesInFolderSync(transformedFolderLocation);
deleteFilesInFolderSync(configOptions.transformedFolderLocation);
console.log(
chalk.green("All files deleted successfully in transform folder")
);
Expand Down Expand Up @@ -137,4 +130,4 @@ Files to migrate manually - ${filesToMigrateManually.length}`)
}
}

startTransformation(projectLocation);
startTransformation(configOptions.projectLocation);
36 changes: 8 additions & 28 deletions src/transformations/convertEmit/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
const { newEmitSyntaxMapping } = require("../../constants/emitSyntax");

const transform = ({ root, j, vueFileData }) => {
let emitNames = vueFileData.emitNames;
let newEmitSyntax = "const emit = ";
// root
// .find(j.Property, {
// key: { name: "emits" },
// })
// .forEach((path) => {
// const args = path.value.value;
// args.elements.forEach((elem) => {
// const emitName = elem.value;
// if (emitName) {
// emitNames.push(emitName);
// }
// });
// // j(path).replaceWith(newSyntax);
// });
// // In case emit is not defined as property
// root
// .find(j.MemberExpression, {
// object: { type: "ThisExpression" },
// property: { name: "$emit" },
// })
// ?.forEach((path) => {
// const args = path.parent.value.arguments;
// const emitName = args?.[0]?.value;
// if (emitName) {
// emitNames.push(emitName);
// }
// });

emitNames = emitNames.map((name) => {
if (newEmitSyntaxMapping[name]) {
return newEmitSyntaxMapping[name];
}
return name;
});
if (emitNames.length) {
newEmitSyntax += j(
j.callExpression(j.identifier("defineEmits"), [
Expand Down
2 changes: 1 addition & 1 deletion src/transformations/convertEmit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ test("convert emit to defineEmits", () => {
codemod.initialiseFile(testFile);
const newSyntax = transformations.convertEmit(codemod.transformationObject);
expect(newSyntax).toBe(
`const emit = defineEmits(["close", "input", "on-change", "on-new-change"])`
`const emit = defineEmits(["close", "update:modelValue", "on-change", "on-new-change"])`
);
});
1 change: 1 addition & 0 deletions src/transformations/convertEmit/test.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default {
}
this.$emit('input', option.value);
this.$emit('on-change', option, test, value);
this.$emit('on-change', option, test, value);
this.$emit('on-new-change', option, test, value, { name: 'jerry' });
</script>
18 changes: 18 additions & 0 deletions src/transformations/convertProps/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { newPropSyntaxMapping } = require("../../constants/propSyntax");

const transform = ({ root, j }) => {
let newPropSyntax = "const props = ";
root
Expand All @@ -6,6 +8,22 @@ const transform = ({ root, j }) => {
})
.forEach((path) => {
const args = path.value.value;
if (args.type === j.ObjectExpression.name) {
args.properties = args.properties.map((arg) => {
if (arg?.key?.name && newPropSyntaxMapping[arg.key.name]) {
arg.key.name = newPropSyntaxMapping[arg.key.name];
}
return arg;
});
} else if (args.type === j.ArrayExpression.name) {
args.elements = args.elements.map((arg) => {
if (arg?.value && newPropSyntaxMapping[arg.value]) {
arg.value = newPropSyntaxMapping[arg.value];
}
return arg;
});
}

newPropSyntax += j(
j.callExpression(j.identifier("defineProps"), [args])
).toSource();
Expand Down
4 changes: 3 additions & 1 deletion src/transformations/convertProps/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ test("convert all props of vue which are in array", () => {
const testFile = path.resolve(__dirname, "test2.vue");
codemod.initialiseFile(testFile);
const newSyntax = transformations.convertProps(codemod.transformationObject);
expect(newSyntax).toEqual(`const props = defineProps(['name', 'age'])`);
expect(newSyntax).toEqual(
`const props = defineProps(['name', 'age', "modelValue"])`
);
});
2 changes: 1 addition & 1 deletion src/transformations/convertProps/test2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
</template>
<script>
export default {
props: ['name', 'age']
props: ['name', 'age', 'value']
}
</script>
43 changes: 32 additions & 11 deletions src/transformations/convertThisExpression/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const convertThisStoreGetters = require("../convertThisStoreGetters");
const { getRefSyntax } = require("../../utility/vueThreeSyntax");
const convertThisStoreDispatch = require("../convertThisStoreDispatch");
const componentRefToRefs = require("../componentRefsToRef");
const convertNuxtProperties = require("../convertNuxtProperties");
const { nuxtPropertiesToConvert } = require("../../utility");
const { newEmitSyntaxMapping } = require("../../constants/emitSyntax");
const configOptionsService = require("../../configOptionsService");
const { newPropSyntaxMapping } = require("../../constants/propSyntax");

const transform = ({ root, j, vueFileData }) => {
let configOptions = configOptionsService().get();
const propNames = vueFileData.propNames;
const data = vueFileData.data;
const computedNames = vueFileData.computedNames;
Expand All @@ -16,12 +18,18 @@ const transform = ({ root, j, vueFileData }) => {
vuexStateGetters = Array.from(vuexStateGetters);
convertNuxtProperties({ root, j });
root.find(j.ThisExpression).forEach((path) => {
const propertyName = path.parent?.value?.property?.name;
let propertyName = path.parent?.value?.property?.name;
const args = path.parent.parent.value.args;
if (!propertyName) {
return;
}
if (propNames.includes(propertyName)) {
j(path.parent).replaceWith(`props.${propertyName}`);
const newPropNameForVue3 = newPropSyntaxMapping[propertyName];
if (newPropNameForVue3) {
j(path.parent).replaceWith(`props.${newPropNameForVue3}`);
} else {
j(path.parent).replaceWith(`props.${propertyName}`);
}
} else if (
data.includes(propertyName) ||
computedNames.includes(propertyName) ||
Expand All @@ -35,16 +43,29 @@ const transform = ({ root, j, vueFileData }) => {
j(path.parent).replaceWith(`${propertyName}`);
} else if (["$emit"].includes(propertyName)) {
j(path.parent).replaceWith(`emit`);
if (Array.isArray(args)) {
const [expression] = args;
if (
expression &&
expression.type === j.Literal.name &&
expression.value &&
newEmitSyntaxMapping[expression.value]
) {
console.log(newEmitSyntaxMapping[expression.value]);
expression.value = newEmitSyntaxMapping[expression.value];
}
}
} else if (["$refs"].includes(propertyName)) {
componentRefToRefs({ root, j });
} else if (["$axios"].includes(propertyName)) {
j(path.parent.parent).forEach((path) => {
path.value.comments = [
j.commentLine("TODO Need to migrate manually", false, true),
];
// return j(path).toSource();
});
} else {
if (configOptions.commentAxios) {
j(path.parent.parent).forEach((path) => {
path.value.comments = [
j.commentLine("TODO Need to migrate manually", false, true),
];
});
}
} else if (configOptions.commentOtherCode) {
j(path.parent.parent).forEach((path) => {
path.value.comments = [
j.commentLine("TODO Need to migrate manually", false, true),
Expand Down
8 changes: 4 additions & 4 deletions src/vueProperties/getEmitNames/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const transform = ({ root, j }) => {
let emitNames = [];
let emitNames = new Set();
root
.find(j.Property, {
key: { name: "emits" },
Expand All @@ -9,7 +9,7 @@ const transform = ({ root, j }) => {
args.elements.forEach((elem) => {
const emitName = elem.value;
if (emitName) {
emitNames.push(emitName);
emitNames.add(emitName);
}
});
});
Expand All @@ -22,10 +22,10 @@ const transform = ({ root, j }) => {
const args = path.parent.value.arguments;
const emitName = args?.[0]?.value;
if (emitName) {
emitNames.push(emitName);
emitNames.add(emitName);
}
});
return emitNames;
return Array.from(emitNames);
};

module.exports = transform;
8 changes: 4 additions & 4 deletions src/vueProperties/getPropNames/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const transform = ({ root, j }) => {
let propNames = [];
let propNames = new Set();
root
.find(j.Property, {
key: { name: "props" },
})
.forEach((path) => {
path.value?.value?.properties?.forEach((property) => {
propNames.push(property.key.name);
propNames.add(property.key.name);
});
});
// for array syntax
Expand All @@ -16,10 +16,10 @@ const transform = ({ root, j }) => {
})
.forEach((path) => {
path.value?.value?.elements?.forEach((element) => {
propNames.push(element.value);
propNames.add(element.value);
});
});
return propNames;
return Array.from(propNames);
};

module.exports = transform;
Loading

0 comments on commit 2758c3d

Please sign in to comment.