-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·205 lines (179 loc) · 6.18 KB
/
index.js
File metadata and controls
executable file
·205 lines (179 loc) · 6.18 KB
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
#!/usr/bin/env node
/* eslint-disable no-plusplus */
/* eslint-disable no-console */
import fs from 'node:fs';
import fse from 'fs-extra';
import path from 'node:path';
import {
bold, cyan, gray, green,
} from 'kleur/colors';
import prompts from 'prompts';
import replace from 'replace';
import { pkgManager, getPath } from './utils.js';
import samples from './samples.js';
const disclaimer = `
${bold(cyan('Welcome to Cpp.js!'))}
`;
const { version } = JSON.parse(fs.readFileSync(getPath('package.json'), 'utf-8'));
async function main() {
console.log(gray(`\ncreate-cppjs version ${version}`));
console.log(disclaimer);
let cwd = process.argv[2] || '.';
let name = process.argv[2] || null;
let selectedType = process.argv[3] || null;
let selectedPlatform = process.argv[4] || null;
let selectedBundler = process.argv[5] || null;
if (!name) {
({ name } = await prompts({
type: 'text',
name: 'name',
message: 'Project Name',
initial: 'sample',
validate: (value) => value && value.length > 0,
}, {
onCancel: () => {
process.exit(1);
},
}));
}
if (cwd === '.') {
const { dir } = await prompts({
type: 'text',
name: 'dir',
message: 'Where should we create your project?\n (leave blank to use current directory)',
initial: name,
validate: (value) => value && value.length > 0,
}, {
onCancel: () => {
process.exit(1);
},
});
if (dir) {
cwd = dir;
}
}
if (fs.existsSync(cwd)) {
if (fs.readdirSync(cwd).length > 0) {
const response = await prompts({
type: 'confirm',
name: 'value',
message: 'Directory not empty. Do you want to continue?',
initial: false,
}, {
onCancel: () => {
process.exit(1);
},
});
if (!response.value) {
process.exit(1);
}
}
}
if (!selectedType) {
({ templateType: selectedType } = await prompts({
type: 'select',
name: 'templateType',
message: 'Select a type:',
initial: false,
choices: Object.keys(samples).map((k) => ({ title: k, value: k })),
}, {
onCancel: () => {
process.exit(1);
},
}));
}
if (!selectedPlatform) {
({ appPlatformType: selectedPlatform } = await prompts({
type: 'select',
name: 'appPlatformType',
message: samples[selectedType].Questions[0],
initial: false,
choices: Object.keys(samples[selectedType]).slice(1).map((k) => ({ title: k, value: k })),
}, {
onCancel: () => {
process.exit(1);
},
}));
}
if (!samples[selectedType][selectedPlatform].path && !selectedBundler) {
({ bundler: selectedBundler } = await prompts({
type: 'select',
name: 'bundler',
message: samples[selectedType].Questions[1],
initial: false,
choices: Object.keys(samples[selectedType][selectedPlatform]).map((k) => ({ title: k, value: k })),
}, {
onCancel: () => {
process.exit(1);
},
}));
}
// Get the template object
let template;
if (selectedBundler) {
template = samples[selectedType][selectedPlatform][selectedBundler];
} else {
template = samples[selectedType][selectedPlatform];
}
// Check if multithread is available and ask user
let useMultithread = false;
if (template.multithreadPath) {
({ useMultithread } = await prompts({
type: 'confirm',
name: 'useMultithread',
message: 'Enable multithread support?',
initial: false,
}, {
onCancel: () => {
process.exit(1);
},
}));
}
// Determine the template path based on multithread selection
const templatePath = useMultithread && template.multithreadPath
? template.multithreadPath
: template.path;
fse.copySync(templatePath, cwd, { overwrite: true });
const packageJsonFilePath = `${cwd}/package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
packageJson.name = selectedType === 'app' ? name : `cppjs-lib-${name}`;
fs.writeFileSync(packageJsonFilePath, JSON.stringify(packageJson, null, 2));
const metroJsFilePath = `${cwd}/metro.config.js`;
if (fs.existsSync(metroJsFilePath)) {
replace({
regex: '^(.*?)Delete this line for create-cpp.js(.*?)$\n',
replacement: '',
paths: [metroJsFilePath], recursive: false, silent: true, multiline: true,
});
}
const cppJsFilePaths = [`${cwd}/cppjs.config.js`, `${cwd}/cppjs.config.mjs`];
cppJsFilePaths.forEach(cppJsFilePath => {
if (fs.existsSync(cppJsFilePath)) {
replace({
regex: '^(.*?)Delete this line for create-cpp.js(.*?)$\n',
replacement: '',
paths: [cppJsFilePath], recursive: false, silent: true, multiline: true,
});
}
});
const androidGradlewFilePath = `${cwd}/android/gradlew`;
if (fs.existsSync(androidGradlewFilePath)) {
fs.chmodSync(androidGradlewFilePath, 0o755);
}
console.log(bold(green('\nYour project is ready!')));
console.log('\nNext steps:');
let i = 1;
const relative = path.relative(process.cwd(), cwd);
if (relative !== '') {
console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`);
}
console.log(` ${i++}: ${bold(cyan(`${pkgManager} install`))}`);
if (selectedType === 'app') {
console.log(` ${i++}: ${bold(cyan(`${pkgManager} run dev`))}`);
}
if (selectedType === 'lib') {
console.log(` ${i++}: ${bold(cyan(`${pkgManager} run build`))}`);
}
console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`);
}
main();