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