forked from gauravsoni119/ng2-tel-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
105 lines (95 loc) · 2.97 KB
/
gulpfile.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
/**
* @file gulp tasks to build the package
* @author Gaurav Soni
*/
const { series, src, dest } = require('gulp');
const clean = require('gulp-clean');
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
/**
* Execute command via nodeJs.
* Running command here so that we can run all commands from gulp file
* @param {string} cmd any command that we can execute via terminal
* @param {Object.<string, string>} options that needs to run along with cmd
* @returns {Promise} if commnad runs successfully then resolve it else reject it
*/
const execCommand = (cmd, options) => {
return new Promise((res, rej) => {
exec(cmd, { ...options }, (error, stdout) => {
if (error) {
rej(error);
}
res(stdout);
});
})
};
/**
* Compile the angular code to javascript
* @param {Function} cb needs to execute at the end of the function
*/
const compile = async (cb) => {
try {
await execCommand('npm run build');
} catch (error) {
throw new Error('Error while compiling', error);
} finally {
cb();
}
}
/**
* copy package.jsona and README.md, and example.png to package
* @param {Function} cb needs to execute at the end of the function
*/
copy = (cb) => {
const stream = src(['./package.json', './README.md', './example.png']).pipe(dest('./dist'));
stream.on('end', () => cb());
};
/**
* Run yarn pack command to create .tar file so that we can install package locally and test it
* @param {Function} cb needs to execute at the end of the function
*/
const pack = async (cb) => {
try {
await execCommand('yarn pack', { cwd: path.resolve(__dirname, 'dist') });
} catch (error) {
throw new Error('Error while creating tar file', error);
}
};
/**
* Remove dist folder
* @param {Function} cb needs to execute at the end of the function
*/
const cleanDist = (cb) => {
src(path.resolve(__dirname, 'dist')).pipe(clean());
cb();
};
/**
* Wrapp readFile function inside promise so that we can use promises
* @param {string} path of the file
*/
const readFile = (path) => {
return new Promise((res, rej) => {
fs.readFile(path, (err, data) => {
if (err) {
rej(err);
}
res(data);
});
});
};
/**
* Update package.json to remove unnecessary fields
* @param {Function} cb needs to execute at the end of the function
*/
const updatePackageJson = async (cb) => {
const file = await readFile(path.resolve(`${__dirname}/dist`, 'package.json'));
const packageFile = JSON.parse(file);
packageFile.devDependencies = {};
packageFile.scripts = {};
const data = JSON.stringify(packageFile, null, 2);
fs.writeFileSync(path.resolve(`${__dirname}/dist`, 'package.json'), data);
cb();
};
exports.buildDev = series(cleanDist, compile, copy, updatePackageJson, pack);
exports.default = series(cleanDist, compile, copy, updatePackageJson);