/
generate.ts
executable file
·185 lines (166 loc) · 5.65 KB
/
generate.ts
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
import * as fs from "fs";
import * as path from "path";
// import { api, projectRoot, CLI } from "./../../index";
// we need to load each component directly so we don't accidentally source `config... which doesn't exist`
import { CLI } from "./../../classes/cli";
import { projectRoot } from "./../../classes/process/projectRoot";
import {
createDirSafely,
createFileSafely,
} from "../../modules/utils/fileUtils";
export class Generate extends CLI {
constructor() {
super();
this.name = "generate";
this.description =
"will prepare an empty directory with a template Actionhero project";
}
async run() {
const documents: {
[key: string]: any;
} = {};
documents.projectMap = fs.readFileSync(
path.join(__dirname, "/../../../templates/projectMap.txt")
);
const oldFileMap = {
tsconfig: "tsconfig.json",
serverJs: "/templates/projectServer.ts.template",
configApiJs: "/src/config/api.ts",
configLoggerJs: "/src/config/logger.ts",
configRedisJs: "/src/config/redis.ts",
configTasksJs: "/src/config/tasks.ts",
configErrorsJs: "/src/config/errors.ts",
configPluginsJs: "/src/config/plugins.ts",
configI18nJs: "/src/config/i18n.ts",
configRoutesJs: "/src/config/routes.ts",
configWebJs: "/src/config/servers/web.ts",
configWebsocketJs: "/src/config/servers/websocket.ts",
packageJson: "/package.json",
actionStatus: "/src/actions/status.ts",
actionChatRoom: "/src/actions/createChatRoom.ts",
actionDocumentation: "/src/actions/showDocumentation.ts",
publicIndex: "/public/index.html",
publicChat: "/public/chat.html",
publicLogo: "/public/logo/actionhero.png",
publicCss: "/public/css/cosmo.css",
exampleTest: "/__tests__/template.ts.example",
enLocale: "/locales/en.json",
gitignore: "/templates/gitignore.template",
};
for (const name in oldFileMap) {
const localPath = oldFileMap[name];
const source = path.join(__dirname, "/../../../", localPath);
const extension = localPath.split(".")[1];
documents[name] = fs.readFileSync(source);
if (extension === "ts" || extension === "js" || extension === "json") {
documents[name] = documents[name].toString();
documents[name] = documents[name].replace(
'from "./../index"',
'from "actionhero"'
);
}
}
const AHversionNumber = JSON.parse(documents.packageJson).version;
documents.packageJson = String(
fs.readFileSync(
path.join(__dirname, "/../../../templates/package.json.template")
)
);
documents.packageJson = documents.packageJson.replace(
"%%versionNumber%%",
AHversionNumber
);
documents.readmeMd = String(
fs.readFileSync(
path.join(__dirname, "/../../../templates/README.md.template")
)
);
console.log("Generating a new actionhero project...");
[
"/src",
"/src/config",
"/src/config/servers",
"/src/actions",
"/src/tasks",
"/src/initializers",
"/src/servers",
"/src/bin",
"/log",
"/pids",
"/locales",
"/public",
"/public/javascript",
"/public/css",
"/public/logo",
"/__tests__",
"/__tests__/actions",
"/__tests__/tasks",
].forEach((dir) => {
try {
const message = createDirSafely(projectRoot + dir);
console.log(message);
} catch (error) {
console.log(error.toString());
}
});
const newFileMap = {
"/tsconfig.json": "tsconfig",
"/src/server.ts": "serverJs",
"/src/config/api.ts": "configApiJs",
"/src/config/logger.ts": "configLoggerJs",
"/src/config/redis.ts": "configRedisJs",
"/src/config/tasks.ts": "configTasksJs",
"/src/config/errors.ts": "configErrorsJs",
"/src/config/plugins.ts": "configPluginsJs",
"/src/config/i18n.ts": "configI18nJs",
"/src/config/routes.ts": "configRoutesJs",
"/src/config/servers/web.ts": "configWebJs",
"/src/config/servers/websocket.ts": "configWebsocketJs",
"/package.json": "packageJson",
"/src/actions/status.ts": "actionStatus",
"/src/actions/createChatRoom.ts": "actionChatRoom",
"/src/actions/showDocumentation.ts": "actionDocumentation",
"/public/index.html": "publicIndex",
"/public/chat.html": "publicChat",
"/public/css/cosmo.css": "publicCss",
"/public/logo/actionhero.png": "publicLogo",
"/README.md": "readmeMd",
"/__tests__/actions/status.ts": "exampleTest",
"/locales/en.json": "enLocale",
"/.gitignore": "gitignore",
};
for (const file in newFileMap) {
try {
const message = createFileSafely(
projectRoot + file,
documents[newFileMap[file]]
);
console.log(message);
} catch (error) {
console.log(error.toString());
}
}
console.log("");
console.log(
"Generation Complete. Your project directory should look like this:"
);
console.log("");
documents.projectMap
.toString()
.split("\n")
.forEach(function (line) {
console.log(line);
});
console.log(
`
-------------------------------------------------------------------------------------------------------------------
You need to run \`npm install\` to install dependencies, and then \`npm run build\` to build the .js from this .ts project.
Then, run 'npm run dev' to start your server
<3, the Actionhero Team
www.actionherojs.com
-------------------------------------------------------------------------------------------------------------------
`
);
return true;
}
}