-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathadd.ts
163 lines (140 loc) · 4.64 KB
/
add.ts
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
import path from 'path';
import { Argv, ArgumentsCamelCase } from 'yargs';
import { logger } from '@hubspot/local-dev-lib/logger';
import {
cloneGithubRepo,
fetchReleaseData,
} from '@hubspot/local-dev-lib/github';
import { debugError } from '../../lib/errorHandlers';
import { trackCommandUsage } from '../../lib/usageTracking';
import { i18n } from '../../lib/lang';
import { projectAddPrompt } from '../../lib/prompts/projectAddPrompt';
import { getProjectConfig } from '../../lib/projects';
import { getProjectComponentListFromRepo } from '../../lib/projects/create';
import { findProjectComponents } from '../../lib/projects/structure';
import { ComponentTypes } from '../../types/Projects';
import { uiBetaTag } from '../../lib/ui';
import { HUBSPOT_PROJECT_COMPONENTS_GITHUB_PATH } from '../../lib/constants';
import { EXIT_CODES } from '../../lib/enums/exitCodes';
import { CommonArgs } from '../../types/Yargs';
import { makeYargsBuilder } from '../../lib/yargsUtils';
export const command = 'add';
export const describe = uiBetaTag(i18n(`commands.project.subcommands.add.describe`), false);
type ProjectAddArgs = CommonArgs & {
type: string;
name: string;
};
export async function handler(
args: ArgumentsCamelCase<ProjectAddArgs>
): Promise<void> {
const { derivedAccountId } = args;
trackCommandUsage('project-add', undefined, derivedAccountId);
const { projectConfig, projectDir } = await getProjectConfig();
if (!projectDir || !projectConfig) {
logger.error(i18n(`commands.project.subcommands.add.error.locationInProject`));
process.exit(EXIT_CODES.ERROR);
}
// We currently only support adding private apps to projects
let projectContainsPublicApp = false;
try {
const components = await findProjectComponents(projectDir);
projectContainsPublicApp = components.some(
c => c.type === ComponentTypes.PublicApp
);
} catch (err) {
debugError(err);
}
if (projectContainsPublicApp) {
logger.error(i18n(`commands.project.subcommands.add.error.projectContainsPublicApp`));
process.exit(EXIT_CODES.ERROR);
}
logger.log('');
logger.log(
i18n(`commands.project.subcommands.add.creatingComponent`, {
projectName: projectConfig.name,
})
);
logger.log('');
let latestRepoReleaseTag;
try {
// We want the tag_name from the latest release of the components repo
const repoReleaseData = await fetchReleaseData(
HUBSPOT_PROJECT_COMPONENTS_GITHUB_PATH
);
if (repoReleaseData) {
latestRepoReleaseTag = repoReleaseData.tag_name;
}
} catch (err) {
debugError(err);
}
if (!latestRepoReleaseTag) {
logger.error(i18n(`commands.project.subcommands.add.error.failedToFetchComponentList`));
process.exit(EXIT_CODES.ERROR);
}
const components =
await getProjectComponentListFromRepo(latestRepoReleaseTag);
if (!components.length) {
logger.error(i18n(`commands.project.subcommands.add.error.failedToFetchComponentList`));
process.exit(EXIT_CODES.ERROR);
}
const projectAddPromptResponse = await projectAddPrompt(components, args);
try {
const componentPath = path.join(
projectDir,
projectConfig.srcDir,
projectAddPromptResponse.componentTemplate.insertPath,
projectAddPromptResponse.name
);
await cloneGithubRepo(
HUBSPOT_PROJECT_COMPONENTS_GITHUB_PATH,
componentPath,
{
sourceDir: projectAddPromptResponse.componentTemplate.path,
tag: latestRepoReleaseTag,
hideLogs: true,
}
);
logger.log('');
logger.success(
i18n(`commands.project.subcommands.add.success`, {
componentName: projectAddPromptResponse.name,
})
);
} catch (error) {
debugError(error);
logger.error(i18n(`commands.project.subcommands.add.error.failedToDownloadComponent`));
process.exit(EXIT_CODES.ERROR);
}
process.exit(EXIT_CODES.SUCCESS);
}
function projectAddBuilder(yargs: Argv): Argv<ProjectAddArgs> {
yargs.options({
type: {
describe: i18n(`commands.project.subcommands.add.options.type.describe`),
type: 'string',
},
name: {
describe: i18n(`commands.project.subcommands.add.options.name.describe`),
type: 'string',
},
});
yargs.example([['$0 project add', i18n(`commands.project.subcommands.add.examples.default`)]]);
yargs.example([
[
'$0 project add --name="my-component" --type="components/example-app"',
i18n(`commands.project.subcommands.add.examples.withFlags`),
],
]);
return yargs as Argv<ProjectAddArgs>;
}
export const builder = makeYargsBuilder<ProjectAddArgs>(
projectAddBuilder,
command,
describe!
);
module.exports = {
command,
describe,
builder,
handler,
};