-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwm_index.js
More file actions
165 lines (142 loc) · 7.22 KB
/
wm_index.js
File metadata and controls
165 lines (142 loc) · 7.22 KB
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
/*
* # WM-SSPA-CLI #
* download zip as per proj version //TODO
* generate angular code //TODO
* Update Angular.json
* npm install
* Ng Build & Generate Style & Script
* Copy Style & Script Files to Bundle Folder
* Update Routes & App Module
* Update Markups referring LazyLoad
* Add Single-spa schematics
* Remove Sspa Empty Component
* Run SSPA Prod Build
* Copy Main to Bundle Folder
*/
/* TODO:
* Do not gen NgBuild, if the deployment is Angular has it already
* Add Ng Schematics
*/
const fs = require("fs");
const node_path = require("path");
const util = require("util");
const ncp = util.promisify(require("ncp").ncp);
const rimraf = require("rimraf");
const { updateStatus, printSuccess } = require("./wm_cli_util");
const { replaceAngularJson, updatePackageJson,updateTsConfigAppJson, updateWebpackConfig } = require("./wm_json_utils");
const { prepareApp, updateApp } = require("./wm_prepare_app");
const { getGeneratedApp, getBundlePath, getSspaPath, execCommand} = require("./wm_utils");
const { getProjVersion, generateNgCode } = require("./wm_codegen_utils");
// TODO: Verbose support | --verbose option
const showResult = ({ stdout, stderr }) => {
return;
// stdout && console.log(chalk.grey(stdout));
// stderr && console.error(chalk.red(stderr));
};
const setupSspaProj = async path => {
await generateNgCode(path);
const sourcePath = getGeneratedApp(path);
};
const createBundleFolder = path => {
const bundlePath = getBundlePath(path);
rimraf.sync(bundlePath);
fs.mkdirSync(bundlePath);
};
const copyDistFolder = async (path) => {
const bundlePath = node_path.resolve(`${getSspaPath(path)}/dist/ng-bundle`);
let finalBundlePath = getSourceDir(bundlePath);
const destPath = getBundlePath(path);
await ncp(finalBundlePath, destPath);
}
const getSourceDir = (bundleDir) => {
// list all entries inside bundle
const entries = fs.readdirSync(bundleDir);
// check if there's exactly one subfolder and nothing else
const dirs = entries.filter(entry =>
fs.statSync(node_path.join(bundleDir, entry)).isDirectory()
);
if (dirs.length === 1 && entries.length === 1) {
// New structure → use that single random folder
return node_path.join(bundleDir, dirs[0]);
}
// Old structure → bundle itself contains files
return bundleDir;
}
const copyFileToBundle = async (path, fileName) => {
const bundlePath = node_path.resolve(`${getSspaPath(path)}/dist/ng-bundle`);
const destPath = getBundlePath(path);
const srcFile = fs.readdirSync(bundlePath).filter(file => file.startsWith(fileName))[0]
await ncp(node_path.resolve(bundlePath,srcFile), node_path.resolve(destPath,srcFile));
};
const updateMainJsResourcesPath = async(path, deployUrl) => {
const destPath = getGeneratedApp(path) + "/dist/ng-bundle";
fs.readdirSync(destPath).filter(function(file) {
if (node_path.extname(file).toLowerCase() === ".js") {
let jsFileData = fs.readFileSync(destPath + '/' + file, {encoding: 'utf8'});
jsFileData = jsFileData.replace(new RegExp('^resources/i18n', 'g'), deployUrl + '/resources/i18n');
jsFileData = jsFileData.replace(new RegExp('"\.\/resources\/images', 'g'), '"' + deployUrl + '/resources/images');
jsFileData = jsFileData.replace(new RegExp('"resources\/images', 'g'), '"' + deployUrl + '/resources/images');
jsFileData = jsFileData.replace(new RegExp('.ngDest="ng-bundle/"', 'g'), '.ngDest="' + deployUrl + '/ng-bundle/"');
// jsFileData = jsFileData.replace(new RegExp('"\.\/app\/prefabs\/'), 'g', '"' + deployUrl + '/app/prefabs/');
// jsFileData = jsFileData.replace(new RegExp('"\/app\/prefabs\/', 'g'), '"' + deployUrl + '/app/prefabs/');
fs.writeFileSync(destPath + '/' + file, jsFileData);
}
});
};
const goToPath = path => `cd ${getSspaPath(path)}`;
const installDependencies = path => `cd ${getSspaPath(path)} && npm i && npm i acorn@8.14.0 --save-dev`;
const buildNgApp = path => `cd ${getSspaPath(path)} && npm run build:sspa`;
const copyScripts = async path => await copyFileToBundle(path, "scripts");
const copyStyles = async path => await copyFileToBundle(path, "styles");
const copyMain = async path => await copyFileToBundle(path, "main");
const addSspa = path => `cd ${getSspaPath(path)} && npm run add-single-spa`;
const buildSspaApp = (path, isHashingEnabled) => isHashingEnabled === 'true' ? `cd ${getSspaPath(path)} && npm run build:sspa && npm run postbuild:sspa` : `cd ${getSspaPath(path)} && npm run build:sspa`;
const delSspaEmptyComp = path => {
const compPath = node_path.resolve(`${getSspaPath(path)}/src/app/empty-route`);
rimraf.sync(compPath);
};
const installDeps = async projectPath => {
let chgPathCmd = goToPath(projectPath);
await execCommand(chgPathCmd).catch((err) => { console.error(`Something went wrong while running command ${chgPathCmd}`, err) });
const file = node_path.resolve(`${getSspaPath(projectPath)}/package-lock.json`);
rimraf.sync(file);
let installCmd = installDependencies(projectPath);
await execCommand(installCmd).catch((err) => { console.error(`Something went wrong while running command ${installCmd}`, err) });
}
const invokeMaven = async (projectPath) => {
updateStatus(`Invoking Maven `);
let isPublicApp = getProjVersion(projectPath).indexOf("next") === -1;
let mvnCommand = isPublicApp ? `mvn process-classes` : `mvn process-classes -Pwavemaker-internal`;
let mvnCmd = `cd ${projectPath} && ${mvnCommand}`;
await execCommand(mvnCmd).catch((err) => { console.error(`Something went wrong while running command ${mvnCmd}`, err) });
};
const generateSspaBundle = async (projectPath, deployUrl, sspaDeployUrl, libraryTarget, splitStyles, isHashingEnabled, mountStyles, verbose) => {
updateStatus(`Preparing project `);
await setupSspaProj(projectPath);
updateStatus(`Preparing Bundle Folder `);
createBundleFolder(projectPath);
updateStatus(`Updating WaveMaker App `);
await prepareApp(projectPath, deployUrl);
updatePackageJson(projectPath, isHashingEnabled, sspaDeployUrl);
updateStatus(`Installing Dependencies `);
await installDeps(projectPath)
updateStatus(`Adding Single-spa schematics `);
let sspaCmd = addSspa(projectPath);
await execCommand(sspaCmd).catch((err) => { console.error(`Something went wrong while running command ${sspaCmd}`, err) });
replaceAngularJson(projectPath, splitStyles, libraryTarget);
updateTsConfigAppJson(projectPath);
await updateApp(projectPath, deployUrl, sspaDeployUrl, libraryTarget, splitStyles, mountStyles, isHashingEnabled);
updateStatus(`Installing Single-SPA Dependencies `);
let insCmd = installDependencies(projectPath);
await execCommand(insCmd).catch((err) => { console.error(`Something went wrong while running command ${insCmd}`, err) });
updateStatus(`Building for Single-Spa `);
let buildSSpaCmd = buildSspaApp(projectPath, isHashingEnabled);
await execCommand(buildSSpaCmd).catch((err) => { console.error(`Something went wrong while running command ${buildSSpaCmd}`, err) });
updateStatus(`Copying Final Files `);
await copyDistFolder(projectPath)
printSuccess(`Artifacts are generated at: ${getBundlePath(projectPath)}`);
};
module.exports = {
invokeMaven,
generateSspaBundle
};