-
Notifications
You must be signed in to change notification settings - Fork 2
/
wm_index.js
133 lines (115 loc) · 5.56 KB
/
wm_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
/*
* # 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 exec = util.promisify(require("child_process").exec);
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 } = require("./wm_utils");
const { 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`);
const destPath = getBundlePath(path);
await ncp(bundlePath, destPath);
}
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`;
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-es2015");
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 => {
await exec(goToPath(projectPath));
const file = node_path.resolve(`${getSspaPath(projectPath)}/package-lock.json`);
rimraf.sync(file);
await exec(installDependencies(projectPath));
}
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 `);
await exec(addSspa(projectPath));
replaceAngularJson(projectPath, splitStyles, libraryTarget);
updateTsConfigAppJson(projectPath);
await updateApp(projectPath, deployUrl, sspaDeployUrl, libraryTarget, splitStyles, mountStyles, isHashingEnabled);
updateStatus(`Installing Single-SPA Dependencies `);
await exec(installDependencies(projectPath));
updateStatus(`Building for Single-Spa `);
await exec(buildSspaApp(projectPath, isHashingEnabled));
updateStatus(`Copying Final Files `);
await copyDistFolder(projectPath)
printSuccess(`Artifacts are generated at: ${getBundlePath(projectPath)}`);
};
module.exports = {
generateSspaBundle
};