@hanseltime/template-repo-sync 1.0.1 → 1.2.0

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.
@@ -1,5 +1,5 @@
1
1
  import { join } from "path";
2
- import { existsSync, readFileSync } from "fs";
2
+ import { existsSync, readFileSync, writeFileSync } from "fs";
3
3
  import { getAllFilesInDir } from "./match";
4
4
  import { Config, LocalConfig } from "./types";
5
5
  import { mergeFile } from "./merge-file";
@@ -7,6 +7,10 @@ import { gitClone } from "./clone-drivers/git-clone";
7
7
  import { Change } from "diff";
8
8
  import { TemplateCloneDriverFn } from "./clone-drivers";
9
9
  import { TemplateDiffDriverFn, gitDiff } from "./diff-drivers";
10
+ import { gitCurrentRef } from "./ref-drivers";
11
+ import { TemplateRefDriverFn } from "./ref-drivers/types";
12
+ import { inferJSONIndent } from "./formatting";
13
+ import * as commentJSON from "comment-json";
10
14
 
11
15
  export interface TemplateSyncOptions {
12
16
  repoUrl: string;
@@ -21,6 +25,12 @@ export interface TemplateSyncOptions {
21
25
  */
22
26
  repoDir: string;
23
27
 
28
+ /**
29
+ * If set to true, template sync will apply the current ref
30
+ * of the template repo to afterRef
31
+ */
32
+ updateAfterRef?: boolean;
33
+
24
34
  /**
25
35
  * Defaults to using git clone
26
36
  */
@@ -30,6 +40,11 @@ export interface TemplateSyncOptions {
30
40
  * Defaults to using git diff
31
41
  */
32
42
  diffDriver?: TemplateDiffDriverFn;
43
+
44
+ /**
45
+ * Defaults to using git current ref
46
+ */
47
+ currentRefDriver?: TemplateRefDriverFn;
33
48
  }
34
49
 
35
50
  export interface TemplateSyncReturn {
@@ -56,21 +71,26 @@ export async function templateSync(
56
71
  ): Promise<TemplateSyncReturn> {
57
72
  const cloneDriver = options.cloneDriver ?? gitClone;
58
73
  const diffDriver = options.diffDriver ?? gitDiff;
74
+ const currentRefDriver = options.currentRefDriver ?? gitCurrentRef;
59
75
  const tempCloneDir = await cloneDriver(options.tmpCloneDir, options.repoUrl);
60
76
 
61
77
  // Get the clone Config
62
78
  const cloneConfigPath = join(tempCloneDir, `${TEMPLATE_SYNC_CONFIG}.json`);
63
79
  const templateSyncConfig: Config = existsSync(cloneConfigPath)
64
- ? JSON.parse(readFileSync(cloneConfigPath).toString())
65
- : {};
80
+ ? (commentJSON.parse(
81
+ readFileSync(cloneConfigPath).toString(),
82
+ ) as unknown as Config)
83
+ : { ignore: [] };
66
84
 
67
85
  const localConfigPath = join(
68
86
  options.repoDir,
69
87
  `${TEMPLATE_SYNC_LOCAL_CONFIG}.json`,
70
88
  );
71
- const localTemplateSyncConfig = existsSync(localConfigPath)
72
- ? JSON.parse(readFileSync(localConfigPath).toString())
73
- : ({} as LocalConfig);
89
+ const localTemplateSyncConfig: LocalConfig = existsSync(localConfigPath)
90
+ ? (commentJSON.parse(
91
+ readFileSync(localConfigPath).toString(),
92
+ ) as unknown as LocalConfig)
93
+ : { ignore: [] };
74
94
 
75
95
  let filesToSync: string[];
76
96
  if (localTemplateSyncConfig.afterRef) {
@@ -106,6 +126,28 @@ export async function templateSync(
106
126
  }),
107
127
  );
108
128
 
129
+ // apply after ref
130
+ if (options.updateAfterRef) {
131
+ const ref = await currentRefDriver({
132
+ rootDir: tempCloneDir,
133
+ });
134
+
135
+ if (existsSync(localConfigPath)) {
136
+ const configStr = readFileSync(localConfigPath).toString();
137
+ const config = commentJSON.parse(configStr) as unknown as LocalConfig;
138
+ config.afterRef = ref;
139
+ writeFileSync(
140
+ localConfigPath,
141
+ commentJSON.stringify(config, null, inferJSONIndent(configStr)),
142
+ );
143
+ } else {
144
+ writeFileSync(
145
+ localConfigPath,
146
+ commentJSON.stringify({ afterRef: ref }, null, 4),
147
+ );
148
+ }
149
+ }
150
+
109
151
  return {
110
152
  localSkipFiles,
111
153
  localFileChanges,
package/src/types.ts CHANGED
@@ -42,7 +42,7 @@ export interface MergeConfig<T> {
42
42
  */
43
43
  plugin?: string;
44
44
  /**
45
- * An array of first match file globs that will
45
+ * An array of first match file globs that will then call the plugin with the appropriate options
46
46
  */
47
47
  rules: { glob: string; options: MergePluginOptions | T }[];
48
48
  }