Skip to content

Commit

Permalink
feat(migrate): Expose dependent path migration
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename result properties, default path migrations
  • Loading branch information
DylanPiercey committed Dec 31, 2018
1 parent 622145b commit cac93f1
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 49 deletions.
10 changes: 5 additions & 5 deletions packages/migrate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ migrate({
// The programtic api does not come with this by default and can be overwritten.
return prompt(options);
}
}).then(({ updated, moved }) => {
}).then(({ fileContents, fileNames }) => {
// Output contains an object with all of the migrated component sources.
console.log("migrated all files");

for (const file in updated) {
for (const file in fileContents) {
// Save all updated files to disk.
fs.writeFileSync(file, output[file], "utf-8");
fs.writeFileSync(file, fileContents[file], "utf-8");
}

for (const file in moved) {
for (const file in fileNames) {
// Update locations of moved files on disk.
fs.renameSync(file, moved[file]);
fs.renameSync(file, fileNames[file]);
}
});
```
Expand Down
18 changes: 11 additions & 7 deletions packages/migrate/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,22 @@ export async function run(options, markoCli) {
...options
};

const { updated, moved } = await markoMigrate(options);
const { fileContents, fileNames, dependentPaths } = await markoMigrate(
options
);

await Promise.all(
Object.entries(updated).map(([file, source]) => {
Object.entries(fileContents).map(([file, source]) => {
return fs.writeFile(file, source, "utf-8");
})
);

for (const from in moved) {
const to = moved[from];
await Promise.all(
Object.entries(fileNames).map(([from, to]) => fs.rename(from, to))
);

for (const from in dependentPaths) {
const to = dependentPaths[from];
await Promise.all(
Object.entries(
await dependentPathUpdate({
Expand All @@ -99,9 +105,7 @@ export async function run(options, markoCli) {
from,
to
})
)
.map(([file, source]) => fs.writeFile(file, source, "utf-8"))
.concat(fs.rename(from, to))
).map(([file, source]) => fs.writeFile(file, source, "utf-8"))
);
}
}
33 changes: 15 additions & 18 deletions packages/migrate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ const defaultGlobOptions = {
ignore: ["node_modules/**"]
};

export default async function(options = {}) {
let { dir, ignore, files: filePatterns, prompt } = options;

if (!filePatterns || !filePatterns.length) {
filePatterns = ["**/*.marko"];
}

if (typeof prompt !== "function") {
export default async function(options) {
if (!options || typeof options.prompt !== "function") {
throw new Error("The 'prompt' option is required.");
}

dir = dir || process.cwd();

const {
dir = process.cwd(),
files: filePatterns = ["**/*.marko"],
ignore
} = options;
const packageRoot = getPackageRoot(dir);
const markoCompiler = requireFromRoot("marko/compiler", packageRoot);

Expand All @@ -51,8 +48,11 @@ export default async function(options = {}) {
}

const files = await getFiles(filePatterns, globOptions);
const updatedFiles = {};
const movedFiles = {};
const results = {
dependentPaths: {},
fileContents: {},
fileNames: {}
};

await Promise.all(
files.map(async file => {
Expand All @@ -65,15 +65,15 @@ export default async function(options = {}) {
const ast = markoCompiler.parse(source, file, {
onContext(ctx) {
ctx.addMigration = add;
addDefaultMigrations(ctx, updatedFiles, movedFiles);
addDefaultMigrations(ctx, results);
},
migrate: true,
raw: true
});

await runAutoMigrations(migrateHelper);

updatedFiles[file] = markoPrettyprint.prettyPrintAST(ast, {
results.fileContents[file] = markoPrettyprint.prettyPrintAST(ast, {
syntax: options.syntax,
maxLen: options.maxLen,
noSemi: options.noSemi,
Expand All @@ -84,10 +84,7 @@ export default async function(options = {}) {
})
);

return {
moved: movedFiles,
updated: updatedFiles
};
return results;
}

function getPackageRoot(dir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import migrateWidget from "@marko/migrate-v3-widget";

export default function addComponentMigration(ctx, updatedFiles) {
export default function addComponentMigration(ctx, { fileContents }) {
ctx.addMigration({
name: "componentFile",
description: "Migrating widget file with defineComponent",
async apply(helper, { filename }) {
updatedFiles[filename] = await migrateWidget(filename, {
async apply(_, { componentFile, templateFile }) {
fileContents[componentFile] = await migrateWidget(componentFile, {
templateFile,
onContext(hub) {
hub.addMigration = ctx.addMigration;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/migrate/src/util/default-migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import renameFile from "./rename-file";
import updatePaths from "./update-paths";
import componentFile from "./component-file";
export default function addFileMigrations(ctx, migratedFiles, renamedFiles) {
renameFile(ctx, migratedFiles, renamedFiles);
componentFile(ctx, migratedFiles);
export default function addFileMigrations(ctx, result) {
updatePaths(ctx, result);
componentFile(ctx, result);
}
8 changes: 0 additions & 8 deletions packages/migrate/src/util/default-migrations/rename-file.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/migrate/src/util/default-migrations/update-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default function addComponentMigration(
ctx,
{ fileNames, dependentPaths }
) {
ctx.addMigration({
name: "updateFilePath",
apply(_, { from, to }) {
fileNames[from] = to;
}
});

ctx.addMigration({
name: "updateDependentPaths",
apply(_, { from, to }) {
dependentPaths[from] = to;
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
module.exports = (root, context) => {
context.addMigration({
apply(helper) {
return helper.run("renameFile", {
return helper.run("updateFilePath", {
from: context.filename,
to: path.join(context.dirname, "index.marko")
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"migrator": "./migrator"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

module.exports = (root, context) => {
const widgetFile = path.join(context.dirname, "index.js");
const newWidgetFile = path.join(context.dirname, "component.js");
context.addMigration({
apply(helper) {
return helper.run("updateDependentPaths", {
from: widgetFile,
to: newWidgetFile
});
}
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- packages/migrate/test/fixtures/single-widget-rename-and-dependent-paths/template.marko -->

<div/>


<!-- dependents: packages/migrate/test/fixtures/single-widget-rename-and-dependent-paths/index.js => packages/migrate/test/fixtures/single-widget-rename-and-dependent-paths/component.js -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div/>
19 changes: 16 additions & 3 deletions packages/migrate/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,34 @@ const CWD = process.cwd();
describe("scope(migrate)", () => {
autotest("fixtures", async ({ dir, test, snapshot }) => {
test(async () => {
const { updated, moved } = await migrate({
const { fileContents, fileNames, dependentPaths } = await migrate({
prompt() {},
ignore: ["**/snapshot-*.*"],
files: [`${dir}/**/*.marko`]
});

snapshot(
Object.entries(updated)
Object.entries(fileContents)
.sort(([a], [b]) => a.localeCompare(b))
.map(
([file, source]) =>
`<!-- ${path.relative(CWD, file)}${
moved[file] ? ` => ${path.relative(CWD, moved[file])}` : ""
fileNames[file]
? ` => ${path.relative(CWD, fileNames[file])}`
: ""
} -->\n\n${source}`
)
.concat(
Object.entries(dependentPaths)
.sort(([a], [b]) => a.localeCompare(b))
.map(
([from, to]) =>
`<!-- dependents: ${path.relative(
CWD,
from
)} => ${path.relative(CWD, to)} -->`
)
)
.join("\n\n"),
{
ext: ".marko",
Expand Down

0 comments on commit cac93f1

Please sign in to comment.