@hanseltime/template-repo-sync 2.0.4 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/merge-file.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { isMatch, some } from "micromatch";
2
- import { Config, MergeContext, MergePlugin } from "./types";
2
+ import { Config, FileOperation, MergeContext, MergePlugin } from "./types";
3
3
  import { extname, join } from "path";
4
4
  import { existsSync } from "fs";
5
- import { readFile } from "fs/promises";
5
+ import { readFile, rm } from "fs/promises";
6
6
  import { loadPlugin } from "./load-plugin";
7
7
  import { Change, diffLines } from "diff";
8
8
  import { outputFile } from "fs-extra";
@@ -12,6 +12,7 @@ interface MergeFileOptions {
12
12
  templateSyncConfig: Config;
13
13
  tempCloneDir: string;
14
14
  cwd: string;
15
+ fileOperation: FileOperation;
15
16
  }
16
17
 
17
18
  interface MergeFileReturn {
@@ -51,6 +52,17 @@ export async function mergeFile(
51
52
  const filePath = join(cwd, relPath);
52
53
  const templatePath = join(tempCloneDir, relPath);
53
54
 
55
+ // Unless there's a need, we remove files that were deleted and don't pass them to plugins yet
56
+ if (context.fileOperation === "deleted") {
57
+ if (existsSync(filePath)) {
58
+ await rm(filePath);
59
+ }
60
+ return {
61
+ ignoredDueToLocal: false,
62
+ localChanges: [],
63
+ };
64
+ }
65
+
54
66
  const mergeConfig = templateSyncConfig.merge?.find((mergeConfig) =>
55
67
  isMatch(relPath, mergeConfig.glob),
56
68
  );
@@ -209,9 +209,11 @@ describe("templateSync", () => {
209
209
  );
210
210
 
211
211
  // We will only update the templated.ts
212
- const mockDiffDriver = jest
213
- .fn()
214
- .mockImplementation(async () => ["src/templated.ts"]);
212
+ const mockDiffDriver = jest.fn().mockImplementation(async () => ({
213
+ added: ["src/templated.ts"],
214
+ modified: [],
215
+ deleted: [],
216
+ }));
215
217
  const result = await templateSync({
216
218
  tmpCloneDir: "stubbed-by-driver",
217
219
  cloneDriver: dummyCloneDriver,
@@ -252,9 +254,11 @@ describe("templateSync", () => {
252
254
  );
253
255
 
254
256
  // We will only update the templated.ts
255
- const mockDiffDriver = jest
256
- .fn()
257
- .mockImplementation(async () => ["src/templated.ts"]);
257
+ const mockDiffDriver = jest.fn().mockImplementation(async () => ({
258
+ added: ["src/templated.ts"],
259
+ modified: [],
260
+ deleted: [],
261
+ }));
258
262
  const mockCurrentRefDriver = jest
259
263
  .fn()
260
264
  .mockImplementation(async () => "newestSha");
@@ -297,9 +301,9 @@ describe("templateSync", () => {
297
301
  await rm(join(tmpDir, "templatesync.local.json"));
298
302
 
299
303
  // We will only update the templated.ts
300
- const mockDiffDriver = jest
301
- .fn()
302
- .mockImplementation(async () => ["src/templated.ts"]);
304
+ const mockDiffDriver = jest.fn().mockImplementation(async () => ({
305
+ added: ["src/templated.ts"],
306
+ }));
303
307
  const mockCurrentRefDriver = jest
304
308
  .fn()
305
309
  .mockImplementation(async () => "newestSha");
@@ -1,12 +1,12 @@
1
1
  import { join } from "path";
2
2
  import { existsSync, readFileSync, writeFileSync } from "fs";
3
3
  import { getAllFilesInDir } from "./match";
4
- import { Config, LocalConfig } from "./types";
4
+ import { Config, FileOperation, LocalConfig } from "./types";
5
5
  import { mergeFile } from "./merge-file";
6
6
  import { gitClone } from "./clone-drivers/git-clone";
7
7
  import { Change } from "diff";
8
8
  import { TemplateCloneDriverFn } from "./clone-drivers";
9
- import { TemplateDiffDriverFn, gitDiff } from "./diff-drivers";
9
+ import { DiffResult, TemplateDiffDriverFn, gitDiff } from "./diff-drivers";
10
10
  import { gitCurrentRef } from "./ref-drivers";
11
11
  import { TemplateRefDriverFn } from "./ref-drivers/types";
12
12
  import { inferJSONIndent } from "./formatting";
@@ -124,17 +124,21 @@ export async function templateSync(
124
124
  ) as unknown as LocalConfig)
125
125
  : { ignore: [] };
126
126
 
127
- let filesToSync: string[];
127
+ let filesToSync: DiffResult;
128
128
  if (localTemplateSyncConfig.afterRef) {
129
129
  filesToSync = await diffDriver(
130
130
  tempCloneDir,
131
131
  localTemplateSyncConfig.afterRef,
132
132
  );
133
133
  } else {
134
- filesToSync = getAllFilesInDir(tempCloneDir, [
135
- ...templateSyncConfig.ignore,
136
- ".git/**",
137
- ]);
134
+ filesToSync = {
135
+ added: getAllFilesInDir(tempCloneDir, [
136
+ ...templateSyncConfig.ignore,
137
+ ".git/**",
138
+ ]),
139
+ deleted: [],
140
+ modified: [],
141
+ };
138
142
  }
139
143
 
140
144
  const localSkipFiles: string[] = [];
@@ -142,21 +146,27 @@ export async function templateSync(
142
146
  [filePath: string]: Change[];
143
147
  } = {};
144
148
 
145
- await Promise.all(
146
- filesToSync.map(async (f) => {
149
+ const fileSyncFactory = (op: FileOperation) => {
150
+ return async (f: string) => {
147
151
  const result = await mergeFile(f, {
148
152
  localTemplateSyncConfig,
149
153
  templateSyncConfig,
150
154
  tempCloneDir,
151
155
  cwd: options.repoDir,
156
+ fileOperation: op,
152
157
  });
153
158
  if (result.ignoredDueToLocal) {
154
159
  localSkipFiles.push(f);
155
160
  } else if (result?.localChanges && result.localChanges.length > 0) {
156
161
  localFileChanges[f] = result.localChanges;
157
162
  }
158
- }),
159
- );
163
+ };
164
+ };
165
+
166
+ // Added and modified have the same setup for now
167
+ await Promise.all(filesToSync.added.map(fileSyncFactory("added")));
168
+ await Promise.all(filesToSync.modified.map(fileSyncFactory("modified")));
169
+ await Promise.all(filesToSync.deleted.map(fileSyncFactory("deleted")));
160
170
 
161
171
  // apply after ref
162
172
  if (options.updateAfterRef) {
package/src/types.ts CHANGED
@@ -125,3 +125,5 @@ export interface MergePlugin<PluginOptions> {
125
125
  */
126
126
  validate(options: unknown): string[] | undefined;
127
127
  }
128
+
129
+ export type FileOperation = "added" | "modified" | "deleted";