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 | 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x | import path from 'node:path';
import { z } from 'zod';
import { config } from '../../config/config.js';
import { defineArgument } from '../../lib/define-argument.js';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as AppConfig from '../../models/app_configuration.js';
import * as Application from '../../models/application.js';
import { AVAILABLE_ZONES, listAvailableTypes, listAvailableZones } from '../../models/application.js';
import { LinkedWorktreeNotSupportedError } from '../../models/git-isomorphic.js';
import { Git } from '../../models/git.js';
import { aliasCreationOption, humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
function getGithubDetails(githubOwnerRepo) {
if (githubOwnerRepo != null) {
const [owner, name] = githubOwnerRepo.split('/');
return { owner, name };
}
}
function getCurrentDirectoryName() {
return path.basename(process.cwd());
}
async function displayAppCreation(app, alias, github, taskCommand) {
Logger.printSuccess(`Application ${styleText('green', app.name)} successfully created!`);
const hasDistinctAlias = alias != null && alias !== app.name;
const isTask = app.instance.lifetime === 'TASK';
Logger.println();
printFieldsAsTable(2, {
Type: styleText('blue', `⬢ ${app.instance.variant.name}`),
ID: app.id,
'Organisation ID': app.ownerId,
Name: app.name,
GitHub: github != null && `${github.owner}/${github.name}`,
Alias: hasDistinctAlias && alias,
Zone: app.zone,
Task: isTask && `"${taskCommand}"`,
});
Logger.println();
Logger.println(' ' + styleText('bold', 'Next steps:'));
if (!github) {
const git = await Git.get();
const isInsideGitRepo = await git.isInsideGitRepo();
if (!isInsideGitRepo) {
Logger.println(` ${styleText('yellow', '!')} Initialize a git repository first, for example:`);
Logger.println(` ${shellCommand('git init')}`);
Logger.println(` ${shellCommand('git add .')}`);
Logger.println(` ${shellCommand('git commit -m "Initial commit"')}`);
Logger.println();
} else {
// Best-effort hint: if we can't tell because of a linked worktree on the JS backend, don't nag
const isClean = await git.isGitWorkingDirectoryClean().catch((error) => {
if (error instanceof LinkedWorktreeNotSupportedError) {
return true;
}
throw error;
});
if (!isClean) {
Logger.println(` ${styleText('yellow', '!')} Commit your changes first:`);
Logger.println(` ${shellCommand('git add .')}`);
Logger.println(` ${shellCommand('git commit -m "My changes"')}`);
Logger.println();
}
}
}
if (github) {
Logger.println(
` ${styleText('blue', '→')} Push changes to ${styleText('blue', `${github.owner}/${github.name}`)} GitHub repository to trigger a deployment, or ${styleText('blue', 'clever restart')} the latest pushed commit`,
);
} else {
Logger.println(
` ${styleText('blue', '→')} Run ${styleText('blue', 'clever deploy')} ${isTask ? 'to execute your task' : 'to deploy your application'}`,
);
}
Logger.println(
` ${styleText('blue', '→')} Manage your application at: ${styleText('underline', `${config.GOTO_URL}/${app.id}`)}`,
);
Logger.println('');
}
function shellCommand(command) {
return `${styleText('grey', '$')} ${styleText('yellow', command)}`;
}
function printFieldsAsTable(indent, fields) {
const fieldsWithValues = Object.fromEntries(Object.entries(fields).filter(([_, value]) => typeof value === 'string'));
const labelMaxWidth = Math.max(...Object.keys(fieldsWithValues).map((label) => label.length));
return Object.entries(fieldsWithValues).forEach(([label, value]) => {
Logger.println(`${' '.repeat(indent)}${label.padEnd(labelMaxWidth, ' ')} ${styleText('grey', value)}`);
});
}
export const createCommand = defineCommand({
description: 'Create an application',
since: '0.2.0',
options: {
type: defineOption({
name: 'type',
schema: z.string(),
description: 'Instance type',
aliases: ['t'],
placeholder: 'instance-type',
complete: listAvailableTypes,
}),
region: defineOption({
name: 'region',
schema: z.string().default('par'),
description: `Region, can be ${AVAILABLE_ZONES.map((name) => `'${name}'`).join(', ')}`,
aliases: ['r'],
placeholder: 'zone',
complete: listAvailableZones,
}),
github: defineOption({
name: 'github',
schema: z.string().optional(),
description: 'GitHub application to use for deployments',
placeholder: 'owner/repo',
}),
task: defineOption({
name: 'task',
schema: z.string().min(1).optional(),
description: 'The application launch as a task executing the given command, then stopped',
aliases: ['T'],
placeholder: 'command',
}),
org: orgaIdOrNameOption,
alias: aliasCreationOption,
format: humanJsonOutputFormatOption,
},
args: [
defineArgument({
schema: z.string().optional(),
description: 'Application name (current directory name is used if not specified)',
placeholder: 'app-name',
}),
],
async handler(options, rawName) {
const { type: typeName } = options;
const { org: orgaIdOrName, alias, region, github: githubOwnerRepo, format, task: taskCommand } = options;
const { apps } = await AppConfig.loadApplicationConf();
// Application name is optionnal, use current directory name if not specified (empty string)
const name = rawName !== '' ? rawName : getCurrentDirectoryName();
const isTask = taskCommand != null;
const envVars = isTask ? { CC_RUN_COMMAND: taskCommand } : {};
AppConfig.checkAlreadyLinked(apps, name, alias);
const github = getGithubDetails(githubOwnerRepo);
const app = await Application.create(name, typeName, region, orgaIdOrName, github, isTask, envVars);
await AppConfig.addLinkedApplication(app, alias);
switch (format) {
case 'json': {
Logger.printJson({
id: app.id,
name: app.name,
executedAs: app.instance.lifetime,
env: app.env,
deployUrl: app.deployUrl,
});
break;
}
case 'human':
default:
await displayAppCreation(app, alias, github, taskCommand);
}
},
});
|