-
Notifications
You must be signed in to change notification settings - Fork 50
/
build-mobile.js
139 lines (130 loc) · 4.52 KB
/
build-mobile.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
const fs = require('fs');
const inquirer = require('inquirer');
const chalk = require('chalk');
const { spawn } = require("child_process");
const packageFilepath = "projects/common-form-elements/package.json";
const filesToRemoveLines = [
{
filepath: "projects/common-form-elements/src/lib/dynamic-field/dynamic-field.directive.ts",
keyword: "DynamicRichtextComponent"
},
{
filepath: "projects/common-form-elements/src/lib/common-form-elements.module.ts",
keyword: "DynamicRichtextComponent"
}
];
const run = async () => {
try {
// const answers = await askLibraryDetails();
const answers = {
environment: 'mobile',
name: '@project-sunbird/common-form-elements',
version: '5.1.1'
}
const { environment } = answers;
console.log(answers);
updateFiles(environment);
updatePackageFile(packageFilepath, answers);
console.log(chalk.bgBlue(' =========== Building Angular Package ==========='));
const child = spawn('ng', ['build', 'common-form-elements', '--prod']);
child.stdout.on('data', (data) => {
console.log(chalk.green(`${data}`));
});
child.stderr.on('data', (data) => {
console.error(`${data}`);
});
child.on('close', (code) => {
if (code === 0) {
console.log(chalk.green('Successfully Done!!!'));
} else {
console.log(`child process exited with code ${code}`);
}
});
} catch (error) {
console.log(chalk.red(error));
process.exit(1);
}
};
const askLibraryDetails = () => {
const questions = [
{
type: 'list',
name: 'environment',
message: 'What do you want to build for ?',
choices: ['mobile', 'web'],
default: 'mobile',
filter(val) {
return val.toLowerCase();
}
},
{
type: 'input',
name: 'name',
message: "Please enter library name",
validate(value) {
if (value) {
return true;
}
return 'Please enter a valid package name';
},
},
{
type: 'input',
name: 'version',
message: "Please enter library version",
validate(value) {
const pass = value.match(
/^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
);
if (pass) {
return true;
}
return 'Please enter a valid library version';
},
}
];
return inquirer.prompt(questions);
}
const updateFiles = (env) => {
for (let i = 0; i < filesToRemoveLines.length; i++) {
const fileData = fs.readFileSync(filesToRemoveLines[i].filepath, { encoding: 'utf-8' });
let dataArray = fileData.split('\n');
for (let index = 0; index < dataArray.length; index++) {
if (dataArray[index].includes(filesToRemoveLines[i].keyword)) {
if (env === 'mobile') {
dataArray[index] = dataArray[index].replace('// MOBILE', '');
dataArray[index] = `// MOBILE ${dataArray[index]}`;
}
if (env === 'web') {
dataArray[index] = dataArray[index].replace('// MOBILE', '').trimStart();
}
}
}
const updatedData = dataArray.join('\n');
fs.writeFileSync(filesToRemoveLines[i].filepath, updatedData);
console.log(chalk.yellow(
'Successfully updated the filepath ---> ' +
chalk.green.underline.bold(filesToRemoveLines[i].filepath)
));
}
};
const updatePackageFile = (filepath, { environment, name, version }) => {
const jsonString = fs.readFileSync(filepath);
let packageData = JSON.parse(jsonString);
if(version) {
packageData.version = version;
}
if (name) {
packageData.name = name;
} else if (environment === 'mobile') {
packageData.name = 'common-form-elements';
} else if (environment === 'web') {
packageData.name = 'common-form-elements-full';
}
fs.writeFileSync(filepath, JSON.stringify(packageData, null, 4));
console.log(chalk.yellow(
'Package name updated successfully ---> ' +
chalk.green.underline.bold(packageData.name)
));
};
run();