-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.js
124 lines (107 loc) · 4.59 KB
/
setup.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
// latest versions, download links, and target directories to extract
var resources = {
httpd: {
version: '2.4.62',
url: 'https://www.apachelounge.com/download/VS17/binaries/httpd-2.4.62-240718-win64-VS17.zip',
subfolder: 'Apache24'
},
mysql: {
version: '8.0.39',
url: 'https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.39-winx64.zip',
subfolder: 'mysql-8.0.39-winx64'
},
php: {
version: '8.3.9',
url: 'https://windows.php.net/downloads/releases/archives/php-8.3.9-Win32-vs16-x64.zip'
}
};
try {
main();
} catch(e) {
WScript.Echo('ERROR: ' + e);
for (var key in e) {
WScript.Echo(key + ": " + e[key])
}
} finally {
WScript.Quit(0);
}
function main() {
var fileSystem = WScript.CreateObject('Scripting.FileSystemObject');
var shell = WScript.CreateObject('WScript.Shell');
for (var resourceName in resources) {
var resource = resources[resourceName];
var filename = resource.url.split('/').pop();
var archivePath = fileSystem.GetAbsolutePathName('environment\\archive\\' + filename);
var configPath = fileSystem.GetAbsolutePathName('default-configuration\\' + resourceName);
var targetPath = fileSystem.GetAbsolutePathName('environment\\' + resourceName);
// download and extract resources to their respective directories
if (!fileSystem.FileExists(archivePath)) {
WScript.Echo("Downloading " + resource.url);
downloadFile(resource.url, archivePath);
}
// extract the specified directory within each resource to the target path
WScript.Echo("Extracting " + archivePath + " to " + targetPath);
copyFiles(archivePath, targetPath, {subfolder: resource.subfolder});
// copy configuration to the target path
WScript.Echo("Copying configuration to " + targetPath + "\n");
copyFiles(configPath, targetPath);
}
// initialize mysql
WScript.Echo("Initializing MySQL\n");
shell.Exec('.\\environment\\mysql\\bin\\mysqld.exe --default-authentication-plugin=mysql_native_password --initialize-insecure');
// download composer
WScript.Echo("Downloading Composer\n");
downloadFile('https://getcomposer.org/composer-stable.phar', 'environment\\bin\\composer.php');
WScript.Echo("Setup is complete. Please set a password for the MySQL root user.\n");
WScript.Echo("Next steps:");
WScript.Echo("- Start HTTPD: start_httpd.bat");
WScript.Echo("- Start MySQL Daemon: start_mysqld.bat");
WScript.Echo("- Start MySQL Client (optional): start_mysql_client.bat");
WScript.Echo("- Start Shell w/ CLI tools (composer, php, mysql, etc): shell.bat");
}
/**
* Copies/extracts the contents of a source folder to a target folder
* @param {String} source - Absolute path to the source folder
* @param {String} target - Absolute path to the target folder
* @param {{subfolder: String}} options - The root directory is often duplicated
* within archived folders. The subfolder option allows us to specify a subfolder
* from which to copy items.
*/
function copyFiles(source, target, options) {
// Ensure target path exists
var fileSystem = WScript.CreateObject('Scripting.FileSystemObject')
if (!fileSystem.FolderExists(target))
fileSystem.CreateFolder(target);
// Use Shell.Application instead of Scripting.FileSystemObject to support zip archives
var shell = WScript.CreateObject('Shell.Application');
var sourceFolder = shell.NameSpace(source);
var targetFolder = shell.NameSpace(target);
// The subfolder option is used to to copy items
// from a subfolder within a zip archive
var items = (options && options.subfolder)
? sourceFolder.ParseName(options.subfolder).GetFolder.Items()
: sourceFolder.Items();
// 4: Do not display a progress dialog box
// 16: Respond with "Yes to All" for any dialog box that is displayed
targetFolder.CopyHere(items, 4 | 16);
}
/**
* Downloads a file to the specified location
* @param {String} url - URL of resource to download
* @param {String} filepath - Target filepath for downloaded resource
*/
function downloadFile(url, filepath) {
var request = WScript.CreateObject('WinHttp.WinHttpRequest.5.1');
var stream = WScript.CreateObject('ADODB.Stream');
// send download request
request.Open('GET', url, true);
request.Send();
request.WaitForResponse();
// write response to file
stream.Open();
stream.Type = 1;
stream.Write(request.ResponseBody);
stream.Position = 0;
stream.SaveToFile(filepath, 2);
stream.Close();
}