-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharc_tools_project.js
executable file
·210 lines (165 loc) · 6.22 KB
/
arc_tools_project.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node
"use strict";
var toolName = "arc_project";
var projectFilename = "arctools.json";
var FS = require('fs');
var PATH = require('path');
var TOOLSLIB = require('./arc_tools_lib');
var FILELOADER = TOOLSLIB.jsrcFileLoaderSync;
var chalk = TOOLSLIB.chalk;
var theme = TOOLSLIB.clistyles;
var filters = {};
filters.projectConstruct = require('./arc_tools_project_construct');
filters.projectParse = require('./arc_tools_project_parse');
var arctoolsProjectData = undefined;
var exitCode = 0;
var exitProgram = false;
var errors = [];
var innerResponse;
console.log(TOOLSLIB.createToolBanner(toolName));
var program = TOOLSLIB.commander;
program.version(TOOLSLIB.meta.version).description("ARC tool project").option('--about', "Print tool information and exit.").option('-d, --directory <directory>', "Use <directory> to find " + projectFilename + ".").option('--initialize', "Initialize a new '" + projectFilename + "' file.").option('--verbose', "Log diagnostic and informational messages to console.").parse(process.argv);
if (program.about) {
console.log(theme.infoBody(JSON.stringify(TOOLSLIB.meta, undefined, 4)));
exitProgram = true;
}
while (!exitProgram && !errors.length) {
var filterDocTemplatePath = PATH.resolve(__dirname, 'templates', 'filter.hbs');
var loaderResponse = FILELOADER.request(filterDocTemplatePath);
if (loaderResponse.error) {
errors.unshift(loaderResponse.error);
break;
}
var projectDirectory = TOOLSLIB.paths.normalizePath(program.directory || "./");
var projectPath = undefined;
if (program.verbose) {
console.log("Searching for " + toolName + " project file '" + projectFilename + "'");
console.log("Initial search directory '" + projectDirectory + "'");
}
if (!FS.existsSync(projectDirectory)) {
errors.unshift("Bad search directory. Path '" + projectDirectory + "' does not exist.");
break;
}
if (!FS.statSync(projectDirectory).isDirectory()) {
errors.unshift("Bad search directory. Path '" + request_.parentDirectory + "' is not a directory.");
break;
}
if (program.verbose) {
console.log("Searching in and below:");
}
var innerResponse = TOOLSLIB.fileDirEnumSync.request({
directory: projectDirectory,
callback: function callback(file_) {
var parsePath = PATH.parse(file_);
var include = parsePath.base === projectFilename;
if (program.verbose) {
console.log("'" + file_ + "' " + include);
}
return include;
}
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
if (innerResponse.result.files.length) {
if (innerResponse.result.files.length > 1) {
errors.push("More than one '" + projectFilename + "' files located. Please constrain the search with the --directory option.");
console.log(theme.errorReportErrors(JSON.stringify(innerResponse.result.files, undefined, 4)));
break;
}
projectPath = innerResponse.result.files[0];
projectDirectory = PATH.parse(projectPath).dir;
if (program.verbose) {
console.log("Project directory and path reset below (subpath) of initial directory.");
}
} else {
if (!program.directory) {
if (program.verbose) {
console.log("Searching above:");
}
var seek = true;
var searchDirectory = TOOLSLIB.paths.normalizePath(projectDirectory + "./..");
while (seek) {
var seekProject = PATH.join(searchDirectory, projectFilename);
if (!FS.existsSync(seekProject)) {
if (program.verbose) {
console.log("'" + seekProject + "' false");
}
} else {
projectDirectory = searchDirectory;
projectPath = seekProject;
if (program.verbose) {
console.log("Found project above: '" + theme.dirInput(projectPath) + "'");
}
break;
}
if (searchDirectory === "/") {
if (program.verbose) {
console.log("We're at the root of the filesystem. Giving up.");
}
break;
}
searchDirectory = TOOLSLIB.paths.normalizePath(searchDirectory + "./..");
}
} else {
if (program.verbose) {
console.log("No check of parent directories for project JSON if --directories is specified.");
}
}
}
if (!projectPath) {
projectPath = TOOLSLIB.paths.normalizePath(PATH.join(projectDirectory, projectFilename));
if (program.verbose) {
console.log("Using default project JSON filename '" + projectPath + "'.");
}
}
if (program.initialize !== undefined) {
console.log(theme.processStepHeader("Attempting to initialize a new project..."));
if (FS.existsSync(projectPath)) {
errors.unshift("The target file '" + projectPath + "' already exists. If you really want to re-initialize, please manually remove/rename the existing file.");
break;
}
innerResponse = filters.projectConstruct.request();
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
arctoolsProjectData = innerResponse.result;
} else {
console.log(theme.processStepHeader("Attempting to open existing project..."));
console.log(theme.dirInput("'" + projectPath + "'"));
innerResponse = TOOLSLIB.jsrcFileLoaderSync.request(projectPath);
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
innerResponse = filters.projectParse.request(innerResponse.result.resource);
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
arctoolsProjectData = innerResponse.result;
}
arctoolsProjectData.projectState = arctoolsProjectData.projectState.toObject();
console.log("arctoolsProjectData = '" + JSON.stringify(arctoolsProjectData, undefined, 4));
innerResponse = TOOLSLIB.stringToFileSync.request({
resource: JSON.stringify(arctoolsProjectData, undefined, 4),
path: projectPath
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
console.log("Wrote '" + projectPath + "'.");
break;
}
;
if (errors.length) {
exitCode = 1;
console.log(theme.toolError(errors.join(" ")));
} else {
exitCode = 0;
}
console.log(theme.bannerExit(toolName + " exit with status ") + theme.exitCode(exitCode));
process.exit(exitCode);