Open History to see the last commit
1import { readdir } from 'node:fs/promises'
2import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
3import { basename, extname, resolve } from 'node:path'
4import parseGitignore from 'parse-gitignore'
5import ignore from 'ignore'
6
7import type { Environment } from './types'
8
9const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico']
10
11export function readFileHelper(path: string): string {
12 if (isBinaryFile(path)) {
13 return `base64::${readFileSync(path).toString('base64')}`
14 } else {
15 return readFileSync(path, 'utf-8').toString()
16 }
17}
18
19export function isBinaryFile(path: string): boolean {
20 return BINARY_EXTENSIONS.includes(extname(path))
21}
22
23export function convertBinaryContentsToBase64(contents: any): string {
24 return `base64::${Buffer.from(contents).toString('base64')}`
25}
26
27export function isBase64(content: string): boolean {
28 return content.startsWith('base64::')
29}
30
31export function getBinaryFile(content: string): string | null {
32 if (content.startsWith('base64::')) {
33 const binaryContent = Buffer.from(content.replace('base64::', ''), 'base64')
34 return binaryContent as unknown as string
35 }
36 return null
37}
38
39export function relativePath(from: string, to: string) {
40 const fixedOnWindows = from.startsWith('.\\')
41 ? from.replace(/\\/g, '/')
42 : from
43 const cleanedFrom = fixedOnWindows.startsWith('./')
44 ? fixedOnWindows.slice(2)
45 : from
46 const cleanedTo = to.startsWith('./') ? to.slice(2) : to
47
48 const fromSegments = cleanedFrom.split('/')
49 const toSegments = cleanedTo.split('/')
50
51 fromSegments.pop()
52 toSegments.pop()
53
54 let commonIndex = 0
55 while (
56 commonIndex < fromSegments.length &&
57 commonIndex < toSegments.length &&
58 fromSegments[commonIndex] === toSegments[commonIndex]
59 ) {
60 commonIndex++
61 }
62
63 const upLevels = fromSegments.length - commonIndex
64 const downLevels = toSegments.slice(commonIndex)
65
66 if (upLevels === 0 && downLevels.length === 0) {
67 return `./${basename(to)}`
68 } else if (upLevels === 0 && downLevels.length > 0) {
69 return `./${downLevels.join('/')}/${basename(to)}`
70 } else {
71 const relativePath = [...Array(upLevels).fill('..'), ...downLevels].join(
72 '/',
73 )
74 return `${relativePath}/${basename(to)}`
75 }
76}
77
78export function isDirectory(path: string): boolean {
79 return statSync(path).isDirectory()
80}
81
82export function findFilesRecursively(
83 path: string,
84 files: Record<string, string>,
85) {
86 const dirFiles = readdirSync(path)
87 for (const file of dirFiles) {
88 const filePath = resolve(path, file)
89 if (isDirectory(filePath)) {
90 findFilesRecursively(filePath, files)
91 } else {
92 files[filePath] = readFileHelper(filePath)
93 }
94 }
95}
96
97async function recursivelyGatherFilesHelper(
98 basePath: string,
99 path: string,
100 files: Record<string, string>,
101 ignore: (filePath: string) => boolean,
102) {
103 const dirFiles = await readdir(path, { withFileTypes: true })
104 for (const file of dirFiles) {
105 if (ignore(file.name)) {
106 continue
107 }
108 if (file.isDirectory()) {
109 await recursivelyGatherFilesHelper(
110 basePath,
111 resolve(path, file.name),
112 files,
113 ignore,
114 )
115 } else {
116 const filePath = resolve(path, file.name)
117 files[filePath.replace(basePath, '.')] = await readFileHelper(filePath)
118 }
119 }
120}
121
122export async function recursivelyGatherFiles(
123 path: string,
124 includeProjectFiles = true,
125) {
126 const ignore = createIgnore(path, includeProjectFiles)
127 const files: Record<string, string> = {}
128 await recursivelyGatherFilesHelper(path, path, files, ignore)
129 return files
130}
131
132async function recursivelyGatherFilesFromEnvironmentHelper(
133 environment: Environment,
134 basePath: string,
135 path: string,
136 files: Record<string, string>,
137 ignore: (filePath: string) => boolean,
138) {
139 const dirFiles = await environment.readdir(path)
140 for (const file of dirFiles) {
141 if (ignore(file)) {
142 continue
143 }
144 if (environment.isDirectory(resolve(path, file))) {
145 await recursivelyGatherFilesFromEnvironmentHelper(
146 environment,
147 basePath,
148 resolve(path, file),
149 files,
150 ignore,
151 )
152 } else {
153 const filePath = resolve(path, file)
154 files[filePath.replace(basePath, '.')] =
155 await environment.readFile(filePath)
156 }
157 }
158}
159
160export async function recursivelyGatherFilesFromEnvironment(
161 environment: Environment,
162 path: string,
163 includeProjectFiles = true,
164) {
165 const ignore = createIgnore(path, includeProjectFiles)
166 const files: Record<string, string> = {}
167 await recursivelyGatherFilesFromEnvironmentHelper(
168 environment,
169 path,
170 path,
171 files,
172 ignore,
173 )
174 return files
175}
176
177export const IGNORE_FILES = [
178 '.starter',
179 '.add-on',
180 '.cta.json',
181 '.git',
182 'add-on-info.json',
183 'add-on.json',
184 'build',
185 'bun.lock',
186 'bun.lockb',
187 'deno.lock',
188 'dist',
189 'node_modules',
190 'package-lock.json',
191 'pnpm-lock.yaml',
192 'starter.json',
193 'starter-info.json',
194 'yarn.lock',
195]
196
197const PROJECT_FILES = ['package.json']
198
199export function createIgnore(path: string, includeProjectFiles = true) {
200 const ignoreList = existsSync(resolve(path, '.gitignore'))
201 ? (
202 parseGitignore(
203 readFileSync(resolve(path, '.gitignore')),
204 ) as unknown as { patterns: Array<string> }
205 ).patterns
206 : []
207 const ig = ignore().add(ignoreList)
208 return (filePath: string) => {
209 const fileName = basename(filePath)
210 if (
211 IGNORE_FILES.includes(fileName) ||
212 (includeProjectFiles && PROJECT_FILES.includes(fileName))
213 ) {
214 return true
215 }
216 const nameWithoutDotSlash = fileName.replace(/^\.\//, '')
217 return ig.ignores(nameWithoutDotSlash)
218 }
219}
220
221export function cleanUpFiles(
222 files: Record<string, string>,
223 targetDir?: string,
224) {
225 return Object.keys(files).reduce<Record<string, string>>((acc, file) => {
226 if (basename(file) !== '.cta.json') {
227 acc[targetDir ? file.replace(targetDir, '.') : file] = files[file]
228 }
229 return acc
230 }, {})
231}
232
233export function cleanUpFileArray(files: Array<string>, targetDir?: string) {
234 return files.reduce<Array<string>>((acc, file) => {
235 if (basename(file) !== '.cta.json') {
236 acc.push(targetDir ? file.replace(targetDir, '.') : file)
237 }
238 return acc
239 }, [])
240}