|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import Path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { type App as DBAppType } from '../db/schema.mjs'; |
| 5 | +import { APPS_DIR } from '../constants.mjs'; |
| 6 | +import { toValidPackageName } from './utils.mjs'; |
| 7 | +import { Dirent } from 'node:fs'; |
| 8 | +import { FileType } from '@srcbook/shared'; |
| 9 | + |
| 10 | +export function pathToApp(id: string) { |
| 11 | + return Path.join(APPS_DIR, id); |
| 12 | +} |
| 13 | + |
| 14 | +function pathToTemplate(template: string) { |
| 15 | + return Path.resolve(fileURLToPath(import.meta.url), '..', 'templates', template); |
| 16 | +} |
| 17 | + |
| 18 | +export function deleteViteApp(id: string) { |
| 19 | + return fs.rm(pathToApp(id), { recursive: true }); |
| 20 | +} |
| 21 | + |
| 22 | +export async function createViteApp(app: DBAppType) { |
| 23 | + const appPath = pathToApp(app.externalId); |
| 24 | + |
| 25 | + // Use recursive because its parent directory may not exist. |
| 26 | + await fs.mkdir(appPath, { recursive: true }); |
| 27 | + |
| 28 | + // Scaffold all the necessary project files. |
| 29 | + await scaffold(app, appPath); |
| 30 | + |
| 31 | + return app; |
| 32 | +} |
| 33 | + |
| 34 | +async function scaffold(app: DBAppType, destDir: string) { |
| 35 | + const template = `react-${app.language}`; |
| 36 | + |
| 37 | + function write(file: string, content?: string) { |
| 38 | + const targetPath = Path.join(destDir, file); |
| 39 | + return content === undefined |
| 40 | + ? copy(Path.join(templateDir, file), targetPath) |
| 41 | + : fs.writeFile(targetPath, content, 'utf-8'); |
| 42 | + } |
| 43 | + |
| 44 | + const templateDir = pathToTemplate(template); |
| 45 | + const files = await fs.readdir(templateDir); |
| 46 | + for (const file of files.filter((f) => f !== 'package.json')) { |
| 47 | + await write(file); |
| 48 | + } |
| 49 | + |
| 50 | + const [pkgContents, idxContents] = await Promise.all([ |
| 51 | + fs.readFile(Path.join(templateDir, 'package.json'), 'utf-8'), |
| 52 | + fs.readFile(Path.join(templateDir, 'index.html'), 'utf-8'), |
| 53 | + ]); |
| 54 | + |
| 55 | + const pkg = JSON.parse(pkgContents); |
| 56 | + pkg.name = toValidPackageName(app.name); |
| 57 | + const updatedPkgContents = JSON.stringify(pkg, null, 2) + '\n'; |
| 58 | + |
| 59 | + const updatedIdxContents = idxContents.replace( |
| 60 | + /<title>.*<\/title>/, |
| 61 | + `<title>${app.name}</title>`, |
| 62 | + ); |
| 63 | + |
| 64 | + await Promise.all([ |
| 65 | + write('package.json', updatedPkgContents), |
| 66 | + write('index.html', updatedIdxContents), |
| 67 | + ]); |
| 68 | +} |
| 69 | + |
| 70 | +export function fileUpdated(app: DBAppType, file: FileType) { |
| 71 | + const path = Path.join(pathToApp(app.externalId), file.path); |
| 72 | + return fs.writeFile(path, file.source, 'utf-8'); |
| 73 | +} |
| 74 | + |
| 75 | +async function copy(src: string, dest: string) { |
| 76 | + const stat = await fs.stat(src); |
| 77 | + if (stat.isDirectory()) { |
| 78 | + return copyDir(src, dest); |
| 79 | + } else { |
| 80 | + return fs.copyFile(src, dest); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +async function copyDir(srcDir: string, destDir: string) { |
| 85 | + await fs.mkdir(destDir, { recursive: true }); |
| 86 | + const files = await fs.readdir(srcDir); |
| 87 | + for (const file of files) { |
| 88 | + const srcFile = Path.resolve(srcDir, file); |
| 89 | + const destFile = Path.resolve(destDir, file); |
| 90 | + await copy(srcFile, destFile); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TODO: This does not scale. |
| 95 | +export async function getProjectFiles(app: DBAppType) { |
| 96 | + const projectDir = Path.join(APPS_DIR, app.externalId); |
| 97 | + |
| 98 | + const { files, directories } = await getDiskEntries(projectDir, { |
| 99 | + exclude: ['node_modules', 'dist'], |
| 100 | + }); |
| 101 | + |
| 102 | + const nestedFiles = await Promise.all( |
| 103 | + directories.flatMap(async (dir) => { |
| 104 | + const entries = await fs.readdir(Path.join(projectDir, dir.name), { |
| 105 | + withFileTypes: true, |
| 106 | + recursive: true, |
| 107 | + }); |
| 108 | + return entries.filter((entry) => entry.isFile()); |
| 109 | + }), |
| 110 | + ); |
| 111 | + |
| 112 | + const entries = [...files, ...nestedFiles.flat()]; |
| 113 | + |
| 114 | + return Promise.all( |
| 115 | + entries.map(async (entry) => { |
| 116 | + const fullPath = Path.join(entry.parentPath, entry.name); |
| 117 | + const relativePath = Path.relative(projectDir, fullPath); |
| 118 | + const contents = await fs.readFile(fullPath); |
| 119 | + const binary = isBinary(entry.name); |
| 120 | + const source = !binary ? contents.toString('utf-8') : `TODO: handle this`; |
| 121 | + return { path: relativePath, source, binary }; |
| 122 | + }), |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +async function getDiskEntries(projectDir: string, options: { exclude: string[] }) { |
| 127 | + const result: { files: Dirent[]; directories: Dirent[] } = { |
| 128 | + files: [], |
| 129 | + directories: [], |
| 130 | + }; |
| 131 | + |
| 132 | + for (const entry of await fs.readdir(projectDir, { withFileTypes: true })) { |
| 133 | + if (options.exclude.includes(entry.name)) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if (entry.isFile()) { |
| 138 | + result.files.push(entry); |
| 139 | + } else { |
| 140 | + result.directories.push(entry); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return result; |
| 145 | +} |
| 146 | + |
| 147 | +// TODO: This does not scale. |
| 148 | +// What's the best way to know whether a file is a "binary" |
| 149 | +// file or not? Inspecting bytes for invalid utf8? |
| 150 | +const TEXT_FILE_EXTENSIONS = [ |
| 151 | + '.ts', |
| 152 | + '.cts', |
| 153 | + '.mts', |
| 154 | + '.tsx', |
| 155 | + '.js', |
| 156 | + '.cjs', |
| 157 | + '.mjs', |
| 158 | + '.jsx', |
| 159 | + '.md', |
| 160 | + '.markdown', |
| 161 | + '.json', |
| 162 | + '.css', |
| 163 | + '.html', |
| 164 | +]; |
| 165 | + |
| 166 | +function isBinary(basename: string) { |
| 167 | + const isDotfile = basename.startsWith('.'); // Assume these are text for now, e.g., .gitignore |
| 168 | + const isTextFile = TEXT_FILE_EXTENSIONS.includes(Path.extname(basename)); |
| 169 | + return !(isDotfile || isTextFile); |
| 170 | +} |
0 commit comments