forked from Atyantik/pawjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
executable file
·181 lines (161 loc) · 6.07 KB
/
scripts.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
#! /usr/bin/env node
require("babel-register");
// Required Libraries
const parseArgs = require("minimist");
const _ = require("lodash");
const nodemon = require("nodemon");
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const shell = require("shelljs");
const currentDir = process.cwd();
process.env.__p_root = currentDir + path.sep;
process.env.__c_root = __dirname;
const args = parseArgs(process.argv.slice(2));
const allCommands = args["_"];
let userCommand = "start";
if (allCommands.length) {
userCommand = _.first(allCommands);
}
// Disable babel cache
process.env.BABEL_DISABLE_CACHE = 1;
let allExecutablePaths = process.env.PATH.split(path.delimiter);
// Add current folder to executable path
allExecutablePaths.unshift(__dirname);
// Include current folder bin and node_modules's bin
allExecutablePaths.unshift(path.join(__dirname, ".bin"));
allExecutablePaths.unshift(path.join(__dirname, "node_modules", ".bin"));
// Find command from all the paths possible
function findCommandPath(com) {
let execPath = "";
const possibleExtension = ["", ".exe", ".cmd"];
_.each(allExecutablePaths, executablePath => {
if (execPath.length) return;
_.each(possibleExtension, ext => {
if (execPath.length) return;
const extendedPath = path.join(executablePath, `${com}${ext}`);
if (fs.existsSync(extendedPath)) {
execPath = extendedPath;
}
});
});
if (!execPath.length) throw new Error(`Cannot find command ${com}.`);
return execPath;
}
const lint = () => {
if (!path.resolve(`${process.env.__p_root}.eslintrc`)) {
// eslint-disable-next-line
console.log("Cannot find .eslintrc in root folder! Thus nothing to lint...");
return { code: 0, stdout: "", };
}
return shell.exec(`${findCommandPath("eslint")} -c ${process.env.__p_root}.eslintrc --ignore-path ${process.env.__p_root}.gitignore ${process.env.__p_root}src`);
};
const test = () => {
return shell.exec(`${findCommandPath("cross-env")} NODE_ENV=test ${findCommandPath("mocha")} --require babel-core/register "${process.env.__p_root}src/**/*.test.js"`);
};
switch(userCommand) {
case "clear-src": {
shell.rm("-rf", "src");
shell.mkdir("src");
break;
}
case "start": {
process.env.NODE_ENV = process.env.NODE_ENV || "development";
nodemon({
script: path.join(currentDir, "src", "server.js"),
verbose: true,
execMap: {
"js": findCommandPath("babel-node")
},
"watch": [
path.join(currentDir, "src", "server.js"),
]
});
break;
}
case "build": {
process.env.NODE_ENV = process.env.NODE_ENV || "production";
const prodClientConfig = require(path.resolve(path.join(__dirname, "src", "webpack", "config", "prod.client.babel.js"))).default;
// eslint-disable-next-line
webpack(prodClientConfig, (clientErr, clientStats) => {
if (!clientErr) {
const prodServerConfig = require(path.resolve(path.join(__dirname, "src", "webpack", "config", "prod.server.babel.js"))).default;
// eslint-disable-next-line
webpack(prodServerConfig, (serverErr, serverStats) => {
if (serverErr) {
// eslint-disable-next-line
console.log(serverErr);
}
});
} else {
// eslint-disable-next-line
console.log(clientErr);
}
});
break;
}
case "lib:build": {
// eslint-disable-next-line
console.log(shell.exec("npm run build -- --source-maps --watch && npm run copy-babel-to-src").stdout);
break;
}
case "copy-babel-to-src": {
shell.rm("-rf", `src${path.sep}babel`);
shell.cp("-R", `lib${path.sep}babel`, "src");
break;
}
case "lint": {
const result = lint();
return process.exit(result.code);
}
case "test": {
// try lint and then complete the test
const lintResults = lint();
if (lintResults.code) {
return process.exit(lintResults.code);
}
const testResults = test();
return process.exit(testResults.code);
}
case "docker": {
process.env.NODE_ENV = process.env.NODE_ENV || "development";
if(!shell.which("docker")) {
// eslint-disable-next-line
console.log("command: 'docker' not found. Please install docker and try again. https://www.docker.com/");
return process.exit(1);
}
// If .dockerignore does not exists in project root, then create it
if (!fs.existsSync(path.join(process.env.__p_root, ".dockerignore"))) {
// eslint-disable-next-line
console.log("Creating .dockerignore file as id does not exists in project root");
shell.cp("-R", path.join(process.env.__c_root, ".dockerignore"), process.env.__p_root);
}
let otherArgs = Array.from(process.argv).slice(3);
const hasDockerFileInProjectRoot = fs.existsSync(path.join(process.env.__p_root, "Dockerfile"));
if (
!hasDockerFileInProjectRoot &&
_.first(otherArgs) &&
_.first(otherArgs).toLowerCase() === "build" &&
_.indexOf(otherArgs, "-f") === -1 &&
_.indexOf(otherArgs, "-F") === -1
) {
// eslint-disable-next-line
console.log("No docker file found in project root and no option for docker file specified!");
const devDockerfilePath = path.join(process.env.__p_root, "docker", "Dockerfile");
const prodDockerfilePath = path.join(process.env.__p_root, "docker", "prod", "Dockerfile");
if (
(process.env.NODE_ENV.toLowerCase() === "production" && !fs.existsSync(prodDockerfilePath)) ||
(process.env.NODE_ENV.toLowerCase() === "development" && !fs.existsSync(devDockerfilePath))
) {
// eslint-disable-next-line
console.log("No Dockerfile found! Thus creating new in folder 'docker'");
shell.cp("-R", path.join(process.env.__c_root, "src", "docker"), `${process.env.__p_root}`);
}
const dockerImagePath = process.env.NODE_ENV.toLowerCase() === "production"? prodDockerfilePath : devDockerfilePath;
otherArgs = ["build", "-f", dockerImagePath, ...otherArgs.slice(1)];
}
// eslint-disable-next-line
console.log(shell.exec(["docker", ...otherArgs].join(" ")).stdout);
break;
}
}