Skip to content

Commit 68036a0

Browse files
authored
Merge pull request #14 from houssenedao/master
Refact and intercept file not exist
2 parents 73919c7 + a8f25c6 commit 68036a0

12 files changed

+176
-1577
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/node_modules/
55
npm-debug.log
66
/coverage/
7+
/package-lock.json

lib/actions/helpers.action.js

+74-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,85 @@
1-
const {CURRENT_DIRECTORY, FS, COLORS, NOTIFY, NAME, VERSION} = require('../config/superman.config');
1+
const {CURRENT_DIRECTORY, FS, COLORS, NOTIFY, NAME, VERSION, SHELL} = require('../config/superman.config');
2+
/**
3+
* User package version
4+
* @returns {*}
5+
* @constructor
6+
*/
7+
const PACKAGE_VERSION = () => {
8+
if (checkIfFileExist(`${CURRENT_DIRECTORY}/package.json`)) {
9+
return require(`${CURRENT_DIRECTORY}/package.json`).version;
10+
} else {
11+
console.log('\n', COLORS.white.bold(NAME), COLORS.cyan.bold(VERSION));
12+
console.error('\n', COLORS.red(' Package.json file does\'t exist this directory'), '\n');
213

3-
const PACKAGE_CONTENT = JSON.stringify(require(`${CURRENT_DIRECTORY}/package.json`));
4-
5-
const PACKAGE_VERSION = require(`${CURRENT_DIRECTORY}/package.json`).version;
14+
supermanNotify(`Oh no!, Package.json file does't exist this directory.`);
15+
process.exit();
16+
}
17+
};
618

7-
const WRITE_PACKAGE_FILE = (NEW_VERSION, NEW_PACKAGE_CONTENT) => {
19+
/**
20+
* Write package file
21+
*
22+
* @param NEW_VERSION
23+
* @constructor
24+
*/
25+
const WRITE_PACKAGE_FILE = (NEW_VERSION) => {
826
console.log('\n', COLORS.white.bold(NAME), COLORS.cyan.bold(VERSION));
927
console.log('\n', COLORS.grey(' Pleas wait tasks is running...'), '\n');
1028

11-
FS.writeFile(`${CURRENT_DIRECTORY}/package.json`, NEW_PACKAGE_CONTENT, { flat: 'w' }, function (err) {
12-
if (err) {
13-
return console.log(COLORS.red(err));
29+
SHELL.sed('-i', `"version": "${PACKAGE_VERSION()}"`, `"version": "${NEW_VERSION}"`, `${CURRENT_DIRECTORY}/package.json`);
30+
31+
supermanNotify(`Oh yes!, your application version is now update to ${NEW_VERSION}.`);
32+
console.log(COLORS.green(` Oh yes!, your application version is now update to ${COLORS.yellow(NEW_VERSION)}.`));
33+
};
34+
35+
/**
36+
* Check if file exist
37+
*
38+
* @param filePath
39+
* @returns {boolean}
40+
*/
41+
const checkIfFileExist = (filePath) => {
42+
try {
43+
FS.statSync(filePath);
44+
} catch (err) {
45+
if (err.code === 'ENOENT') {
46+
return false;
1447
}
15-
NOTIFY.notify({
16-
title: 'Superman',
17-
message: `Oh yes!, your application version is now update to ${NEW_VERSION}.`
18-
});
19-
console.log(COLORS.green(` Oh yes!, your application version is now update to ${COLORS.yellow(NEW_VERSION)}.`));
48+
return false;
49+
}
50+
return true;
51+
};
52+
53+
/**
54+
* Notification function
55+
*
56+
* @param message
57+
*/
58+
const supermanNotify = (message) => {
59+
NOTIFY.notify({
60+
title: 'Superman Versioning',
61+
message: `${message}`
2062
});
2163
};
2264

65+
/**
66+
* Split current version
67+
* @returns {*|string[]}
68+
*/
69+
const SPLIT_VERSION = () => {
70+
let version = PACKAGE_VERSION().split('.');
71+
72+
return {
73+
major: parseInt(version[0]),
74+
minor: parseInt(version[1]),
75+
patch: parseInt(version[2]),
76+
}
77+
};
78+
79+
/**
80+
* Export modules
81+
*/
2382
module.exports = {
24-
PACKAGE_CONTENT,
25-
PACKAGE_VERSION,
26-
WRITE_PACKAGE_FILE
83+
WRITE_PACKAGE_FILE,
84+
SPLIT_VERSION
2785
};

lib/actions/init.action.js

+26-112
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,68 @@
1-
const {SHELL, CURRENT_DIRECTORY, COLORS, NAME, VERSION, NOTIFY} = require('../config/superman.config');
2-
const {PACKAGE_CONTENT, PACKAGE_VERSION} = require('../actions/helpers.action');
1+
const {SPLIT_VERSION, WRITE_PACKAGE_FILE} = require('../actions/helpers.action');
32
/**
43
* Update Package
54
*
65
* @param options
76
*/
87
const initVersion = (options) => {
98
if (options.major) {
10-
initMajor(PACKAGE_CONTENT, PACKAGE_VERSION);
9+
initMajor();
1110
} else if (options.minor) {
12-
initMinor(PACKAGE_CONTENT, PACKAGE_VERSION);
11+
initMinor();
1312
} else if (options.patch) {
14-
initPatch(PACKAGE_CONTENT, PACKAGE_VERSION)
13+
initPatch()
1514
} else {
16-
initFileVersion(PACKAGE_CONTENT, PACKAGE_VERSION);
15+
initFileVersion();
1716
}
1817
};
18+
1919
/**
2020
* Set MAJOR
2121
*
22-
* @param packageContent
23-
* @param packageVersion
2422
*/
25-
const initMajor = (packageContent, packageVersion) => {
26-
/**
27-
* Variables
28-
* @type {*|string[]}
29-
*/
30-
let version = packageVersion.split('.'),
31-
minor = parseInt(version[1]),
32-
patch = parseInt(version[2]),
33-
newVersion = `${0}.${minor}.${patch}`;
34-
35-
/**
36-
* Output console
37-
*/
38-
outputConsole();
39-
40-
/**
41-
* Set Package value
42-
*/
43-
SHELL.sed('-i', `"version": "${packageVersion}"`, `"version": "${newVersion}"`, `${CURRENT_DIRECTORY}/package.json`)
44-
23+
const initMajor = () => {
24+
let version = SPLIT_VERSION();
4525
/**
46-
* output notification
26+
* Write package file
4727
*/
48-
outputFinish(newVersion);
28+
WRITE_PACKAGE_FILE(`${0}.${version.minor}.${version.patch}`);
4929
};
30+
5031
/**
5132
* Set MINOR
5233
*
53-
* @param packageContent
54-
* @param packageVersion
5534
*/
56-
const initMinor = (packageContent, packageVersion) => {
35+
const initMinor = () => {
36+
let version = SPLIT_VERSION();
5737
/**
58-
* Variables
59-
* @type {*|string[]}
38+
/**
39+
* Write package file
6040
*/
61-
let version = packageVersion.split('.'),
62-
major = parseInt(version[0]),
63-
patch = parseInt(version[2]),
64-
newVersion = `${major}.${0}.${patch}`;
65-
66-
/**
67-
* Output console
68-
*/
69-
outputConsole();
70-
71-
/**
72-
* Set Package value
73-
*/
74-
SHELL.sed('-i', `"version": "${packageVersion}"`, `"version": "${newVersion}"`, `${CURRENT_DIRECTORY}/package.json`)
75-
76-
/**
77-
* output notification
78-
*/
79-
outputFinish(newVersion);
41+
WRITE_PACKAGE_FILE(`${version.major}.${0}.${version.patch}`);
8042
};
43+
8144
/**
8245
* Set PATCH
8346
*
84-
* @param packageContent
85-
* @param packageVersion
8647
*/
87-
const initPatch = (packageContent, packageVersion) => {
48+
const initPatch = () => {
49+
let version = SPLIT_VERSION();
8850
/**
89-
* Variables
90-
* @type {*|string[]}
51+
* Write package file
9152
*/
92-
let version = packageVersion.split('.'),
93-
major = parseInt(version[0]),
94-
minor = parseInt(version[1]),
95-
newVersion = `${major}.${minor}.${0}`;
96-
97-
/**
98-
* Output console
99-
*/
100-
outputConsole();
101-
102-
/**
103-
* Set Package value
104-
*/
105-
SHELL.sed('-i', `"version": "${packageVersion}"`, `"version": "${newVersion}"`, `${CURRENT_DIRECTORY}/package.json`)
106-
107-
/**
108-
* output notification
109-
*/
110-
outputFinish(newVersion);
53+
WRITE_PACKAGE_FILE(`${version.major}.${version.minor}.${0}`);
11154
};
55+
11256
/**
11357
* Set version
114-
*
115-
* @param packageContent
116-
* @param packageVersion
11758
*/
118-
const initFileVersion = (packageContent, packageVersion) => {
119-
/**
120-
* Output console
121-
*/
122-
outputConsole();
123-
124-
/**
125-
* Set Package value
126-
*/
127-
SHELL.sed('-i', `"version": "${packageVersion}"`, `"version": "0.0.0"`, `${CURRENT_DIRECTORY}/package.json`)
128-
59+
const initFileVersion = () => {
12960
/**
130-
* output notification
61+
* Write package file
13162
*/
132-
outputFinish('0.0.0');
133-
};
134-
/**
135-
* Output Console
136-
*/
137-
const outputConsole = () => {
138-
console.log('\n', COLORS.white.bold(NAME), COLORS.cyan.bold(VERSION));
139-
console.log('\n', COLORS.grey(' Pleas wait tasks is running...'), '\n');
140-
};
141-
/**
142-
* Output notify
143-
* @param version
144-
*/
145-
const outputFinish = (version) => {
146-
NOTIFY.notify({
147-
title: 'Superman',
148-
message: `Oh yes!, your application version is now update to ${version}.`
149-
});
150-
console.log(COLORS.green(` Oh yes!, your application version is now update to ${COLORS.yellow(version)}.`));
63+
WRITE_PACKAGE_FILE('0.0.0');
15164
};
65+
15266
/**
15367
* Export module
15468
* @type {{updatePackage: initVersion}}

0 commit comments

Comments
 (0)