-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (87 loc) · 3.37 KB
/
index.js
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
#!/usr/bin/env node
const fs = require('fs');
const readline = require('readline');
const { exec } = require('child_process');
const blacklist = [
'node_modules',
'package-lock.json',
'dist',
'.env',
]
let inputDir, globalDir;
if (process.env.npm_execpath) {
if (process.env.npm_execpath.includes('yarn')) {
globalDir = require('child_process').execSync('yarn global dir').toString().trim();
inputDir = `${globalDir}/node_modules/create-jeff-frontend/dist`;
} else if (process.env.npm_execpath.includes('npm')) {
globalDir = require('child_process').execSync('npm root -g').toString().trim();
inputDir = `${globalDir}/create-jeff-frontend/dist`;
}
} else {
inputDir = './dist';
}
// Recursively copy all files and directories from dist to output
function copyRecursive(src, dest) {
const files = fs.readdirSync(src);
for (const file of files) {
if (blacklist.includes(file)) {
continue;
}
const srcPath = `${src}/${file}`;
const destPath = `${dest}/${file}`;
const fileStat = fs.statSync(srcPath);
if (fileStat.isDirectory()) {
fs.mkdirSync(destPath, { recursive: true });
copyRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
async function main() {
// Replace {{this_name_will_be_replaced}} with provided app name in package.json
let appName = await askQuestion('Enter app name: ') || 'app-name';
appName = appName.replace(/\s+/g, '-').toLowerCase();
const outputDir = appName;
// Check if output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
copyRecursive(inputDir, outputDir);
// rename .env.example to .env
fs.renameSync(`${outputDir}/.env.example`, `${outputDir}/.env`);
// Rewrite package name
let packageJson = fs.readFileSync(`${outputDir}/package.json`, 'utf8');
packageJson = packageJson.replace('this_name_will_be_replaced', appName);
fs.writeFileSync(`${outputDir}/package.json`, packageJson);
// Replace {{VITE_STRAPI_REST_ENDPOINT}} in .env file
let VITE_STRAPI_REST_ENDPOINT = await askQuestion('Strapi backend location (default:http://localhost:1337): ')
VITE_STRAPI_REST_ENDPOINT = VITE_STRAPI_REST_ENDPOINT || 'http://localhost:1337';
envFile = fs.readFileSync(`${outputDir}/.env`, 'utf8');
envFile = envFile.replace('{{VITE_STRAPI_REST_ENDPOINT}}', VITE_STRAPI_REST_ENDPOINT);
fs.writeFileSync(`${outputDir}/.env`, envFile);
// rewrite _.gitignore to .gitignore
fs.renameSync(`${outputDir}/_.gitignore`, `${outputDir}/.gitignore`);
if (process.env.npm_execpath) {
if (process.env.npm_execpath.includes('yarn')) {
console.log('Installing dependencies, might take a few minutes')
exec(`cd ${outputDir} && yarn install`)
} else if (process.env.npm_execpath.includes('npm')) {
console.log('Installing dependencies, might take a few minutes')
exec(`cd ${outputDir} && npm install`)
}
}
rl.close()
}
main()