-
-
Notifications
You must be signed in to change notification settings - Fork 997
/
init.ts
207 lines (190 loc) · 5.58 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
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
import chalk from 'chalk';
import execa from 'execa';
import path from 'node:path';
import {createYarnYmlFile} from './add-yarn2-support';
import {degit} from './degit';
import {getLatestRemotionVersion} from './latest-remotion-version';
import {Log} from './log';
import {openInEditorFlow} from './open-in-editor-flow';
import {patchPackageJson} from './patch-package-json';
import {patchReadmeMd} from './patch-readme';
import {
getDevCommand,
getPackageManagerVersionOrNull,
getRenderCommandForTemplate,
selectPackageManager,
} from './pkg-managers';
import {resolveProjectRoot} from './resolve-project-root';
import {selectTemplate} from './select-template';
import {yesOrNo} from './yesno';
const gitExists = (commandToCheck: string, argsToCheck: string[]) => {
try {
execa.sync(commandToCheck, argsToCheck);
return true;
} catch (err) {
return false;
}
};
export const checkGitAvailability = async (
cwd: string,
commandToCheck: string,
argsToCheck: string[],
): Promise<
| {type: 'no-git-repo'}
| {type: 'is-git-repo'; location: string}
| {type: 'git-not-installed'}
> => {
if (!gitExists(commandToCheck, argsToCheck)) {
return {type: 'git-not-installed'};
}
try {
const result = await execa('git', ['rev-parse', '--show-toplevel'], {
cwd,
});
return {type: 'is-git-repo', location: result.stdout};
} catch (e) {
return {
type: 'no-git-repo',
};
}
};
const getGitStatus = async (root: string): Promise<void> => {
// not in git tree, so let's init
try {
await execa('git', ['init'], {cwd: root});
await execa('git', ['add', '--all'], {cwd: root, stdio: 'ignore'});
await execa('git', ['commit', '-m', 'Create new Remotion video'], {
cwd: root,
stdio: 'ignore',
});
await execa('git', ['branch', '-M', 'main'], {
cwd: root,
stdio: 'ignore',
});
} catch (e) {
Log.error('Error creating git repository:', e);
Log.error('Project has been created nonetheless.');
// no-op -- this is just a convenience and we don't care if it fails
}
};
export const init = async () => {
const result = await checkGitAvailability(process.cwd(), 'git', [
'--version',
]);
if (result.type === 'git-not-installed') {
Log.error(
'Git is not installed or not in the path. Install Git to continue.',
);
process.exit(1);
}
if (result.type === 'is-git-repo') {
const should = await yesOrNo({
defaultValue: false,
question: `You are already inside a Git repo (${path.resolve(
result.location,
)}).\nThis might lead to a Git Submodule being created. Do you want to continue? (y/N):`,
});
if (!should) {
Log.error('Aborting.');
process.exit(1);
}
}
const [projectRoot, folderName] = await resolveProjectRoot();
const latestRemotionVersionPromise = getLatestRemotionVersion();
const selectedTemplate = await selectTemplate();
const pkgManager = selectPackageManager();
const pkgManagerVersion = await getPackageManagerVersionOrNull(pkgManager);
try {
await degit({
repoOrg: selectedTemplate.org,
repoName: selectedTemplate.repoName,
dest: projectRoot,
});
patchReadmeMd(projectRoot, pkgManager, selectedTemplate);
const latestVersion = await latestRemotionVersionPromise;
patchPackageJson({
projectRoot,
projectName: folderName,
latestRemotionVersion: latestVersion,
packageManager: pkgManagerVersion
? `${pkgManager}@${pkgManagerVersion}`
: null,
});
} catch (e) {
Log.error(e);
Log.error('Error with template cloning. Aborting');
process.exit(1);
}
Log.info(
`Copied ${chalk.blueBright(
selectedTemplate.shortName,
)} to ${chalk.blueBright(folderName)}. Installing dependencies...`,
);
createYarnYmlFile({
pkgManager,
pkgManagerVersion,
projectRoot,
});
if (pkgManager === 'yarn') {
Log.info('> yarn');
const promise = execa('yarn', [], {
cwd: projectRoot,
stdio: 'inherit',
env: {...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1'},
});
promise.stderr?.pipe(process.stderr);
promise.stdout?.pipe(process.stdout);
await promise;
} else if (pkgManager === 'pnpm') {
Log.info('> pnpm i');
const promise = execa('pnpm', ['i'], {
cwd: projectRoot,
stdio: 'inherit',
env: {...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1'},
});
promise.stderr?.pipe(process.stderr);
promise.stdout?.pipe(process.stdout);
await promise;
} else if (pkgManager === 'bun') {
Log.info('> bun install');
const promise = execa('bun', ['install'], {
cwd: projectRoot,
stdio: 'inherit',
env: {...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1'},
});
promise.stderr?.pipe(process.stderr);
promise.stdout?.pipe(process.stdout);
await promise;
} else {
Log.info('> npm install');
const promise = execa('npm', ['install', '--no-fund', '--no-audit'], {
stdio: 'inherit',
cwd: projectRoot,
env: {...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1'},
});
promise.stderr?.pipe(process.stderr);
promise.stdout?.pipe(process.stdout);
await promise;
}
await getGitStatus(projectRoot);
Log.info();
Log.info(`Welcome to ${chalk.blueBright('Remotion')}!`);
Log.info(
`✨ Your video has been created at ${chalk.blueBright(folderName)}.`,
);
await openInEditorFlow(projectRoot);
Log.info('Get started by running');
Log.info(chalk.blueBright(`cd ${folderName}`));
Log.info(chalk.blueBright(getDevCommand(pkgManager, selectedTemplate)));
Log.info('');
Log.info('To render a video, run');
Log.info(
chalk.blueBright(getRenderCommandForTemplate(pkgManager, selectedTemplate)),
);
Log.info('');
Log.info(
'Docs to get you started:',
chalk.underline('https://www.remotion.dev/docs/the-fundamentals'),
);
Log.info('Enjoy Remotion!');
};