forked from kordwarshuis/spec-up-t-starter-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-spec-up-t-starterpack.js
73 lines (58 loc) · 2.46 KB
/
create-spec-up-t-starterpack.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
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const { setupCompleteMessage, errorDirExistsMessage } = require('./messages.js');
// Main setup function
async function setupSpecPack(dirName) {
const sourceDir = path.join(__dirname, 'spec-up-t-starterpack');
const targetDir = path.join(process.cwd(), dirName);
try {
if (fs.existsSync(targetDir)) {
console.error(`${errorDirExistsMessage[0]}${dirName}${errorDirExistsMessage[1]}`);
process.exit(1);
}
// Copying starter pack files
await copyStarterPack(sourceDir, targetDir);
await renameGitignore(targetDir);
await updateReadme(dirName, targetDir);
console.log(`${setupCompleteMessage[0]}`);
// Run git init
console.log(`Initialize git repository`);
execSync(`git init`, { cwd: targetDir, stdio: 'inherit' });
console.log(`Git repository initialized`);
// Install dependencies with npm
console.log(`Using npm to install dependencies.`);
// Suppress npm audit and fund messages in the project-specific .npmrc
const npmrcPath = path.join(targetDir, '.npmrc');
await fs.writeFile(npmrcPath, 'audit=false\nfund=false\n', 'utf8');
// Run npm install
// execSync(`npm install`, { cwd: targetDir, stdio: 'inherit' });
execSync(`npm install --silent`, { cwd: targetDir, stdio: 'inherit' });
} catch (err) {
console.error('Error during setup:', err);
process.exit(1);
}
}
// Helper function to copy files
async function copyStarterPack(source, target) {
await fs.copy(source, target);
}
// Helper function to rename gitignore
async function renameGitignore(targetDir) {
const gitignorePath = path.join(targetDir, 'gitignore');
const dotGitignorePath = path.join(targetDir, '.gitignore');
if (await fs.existsSync(gitignorePath)) {
await fs.rename(gitignorePath, dotGitignorePath);
}
}
// Helper function to update README
async function updateReadme(dirName, targetDir) {
const readmeFile = path.join(targetDir, 'README.md');
const data = await fs.readFile(readmeFile, 'utf8');
const result = data.replace(/spec-up-t-starterpack/g, dirName);
await fs.writeFile(readmeFile, result, 'utf8');
}
// Start setup process with provided directory name
const dirName = process.argv[2] || 'spec-up-t-starterpack';
setupSpecPack(dirName);