-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.js
165 lines (153 loc) · 5.55 KB
/
command.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
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
const chalk = require('chalk');
const fs = require('fs-extra');
const path = require('path');
const utils = require('./src/common');
const { configNeedMigration, migrate } = require('./src/migration');
const { updateForm } = require('./src/features/form-update');
const { updateStockCount } = require('./src/features/stock-count');
const { updateStockReturn } = require('./src/features/stock-return');
const { updateStockSupply } = require('./src/features/stock-supply');
const { updateTranslations, getTranslations } = require('./src/common');
const { updateStockReturned } = require('./src/features/stock-returned');
const { updateStockConfirmation } = require('./src/features/stock-received');
const { updateStockDiscrepancy } = require('./src/features/stock-discrepancy');
const { updateStockOut } = require('./src/features/stock-out');
const { updateStockLogs } = require('./src/features/stock-logs');
const { updateStockOrder } = require('./src/features/stock-order');
const { updateOrderStockSupply } = require('./src/features/stock-order-supply');
const { getInitConfigs, createConfigFile } = require('./src/init');
const { getItemConfig, addConfigItem } = require('./src/add-item');
const { getFeatureConfigs, addFeatureConfigs, selectFeature } = require('./src/add-feature');
const inquirer = require('inquirer');
//Stock monitoring module initialization
async function init() {
const processDir = process.cwd();
if (utils.isAlreadyInit(processDir)) {
console.log(chalk.red.bold('Stock monitoring module already init'));
return;
}
const answers = await getInitConfigs();
const config = createConfigFile(answers);
await proccessFeatureForm(config);
}
function getConfig() {
const processDir = process.cwd();
if (!utils.isAlreadyInit(processDir)) {
console.log(chalk.red.bold('Stock monitoring module not found'));
return;
}
const configFilePath = path.join(processDir, 'stock-monitoring.config.json');
const configStr = fs.readFileSync(configFilePath);
return JSON.parse(configStr);
}
async function verifyConfigs(configs) {
if (configs.features.stock_order && !configs.features.stock_supply) {
console.log(chalk.red('Please enable stock supply first'));
const supplyConfigs = await getFeatureConfigs(configs, {
name: 'stock_supply',
});
return await addFeatureConfigs(configs, supplyConfigs);
}
return configs;
}
async function proccessFeatureForm(configs) {
const items = Object.keys(configs.items);
if (items.length === 0) {
console.log(chalk.red('Please add items first'));
const itemConfig = await getItemConfig(configs);
const updatedConfig = await addConfigItem(configs, itemConfig);
await proccessFeatureForm(updatedConfig);
return;
}
// Update app translations
updateTranslations(configs);
const messages = getTranslations();
for (const feature of Object.keys(configs.features)) {
const featureToFunctionMap = {
stock_count: updateStockCount,
stock_supply: async (configs) => {
await updateStockSupply(configs);
if (configs.features.stock_supply && configs.features.stock_supply.confirm_supply && configs.features.stock_supply.confirm_supply.active) {
await updateStockConfirmation(configs, messages);
await updateStockDiscrepancy(configs);
}
},
stock_return: async (configs) => {
await updateStockReturn(configs);
await updateStockReturned(configs);
},
stock_out: updateStockOut,
stock_logs: updateStockLogs,
stock_order: async (configs) => {
await updateStockOrder(configs);
await updateOrderStockSupply(configs);
}
};
await featureToFunctionMap[feature](configs);
}
await updateForm(configs);
console.log(chalk.green(`INFO All actions completed`));
}
const beforeCommand = async () => {
const needMigration = configNeedMigration();
if (needMigration) {
console.log(chalk.green('INFO Stock monitoring module need migration from version ' + needMigration[0] + ' to ' + needMigration[1]));
const question = await inquirer.prompt([
{
type: 'confirm',
name: 'migrate',
message: 'Do you want to migrate now ?',
}
]);
if (question.migrate) {
await migrate(needMigration[0], needMigration[1]);
}
}
};
module.exports = {
init,
addItem: async () => {
await beforeCommand();
const config = getConfig();
if (!config) {
return;
}
const itemConfig = await getItemConfig(config);
let updatedConfig = await addConfigItem(config, itemConfig);
updatedConfig = await verifyConfigs(updatedConfig);
await proccessFeatureForm(updatedConfig);
},
addFeature: async () => {
await beforeCommand();
const config = getConfig();
if (!config) {
return;
}
const feature = await selectFeature(config);
const featureConfigs = await getFeatureConfigs(config, feature);
let updatedConfig = await addFeatureConfigs(config, featureConfigs);
updatedConfig = await verifyConfigs(updatedConfig);
await proccessFeatureForm(updatedConfig);
},
build: async () => {
await beforeCommand();
const config = getConfig();
if (!config) {
return;
}
const updatedConfigs = await verifyConfigs(config);
await proccessFeatureForm(updatedConfigs);
},
migrate: async () => {
await beforeCommand();
const config = getConfig();
if (!config) {
return;
}
const updatedConfigs = await verifyConfigs(config);
await proccessFeatureForm(updatedConfigs);
},
info: function (message) {
console.log(chalk.blue.italic(message));
}
};