-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·185 lines (138 loc) · 6.09 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
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
#!/usr/bin/env node
"use strict";
var shell = require("shelljs");
var path = require("path");
var fs = require("fs");
var modulePathPrefix = 'node_modules/frontend-dependencies/tmp';
shell.config.fatal = true;
module.exports = frontendDependencies;
if (require.main === module) frontendDependencies();
// main function
function frontendDependencies(workDir) {
// prepare everything
workDir = workDir || process.cwd();
var packageJson = getAndValidatePackageJson(workDir);
var packages = packageJson.frontendDependencies.packages || {};
// install all packages via npm
var npmPackageList = "";
for (var pkgName in packages) {
var pkg = packages[pkgName];
npmPackageList += getNpmPackageString(pkg, pkgName);
}
// npm install options:
// * --no-save: ignore automatic dependencies adding (since npm 5) to the package.json on "npm i"
// * --production: do not install dev dependencies as we need only some files from the npm module folders itself.
// actually we also do not need any regular dependencies, but there is currently no option to disable that
// * --no-fund: hide funding message
// * --prefix folderPath: store dependencies in a separate folder, so there will be no interference between the
// main "npm install" and the one from the frontendDependencies
var npmInstallCommand = 'npm i --no-save --no-optional --production --no-fund --prefix ' + modulePathPrefix + ' ' + npmPackageList;
log('build the "npm install" command: ' + npmInstallCommand)
log('installing ...')
try {
shell.mkdir('-p', modulePathPrefix);
fs.writeFileSync(modulePathPrefix + '/package.json', '{"description": "_", "repository": "_", "license": "UNLICENSED"}');
shell.exec(npmInstallCommand);
} catch (err) {
fail(err);
}
// copy new installed modules from tmp folder back to node_modules
// this allows us to use them also in the backend
var frontendSrcPath = path.join(workDir, modulePathPrefix, "node_modules/*");
var nodeModulePath = path.join(workDir, "node_modules/");
log("copy " + frontendSrcPath + " to " + nodeModulePath);
shell.cp("-r", frontendSrcPath, nodeModulePath);
log("copy all specified files");
for (var pkgName in packages) {
var pkg = packages[pkgName];
// process further options
var namespaced = pkg.namespaced || false;
// all files of package are copied (src not defined)
// => prevent namespace errors by creating a subfolder
if (!pkg.hasOwnProperty('namespaced') && !pkg.hasOwnProperty('src')){
namespaced = true
}
// prepare folder pathes
var modulePath = getAndValidateModulePath(workDir, pkgName)
var sourceFilesPath = path.join(modulePath, pkg.src || "/*");
// eg.: /opt/myProject/node_modules/jquery/dist/*
// eg.: /opt/myProject/node_modules/jquery/dist/{file1,file2}
var targetPath = getAndValidateTargetPath(pkg, packageJson, workDir, pkgName);
copyFiles(sourceFilesPath, targetPath, pkgName, namespaced);
}
}
// helper functions
function getAndValidatePackageJson(workDir){
var pkgJson = require(path.join(workDir, "package.json"));
var fd = pkgJson.frontendDependencies;
if (!fd) fail("No 'frontendDependencies' key in package.json");
if (!fd.packages) fail("No 'frontendDependencies.packages' in package.json");
// maybe remove this code in later versions
if (fd.packages.constructor === Array) {
fail("Update your package.json frontendDependencies format to > 1.0.0 syntax as explained at https://github.com/msurdi/frontend-dependencies");
}
return pkgJson;
}
function getNpmPackageString(pkg, pkgName){
// list of npm commands: https://docs.npmjs.com/cli/install
if (pkg.url) {
/* install via url
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <githubname>/<githubrepo>[#<commit-ish>]
npm install github:<githubname>/<githubrepo>[#<commit-ish>]
npm install gist:[<githubname>/]<gistID>[#<commit-ish>]
npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]
npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]
*/
return pkg.url + " ";
} else { // pkg.version might be present or not
/* install via package name
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
*/
if (pkg.version) pkgName += ('@"' + pkg.version + '"');
if (typeof pkg === 'string') pkgName += ('@"' + pkg + '"');
return pkgName + " ";
}
}
function getAndValidateModulePath(workDir, pkgName){
var mdPath = path.join(workDir, "node_modules/", pkgName);
if (!shell.test("-d", mdPath)) fail("Module not found or not a directory: " + mdPath);
return mdPath
// eg.: /opt/myProject/node_modules/jquery
}
function getAndValidateTargetPath(pkg, packageJson, workDir, pkgName){
var tarPath = null
if (pkg.hasOwnProperty('target')) { // specific target?
tarPath = pkg.target
} else { // or try default path
if (!packageJson.frontendDependencies.target) {
fail("No 'frontendDependencies.target' key in package.json");
}
tarPath = packageJson.frontendDependencies.target;
}
return path.join(workDir, tarPath);
// eg.: /opt/myProject/build/static
}
function copyFiles (sourceFilesPath, targetPath, pkgName, namespaced){
// put target into a subfolder with package name?
if (namespaced) targetPath = path.join(targetPath, pkgName);
shell.mkdir("-p", targetPath);
log("copy " + pkgName + " to " + targetPath);
shell.cp("-r", sourceFilesPath, targetPath);
}
function fail(reason) {
console.log(reason);
process.exit(1);
}
function log(message) {
var blue = '\x1b[34m';
var black = '\x1b[0m';
console.log(blue, '[frontend-deps]: ' + message, black)
}