-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackaging.ts
137 lines (123 loc) · 4.07 KB
/
packaging.ts
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
// import libraries
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/operator/mergeMap';
import * as fs from 'fs-extra';
/**
* Interface for file object definition
*/
interface FileObject {
name: string;
}
/**
* Class declaration
*/
class Packaging {
// private property to store files list
private _files: FileObject[];
// private property to store src path
private _srcPath: string;
// private property to store dest path
private _destPath: string;
/**
* Class constructor
*
* @param files {FileObject[]} name of each files to package and flag to know if we need to delete it after
* @param src {string} src base path from current process
* @param dest {string} dest base path from current process
*/
constructor(files: FileObject[], src: string = '', dest: string = '/dist') {
this._files = files;
this._srcPath = `${process.cwd()}${src}/`;
this._destPath = `${process.cwd()}${dest}/`;
}
/**
* Function to copy one file
*
* @param file {string}
*
* @return {Observable<any>}
*/
private _copy(file: string): Observable<any> {
// copy package.json
if (file.indexOf('package.json') !== -1) {
return this._copyAndCleanupPackageJson(file);
}
// copy other files
return <Observable<any>> Observable.create((observer) => {
fs.stat(`${this._srcPath}${file}`, (error, stats) => {
if (error) {
console.error('doesn\'t exist on copy =>', error.message);
}
if (stats && (stats.isFile() || stats.isDirectory())) {
fs.copy(`${this._srcPath}${file}`, `${this._destPath}${file}`, (err) => {
if (err) {
console.error('copy failed =>', err.message);
}
observer.next();
observer.complete();
});
} else {
observer.next();
observer.complete();
}
});
});
}
/**
* Function to cleanup package.json and _copy it to dist directory
*
* @param file {string}
*
* @return {Observable<any>}
*
* @private
*/
private _copyAndCleanupPackageJson(file: string): Observable<any> {
// function to read JSON
const readJson = (src: string): Observable<any> => {
return <Observable<any>> Observable.create((observer) => {
fs.readJson(src, (error, packageObj) => {
if (error) {
return observer.error(error);
}
observer.next(packageObj);
observer.complete();
});
});
};
// function to write JSON
const writeJson = (dest: string, data: any): Observable<any> => {
return <Observable<any>> Observable.create((observer) => {
fs.outputJson(dest, data, (error) => {
if (error) {
return observer.error(error);
}
observer.next();
observer.complete();
});
});
};
// read package.json
return readJson(`${this._srcPath}${file}`).flatMap(packageObj => {
// delete obsolete data in package.json
delete packageObj.scripts;
delete packageObj.devDependencies;
// write new package.json
return writeJson(`${this._destPath}${file}`, packageObj);
});
}
/**
* Function that _copy all files in dist directory
*/
process() {
Observable.forkJoin(
this._files.map(
(fileObject: FileObject) => this._copy(fileObject.name)
)
)
.subscribe(null, error => console.error(error));
}
}
// process packaging
new Packaging(require('./files')).process();