Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/typegpu-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
"prepublishOnly": "pnpm run build"
},
"dependencies": {
"@clack/prompts": "^0.9.1",
"@clack/prompts": "^1.5.0",
"arktype": "catalog:",
"comment-json": "^5.0.0",
"cross-spawn": "^7.0.6",
"magicast": "^0.5.2",
"mri": "^1.2.0",
"package-manager-detector": "^1.3.0",
"rimraf": "^6.1.3"
"package-manager-detector": "^1.3.0"
},
"devDependencies": {
"@types/cross-spawn": "^6.0.6",
Expand Down
33 changes: 26 additions & 7 deletions packages/typegpu-cli/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'node:fs';
import { rimraf } from 'rimraf';
import path from 'node:path';
import * as p from '@clack/prompts';

import { cancelExit, failAndExit } from './prompts.ts';
import { type } from 'arktype';
import { PackageJsonSchema } from './types.ts';
import { multiselectPkgs } from './inputs.ts';
import { PackageJsonSchema, AppJsonSchema } from './types.ts';
import { multiselectPkgs, sanitizeToExpoSlug } from './inputs.ts';

const renameFiles = {
_gitignore: '.gitignore',
Expand All @@ -22,10 +21,15 @@ function isEmptyDir(dir: string) {
return entries.length === 0 || (entries.length === 1 && entries[0] === '.git');
}

async function emptyDir(dir: string) {
function emptyDir(dir: string) {
for (const entry of fs.readdirSync(dir)) {
if (entry === '.git') continue;
await rimraf(path.join(dir, entry));
fs.rmSync(path.join(dir, entry), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
}
}

Expand All @@ -45,7 +49,7 @@ export async function prepareDirectory(cwd: string, projectDir: string) {
if (p.isCancel(overwrite) || overwrite === 'no') {
cancelExit();
}
await emptyDir(dir);
emptyDir(dir);
}

return dir;
Expand All @@ -57,7 +61,9 @@ export async function scaffoldProject(
packageName: string,
) {
const entries = fs.readdirSync(templateDir);
for (const entry of entries.filter((f) => f !== '_package.json' && f !== 'index.html')) {
for (const entry of entries.filter(
(f) => f !== '_package.json' && f !== 'index.html' && f !== 'app.json',
)) {
const src = path.join(templateDir, entry);
const dest = path.join(projectDir, renameFiles[entry] ?? entry);
fs.cpSync(src, dest, { recursive: true });
Expand All @@ -74,6 +80,19 @@ export async function scaffoldProject(
fs.writeFileSync(destIndex, updatedContent);
}

const srcApp = path.join(templateDir, 'app.json');
const destApp = path.join(projectDir, 'app.json');
if (fs.existsSync(srcApp)) {
const app = AppJsonSchema(JSON.parse(fs.readFileSync(srcApp, 'utf-8')));
if (app instanceof type.errors) {
failAndExit(`[INTERNAL] Invalid app.json in template ${templateDir}`, app.summary);
}
app.expo.name = packageName;
Comment thread
cieplypolar marked this conversation as resolved.
app.expo.slug = sanitizeToExpoSlug(packageName);

fs.writeFileSync(destApp, JSON.stringify(app, null, 2) + '\n' /* to make oxfmt happy */);
}

const srcPackage = path.join(templateDir, '_package.json');
const destPackage = path.join(projectDir, 'package.json');
const pkg = PackageJsonSchema(JSON.parse(fs.readFileSync(srcPackage, 'utf-8')));
Expand Down
15 changes: 13 additions & 2 deletions packages/typegpu-cli/src/utils/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ export function isValidPackageName(packageName: string) {
return /^(?:@[a-z\d][a-z\d\-._]*\/)?[a-z\d][a-z\d\-._]*$/.test(trimmedName);
}

export function sanitizeToExpoSlug(packageName: string) {
const baseName = packageName.includes('/') ? packageName.split('/').pop() : packageName;

const slug = baseName
?.replace(/[._]/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');

return slug || 'typegpu-expo-simple-project';
}

export async function getProjectName(initialValue: string) {
let projectName = await p.text({
message: 'Project name:',
placeholder: initialValue,
defaultValue: initialValue,
validate: (value) => {
return !isValidProjectDirectory(value) ? 'Invalid project name.' : undefined;
return value && !isValidProjectDirectory(value) ? 'Invalid project name.' : undefined;
},
});

Expand All @@ -36,7 +47,7 @@ export async function getPackageName() {
const packageName = await p.text({
message: 'Package name:',
validate: (value) => {
return !isValidPackageName(value) ? 'Invalid package name.' : undefined;
return value && !isValidPackageName(value) ? 'Invalid package name.' : undefined;
},
});

Expand Down
8 changes: 8 additions & 0 deletions packages/typegpu-cli/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export const PackageJsonSchema = type({
});
export type PackageJson = typeof PackageJsonSchema.infer;

export const AppJsonSchema = type({
expo: {
name: 'string',
slug: 'string',
},
});
export type AppJson = typeof AppJsonSchema.infer;

export const TsConfigSchema = type({
'compilerOptions?': {
'types?': 'string[]',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"ignorePatterns": [".agents/**"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"ignorePatterns": [".agents/**"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"ignorePatterns": [".agents/**"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"ignorePatterns": [".agents/**"]
}
92 changes: 39 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading