-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-odd-app.ts
256 lines (225 loc) · 6.86 KB
/
create-odd-app.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import retry from 'async-retry'
import chalk from 'chalk'
import fs from 'fs'
import path from 'path'
import { ORANGE } from './helpers/colours'
import makeDir from './helpers/make-dir'
import {
tryGitInit,
downloadAndExtractRepo,
getRepoInfo,
hasRepo,
RepoInfo,
} from './helpers/git'
import install from './helpers/install'
import isFolderEmpty from './helpers/is-folder-empty'
import getOnline from './helpers/is-online'
import isWriteable from './helpers/is-writeable'
import { writeAppInfo, type AppInfo } from './helpers/set-app-info'
import { switchToJavaScript } from './helpers/set-typescript'
import type { AuthFlow } from './helpers/set-auth-flow'
import type { Framework } from './helpers/set-framework'
import type { PackageManager } from './helpers/get-pkg-manager'
export class DownloadError extends Error {}
type Options = {
appInfo?: AppInfo
appPath: string
authFlow: AuthFlow
framework: Framework
packageManager: PackageManager
removeTypescript?: boolean
}
type ReposType = {
[authFlow: string]: {
[framework: string]: string
}
}
const ODD_EXAMPLES_URL = 'https://github.com/oddsdk/'
const Repos: ReposType = {
webcrypto: {
react: `${ODD_EXAMPLES_URL}odd-app-template-react`,
sveltekit: `${ODD_EXAMPLES_URL}odd-app-template`,
},
walletauth: {
react: `${ODD_EXAMPLES_URL}walletauth-react`,
sveltekit: `${ODD_EXAMPLES_URL}walletauth`,
},
};
/**
* Kick off the app creation using the selection options passed in by the user
*
* @param Options
*/
const createODDApp = async ({
appInfo,
appPath,
authFlow,
framework,
packageManager,
removeTypescript,
}: Options): Promise<void> => {
let repoInfo: RepoInfo | undefined
let repoUrl: URL | undefined
if (framework && authFlow) {
try {
repoUrl = new URL(Repos[authFlow][framework])
} catch (error: any) {
if (error.code !== 'ERR_INVALID_URL') {
console.error(error)
process.exit(1)
}
}
if (repoUrl) {
if (repoUrl.origin !== 'https://github.com') {
console.error(
`Invalid URL: ${chalk.red(
`"${repoUrl}"`
)}. Only GitHub repositories are supported. Please use a GitHub URL and try again.`
);
process.exit(1)
}
repoInfo = await getRepoInfo(repoUrl)
if (!repoInfo) {
console.error(
`Found invalid GitHub URL: ${chalk.red(
`"${repoUrl}"`
)}. Please fix the URL and try again.`
);
process.exit(1);
}
const found = await hasRepo(repoInfo);
if (!found) {
console.error(
`Could not locate the repository for ${chalk.red(
`"${repoUrl}"`
)}. Please check that the repository exists and try again.`
);
process.exit(1);
}
}
}
const root = path.resolve(appPath);
if (!(await isWriteable(path.dirname(root)))) {
console.error(
"The application path is not writable, please check folder permissions and try again."
);
console.error(
"It is likely you do not have write permissions for this folder."
);
process.exit(1);
}
const appName = path.basename(root);
await makeDir(root);
if (!isFolderEmpty(root, appName)) {
process.exit(1);
}
const useYarn = packageManager === "yarn";
const isOnline = !useYarn || (await getOnline());
const originalDirectory = process.cwd();
console.log();
console.log(`Creating a new ODD app in ${chalk.green(root)}.`);
console.log();
process.chdir(root);
const packageJsonPath = path.join(root, "package.json");
let hasPackageJson = false;
if (repoInfo && repoUrl) {
/**
* Clone the repo if it exists
*/
try {
console.log(
`Downloading files from repo ${chalk.green(
`${repoUrl}`,
)}. This might take a moment.`,
)
console.log()
const repoInfo2 = repoInfo
await retry(() => downloadAndExtractRepo(root, repoInfo2), {
// @ts-ignore-next-line
retries: 3,
})
} catch (reason) {
function isErrorLike(err: unknown): err is { message: string } {
return (
typeof err === 'object' &&
err !== null &&
typeof (err as { message?: unknown }).message === 'string'
)
}
throw new DownloadError(
isErrorLike(reason) ? reason.message : reason + '',
)
}
// Write app-info.ts values
if (appInfo) {
await writeAppInfo({ appInfo, authFlow, framework, root })
}
// Conver TS project to JS
if (removeTypescript) {
await switchToJavaScript({ framework, root })
}
hasPackageJson = fs.existsSync(packageJsonPath)
if (hasPackageJson) {
console.log()
console.log('Installing packages. This might take a couple of minutes...')
console.log()
await install(root, null, { packageManager, isOnline })
}
}
if (tryGitInit(root)) {
console.log("Initialized a git repository.");
console.log();
}
let cdpath: string;
if (path.join(originalDirectory, appName) === appPath) {
cdpath = appName;
} else {
cdpath = appPath;
}
console.log()
console.log(
`${chalk.hex(ORANGE)(` %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@% %@@@@@@% %@@@@@@@% %@@@@@
@@@@@ @@@@@% @@@@@@ @@@@@
@@@@@% @@@@@ %@@@@@ %@@@@@
@@@@@@% @@@@@ %@@% @@@@@ %@@@@@@
@@@@@@@ @@@@@ %@@@@% @@@@@ @@@@@@@
@@@@@@@ @@@@% @@@@@@ @@@@@ @@@@@@@
@@@@@@@ %@@@@ @@@@@@ @@@@@% @@@@@@@
@@@@@@@ @@@@@ @@@@@@ %@@@@@ @@@@@@@
@@@@@@@ @@@@@@@@@@@@@@@@ @@@@@ @@@@@@@
@@@@@@@ %@@@@@@@@@@@@@@@ @@@@% @@@@@@@
@@@@@@@ %@@% @@@@@@ %@@% @@@@@@@
@@@@@@@ @@@@@@ @@@@@@@
@@@@@@@% %@@@@@@% %@@@@@@@
@@@@@@@@@% %@@@@@@@@@@% %@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@% `)}`,
)
console.log()
console.log()
console.log(`${chalk.green("Success!")} Created ${chalk.green(appName)} at ${chalk.green(appPath)}`);
if (hasPackageJson) {
console.log("Inside that directory, you can run several commands:");
console.log();
console.log(` ${packageManager} ${useYarn ? "" : "run "}dev`);
console.log(" Starts the development server.");
console.log();
console.log(
` ${packageManager} ${useYarn ? "" : "run "}build`
);
console.log(" Builds the app for production.");
console.log();
console.log("We suggest you begin by typing:");
console.log();
console.log(` cd ${chalk.green(cdpath)}`);
console.log(
` ${packageManager} ${useYarn ? "" : "run "}dev`
);
}
console.log();
};
export default createODDApp