Skip to content

Commit

Permalink
refactor: Change config file names and locations
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypkhantc committed Apr 23, 2024
1 parent 5515547 commit 43c6f7b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
60 changes: 47 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from "zod";
import { join } from "path";
import { readJson } from "./utils";
import { pathExists } from "fs-extra";

const configSchema = z.strictObject({
const sourceConfigSchema = z.strictObject({
repositories: z
.array(
z.strictObject({
Expand All @@ -13,7 +14,7 @@ const configSchema = z.strictObject({
.min(1),
});

const pluginsConfigSchema = z.strictObject({
const templateConfigSchema = z.strictObject({
plugins: z
.array(
z.union([
Expand All @@ -28,20 +29,53 @@ const pluginsConfigSchema = z.strictObject({
.min(1),
});

export const CONFIG_FILE_NAME = "template-sync.json";
export async function loadConfig(root: string): Promise<Config> {
const rawConfig = await readJson(join(root, CONFIG_FILE_NAME));
const SOURCE_CONFIG_FILE_NAME = "template-sync.json";
const POSSIBLE_SOURCE_CONFIG_FILE_NAMES = [
SOURCE_CONFIG_FILE_NAME,
".github/" + SOURCE_CONFIG_FILE_NAME,
];
export async function loadSourceConfig(root: string): Promise<SourceConfig> {
const rawConfig = await loadConfigFromPossible(
root,
POSSIBLE_SOURCE_CONFIG_FILE_NAMES,
);

return configSchema.parse(rawConfig);
return sourceConfigSchema.parse(rawConfig);
}

export const PLUGINS_CONFIG_FILE_NAME = "template-sync.plugins.json";
export async function loadPluginsConfig(root: string): Promise<PluginsConfig> {
const rawConfig = await readJson(join(root, PLUGINS_CONFIG_FILE_NAME));
const TEMPLATE_CONFIG_FILE_NAME = "template-sync.template.json";
export const POSSIBLE_TEMPLATE_CONFIG_FILE_NAMES = [
TEMPLATE_CONFIG_FILE_NAME,
".github/" + TEMPLATE_CONFIG_FILE_NAME,
];
export async function loadTemplateConfig(
root: string,
): Promise<TemplateConfig> {
const rawConfig = await loadConfigFromPossible(
root,
POSSIBLE_TEMPLATE_CONFIG_FILE_NAMES,
);

return pluginsConfigSchema.parse(rawConfig);
return templateConfigSchema.parse(rawConfig);
}

export type Config = z.infer<typeof configSchema>;
export type PluginsConfig = z.infer<typeof pluginsConfigSchema>;
export type PluginConfig = z.infer<typeof pluginsConfigSchema>["plugins"][0];
async function loadConfigFromPossible(
root: string,
possibleFileNames: string[],
): Promise<any> {
for (const fileName of possibleFileNames) {
const path = join(root, fileName);

if (!(await pathExists(path))) {
continue;
}

return await readJson(path);
}

throw new Error("Could not find config");
}

export type SourceConfig = z.infer<typeof sourceConfigSchema>;
export type TemplateConfig = z.infer<typeof templateConfigSchema>;
export type PluginConfig = z.infer<typeof templateConfigSchema>["plugins"][0];
14 changes: 7 additions & 7 deletions src/sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
loadConfig,
loadPluginsConfig,
PLUGINS_CONFIG_FILE_NAME,
loadSourceConfig,
loadTemplateConfig,
POSSIBLE_TEMPLATE_CONFIG_FILE_NAMES,
} from "./config";
import { loadPlugin } from "./plugins/load-plugin";
import { RepositoryCloner } from "./repositories/cloning";
Expand All @@ -15,19 +15,19 @@ export async function sync(
repositoryCloner: RepositoryCloner;
},
): Promise<void> {
const sourceConfig = await loadConfig(sourceRoot);
const sourceConfig = await loadSourceConfig(sourceRoot);
const source = new Repository(sourceRoot);
let reserved: string[] = [PLUGINS_CONFIG_FILE_NAME];
let reserved: string[] = [...POSSIBLE_TEMPLATE_CONFIG_FILE_NAMES];

for (const repositoryConfig of sourceConfig.repositories) {
const template = await repositoryCloner.clone(
repositoryConfig.url,
repositoryConfig.branch,
);

const pluginsConfig = await loadPluginsConfig(template.root);
const templateConfig = await loadTemplateConfig(template.root);

for (const pluginConfig of pluginsConfig.plugins) {
for (const pluginConfig of templateConfig.plugins) {
const plugin = await loadPlugin(pluginConfig, template.root);

const { reserved: pluginReserved } = await plugin(
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/case1/expected/custom-plugin.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["template-sync.plugins.json","composer.json"]
["template-sync.template.json",".github/template-sync.template.json","composer.json"]

0 comments on commit 43c6f7b

Please sign in to comment.