-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
executable file
·111 lines (95 loc) · 3.04 KB
/
index.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
#!/usr/bin/env node
import * as fs from 'node:fs';
import * as path from 'node:path';
import { createOrEmptyTargetDirectory } from './utils/filesystem';
import { getInputs } from './utils/inputAll';
import { getLanguage } from './utils/localization';
import { logBanner, logDone, logStep } from './utils/logs';
import { RenderContext, getRenderContext } from './utils/renderContext';
import { renderTemplate } from './utils/renderTemplates';
import { generateKeypair, patchSolanaDependencies } from './utils/solanaCli';
import { detectAnchorVersion } from './utils/versionAnchor';
import { detectRustVersion } from './utils/versionRust';
import { detectSolanaVersion } from './utils/versionSolana';
import { Version } from './utils/versionCore';
(async function init() {
logBanner();
// Get arguments from CLI and prompt.
const language = getLanguage();
const inputs = await getInputs(language);
// Create or empty the target directory.
createOrEmptyTargetDirectory(
language,
inputs.targetDirectoryName,
inputs.shouldOverride
);
// Detect the Solana version.
const solanaVersionDetected = await logStep(
language.infos.detectSolanaVersion,
() => detectSolanaVersion(language)
);
// Detect the Rust version.
const rustVersionDetected = await logStep(
language.infos.detectRustVersion,
() => detectRustVersion()
);
// Detect the Anchor version.
let anchorVersionDetected: Version | undefined;
if (inputs.programFramework === 'anchor') {
anchorVersionDetected = await logStep(
language.infos.detectAnchorVersion,
() => detectAnchorVersion(language)
);
}
// Generate a keypair if needed.
const programAddress =
inputs.programAddress ??
(await logStep(language.infos.generateKeypair, () => {
const outfile = path.join(
process.cwd(),
inputs.targetDirectoryName,
'program',
'keypair.json'
);
return generateKeypair(language, outfile);
}));
// Get the args inputs, prompt inputs and computed values.
const ctx = getRenderContext({
language,
inputs,
programAddress,
solanaVersionDetected,
rustVersionDetected,
anchorVersionDetected,
});
// Render the templates.
await logStep(
language.infos.scaffold.replace(
'$targetDirectory',
inputs.targetDirectoryName
),
async () => {
renderTemplates(ctx);
await patchSolanaDependencies(ctx);
}
);
// Done.
logDone(ctx);
})().catch((e) => console.error(e));
function renderTemplates(ctx: RenderContext) {
const render = (templateName: string) => {
const directory = path.resolve(ctx.templateDirectory, templateName);
if (!fs.existsSync(directory)) return;
renderTemplate(ctx, directory, ctx.targetDirectory);
};
render('base');
render(`${ctx.programFramework}/base`);
if (ctx.clients.length > 0) {
render('clients/base');
render(`${ctx.programFramework}/clients/base`);
}
ctx.clients.forEach((client) => {
render(`clients/${client}`);
render(`${ctx.programFramework}/clients/${client}`);
});
}