@shopify/cli-kit 3.33.0 → 3.35.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.
Files changed (212) hide show
  1. package/dist/git.js +3 -3
  2. package/dist/git.js.map +1 -1
  3. package/dist/http/fetch.js +1 -1
  4. package/dist/http/fetch.js.map +1 -1
  5. package/dist/http.js +2 -2
  6. package/dist/http.js.map +1 -1
  7. package/dist/index.d.ts +0 -7
  8. package/dist/index.js +0 -7
  9. package/dist/index.js.map +1 -1
  10. package/dist/metadata.js +1 -1
  11. package/dist/metadata.js.map +1 -1
  12. package/dist/npm.js +3 -3
  13. package/dist/npm.js.map +1 -1
  14. package/dist/output.js +4 -4
  15. package/dist/output.js.map +1 -1
  16. package/dist/path.d.ts +1 -1
  17. package/dist/path.js +5 -3
  18. package/dist/path.js.map +1 -1
  19. package/dist/private/node/analytics.js +6 -9
  20. package/dist/private/node/analytics.js.map +1 -1
  21. package/dist/private/node/api/graphql.js +2 -1
  22. package/dist/private/node/api/graphql.js.map +1 -1
  23. package/dist/private/node/api/headers.d.ts +2 -2
  24. package/dist/private/node/api/headers.js +4 -4
  25. package/dist/private/node/api/headers.js.map +1 -1
  26. package/dist/private/node/api/rest.d.ts +6 -4
  27. package/dist/private/node/api/rest.js +8 -7
  28. package/dist/private/node/api/rest.js.map +1 -1
  29. package/dist/private/node/constants.d.ts +42 -0
  30. package/dist/private/node/constants.js +58 -0
  31. package/dist/private/node/constants.js.map +1 -0
  32. package/dist/private/node/environment/service.d.ts +18 -0
  33. package/dist/private/node/environment/service.js +32 -0
  34. package/dist/private/node/environment/service.js.map +1 -0
  35. package/dist/{environment → private/node/environment}/spin-cache.d.ts +0 -0
  36. package/dist/{environment → private/node/environment}/spin-cache.js +0 -0
  37. package/dist/private/node/environment/spin-cache.js.map +1 -0
  38. package/dist/{environment → private/node/environment}/utilities.d.ts +0 -0
  39. package/dist/{environment → private/node/environment}/utilities.js +0 -0
  40. package/dist/private/node/environment/utilities.js.map +1 -0
  41. package/dist/{session → private/node/session}/authorize.d.ts +1 -5
  42. package/dist/{session → private/node/session}/authorize.js +9 -9
  43. package/dist/private/node/session/authorize.js.map +1 -0
  44. package/dist/{session → private/node/session}/device-authorization.d.ts +0 -0
  45. package/dist/{session → private/node/session}/device-authorization.js +7 -9
  46. package/dist/private/node/session/device-authorization.js.map +1 -0
  47. package/dist/{session → private/node/session}/exchange.d.ts +2 -2
  48. package/dist/{session → private/node/session}/exchange.js +5 -5
  49. package/dist/private/node/session/exchange.js.map +1 -0
  50. package/dist/{session → private/node/session}/identity-token-validation.d.ts +0 -0
  51. package/dist/{session → private/node/session}/identity-token-validation.js +4 -4
  52. package/dist/private/node/session/identity-token-validation.js.map +1 -0
  53. package/dist/{session → private/node/session}/identity.d.ts +1 -1
  54. package/dist/{session → private/node/session}/identity.js +2 -3
  55. package/dist/private/node/session/identity.js.map +1 -0
  56. package/dist/{session → private/node/session}/post-auth.d.ts +1 -1
  57. package/dist/{session → private/node/session}/post-auth.js +10 -10
  58. package/dist/private/node/session/post-auth.js.map +1 -0
  59. package/dist/{session → private/node/session}/redirect-listener.d.ts +0 -0
  60. package/dist/{session → private/node/session}/redirect-listener.js +2 -2
  61. package/dist/private/node/session/redirect-listener.js.map +1 -0
  62. package/dist/{session → private/node/session}/schema.d.ts +1 -1
  63. package/dist/{session → private/node/session}/schema.js +1 -1
  64. package/dist/private/node/session/schema.js.map +1 -0
  65. package/dist/{session → private/node/session}/scopes.d.ts +1 -1
  66. package/dist/{session → private/node/session}/scopes.js +2 -2
  67. package/dist/private/node/session/scopes.js.map +1 -0
  68. package/dist/{session → private/node/session}/store.d.ts +0 -0
  69. package/dist/{session → private/node/session}/store.js +10 -10
  70. package/dist/private/node/session/store.js.map +1 -0
  71. package/dist/{session → private/node/session}/validate.d.ts +0 -0
  72. package/dist/{session → private/node/session}/validate.js +4 -4
  73. package/dist/private/node/session/validate.js.map +1 -0
  74. package/dist/private/node/session.d.ts +56 -0
  75. package/dist/{session.js → private/node/session.js} +98 -134
  76. package/dist/private/node/session.js.map +1 -0
  77. package/dist/private/node/ui/components/SelectPrompt.d.ts +2 -1
  78. package/dist/private/node/ui/components/SelectPrompt.js +2 -1
  79. package/dist/private/node/ui/components/SelectPrompt.js.map +1 -1
  80. package/dist/private/node/ui/components/Table/Column.d.ts +5 -0
  81. package/dist/private/node/ui/components/Table/Column.js +2 -0
  82. package/dist/private/node/ui/components/Table/Column.js.map +1 -0
  83. package/dist/private/node/ui/components/Table/Row.d.ts +12 -0
  84. package/dist/private/node/ui/components/Table/Row.js +24 -0
  85. package/dist/private/node/ui/components/Table/Row.js.map +1 -0
  86. package/dist/private/node/ui/components/Table/ScalarDict.d.ts +5 -0
  87. package/dist/private/node/ui/components/Table/ScalarDict.js +2 -0
  88. package/dist/private/node/ui/components/Table/ScalarDict.js.map +1 -0
  89. package/dist/private/node/ui/components/Table/Table.d.ts +12 -0
  90. package/dist/private/node/ui/components/Table/Table.js +30 -0
  91. package/dist/private/node/ui/components/Table/Table.js.map +1 -0
  92. package/dist/private/node/ui/components/Table/Table.test.d.ts +1 -0
  93. package/dist/private/node/ui/components/Table/Table.test.js +41 -0
  94. package/dist/private/node/ui/components/Table/Table.test.js.map +1 -0
  95. package/dist/private/node/ui.js +1 -1
  96. package/dist/private/node/ui.js.map +1 -1
  97. package/dist/public/common/string.d.ts +11 -0
  98. package/dist/public/common/string.js +21 -0
  99. package/dist/public/common/string.js.map +1 -1
  100. package/dist/public/common/version.d.ts +1 -0
  101. package/dist/public/common/version.js +2 -0
  102. package/dist/public/common/version.js.map +1 -0
  103. package/dist/public/node/analytics.js +5 -5
  104. package/dist/public/node/analytics.js.map +1 -1
  105. package/dist/public/node/api/admin.d.ts +5 -2
  106. package/dist/public/node/api/admin.js +4 -3
  107. package/dist/public/node/api/admin.js.map +1 -1
  108. package/dist/public/node/api/partners.js +1 -1
  109. package/dist/public/node/api/partners.js.map +1 -1
  110. package/dist/public/node/base-command.js +2 -2
  111. package/dist/public/node/base-command.js.map +1 -1
  112. package/dist/public/node/cli.d.ts +8 -0
  113. package/dist/public/node/cli.js +25 -8
  114. package/dist/public/node/cli.js.map +1 -1
  115. package/dist/public/node/dot-env.js +2 -2
  116. package/dist/public/node/dot-env.js.map +1 -1
  117. package/dist/public/node/environment/fqdn.d.ts +26 -0
  118. package/dist/{environment → public/node/environment}/fqdn.js +15 -31
  119. package/dist/public/node/environment/fqdn.js.map +1 -0
  120. package/dist/{environment → public/node/environment}/local.d.ts +62 -15
  121. package/dist/{environment → public/node/environment}/local.js +81 -36
  122. package/dist/public/node/environment/local.js.map +1 -0
  123. package/dist/public/node/environment/spin.d.ts +43 -0
  124. package/dist/{environment → public/node/environment}/spin.js +31 -38
  125. package/dist/public/node/environment/spin.js.map +1 -0
  126. package/dist/public/node/error-handler.js +6 -5
  127. package/dist/public/node/error-handler.js.map +1 -1
  128. package/dist/public/node/fs.d.ts +222 -3
  129. package/dist/public/node/fs.js +338 -2
  130. package/dist/public/node/fs.js.map +1 -1
  131. package/dist/public/node/git.d.ts +90 -0
  132. package/dist/public/node/git.js +174 -0
  133. package/dist/public/node/git.js.map +1 -0
  134. package/dist/public/node/liquid.js +6 -6
  135. package/dist/public/node/liquid.js.map +1 -1
  136. package/dist/public/node/node-package-manager.d.ts +0 -10
  137. package/dist/public/node/node-package-manager.js +3 -20
  138. package/dist/public/node/node-package-manager.js.map +1 -1
  139. package/dist/public/node/presets.js +2 -2
  140. package/dist/public/node/presets.js.map +1 -1
  141. package/dist/public/node/ruby.d.ts +1 -1
  142. package/dist/public/node/ruby.js +19 -20
  143. package/dist/public/node/ruby.js.map +1 -1
  144. package/dist/public/node/session.d.ts +53 -0
  145. package/dist/public/node/session.js +95 -0
  146. package/dist/public/node/session.js.map +1 -0
  147. package/dist/public/node/system.d.ts +44 -0
  148. package/dist/{system.js → public/node/system.js} +34 -54
  149. package/dist/public/node/system.js.map +1 -0
  150. package/dist/public/node/tcp.js +1 -1
  151. package/dist/public/node/tcp.js.map +1 -1
  152. package/dist/public/node/ui.d.ts +13 -0
  153. package/dist/public/node/ui.js +14 -0
  154. package/dist/public/node/ui.js.map +1 -1
  155. package/dist/public/node/vscode.js +5 -5
  156. package/dist/public/node/vscode.js.map +1 -1
  157. package/dist/secure-store.js +4 -4
  158. package/dist/secure-store.js.map +1 -1
  159. package/dist/store.d.ts +10 -10
  160. package/dist/store.js +21 -22
  161. package/dist/store.js.map +1 -1
  162. package/dist/testing/store.js +3 -3
  163. package/dist/testing/store.js.map +1 -1
  164. package/dist/testing/ui.js +1 -1
  165. package/dist/testing/ui.js.map +1 -1
  166. package/dist/tsconfig.tsbuildinfo +1 -1
  167. package/dist/ui.js +4 -4
  168. package/dist/ui.js.map +1 -1
  169. package/package.json +10 -7
  170. package/dist/cli.d.ts +0 -8
  171. package/dist/cli.js +0 -18
  172. package/dist/cli.js.map +0 -1
  173. package/dist/constants.d.ts +0 -51
  174. package/dist/constants.js +0 -70
  175. package/dist/constants.js.map +0 -1
  176. package/dist/environment/fqdn.d.ts +0 -29
  177. package/dist/environment/fqdn.js.map +0 -1
  178. package/dist/environment/local.js.map +0 -1
  179. package/dist/environment/service.d.ts +0 -4
  180. package/dist/environment/service.js +0 -19
  181. package/dist/environment/service.js.map +0 -1
  182. package/dist/environment/spin-cache.js.map +0 -1
  183. package/dist/environment/spin.d.ts +0 -50
  184. package/dist/environment/spin.js.map +0 -1
  185. package/dist/environment/utilities.js.map +0 -1
  186. package/dist/environment.d.ts +0 -7
  187. package/dist/environment.js +0 -8
  188. package/dist/environment.js.map +0 -1
  189. package/dist/file.d.ts +0 -98
  190. package/dist/file.js +0 -216
  191. package/dist/file.js.map +0 -1
  192. package/dist/network/service.d.ts +0 -14
  193. package/dist/network/service.js +0 -11
  194. package/dist/network/service.js.map +0 -1
  195. package/dist/session/authorize.js.map +0 -1
  196. package/dist/session/device-authorization.js.map +0 -1
  197. package/dist/session/exchange.js.map +0 -1
  198. package/dist/session/identity-token-validation.js.map +0 -1
  199. package/dist/session/identity.js.map +0 -1
  200. package/dist/session/post-auth.js.map +0 -1
  201. package/dist/session/redirect-listener.js.map +0 -1
  202. package/dist/session/schema.js.map +0 -1
  203. package/dist/session/scopes.js.map +0 -1
  204. package/dist/session/store.js.map +0 -1
  205. package/dist/session/token.d.ts +0 -40
  206. package/dist/session/token.js +0 -22
  207. package/dist/session/token.js.map +0 -1
  208. package/dist/session/validate.js.map +0 -1
  209. package/dist/session.d.ts +0 -96
  210. package/dist/session.js.map +0 -1
  211. package/dist/system.d.ts +0 -51
  212. package/dist/system.js.map +0 -1
@@ -1,10 +1,346 @@
1
- import { exists as fileExists } from '../../file.js';
2
- import { join as joinPath } from '../../path.js';
1
+ import { content as outputContent, token, debug } from '../../output.js';
3
2
  import { getRandomName } from '../common/string.js';
3
+ import { join as joinPath } from '../../path.js';
4
+ import { copy as fsCopy, ensureFile as fsEnsureFile, ensureFileSync as fsEnsureFileSync, remove as fsRemove, removeSync as fsRemoveSync, move as fsMove,
5
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
+ // @ts-ignore
7
+ } from 'fs-extra/esm';
8
+ import { temporaryDirectoryTask } from 'tempy';
9
+ import { sep, join, extname } from 'pathe';
10
+ import { mkdirSync as fsMkdirSync, readFileSync as fsReadFileSync, writeFileSync as fsWriteFileSync, appendFileSync as fsAppendFileSync, statSync as fsStatSync, createReadStream as fsCreateReadStream, constants as fsConstants, } from 'fs';
11
+ import { mkdir as fsMkdir, writeFile as fsWriteFile, readFile as fsReadFile, realpath as fsRealPath, appendFile as fsAppendFile, mkdtemp as fsMkdtemp, stat as fsStat, lstat as fsLstat, chmod as fsChmod, access as fsAccess, } from 'fs/promises';
12
+ const DEFAULT_PRETTIER_CONFIG = {
13
+ arrowParens: 'always',
14
+ singleQuote: true,
15
+ bracketSpacing: false,
16
+ trailingComma: 'all',
17
+ };
18
+ /**
19
+ * Strip the first `strip` parts of the path.
20
+ *
21
+ * @param path - Path to strip.
22
+ * @param strip - Number of parts to strip.
23
+ * @returns The stripped path.
24
+ */
25
+ export function stripUpPath(path, strip) {
26
+ const parts = path.split(sep);
27
+ return join(...parts.slice(strip));
28
+ }
29
+ /**
30
+ * Creates a temporary directory and ties its lifecycle to the lifecycle of the callback.
31
+ *
32
+ * @param callback - The callback that receives the temporary directory.
33
+ */
34
+ export async function inTemporaryDirectory(callback) {
35
+ return temporaryDirectoryTask(callback);
36
+ }
37
+ /**
38
+ * It reads a file and returns its content as a string.
39
+ * Uses utf-8 encoding by default.
40
+ *
41
+ * @param path - Path to the file to read.
42
+ * @param options - Options to read the file with (defaults to utf-8 encoding).
43
+ * @returns A promise that resolves with the content of the file.
44
+ */
45
+ export async function readFile(path, options = { encoding: 'utf8' }) {
46
+ debug(outputContent `Reading the content of file at ${token.path(path)}...`);
47
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
48
+ // @ts-ignore
49
+ return fsReadFile(path, options);
50
+ }
51
+ /**
52
+ * Synchronously reads a file and returns its content as a string.
53
+ *
54
+ * @param path - Path to the file to read.
55
+ * @param options - Options to read the file with (defaults to utf-8 encoding).
56
+ * @returns The content of the file.
57
+ */
58
+ export function readSync(path, options = { encoding: 'utf-8' }) {
59
+ debug(outputContent `Sync-reading the content of file at ${token.path(path)}...`);
60
+ const content = fsReadFileSync(path, options);
61
+ return content.toString();
62
+ }
63
+ /**
64
+ * Given a path, it determines the actual path. This is useful when working
65
+ * with paths that represent symlinks.
66
+ *
67
+ * @param path - Path whose real path will be returned.
68
+ */
69
+ export async function fileRealPath(path) {
70
+ return fsRealPath(path);
71
+ }
72
+ /**
73
+ * Copies a file.
74
+ *
75
+ * @param from - Path to the directory or file to be copied.
76
+ * @param to - Destination path.
77
+ */
78
+ export async function copyFile(from, to) {
79
+ debug(outputContent `Copying file from ${token.path(from)} to ${token.path(to)}...`);
80
+ await fsCopy(from, to);
81
+ }
82
+ /**
83
+ * Creates a file at the given path.
84
+ *
85
+ * @param path - Path to the file to be created.
86
+ */
87
+ export async function touchFile(path) {
88
+ debug(outputContent `Creating an empty file at ${token.path(path)}...`);
89
+ await fsEnsureFile(path);
90
+ }
91
+ /**
92
+ * Synchronously creates a file at the given path.
93
+ *
94
+ * @param path - Path to the file to be created.
95
+ */
96
+ export function touchFileSync(path) {
97
+ debug(outputContent `Creating an empty file at ${token.path(path)}...`);
98
+ fsEnsureFileSync(path);
99
+ }
100
+ /**
101
+ * Appnds content to file at path.
102
+ *
103
+ * @param path - Path to the file to be appended.
104
+ * @param content - Content to be appended.
105
+ */
106
+ export async function appendFile(path, content) {
107
+ debug(outputContent `Appending the following content to ${token.path(path)}:
108
+ ${content
109
+ .split('\n')
110
+ .map((line) => ` ${line}`)
111
+ .join('\n')}
112
+ `);
113
+ await fsAppendFile(path, content);
114
+ }
115
+ /**
116
+ * Synchronously appends content to file at path.
117
+ *
118
+ * @param path - Path to the file to be appended.
119
+ * @param data - Content to be appended.
120
+ */
121
+ export function appendFileSync(path, data) {
122
+ fsAppendFileSync(path, data);
123
+ }
124
+ /**
125
+ * Writes content to file at path.
126
+ *
127
+ * @param path - Path to the file to be written.
128
+ * @param data - Content to be written.
129
+ */
130
+ export async function writeFile(path, data) {
131
+ debug(outputContent `Writing some content to file at ${token.path(path)}...`);
132
+ await fsWriteFile(path, data, { encoding: 'utf8' });
133
+ }
134
+ /**
135
+ * Synchronously writes content to file at path.
136
+ *
137
+ * @param path - Path to the file to be written.
138
+ * @param data - Content to be written.
139
+ */
140
+ export function writeFileSync(path, data) {
141
+ debug(outputContent `File-writing some content to file at ${token.path(path)}...`);
142
+ fsWriteFileSync(path, data);
143
+ }
144
+ /**
145
+ * Creates a directory at the given path.
146
+ *
147
+ * @param path - Path to the directory to be created.
148
+ */
149
+ export async function mkdir(path) {
150
+ debug(outputContent `Creating directory at ${token.path(path)}...`);
151
+ await fsMkdir(path, { recursive: true });
152
+ }
153
+ /**
154
+ * Synchronously creates a directory at the given path.
155
+ *
156
+ * @param path - Path to the directory to be created.
157
+ */
158
+ export function mkdirSync(path) {
159
+ debug(outputContent `Sync-creating directory at ${token.path(path)}...`);
160
+ fsMkdirSync(path, { recursive: true });
161
+ }
162
+ /**
163
+ * Removes a file at the given path.
164
+ *
165
+ * @param path - Path to the file to be removed.
166
+ */
167
+ export async function removeFile(path) {
168
+ debug(outputContent `Removing file at ${token.path(path)}...`);
169
+ await fsRemove(path);
170
+ }
171
+ /**
172
+ * Synchronously removes a file at the given path.
173
+ *
174
+ * @param path - Path to the file to be removed.
175
+ */
176
+ export function removeFileSync(path) {
177
+ debug(outputContent `Sync-removing file at ${token.path(path)}...`);
178
+ fsRemoveSync(path);
179
+ }
180
+ /**
181
+ * Removes a directory at the given path.
182
+ *
183
+ * @param path - Path to the directory to be removed.
184
+ * @param options - Options to remove the directory with.
185
+ */
186
+ export async function rmdir(path, options = {}) {
187
+ const { default: del } = await import('del');
188
+ debug(outputContent `Removing directory at ${token.path(path)}...`);
189
+ await del(path, { force: options.force });
190
+ }
191
+ /**
192
+ * Create a temporary directory.
193
+ *
194
+ * @returns Path to the temporary directory.
195
+ */
196
+ export async function mkTmpDir() {
197
+ debug(outputContent `Creating a temporary directory...`);
198
+ const directory = await fsMkdtemp('tmp-');
199
+ return directory;
200
+ }
201
+ /**
202
+ * Check whether a path is a directory.
203
+ *
204
+ * @param path - Path to check.
205
+ * @returns True if the path is a directory, false otherwise.
206
+ */
207
+ export async function isDirectory(path) {
208
+ debug(outputContent `Checking if ${token.path(path)} is a directory...`);
209
+ return (await fsLstat(path)).isDirectory();
210
+ }
211
+ /**
212
+ * Get the size of a file.
213
+ *
214
+ * @param path - Path to the file.
215
+ * @returns The size of the file in bytes.
216
+ */
217
+ export async function fileSize(path) {
218
+ debug(outputContent `Getting the size of file file at ${token.path(path)}...`);
219
+ return (await fsStat(path)).size;
220
+ }
221
+ /**
222
+ * Synchronously get the size of a file.
223
+ *
224
+ * @param path - Path to the file.
225
+ * @returns The size of the file in bytes.
226
+ */
227
+ export function fileSizeSync(path) {
228
+ debug(outputContent `Sync-getting the size of file file at ${token.path(path)}...`);
229
+ return fsStatSync(path).size;
230
+ }
231
+ /**
232
+ * Create a read stream for a file.
233
+ *
234
+ * @param path - Path to the file.
235
+ * @returns A read stream for the file.
236
+ */
237
+ export function createFileReadStream(path) {
238
+ return fsCreateReadStream(path);
239
+ }
240
+ /**
241
+ * Returns the Date object for the last modification of a file.
242
+ *
243
+ * @param path - Path to the directory or file.
244
+ * @returns A unix timestamp.
245
+ */
246
+ export async function fileLastUpdated(path) {
247
+ debug(outputContent `Getting last updated timestamp for file at ${token.path(path)}...`);
248
+ return (await fsStat(path)).ctime;
249
+ }
250
+ /**
251
+ * Returns the unix timestamp of the last modification of a file.
252
+ *
253
+ * @param path - Path to the directory or file.
254
+ * @returns A unix timestamp.
255
+ */
256
+ export async function fileLastUpdatedTimestamp(path) {
257
+ try {
258
+ const lastUpdatedDateTime = await fileLastUpdated(path);
259
+ return lastUpdatedDateTime.getTime();
260
+ // eslint-disable-next-line no-catch-all/no-catch-all
261
+ }
262
+ catch {
263
+ return undefined;
264
+ }
265
+ }
266
+ /**
267
+ * Moves a file.
268
+ *
269
+ * @param src - File to be moved.
270
+ * @param dest - Path to be moved to.
271
+ * @param options - Moving options.
272
+ */
273
+ export async function moveFile(src, dest, options = {}) {
274
+ await fsMove(src, dest, options);
275
+ }
276
+ /**
277
+ * Changes the permissions of a directory or file.
278
+ *
279
+ * @param path - Path to the file or directory whose permissions will be modified.
280
+ * @param mode - Permissions to set to the file or directory.
281
+ */
282
+ export async function chmod(path, mode) {
283
+ await fsChmod(path, mode);
284
+ }
285
+ /**
286
+ * Checks if a file has executable permissions.
287
+ *
288
+ * @param path - Path to the file whose permissions will be checked.
289
+ */
290
+ export async function fileHasExecutablePermissions(path) {
291
+ try {
292
+ await fsAccess(path, fsConstants.X_OK);
293
+ return true;
294
+ // eslint-disable-next-line no-catch-all/no-catch-all
295
+ }
296
+ catch {
297
+ return false;
298
+ }
299
+ }
300
+ /**
301
+ * Returns true if a file or directory exists.
302
+ *
303
+ * @param path - Path to the directory or file.
304
+ * @returns True if it exists.
305
+ */
306
+ export async function fileExists(path) {
307
+ try {
308
+ await fsAccess(path);
309
+ return true;
310
+ // eslint-disable-next-line no-catch-all/no-catch-all
311
+ }
312
+ catch {
313
+ return false;
314
+ }
315
+ }
316
+ /**
317
+ * Format a string using prettier. Return the formatted content.
318
+ *
319
+ * @param content - Content to be formatted.
320
+ * @param options - Options to format the content with.
321
+ * @returns The formatted content.
322
+ */
323
+ export async function fileContentPrettyFormat(content, options) {
324
+ const { default: prettier } = await import('prettier');
325
+ const ext = extname(options.path);
326
+ const prettierConfig = {
327
+ ...DEFAULT_PRETTIER_CONFIG,
328
+ parser: 'babel',
329
+ };
330
+ switch (ext) {
331
+ case '.html':
332
+ case '.css':
333
+ prettierConfig.parser = ext.slice(1);
334
+ break;
335
+ }
336
+ const formattedContent = await prettier.format(content, prettierConfig);
337
+ return formattedContent;
338
+ }
4
339
  /**
5
340
  * It generates a random directory directory name for a sub-directory.
6
341
  * It ensures that the returned directory name doesn't exist.
7
342
  *
343
+ * @param options - Options to generate the random directory name.
8
344
  * @returns It returns the name of the directory.
9
345
  */
10
346
  export async function generateRandomNameForSubdirectory(options) {
@@ -1 +1 @@
1
- {"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../src/public/node/fs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,IAAI,UAAU,EAAC,MAAM,eAAe,CAAA;AAClD,OAAO,EAAC,IAAI,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAA;AAcjD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iCAAiC,CAAC,OAAuC;IAC7F,MAAM,SAAS,GAAG,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAA;IACpF,MAAM,mBAAmB,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;IAClE,MAAM,mBAAmB,GAAG,MAAM,UAAU,CAAC,mBAAmB,CAAC,CAAA;IAEjE,IAAI,mBAAmB,EAAE;QACvB,OAAO,iCAAiC,CAAC,OAAO,CAAC,CAAA;KAClD;SAAM;QACL,OAAO,SAAS,CAAA;KACjB;AACH,CAAC","sourcesContent":["import {exists as fileExists} from '../../file.js'\nimport {join as joinPath} from '../../path.js'\nimport {getRandomName} from '../common/string.js'\nimport type {RandomNameFamily} from '../common/string.js'\n\ninterface GenerateRandomDirectoryOptions {\n /** Suffix to include in the randomly generated directory name */\n suffix: string\n\n /** Absolute path to the directory where the random directory will be created. */\n directory: string\n\n /** Type of word to use for random name */\n family?: RandomNameFamily\n}\n\n/**\n * It generates a random directory directory name for a sub-directory.\n * It ensures that the returned directory name doesn't exist.\n *\n * @returns It returns the name of the directory.\n */\nexport async function generateRandomNameForSubdirectory(options: GenerateRandomDirectoryOptions): Promise<string> {\n const generated = `${getRandomName(options.family ?? 'business')}-${options.suffix}`\n const randomDirectoryPath = joinPath(options.directory, generated)\n const isAppDirectoryTaken = await fileExists(randomDirectoryPath)\n\n if (isAppDirectoryTaken) {\n return generateRandomNameForSubdirectory(options)\n } else {\n return generated\n }\n}\n"]}
1
+ {"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../src/public/node/fs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,aAAa,EAAE,KAAK,EAAE,KAAK,EAAC,MAAM,iBAAiB,CAAA;AACtE,OAAO,EAAC,aAAa,EAAmB,MAAM,qBAAqB,CAAA;AACnE,OAAO,EAAC,IAAI,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAA;AAC9C,OAAO,EACL,IAAI,IAAI,MAAM,EACd,UAAU,IAAI,YAAY,EAC1B,cAAc,IAAI,gBAAgB,EAClC,MAAM,IAAI,QAAQ,EAClB,UAAU,IAAI,YAAY,EAC1B,IAAI,IAAI,MAAM;AACd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E;QACX,KAAK,OAAO,CAAC;QACb,KAAK,MAAM;YACT,cAAc,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;YACpC,MAAK;KACR;IAED,MAAM,gBAAgB,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;IAEvE,OAAO,gBAAgB,CAAA;AACzB,CAAC;AAaD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,iCAAiC,CAAC,OAAuC;IAC7F,MAAM,SAAS,GAAG,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAA;IACpF,MAAM,mBAAmB,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;IAClE,MAAM,mBAAmB,GAAG,MAAM,UAAU,CAAC,mBAAmB,CAAC,CAAA;IAEjE,IAAI,mBAAmB,EAAE;QACvB,OAAO,iCAAiC,CAAC,OAAO,CAAC,CAAA;KAClD;SAAM;QACL,OAAO,SAAS,CAAA;KACjB;AACH,CAAC","sourcesContent":["import {content as outputContent, token, debug} from '../../output.js'\nimport {getRandomName, RandomNameFamily} from '../common/string.js'\nimport {join as joinPath} from '../../path.js'\nimport {\n copy as fsCopy,\n ensureFile as fsEnsureFile,\n ensureFileSync as fsEnsureFileSync,\n remove as fsRemove,\n removeSync as fsRemoveSync,\n move as fsMove,\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n} from 'fs-extra/esm'\n\nimport {temporaryDirectoryTask} from 'tempy'\nimport {sep, join, extname} from 'pathe'\nimport {\n mkdirSync as fsMkdirSync,\n readFileSync as fsReadFileSync,\n writeFileSync as fsWriteFileSync,\n appendFileSync as fsAppendFileSync,\n statSync as fsStatSync,\n createReadStream as fsCreateReadStream,\n constants as fsConstants,\n ReadStream,\n} from 'fs'\nimport {\n mkdir as fsMkdir,\n writeFile as fsWriteFile,\n readFile as fsReadFile,\n realpath as fsRealPath,\n appendFile as fsAppendFile,\n mkdtemp as fsMkdtemp,\n stat as fsStat,\n lstat as fsLstat,\n chmod as fsChmod,\n access as fsAccess,\n} from 'fs/promises'\nimport type {Options} from 'prettier'\n\nconst DEFAULT_PRETTIER_CONFIG: Options = {\n arrowParens: 'always',\n singleQuote: true,\n bracketSpacing: false,\n trailingComma: 'all',\n}\n\n/**\n * Strip the first `strip` parts of the path.\n *\n * @param path - Path to strip.\n * @param strip - Number of parts to strip.\n * @returns The stripped path.\n */\nexport function stripUpPath(path: string, strip: number): string {\n const parts = path.split(sep)\n return join(...parts.slice(strip))\n}\n\n/**\n * Creates a temporary directory and ties its lifecycle to the lifecycle of the callback.\n *\n * @param callback - The callback that receives the temporary directory.\n */\nexport async function inTemporaryDirectory<T>(callback: (tmpDir: string) => T | Promise<T>): Promise<T> {\n return temporaryDirectoryTask(callback)\n}\n\n/**\n * It reads a file and returns its content as a string using the\n * utf-8 encoding.\n *\n * @param path - Path to the file to read.\n * @returns A promise that resolves with the content of the file.\n */\n\nexport type ReadOptions =\n | undefined\n | {flag?: string | undefined}\n | {\n encoding: BufferEncoding | string\n flag?: string | undefined\n }\nexport async function readFile(path: string, options?: ReadOptions): Promise<string>\nexport async function readFile(path: string, options?: ReadOptions): Promise<Buffer>\n\n/**\n * It reads a file and returns its content as a string.\n * Uses utf-8 encoding by default.\n *\n * @param path - Path to the file to read.\n * @param options - Options to read the file with (defaults to utf-8 encoding).\n * @returns A promise that resolves with the content of the file.\n */\nexport async function readFile(path: string, options: ReadOptions = {encoding: 'utf8'}): Promise<string | Buffer> {\n debug(outputContent`Reading the content of file at ${token.path(path)}...`)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n return fsReadFile(path, options)\n}\n\n/**\n * Synchronously reads a file and returns its content as a string.\n *\n * @param path - Path to the file to read.\n * @param options - Options to read the file with (defaults to utf-8 encoding).\n * @returns The content of the file.\n */\nexport function readSync(path: string, options: object = {encoding: 'utf-8'}): string {\n debug(outputContent`Sync-reading the content of file at ${token.path(path)}...`)\n const content = fsReadFileSync(path, options)\n return content.toString()\n}\n\n/**\n * Given a path, it determines the actual path. This is useful when working\n * with paths that represent symlinks.\n *\n * @param path - Path whose real path will be returned.\n */\nexport async function fileRealPath(path: string): Promise<string> {\n return fsRealPath(path)\n}\n\n/**\n * Copies a file.\n *\n * @param from - Path to the directory or file to be copied.\n * @param to - Destination path.\n */\nexport async function copyFile(from: string, to: string): Promise<void> {\n debug(outputContent`Copying file from ${token.path(from)} to ${token.path(to)}...`)\n await fsCopy(from, to)\n}\n\n/**\n * Creates a file at the given path.\n *\n * @param path - Path to the file to be created.\n */\nexport async function touchFile(path: string): Promise<void> {\n debug(outputContent`Creating an empty file at ${token.path(path)}...`)\n await fsEnsureFile(path)\n}\n\n/**\n * Synchronously creates a file at the given path.\n *\n * @param path - Path to the file to be created.\n */\nexport function touchFileSync(path: string): void {\n debug(outputContent`Creating an empty file at ${token.path(path)}...`)\n fsEnsureFileSync(path)\n}\n\n/**\n * Appnds content to file at path.\n *\n * @param path - Path to the file to be appended.\n * @param content - Content to be appended.\n */\nexport async function appendFile(path: string, content: string): Promise<void> {\n debug(outputContent`Appending the following content to ${token.path(path)}:\n ${content\n .split('\\n')\n .map((line) => ` ${line}`)\n .join('\\n')}\n `)\n await fsAppendFile(path, content)\n}\n\n/**\n * Synchronously appends content to file at path.\n *\n * @param path - Path to the file to be appended.\n * @param data - Content to be appended.\n */\nexport function appendFileSync(path: string, data: string): void {\n fsAppendFileSync(path, data)\n}\n\n/**\n * Writes content to file at path.\n *\n * @param path - Path to the file to be written.\n * @param data - Content to be written.\n */\nexport async function writeFile(path: string, data: string): Promise<void> {\n debug(outputContent`Writing some content to file at ${token.path(path)}...`)\n await fsWriteFile(path, data, {encoding: 'utf8'})\n}\n\n/**\n * Synchronously writes content to file at path.\n *\n * @param path - Path to the file to be written.\n * @param data - Content to be written.\n */\nexport function writeFileSync(path: string, data: string): void {\n debug(outputContent`File-writing some content to file at ${token.path(path)}...`)\n fsWriteFileSync(path, data)\n}\n\n/**\n * Creates a directory at the given path.\n *\n * @param path - Path to the directory to be created.\n */\nexport async function mkdir(path: string): Promise<void> {\n debug(outputContent`Creating directory at ${token.path(path)}...`)\n await fsMkdir(path, {recursive: true})\n}\n\n/**\n * Synchronously creates a directory at the given path.\n *\n * @param path - Path to the directory to be created.\n */\nexport function mkdirSync(path: string): void {\n debug(outputContent`Sync-creating directory at ${token.path(path)}...`)\n fsMkdirSync(path, {recursive: true})\n}\n\n/**\n * Removes a file at the given path.\n *\n * @param path - Path to the file to be removed.\n */\nexport async function removeFile(path: string): Promise<void> {\n debug(outputContent`Removing file at ${token.path(path)}...`)\n await fsRemove(path)\n}\n\n/**\n * Synchronously removes a file at the given path.\n *\n * @param path - Path to the file to be removed.\n */\nexport function removeFileSync(path: string): void {\n debug(outputContent`Sync-removing file at ${token.path(path)}...`)\n fsRemoveSync(path)\n}\n\ninterface RmDirOptions {\n force?: boolean\n}\n/**\n * Removes a directory at the given path.\n *\n * @param path - Path to the directory to be removed.\n * @param options - Options to remove the directory with.\n */\nexport async function rmdir(path: string, options: RmDirOptions = {}): Promise<void> {\n const {default: del} = await import('del')\n debug(outputContent`Removing directory at ${token.path(path)}...`)\n await del(path, {force: options.force})\n}\n\n/**\n * Create a temporary directory.\n *\n * @returns Path to the temporary directory.\n */\nexport async function mkTmpDir(): Promise<string> {\n debug(outputContent`Creating a temporary directory...`)\n const directory = await fsMkdtemp('tmp-')\n return directory\n}\n\n/**\n * Check whether a path is a directory.\n *\n * @param path - Path to check.\n * @returns True if the path is a directory, false otherwise.\n */\nexport async function isDirectory(path: string): Promise<boolean> {\n debug(outputContent`Checking if ${token.path(path)} is a directory...`)\n return (await fsLstat(path)).isDirectory()\n}\n\n/**\n * Get the size of a file.\n *\n * @param path - Path to the file.\n * @returns The size of the file in bytes.\n */\nexport async function fileSize(path: string): Promise<number> {\n debug(outputContent`Getting the size of file file at ${token.path(path)}...`)\n return (await fsStat(path)).size\n}\n\n/**\n * Synchronously get the size of a file.\n *\n * @param path - Path to the file.\n * @returns The size of the file in bytes.\n */\nexport function fileSizeSync(path: string): number {\n debug(outputContent`Sync-getting the size of file file at ${token.path(path)}...`)\n return fsStatSync(path).size\n}\n\n/**\n * Create a read stream for a file.\n *\n * @param path - Path to the file.\n * @returns A read stream for the file.\n */\nexport function createFileReadStream(path: string): ReadStream {\n return fsCreateReadStream(path)\n}\n\n/**\n * Returns the Date object for the last modification of a file.\n *\n * @param path - Path to the directory or file.\n * @returns A unix timestamp.\n */\nexport async function fileLastUpdated(path: string): Promise<Date> {\n debug(outputContent`Getting last updated timestamp for file at ${token.path(path)}...`)\n return (await fsStat(path)).ctime\n}\n\n/**\n * Returns the unix timestamp of the last modification of a file.\n *\n * @param path - Path to the directory or file.\n * @returns A unix timestamp.\n */\nexport async function fileLastUpdatedTimestamp(path: string): Promise<number | undefined> {\n try {\n const lastUpdatedDateTime = await fileLastUpdated(path)\n return lastUpdatedDateTime.getTime()\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch {\n return undefined\n }\n}\n\ninterface MoveFileOptions {\n overwrite?: boolean\n}\n\n/**\n * Moves a file.\n *\n * @param src - File to be moved.\n * @param dest - Path to be moved to.\n * @param options - Moving options.\n */\nexport async function moveFile(src: string, dest: string, options: MoveFileOptions = {}): Promise<void> {\n await fsMove(src, dest, options)\n}\n\n/**\n * Changes the permissions of a directory or file.\n *\n * @param path - Path to the file or directory whose permissions will be modified.\n * @param mode - Permissions to set to the file or directory.\n */\nexport async function chmod(path: string, mode: number | string): Promise<void> {\n await fsChmod(path, mode)\n}\n\n/**\n * Checks if a file has executable permissions.\n *\n * @param path - Path to the file whose permissions will be checked.\n */\nexport async function fileHasExecutablePermissions(path: string): Promise<boolean> {\n try {\n await fsAccess(path, fsConstants.X_OK)\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch {\n return false\n }\n}\n\n/**\n * Returns true if a file or directory exists.\n *\n * @param path - Path to the directory or file.\n * @returns True if it exists.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fsAccess(path)\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch {\n return false\n }\n}\n\ninterface FileOptions {\n path: string\n}\n\n/**\n * Format a string using prettier. Return the formatted content.\n *\n * @param content - Content to be formatted.\n * @param options - Options to format the content with.\n * @returns The formatted content.\n */\nexport async function fileContentPrettyFormat(content: string, options: FileOptions): Promise<string> {\n const {default: prettier} = await import('prettier')\n\n const ext = extname(options.path)\n const prettierConfig: Options = {\n ...DEFAULT_PRETTIER_CONFIG,\n parser: 'babel',\n }\n\n switch (ext) {\n case '.html':\n case '.css':\n prettierConfig.parser = ext.slice(1)\n break\n }\n\n const formattedContent = await prettier.format(content, prettierConfig)\n\n return formattedContent\n}\n\ninterface GenerateRandomDirectoryOptions {\n /** Suffix to include in the randomly generated directory name. */\n suffix: string\n\n /** Absolute path to the directory where the random directory will be created. */\n directory: string\n\n /** Type of word to use for random name. */\n family?: RandomNameFamily\n}\n\n/**\n * It generates a random directory directory name for a sub-directory.\n * It ensures that the returned directory name doesn't exist.\n *\n * @param options - Options to generate the random directory name.\n * @returns It returns the name of the directory.\n */\nexport async function generateRandomNameForSubdirectory(options: GenerateRandomDirectoryOptions): Promise<string> {\n const generated = `${getRandomName(options.family ?? 'business')}-${options.suffix}`\n const randomDirectoryPath = joinPath(options.directory, generated)\n const isAppDirectoryTaken = await fileExists(randomDirectoryPath)\n\n if (isAppDirectoryTaken) {\n return generateRandomNameForSubdirectory(options)\n } else {\n return generated\n }\n}\n"]}
@@ -0,0 +1,90 @@
1
+ import { Abort } from '../../error.js';
2
+ import { DefaultLogFields, ListLogLine } from 'simple-git';
3
+ export declare const gitFactory: import("simple-git").SimpleGitFactory;
4
+ /**
5
+ * Initialize a git repository at the given directory.
6
+ *
7
+ * @param directory - The directory where the git repository will be initialized.
8
+ * @param initialBranch - The name of the initial branch.
9
+ */
10
+ export declare function initializeGitRepository(directory: string, initialBranch?: string): Promise<void>;
11
+ export interface GitIgnoreTemplate {
12
+ [section: string]: string[];
13
+ }
14
+ /**
15
+ * Create a .gitignore file in the given directory.
16
+ *
17
+ * @param directory - The directory where the .gitignore file will be created.
18
+ * @param template - The template to use to create the .gitignore file.
19
+ */
20
+ export declare function createGitIgnore(directory: string, template: GitIgnoreTemplate): void;
21
+ /**
22
+ * Options to use when cloning a git repository.
23
+ *
24
+ * @param repoUrl - The URL of the repository to clone.
25
+ * @param destination - The directory where the repository will be cloned.
26
+ * @param progressUpdater - A function that will be called with the progress of the clone.
27
+ * @param shallow - Whether to clone the repository shallowly.
28
+ * @param latestTag - Whether to clone the latest tag instead of the default branch.
29
+ */
30
+ export interface GitCloneOptions {
31
+ repoUrl: string;
32
+ destination: string;
33
+ progressUpdater?: (statusString: string) => void;
34
+ shallow?: boolean;
35
+ latestTag?: boolean;
36
+ }
37
+ /**
38
+ * Clone a git repository.
39
+ *
40
+ * @param cloneOptions - The options to use to clone the repository.
41
+ * @returns A promise that resolves when the clone is complete.
42
+ */
43
+ export declare function downloadGitRepository(cloneOptions: GitCloneOptions): Promise<void>;
44
+ /**
45
+ * Get the latest commit of a git repository.
46
+ *
47
+ * @param directory - The directory of the git repository.
48
+ * @returns The latest commit of the repository.
49
+ */
50
+ export declare function getLatestGitCommit(directory?: string): Promise<DefaultLogFields & ListLogLine>;
51
+ /**
52
+ * Add all files to the git index from the given directory.
53
+ *
54
+ * @param directory - The directory where the git repository is located.
55
+ * @returns A promise that resolves when the files are added to the index.
56
+ */
57
+ export declare function addAllToGitFromDirectory(directory?: string): Promise<void>;
58
+ export interface CreateGitCommitOptions {
59
+ directory?: string;
60
+ author?: string;
61
+ }
62
+ /**
63
+ * Create a git commit.
64
+ *
65
+ * @param message - The message of the commit.
66
+ * @param options - The options to use to create the commit.
67
+ * @returns The hash of the created commit.
68
+ */
69
+ export declare function createGitCommit(message: string, options?: CreateGitCommitOptions): Promise<string>;
70
+ /**
71
+ * Get the HEAD symbolic reference of a git repository.
72
+ *
73
+ * @param directory - The directory of the git repository.
74
+ * @returns The HEAD symbolic reference of the repository.
75
+ */
76
+ export declare function getHeadSymbolicRef(directory?: string): Promise<string>;
77
+ /**
78
+ * If "git" is not present in the environment it throws
79
+ * an abort error.
80
+ */
81
+ export declare function ensureGitIsPresentOrAbort(): Promise<void>;
82
+ export declare class OutsideGitDirectoryError extends Abort {
83
+ }
84
+ /**
85
+ * If command run from outside a .git directory tree
86
+ * it throws an abort error.
87
+ *
88
+ * @param directory - The directory to check.
89
+ */
90
+ export declare function ensureInsideGitDirectory(directory?: string): Promise<void>;
@@ -0,0 +1,174 @@
1
+ import { hasGit, isTerminalInteractive } from './environment/local.js';
2
+ import { appendFileSync } from './fs.js';
3
+ import { Abort } from '../../error.js';
4
+ import { content, token, debug } from '../../output.js';
5
+ import git from 'simple-git';
6
+ export const gitFactory = git;
7
+ /**
8
+ * Initialize a git repository at the given directory.
9
+ *
10
+ * @param directory - The directory where the git repository will be initialized.
11
+ * @param initialBranch - The name of the initial branch.
12
+ */
13
+ export async function initializeGitRepository(directory, initialBranch = 'main') {
14
+ debug(content `Initializing git repository at ${token.path(directory)}...`);
15
+ await ensureGitIsPresentOrAbort();
16
+ // We use init and checkout instead of `init --initial-branch` because the latter is only supported in git 2.28+
17
+ const repo = git(directory);
18
+ await repo.init();
19
+ await repo.checkoutLocalBranch(initialBranch);
20
+ }
21
+ /**
22
+ * Create a .gitignore file in the given directory.
23
+ *
24
+ * @param directory - The directory where the .gitignore file will be created.
25
+ * @param template - The template to use to create the .gitignore file.
26
+ */
27
+ export function createGitIgnore(directory, template) {
28
+ debug(content `Creating .gitignore at ${token.path(directory)}...`);
29
+ const filePath = `${directory}/.gitignore`;
30
+ let fileContent = '';
31
+ for (const [section, lines] of Object.entries(template)) {
32
+ fileContent += `# ${section}\n`;
33
+ fileContent += `${lines.join('\n')}\n\n`;
34
+ }
35
+ appendFileSync(filePath, fileContent);
36
+ }
37
+ /**
38
+ * Clone a git repository.
39
+ *
40
+ * @param cloneOptions - The options to use to clone the repository.
41
+ * @returns A promise that resolves when the clone is complete.
42
+ */
43
+ export async function downloadGitRepository(cloneOptions) {
44
+ const { repoUrl, destination, progressUpdater, shallow, latestTag } = cloneOptions;
45
+ debug(content `Git-cloning repository ${repoUrl} into ${token.path(destination)}...`);
46
+ await ensureGitIsPresentOrAbort();
47
+ const [repository, branch] = repoUrl.split('#');
48
+ const options = { '--recurse-submodules': null };
49
+ if (branch && latestTag) {
50
+ throw new Abort("Error cloning the repository. Git can't clone the latest release with a 'branch'.");
51
+ }
52
+ if (branch) {
53
+ options['--branch'] = branch;
54
+ }
55
+ if (shallow && latestTag) {
56
+ throw new Abort("Error cloning the repository. Git can't clone the latest release with the 'shallow' property.");
57
+ }
58
+ if (shallow) {
59
+ options['--depth'] = 1;
60
+ }
61
+ const progress = ({ stage, progress, processed, total }) => {
62
+ const updateString = `${stage}, ${processed}/${total} objects (${progress}% complete)`;
63
+ if (progressUpdater)
64
+ progressUpdater(updateString);
65
+ };
66
+ const simpleGitOptions = {
67
+ progress,
68
+ ...(!isTerminalInteractive() && { config: ['core.askpass=true'] }),
69
+ };
70
+ try {
71
+ await git(simpleGitOptions).clone(repository, destination, options);
72
+ if (latestTag) {
73
+ const localGitRepository = git(destination);
74
+ const latestTag = await getLocalLatestTag(localGitRepository, repoUrl);
75
+ await localGitRepository.checkout(latestTag);
76
+ }
77
+ }
78
+ catch (err) {
79
+ if (err instanceof Error) {
80
+ const abortError = new Abort(err.message);
81
+ abortError.stack = err.stack;
82
+ throw abortError;
83
+ }
84
+ throw err;
85
+ }
86
+ }
87
+ /**
88
+ * Get the most recent tag of a local git repository.
89
+ *
90
+ * @param repository - The local git repository.
91
+ * @param repoUrl - The URL of the repository.
92
+ * @returns The most recent tag of the repository.
93
+ */
94
+ async function getLocalLatestTag(repository, repoUrl) {
95
+ const latest = (await repository.tags()).latest;
96
+ if (!latest) {
97
+ throw new Abort(`Couldn't obtain the most recent tag of the repository ${repoUrl}`);
98
+ }
99
+ return latest;
100
+ }
101
+ /**
102
+ * Get the latest commit of a git repository.
103
+ *
104
+ * @param directory - The directory of the git repository.
105
+ * @returns The latest commit of the repository.
106
+ */
107
+ export async function getLatestGitCommit(directory) {
108
+ const logs = await git({ baseDir: directory }).log({
109
+ maxCount: 1,
110
+ });
111
+ if (!logs.latest) {
112
+ throw new Abort('Must have at least one commit to run command', content `Run ${token.genericShellCommand("git commit -m 'Initial commit'")} to create your first commit.`);
113
+ }
114
+ return logs.latest;
115
+ }
116
+ /**
117
+ * Add all files to the git index from the given directory.
118
+ *
119
+ * @param directory - The directory where the git repository is located.
120
+ * @returns A promise that resolves when the files are added to the index.
121
+ */
122
+ export async function addAllToGitFromDirectory(directory) {
123
+ const simpleGit = git({ baseDir: directory });
124
+ await simpleGit.raw('add', '--all');
125
+ }
126
+ /**
127
+ * Create a git commit.
128
+ *
129
+ * @param message - The message of the commit.
130
+ * @param options - The options to use to create the commit.
131
+ * @returns The hash of the created commit.
132
+ */
133
+ export async function createGitCommit(message, options) {
134
+ const simpleGit = git({ baseDir: options?.directory });
135
+ const commitOptions = options?.author ? { '--author': options.author } : undefined;
136
+ const result = await simpleGit.commit(message, commitOptions);
137
+ return result.commit;
138
+ }
139
+ /**
140
+ * Get the HEAD symbolic reference of a git repository.
141
+ *
142
+ * @param directory - The directory of the git repository.
143
+ * @returns The HEAD symbolic reference of the repository.
144
+ */
145
+ export async function getHeadSymbolicRef(directory) {
146
+ const ref = await git({ baseDir: directory }).raw('symbolic-ref', '-q', 'HEAD');
147
+ if (!ref) {
148
+ throw new Abort("Git HEAD can't be detached to run command", content `Run ${token.genericShellCommand('git checkout [branchName]')} to reattach HEAD or see git ${token.link('documentation', 'https://git-scm.com/book/en/v2/Git-Internals-Git-References')} for more details`);
149
+ }
150
+ return ref.trim();
151
+ }
152
+ /**
153
+ * If "git" is not present in the environment it throws
154
+ * an abort error.
155
+ */
156
+ export async function ensureGitIsPresentOrAbort() {
157
+ if (!(await hasGit())) {
158
+ throw new Abort(`Git is necessary in the environment to continue`, content `Install ${token.link('git', 'https://git-scm.com/book/en/v2/Getting-Started-Installing-Git')}`);
159
+ }
160
+ }
161
+ export class OutsideGitDirectoryError extends Abort {
162
+ }
163
+ /**
164
+ * If command run from outside a .git directory tree
165
+ * it throws an abort error.
166
+ *
167
+ * @param directory - The directory to check.
168
+ */
169
+ export async function ensureInsideGitDirectory(directory) {
170
+ if (!(await git({ baseDir: directory }).checkIsRepo())) {
171
+ throw new OutsideGitDirectoryError(`${token.path(directory || process.cwd())} is not a Git directory`);
172
+ }
173
+ }
174
+ //# sourceMappingURL=git.js.map