-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitTestProject.js
More file actions
91 lines (77 loc) · 3.45 KB
/
initTestProject.js
File metadata and controls
91 lines (77 loc) · 3.45 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
import { init } from './scripts/init.js';
import { execSync } from 'child_process';
import { join, resolve } from 'path/posix';
import { rmSync, existsSync, readFileSync, writeFileSync, cpSync } from 'fs';
import { packageName } from './scripts/config.js';
import chalk from 'chalk';
const projectFolder = 'simple-reporting-library-test-project';
const parentPath = resolve(process.cwd(), '..');
const projectPath = join(parentPath, projectFolder);
const packageJsonPath = join(projectPath, 'package.json');
const testDataPath = resolve(process.cwd(), 'test-data');
const publicPath = join(projectPath, 'public');
const msg = {
info: (text) => chalk.black.bgYellowBright.bold(` INFO: ${text} `),
action: (text) => chalk.black.bgBlueBright.bold(` ACTION: ${text} `),
error: (text) => chalk.black.bgRedBright.bold(` ERROR: ${text} `),
success: (text) => chalk.black.bgGreenBright.bold(` SUCCESS: ${text} `),
};
if (existsSync(projectPath)) {
console.log(msg.info(`Deleting folder: ${projectFolder}`));
rmSync(projectPath, { recursive: true, force: true });
}
process.chdir(parentPath);
console.log(msg.action(`Initializing project in ${projectFolder}...`));
init(projectFolder)
.then(() => {
console.log(msg.info(`Project ${projectFolder} initialized successfully.`));
if (existsSync(testDataPath)) {
console.log(msg.action('Copying test-data to public folder...'));
cpSync(testDataPath, publicPath, { recursive: true, force: true });
console.log(msg.success('test-data copied successfully.'));
} else {
console.log(msg.info('No test-data folder found.'));
}
if (existsSync(packageJsonPath)) {
console.log(msg.action('Updating package.json...'));
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
if (packageJson.dependencies && packageJson.dependencies[packageName]) {
packageJson.dependencies[packageName] =
'file:../simple-reporting-library';
writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2),
'utf-8',
);
console.log(msg.success(`Updated ${packageName} in package.json.`));
} else {
console.log(msg.error(`${packageName} not found in devDependencies.`));
}
} else {
console.error(msg.error('package.json not found.'));
}
console.log(msg.action('Initializing a new Git repository...'));
execSync('git init', { cwd: projectPath, stdio: 'inherit' });
console.log(msg.success('Git repository initialized.'));
console.log(msg.action('Running npm install...'));
execSync('npm install', { cwd: projectPath, stdio: 'inherit' });
console.log(msg.success('npm install completed.'));
console.log(msg.action('Adding all files to Git...'));
execSync('git add .', { cwd: projectPath, stdio: 'inherit' });
console.log(msg.success('All files added to Git.'));
console.log(msg.action('Creating initial commit...'));
execSync('git commit -m "init"', { cwd: projectPath, stdio: 'inherit' });
console.log(msg.success('Initial commit created.'));
console.log('');
console.log(
msg.success(`
---------------------------------------------------------------------
Project initialization completed successfully.
You can now navigate to the project folder:
${projectPath}
---------------------------------------------------------------------`),
);
})
.catch((error) => {
console.error(msg.error('Error initializing project:'), error);
});