Skip to content

Commit

Permalink
Merge e65424a into 1aaf21a
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Dec 31, 2018
2 parents 1aaf21a + e65424a commit f2507c8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CWD = process.cwd();
interface MigrateHelper {
run(name: string, ...args): Promise<void>;
has(name: string): boolean;
prompt(options: any): Promise<any>;
}

export default class Hub extends BabelHub {
Expand All @@ -24,7 +25,7 @@ export default class Hub extends BabelHub {
constructor(
public filename: string,
public code: string,
public options = {}
public options: { templateFile?: string } = {}
) {
super(filename, options);
this.code = code;
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import Hub from "./hub";
import visitor from "./visitor";

export interface Options {
templateFile: string;
onContext?: (hub: Hub) => void;
}

export default async function migrateFile(
filename: string,
options: Options = {}
options: Options = { templateFile: "" }
) {
const source = await fs.readFile(filename, "utf-8");
const ast = parse(source, { parser: babelParser });
const hub = new Hub(filename, source);
const hub = new Hub(filename, source, options);
const nodePath = hub.createNodePath(ast);
if (options.onContext) {
options.onContext(hub);
Expand Down
38 changes: 37 additions & 1 deletion src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import getWidgetConfig from "./getWidgetConfig";
import getInitialBody from "./getInitialBody";
import template from "./template";
import Hub from "../hub";
import getNewComponentPath from "../util/get-new-component-path";

const METHOD_LOOKUP = {
template,
Expand All @@ -35,7 +36,10 @@ const METHOD_LOOKUP = {
onDestroy
};

export default function(path: NodePath<ObjectExpression>): void {
export default function(
path: NodePath<ObjectExpression>,
type: "defineComponent" | "defineWidget"
): void {
const { node } = path;
const hub = path.hub as Hub;
hub.lifecycleMethods = {
Expand Down Expand Up @@ -94,6 +98,38 @@ export default function(path: NodePath<ObjectExpression>): void {
}
});
});

hub.addMigration({
async apply(helper) {
const shouldRename = await helper.prompt({
type: "confirm",
message:
"Would you like to rename the component file?\n" +
"Note: Marko 4 automatically discovers these files based on the naming convention, you may be able to remove them from a browser.json file after this.",
initial: true
});

if (!shouldRename) {
return;
}

const componentFile = hub.filename;
const templateFile = hub.options.templateFile;
const newFile = getNewComponentPath(componentFile, templateFile, type);

await helper.run("updateFilePath", {
from: componentFile,
to: newFile
});

if (type !== "defineComponent") {
await helper.run("updateDependentPaths", {
from: componentFile,
to: newFile
});
}
}
});
}

function createLifecycleMethod(name, params = []) {
Expand Down
20 changes: 20 additions & 0 deletions src/util/get-new-component-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from "path";

const EXT_REG = /\.[^.]+$/;
const FILE_NAME_MAP = {
defineComponent: "component",
defineWidget: "component-browser"
};

export default function(componentFile, templateFile, type) {
let base = templateFile.replace(EXT_REG, "");
let name = FILE_NAME_MAP[type];

if (base === "index") {
base = "";
} else {
name = `.${name}`;
}

return base + name + path.extname(componentFile);
}
14 changes: 8 additions & 6 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export default {
const hub = path.hub as Hub;
const { markoWidgetsIdentifier } = hub;

if (
!t.isMemberExpression(callee) ||
!t.isIdentifier(callee.property) ||
callee.property.name !== "defineComponent"
) {
if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) {
return;
}

const type = callee.property.name;

if (type !== "defineComponent" && type !== "defineWidget") {
return;
}

Expand All @@ -49,7 +51,7 @@ export default {
return;
}

transformMethods(argPath);
transformMethods(argPath, type);
path.replaceWith(argPath);
}
},
Expand Down
4 changes: 3 additions & 1 deletion test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe("CODEMOD", () => {
const entryFile = path.join(entryDir, "input.js");
it(entry, async () => {
try {
const code = await transform(entryFile);
const code = await transform(entryFile, {
templateFile: "./index.marko"
});
snapshot(entryDir, "output.js", code);
} catch (err) {
if (err.snapshot) {
Expand Down

0 comments on commit f2507c8

Please sign in to comment.