generated from zahmo/zajsf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_env.js
81 lines (71 loc) · 2.25 KB
/
update_env.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
//this will update the environment files with the
//package version info
var fs = require("fs");
var pakjson = require('./package.json');
var pakjsonAng = require('./node_modules/@angular/core/package.json');
var pakjsonMat = require('./node_modules/@angular/material/package.json');
console.log(`updating environment files with v${pakjson.version}...`);
const envFiles = [
'./demo/environments/environment.ts',
'./demo/environments/environment.prod.ts'
]
function backupFiles() {
console.log(`backing up files...`);
envFiles.forEach(fpath => {
fs.copyFileSync(fpath, fpath + ".bak");
})
}
function renameFiles() {
console.log(`renaming files...`);
envFiles.forEach(fpath => {
let newPath = fpath.split('.ts').join('.mjs')
fs.renameSync(fpath, newPath);
})
}
function getPackageInfo() {
return {
version: pakjson.version,
angularVersion: pakjsonAng.version,
//pakjson.dependencies && pakjson.dependencies["@angular/core"],
materialVersion: pakjsonMat.version
//pakjson.dependencies && pakjson.dependencies["@angular/material"]
}
}
function writeFile(environmentData, outputPath) {
console.log(`writing out file ${outputPath}`);
let env = environmentData;
//env.version = pakjson.version;
let pakInfo = getPackageInfo();
Object.assign(env, pakInfo);
let fileData = `
export const environment =${JSON.stringify(env)}
`
fs.writeFileSync(outputPath, fileData);
}
function processFile(importPath, outputPath) {
return import (importPath).then(envmod => {
let envData = envmod.environment;
writeFile(envData, outputPath);
console.log(`removing ${importPath}...`);
fs.rmSync(importPath);
console.log(`removing ${outputPath}.bak...`);
fs.rmSync(outputPath + '.bak');
})
}
function processAll() {
let mainProm = Promise.resolve(true);
envFiles.forEach(fpath => {
let importPath = fpath.split('.ts').join('.mjs');
let outputPath = fpath;
mainProm = mainProm.then(tr => {
return processFile(importPath, outputPath)
})
})
mainProm.catch(err => {
console.log(err);
process.exit(1);
})
}
backupFiles();
renameFiles();
processAll();