-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·137 lines (110 loc) · 4.14 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
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
#!/usr/bin/env node
'use strict';
var Command = require('commander').Command;
var simpleGit = require('simple-git');
var path = require('path');
var fs = require('fs-extra');
var spawn = require('child_process').spawn;
var program = new Command();
var git = simpleGit();
var pkg = require('./package.json');
var templateRepoUrl = 'https://github.com/hydra-js/hello-world.git';
program.name('hydra').description(pkg.description).version(pkg.version);
program
.command('create [namespace]')
.description('Initialize a Hydra App')
.option('-f, --force', 'Force creation even if directory exists')
.action(function (namespace, options) {
namespace = namespace || 'my-hydra-app';
console.log('Creating a new Hydra app...');
// Resolve paths
var tempRepoPath = path.join(process.cwd(), namespace, '__hydra');
var appPath = path.join(process.cwd(), namespace);
if (fs.existsSync(appPath) && !options.force) {
console.error(
'Error: Directory ' +
namespace +
' already exists. Use --force to overwrite.'
);
process.exit(1);
}
if (fs.existsSync(appPath) && options.force) {
console.log('Directory ' + namespace + ' already exists. Overwriting...');
fs.removeSync(appPath);
}
git
.clone(templateRepoUrl, tempRepoPath)
.then(function () {
console.log('Repository cloned successfully.');
var sourcePath = tempRepoPath;
if (!fs.existsSync(sourcePath)) {
throw new Error(tempRepoPath + ' does not exist in the repository.');
}
console.log('Copying files to ' + namespace + '...');
return fs.copy(sourcePath, appPath);
})
.then(function () {
console.log('File structure created successfully.');
/**
* @TODO: Make necessary changes
* - Improve logs
* -- Show progress
* -- Show next steps
* - Create .env file
* - Install dependencies
*/
console.log('Cleaning up temporary files...');
return fs.remove(tempRepoPath);
})
.then(function () {
console.log('Cleanup completed.');
console.log('Project ' + namespace + ' generated successfully.');
})
.catch(function (err) {
console.error('Failed to generate project:', err);
fs.remove(tempRepoPath);
process.exit(1);
});
});
program
.command('serve')
.description('Start the Hydra server')
.option('-s, --script <script>', 'Specify the npm script to run', 'start')
.option('-d, --dev', 'Run in development mode')
.action(function (options) {
// Validate if the current directory is a Hydra app
if (!fs.existsSync('package.json')) {
console.error('Error: package.json not found. Are you in the Application root?');
process.exit(1);
}
// Read package.json to check for necessary scripts and dependencies
var packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
// Determine which script to run
var scriptToRun = options.dev ? 'dev' : options.script;
// Check if the specified script exists
if (!packageJson.scripts || !packageJson.scripts[scriptToRun]) {
console.error('Error: Script \'' + scriptToRun + '\' not found in package.json');
process.exit(1);
}
// Check for essential dependencies
var requiredDeps = ['@hydra-js/core'];
var missingDeps = requiredDeps.filter(function(dep) {
return !packageJson.dependencies || !packageJson.dependencies[dep];
});
if (missingDeps.length > 0) {
console.error('Error: Missing essential dependencies: ' + missingDeps.join(', '));
process.exit(1);
}
console.log('Application integrity check passed.');
console.log('Running npm script: ' + scriptToRun);
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
var child = spawn(npm, ['run', scriptToRun], { stdio: 'inherit' });
child.on('close', function(code) {
console.log('npm script exited with code ' + code);
});
child.on('error', function(err) {
console.error('Failed to start npm script:', err);
process.exit(1);
});
});
program.parse(process.argv);