packages/cli: move plugin diff modules to separate diff lib

This commit is contained in:
Patrik Oldsberg
2020-05-21 15:54:02 +02:00
parent 2d2710a7c4
commit 18139238a4
6 changed files with 32 additions and 13 deletions
@@ -15,13 +15,14 @@
*/
import { Command } from 'commander';
import { readTemplateFiles } from './read';
import { handlers, handleAllFiles } from './handlers';
import {
readTemplateFiles,
handlers,
handleAllFiles,
inquirerPromptFunc,
makeCheckPromptFunc,
yesPromptFunc,
} from './prompts';
} from '../../lib/diff';
const fileHandlers = [
{
@@ -18,7 +18,7 @@ import fs from 'fs-extra';
import chalk from 'chalk';
import { dirname } from 'path';
import { diffLines } from 'diff';
import { paths } from '../../../lib/paths';
import { paths } from '../paths';
import { TemplateFile, PromptFunc, FileHandler } from './types';
export async function writeTargetFile(targetPath: string, contents: string) {
@@ -221,8 +221,8 @@ export async function handleAllFiles(
) {
for (const file of files) {
const { targetPath } = file;
const fileHandler = fileHandlers.find((handler) =>
handler.patterns.some((pattern) =>
const fileHandler = fileHandlers.find(handler =>
handler.patterns.some(pattern =>
typeof pattern === 'string'
? pattern === targetPath
: pattern.test(targetPath),
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './handlers';
export * from './prompts';
export * from './read';
export * from './types';
@@ -18,8 +18,8 @@ import fs from 'fs-extra';
import { relative as relativePath } from 'path';
import handlebars from 'handlebars';
import recursiveReadDir from 'recursive-readdir';
import { paths } from '../../../lib/paths';
import { version } from '../../../lib/version';
import { paths } from '../paths';
import { version } from '../version';
import { PluginInfo, TemplateFile } from './types';
// Reads info from the existing plugin
@@ -64,11 +64,9 @@ export async function readTemplate(
templateDir: string,
templateVars: any,
): Promise<TemplateFile[]> {
const templateFilePaths = await recursiveReadDir(templateDir).catch(
(error) => {
throw new Error(`Failed to read template directory: ${error.message}`);
},
);
const templateFilePaths = await recursiveReadDir(templateDir).catch(error => {
throw new Error(`Failed to read template directory: ${error.message}`);
});
const templateFiles = new Array<TemplateFile>();
for (const templateFile of templateFilePaths) {