forked from jrsdev2024/structor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postexec.js
executable file
·120 lines (113 loc) · 4.09 KB
/
postexec.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
/*
* Copyright 2017 Alexander Pustovalov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var path = require('path');
var currentDir = process.cwd();
if (currentDir) {
currentDir = currentDir.replace(/\\/g, '/');
}
var pathParts = currentDir.split('/');
var PACKAGE_NAME = 'structor';
var NODE_DIR_NAME = 'node_modules';
var META_REPO_NAME = 'structor-meta';
var META_REPO_ARCHIVE_EXT = '.tar.gz';
var META_REPO_URL = 'https://github.com/ipselon/' + META_REPO_NAME;
var META_REPO_ARCHIVE_URL = META_REPO_URL + '/archive';
var questions = [
{
name: 'srcPath',
message: 'Specify a directory name where the source code of the generated components will be.\n ' +
'If the directory does not exist in the current project structure it will be created.\n',
default: 'app',
validate: (input) => /^\w+$/i.test(input) ? true : 'Alphanumeric value is acceptable only.'
}
];
var downloadController;
try{
downloadController = require(currentDir + '/server/structor/downloadManager.js');
} catch (e) {
// do nothing;
}
if (process.env.npm_config_global) {
console.error('Structor must be installed locally.');
} else if (downloadController) {
if (pathParts && pathParts.length > 0) {
var lastDir = pathParts[pathParts.length - 1];
var nodeDir = pathParts[pathParts.length - 2];
if (lastDir === PACKAGE_NAME && nodeDir === NODE_DIR_NAME) {
const packageVersion = process.env.npm_package_version;
const versionParts = packageVersion.split('-');
let packageFileName = packageVersion + META_REPO_ARCHIVE_EXT;
if (versionParts.length > 1) {
packageFileName = versionParts[1] + META_REPO_ARCHIVE_EXT;
}
const appDirPath = path.resolve(currentDir, '../..');
downloadController.checkMetaFolderVersion(appDirPath, packageVersion)
.then((isVersionMatch) => {
if (!isVersionMatch) {
downloadController.downloadMetaDistr(META_REPO_ARCHIVE_URL + '/' + packageFileName, appDirPath)
.then(paths => {
return downloadController.updateMetaFolder(paths.innerDirPath, appDirPath)
.then(() => {
return downloadController.removeFile(paths.tempDirPath);
});
})
.then(() => {
return downloadController.setMetaFolderVersion(appDirPath, packageVersion)
})
.then(() => {
console.log('Meta data folder is updated successfully to the version: ' + packageVersion);
})
.catch(error => {
console.error("Can not download " + META_REPO_ARCHIVE_URL + '/' + packageFileName, error);
});
}
})
.catch(error => {
downloadController.downloadMetaDistr(META_REPO_ARCHIVE_URL + '/' + packageFileName, appDirPath)
.then(paths => {
var inquirer = require('inquirer');
return inquirer.prompt(questions)
.then(answers => {
const options = {
srcPath: answers.srcPath,
};
return downloadController.createMetaFolder(paths.innerDirPath, appDirPath, options);
})
.then(() => {
return downloadController.setMetaFolderVersion(appDirPath, packageVersion)
})
.then(() => {
console.log('Meta data folder is created successfully. Version: ' + packageVersion);
})
.catch(error => {
console.error("Can not download " + META_REPO_ARCHIVE_URL + '/' + packageFileName, error);
})
.then(() => {
return downloadController.removeFile(paths.tempDirPath);
});
})
.catch(error => {
console.error(error);
});
});
} else {
// Installation of some local module.
// do nothing;
}
} else {
console.error('Can not parse current dir path.');
}
}