Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 7x 4x 4x 24x 17x 17x 24x 24x 24x | import { existsSync, mkdirSync } from 'node:fs';
import { dirname, isAbsolute, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { cancel, confirm, isCancel } from '@clack/prompts';
import { copyTemplateFiles, isDirectoryEmpty } from './template';
import type { TemplateVariables } from './types';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Create a project from template
*/
export async function createProjectFromTemplate(
targetDir: string,
templateType: string,
workingDir: string,
force: boolean,
variables: TemplateVariables
): Promise<void> {
const templatePath = resolve(__dirname, '../template', templateType);
const targetPath = isAbsolute(targetDir)
? targetDir
: targetDir === '.'
? workingDir
: resolve(workingDir, targetDir);
if (!existsSync(templatePath)) {
throw new Error(`Template "${templateType}" not found`);
}
// Handle directory existence and overwrite confirmation
if (targetDir !== '.' && existsSync(targetPath)) {
if (!isDirectoryEmpty(targetPath)) {
if (!force) {
const shouldOverwrite = await confirm({
message: `Directory "${targetDir}" is not empty. Do you want to overwrite it?`
});
if (isCancel(shouldOverwrite)) {
cancel('Operation cancelled');
return;
}
if (!shouldOverwrite) {
throw new Error('Operation cancelled by user');
}
}
// Files will be overwritten during copyTemplateFiles
}
} else if (targetDir !== '.') {
mkdirSync(targetPath, { recursive: true });
}
// Handle current directory case
if (targetDir === '.' && !isDirectoryEmpty(targetPath)) {
if (!force) {
const shouldOverwrite = await confirm({
message:
'Current directory is not empty. Do you want to overwrite existing files?'
});
if (isCancel(shouldOverwrite)) {
cancel('Operation cancelled');
return;
}
if (!shouldOverwrite) {
throw new Error('Operation cancelled by user');
}
}
}
copyTemplateFiles(templatePath, targetPath, variables);
}
|