All files / src cli.ts

79.05% Statements 117/148
75% Branches 18/24
80% Functions 4/5
79.05% Lines 117/148

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 2051x                 1x 1x 1x 1x   1x         1x 1x   1x 1x   1x 1x 1x   1x             1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x         14x 14x 14x 14x 14x 14x 14x 14x                                         14x 14x   14x 14x 14x 14x 14x 14x                                     16x 16x 16x 16x   16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x   16x 1x 1x 1x   16x 1x 1x 1x   14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 16x         14x   14x 16x         14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x 14x 14x 16x 16x 16x 16x 16x 16x 16x 16x 14x 14x   14x 16x   16x 16x 16x 16x 16x 16x   16x 54x 16x   16x   16x 16x  
import {
    cancel,
    intro,
    isCancel,
    note,
    outro,
    select,
    text
} from '@clack/prompts';
import minimist from 'minimist';
import color from 'picocolors';
import { createProjectFromTemplate } from './project';
import { getAvailableTemplates, getEsmxVersion } from './template';
import type { CliOptions } from './types';
import { formatProjectName, getCommand } from './utils/index';
 
/**
 * Display help information
 */
function showHelp(userAgent?: string): void {
    const createCmd = getCommand('create', userAgent);
 
    console.log(`
${color.reset(color.bold(color.blue('🚀 Create Esmx Project')))}
 
${color.bold('Usage:')}
  ${createCmd} [project-name]
  ${createCmd} [project-name] [options]
 
${color.bold('Options:')}
  -t, --template <template>    Template to use (default: vue2-csr)
  -n, --name <name>            Project name or path
  -f, --force                  Force overwrite existing directory
  -h, --help                   Show help information
  -v, --version                Show version number
 
${color.bold('Examples:')}
  ${createCmd} my-project
  ${createCmd} my-project -t vue2-csr
  ${createCmd} my-project --force
  ${createCmd} . -f -t vue2-csr
 
${color.bold('Available Templates:')}
${getAvailableTemplates()
    .map((t) => `  ${t.folder.padEnd(25)} ${t.description}`)
    .join('\n')}
 
For more information, visit: ${color.cyan('https://esmnext.com')}
`);
}
 
/**
 * Get project name from arguments or prompt user
 */
async function getProjectName(
    argName?: string,
    positionalName?: string
): Promise<string | symbol> {
    const providedName = argName || positionalName;
    if (providedName) {
        return providedName;
    }
 
    const projectName = await text({
        message: 'Project name or path:',
        placeholder: 'my-esmx-project',
        validate: (value: string) => {
            if (!value.trim()) {
                return 'Project name or path is required';
            }
            if (!/^[a-zA-Z0-9_.\/@-]+$/.test(value.trim())) {
                return 'Project name or path should only contain letters, numbers, hyphens, underscores, dots, and slashes';
            }
        }
    });
 
    return String(projectName).trim();
}
 
/**
 * Get template type from arguments or prompt user
 */
async function getTemplateType(argTemplate?: string): Promise<string> {
    const availableTemplates = getAvailableTemplates();
 
    if (
        argTemplate &&
        availableTemplates.some((t) => t.folder === argTemplate)
    ) {
        return argTemplate;
    }
 
    const options = availableTemplates.map((t) => ({
        label: color.reset(color.gray(`${t.folder} - `) + color.bold(t.name)),
        value: t.folder,
        hint: t.description
    }));
 
    const template = await select({
        message: 'Select a template:',
        options: options
    });
 
    return String(template);
}
 
/**
 * Main function to create a project
 */
export async function cli(options: CliOptions = {}): Promise<void> {
    const { argv, cwd, userAgent, version } = options;
    const commandLineArgs = argv || process.argv.slice(2);
    const workingDir = cwd || process.cwd();
 
    const parsedArgs = minimist(commandLineArgs, {
        string: ['template', 'name'],
        boolean: ['help', 'version', 'force'],
        alias: {
            t: 'template',
            n: 'name',
            f: 'force',
            h: 'help',
            v: 'version'
        }
    });
 
    if (parsedArgs.help) {
        showHelp(userAgent);
        return;
    }
 
    if (parsedArgs.version) {
        console.log(getEsmxVersion());
        return;
    }
 
    console.log();
    intro(
        color.reset(
            color.bold(color.blue('🚀 Welcome to Esmx Project Creator!'))
        )
    );
 
    const projectNameInput = await getProjectName(
        parsedArgs.name,
        parsedArgs._[0]
    );
    if (isCancel(projectNameInput)) {
        cancel('Operation cancelled');
        return;
    }
 
    const { name, root } = formatProjectName(projectNameInput, workingDir);
 
    const templateType = await getTemplateType(parsedArgs.template);
    if (isCancel(templateType)) {
        cancel('Operation cancelled');
        return;
    }
 
    const installCommand = getCommand('install', userAgent);
    const devCommand = getCommand('dev', userAgent);
    const buildCommand = getCommand('build', userAgent);
    const startCommand = getCommand('start', userAgent);
    const buildTypeCommand = getCommand('build:type', userAgent);
    const lintTypeCommand = getCommand('lint:type', userAgent);
 
    await createProjectFromTemplate(
        root,
        templateType,
        workingDir,
        parsedArgs.force,
        {
            projectName: name,
            esmxVersion: version || getEsmxVersion(),
            installCommand,
            devCommand,
            buildCommand,
            startCommand,
            buildTypeCommand,
            lintTypeCommand
        }
    );
    const installCmd = installCommand;
    const devCmd = devCommand;
 
    const targetDirForDisplay =
        projectNameInput === '.' ? '.' : projectNameInput;
 
    const steps = [
        projectNameInput !== '.' ? `cd ${targetDirForDisplay}` : null,
        installCmd,
        `git init ${color.gray('(optional)')}`,
        devCmd
    ].filter(Boolean);
 
    const nextSteps = steps.map((step, index) => {
        return color.reset(`${index + 1}. ${color.cyan(step)}`);
    });
 
    note(nextSteps.join('\n'), 'Next steps');
 
    outro(color.reset(color.green('Happy coding! 🎉')));
}