packages/cli: separate handlers for different template files in diff

This commit is contained in:
Patrik Oldsberg
2020-05-03 12:04:17 +02:00
parent c348444fa6
commit 323f6b715a
3 changed files with 87 additions and 5 deletions
+2
View File
@@ -29,6 +29,7 @@
"backstage-cli": "bin/backstage-cli"
},
"devDependencies": {
"@types/diff": "^4.0.2",
"@types/fs-extra": "^8.1.0",
"@types/html-webpack-plugin": "^3.2.2",
"@types/inquirer": "^6.5.0",
@@ -58,6 +59,7 @@
"chokidar": "^3.3.1",
"commander": "^4.1.1",
"dashify": "^2.0.0",
"diff": "^4.0.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-monorepo": "^0.2.1",
"fork-ts-checker-webpack-plugin": "^4.0.5",
+79 -4
View File
@@ -16,10 +16,12 @@
import fs from 'fs-extra';
import { relative as relativePath } from 'path';
import handlebars from 'handlebars';
import { diffLines } from 'diff';
import handlebars, { Template } from 'handlebars';
import recursiveReadDir from 'recursive-readdir';
import { paths } from 'lib/paths';
import { version } from 'lib/version';
import chalk from 'chalk';
type PluginInfo = {
id: string;
@@ -121,14 +123,87 @@ async function readTemplate(
return templateFiles;
}
async function packageJsonHandler(file: TemplateFile) {
if (!file.targetExists) {
throw new Error(`${file.targetPath} doesn't exist`);
}
console.log(`pkg.json handler: ${file.targetPath}`);
const pkg = JSON.parse(file.templateContents);
console.log('DEBUG: pkg =', pkg);
const targetPkg = JSON.parse(file.targetContents);
console.log('DEBUG: targetPkg =', targetPkg);
}
async function diffHandler(file: TemplateFile) {
if (!file.targetExists) {
// TODO: prompt to write template file
return;
}
if (file.targetContents === file.templateContents) {
return;
}
const diffs = diffLines(file.targetContents, file.templateContents);
for (const diff of diffs) {
if (diff.added) {
process.stdout.write(chalk.green(`+${diff.value}`));
} else if (diff.removed) {
process.stdout.write(chalk.red(`-${diff.value}`));
} else {
process.stdout.write(` ${diff.value}`);
}
}
}
async function skipHandler(file: TemplateFile) {
console.log(`Skipping ${file.targetPath}`);
}
type FileHandler = {
patterns: Array<string | RegExp>;
handler: (file: TemplateFile) => Promise<void>;
};
const fileHandlers: FileHandler[] = [
{
patterns: ['package.json'],
handler: packageJsonHandler,
},
{
patterns: ['.eslintrc.js', 'tsconfig.json'],
handler: diffHandler,
},
{
patterns: ['README.md', /^^src\//],
handler: skipHandler,
},
];
export default async () => {
const pluginInfo = await readPluginInfo();
const templateVars = { version, ...pluginInfo };
console.log('DEBUG: templateVars =', templateVars);
const templateDir = paths.resolveOwn('templates/default-plugin');
console.log('DEBUG: templateDir =', templateDir);
const templateFiles = await readTemplate(templateDir, templateVars);
console.log('DEBUG: templateFiles =', templateFiles);
for (const templateFile of templateFiles) {
const { targetPath } = templateFile;
const fileHandler = fileHandlers.find(handler =>
handler.patterns.some(pattern =>
typeof pattern === 'string'
? pattern === targetPath
: pattern.test(targetPath),
),
);
if (fileHandler) {
await fileHandler.handler(templateFile);
} else {
throw new Error(`No template file handler found for ${targetPath}`);
}
}
console.log(`DEBUG: done!`);
};
+6 -1
View File
@@ -3915,6 +3915,11 @@
resolved "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
"@types/diff@^4.0.2":
version "4.0.2"
resolved "https://registry.npmjs.org/@types/diff/-/diff-4.0.2.tgz#2e9bb89f9acc3ab0108f0f3dc4dbdcf2fff8a99c"
integrity sha512-mIenTfsIe586/yzsyfql69KRnA75S8SVXQbTLpDejRrjH0QSJcpu3AUOi/Vjnt9IOsXKxPhJfGpQUNMueIU1fQ==
"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.npmjs.org/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@@ -8114,7 +8119,7 @@ diff-sequences@^25.1.0:
resolved "https://registry.npmjs.org/diff-sequences/-/diff-sequences-25.1.0.tgz#fd29a46f1c913fd66c22645dc75bffbe43051f32"
integrity sha512-nFIfVk5B/NStCsJ+zaPO4vYuLjlzQ6uFvPxzYyHlejNZ/UGa7G/n7peOXVrVNvRuyfstt+mZQYGpjxg9Z6N8Kw==
diff@^4.0.1:
diff@^4.0.1, diff@^4.0.2:
version "4.0.2"
resolved "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==