-
Notifications
You must be signed in to change notification settings - Fork 253
/
init.ts
174 lines (159 loc) · 5.64 KB
/
init.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
164
165
166
167
168
169
170
171
172
173
174
import { Command, flags } from '@oclif/command'
import chalk from 'chalk'
import ora from 'ora'
import path from 'path'
import toTitleCase from 'to-title-case'
import { autoPrompt } from '../lib/prompt'
import { generateSlug } from '../lib/slugs'
import { renderTemplates } from '../lib/templates'
import GenerateTypes from './generate/types'
export default class Init extends Command {
private spinner: ora.Ora = ora()
static description = `Scaffolds a new integration with a template. This does not register or deploy the integration.`
static examples = [
`$ ./bin/run init my-integration`,
`$ ./bin/run init my-integration --directory packages/destination-actions --template basic-auth`
]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
directory: flags.string({
char: 'd',
description: 'target directory to scaffold the integration',
default: './packages/destination-actions/src/destinations'
}),
name: flags.string({ char: 'n', description: 'name of the integration' }),
slug: flags.string({ char: 's', description: 'url-friendly slug of the integration' }),
template: flags.enum({
char: 't',
options: ['basic-auth', 'custom-auth', 'oauth2-auth', 'minimal'],
description: 'the template to use to scaffold your integration'
})
}
static args = [
{
name: 'path',
description: 'path to scaffold the integration'
}
]
parseFlags(): flags.Output {
return this.parse(Init)
}
async run() {
const { args, flags } = this.parseFlags()
const answers = await autoPrompt(flags, [
{
type: 'text',
name: 'name',
message: 'Integration name:',
format: (val) => toTitleCase(val)
},
{
type: 'text',
name: 'slug',
// @ts-ignore the types are wrong
initial: (prev) => generateSlug(`actions-${flags.name || prev}`),
message: 'Integration slug:',
format: (val) => generateSlug(val)
},
{
type: 'select',
name: 'template',
message: 'What template do you want to use?',
choices: [
{
title: 'Custom Auth',
description: 'Most "API Key" based integrations should use this.',
value: 'custom-auth'
},
{
title: 'Browser Destination',
description: 'Creates an Analytics JS compatible Destination.',
value: 'browser'
},
{
title: 'Basic Auth',
description: 'Integrations that use Basic Auth: https://tools.ietf.org/html/rfc7617',
value: 'basic-auth'
},
{
title: 'OAuth2 Auth',
description: 'Use for APIs that support OAuth2.',
value: 'oauth2-auth'
},
{
title: 'Audiences with OAuth2',
description: 'Creates an OAuth2 integration with Get/Create Audience methods',
value: 'audience-oauth2'
},
{
title: 'Audiences with Custom Auth',
description: 'Creates a Custom Auth integration with Get/Create Audience methods',
value: 'audience-custom-auth'
},
{
title: 'Minimal',
value: 'minimal'
}
],
initial: 0
}
])
const { name, slug, template } = answers
if (!name || !slug || !template) {
this.exit()
}
let directory = answers.directory
const isBrowserTemplate = template === 'browser'
if (isBrowserTemplate && directory === Init.flags.directory.default) {
directory = './packages/browser-destinations/destinations'
}
// For now, include the slug in the path, but when we support external repos, we'll have to change this
const slugWithoutActions = String(slug).replace('actions-', '')
const relativePath = path.join(directory, args.path || slugWithoutActions)
const targetDirectory = path.join(process.cwd(), relativePath)
const templatePath = path.join(__dirname, '../../templates/destinations', template)
const snapshotPath = path.join(__dirname, '../../templates/actions/snapshot')
const entryPath = isBrowserTemplate ? `${relativePath}/src/index.ts` : `${relativePath}/index.ts`
try {
this.spinner.start(`Creating ${chalk.bold(name)}`)
renderTemplates(templatePath, targetDirectory, { ...answers, slugWithoutActions })
this.spinner.succeed(`Scaffold integration`)
} catch (err) {
this.spinner.fail(`Scaffold integration: ${chalk.red(err.message)}`)
this.exit()
}
try {
this.spinner.start(chalk`Generating types for {magenta ${slug}} destination`)
await GenerateTypes.run(['--path', entryPath])
this.spinner.succeed()
} catch (err) {
this.spinner.fail(chalk`Generating types for {magenta ${slug}} destination: ${err.message}`)
}
if (!isBrowserTemplate) {
try {
this.spinner.start(`Creating snapshot tests for ${chalk.bold(slug)} destination`)
renderTemplates(
snapshotPath,
targetDirectory,
{
destination: slug
},
true
)
this.spinner.succeed(`Created snapshot tests for ${slug} destination`)
} catch (err) {
this.spinner.fail(`Snapshot test creation failed: ${chalk.red(err.message)}`)
this.exit()
}
}
this.log(chalk.green(`Done creating "${name}" 🎉`))
this.log(chalk.green(`Start coding! cd ${targetDirectory}`))
}
async catch(error: unknown) {
if (this.spinner?.isSpinning) {
this.spinner.fail()
}
throw error
}
}