-
Notifications
You must be signed in to change notification settings - Fork 3
/
fuse.js
233 lines (183 loc) · 7.16 KB
/
fuse.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const { FuseBox, JSONPlugin, RawPlugin, QuantumPlugin } = require( "fuse-box" );
const { task, src, watch, tsc } = require( "fuse-box/sparky" );
const { exec: pkg } = require( 'pkg' );
const makeDir = require( 'make-dir' );
const path = require( 'path' );
const fs = require( 'mz/fs' );
const del = require( "del" );
const got = require('got');
const nativeFolders = [
'node_modules/sharp/build/Release'
];
const build = async ( isProduction, watch ) => {
const fuse = FuseBox.init( {
homeDir : "src",
target : 'server@es6',
output : "dist/$name.js",
useTypescriptCompiler : true,
log: {
showBundledFiles: false // Don't list all the bundled files every time we bundle
},
plugins: [
JSONPlugin(),
RawPlugin( [ '.txt.js', '.txt', '.txt.ts' ] ),
isProduction && QuantumPlugin( {
uglify: true,
css: true,
bakeApiIntoBundle: true,
ensureES5: false,
processPolyfill: true
} )
]
} );
fuse.bundle( "vendor" )
.instructions( "~ cli.ts" )
const app = fuse.bundle( "app" )
.completed( proc => proc.start() )
.instructions( "> [cli.ts]" );
if ( watch ) app.watch();
await fuse.run();
};
task( 'build:dev', async context => build( false, true ) );
task( 'build:rel', async context => build( true, true ) );
task( 'build:copy', async context => {
await watch( '**/*.(yaml|json)', { base: "src" } )
.dest( "lib/" )
.exec();
} );
let packageDependencies = {
'win32': {
'ffmpeg': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n5.1-latest-win64-gpl-5.1.zip',
// 'rethinkdb': 'https://download.rethinkdb.com/repository/raw/windows/rethinkdb-2.3.6.zip',
'sharp': 'https://github.com/lovell/sharp/releases/download/v0.21.1/sharp-v0.21.1-node-v64-win32-x64.tar.gz',
'libvips': 'https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-win32-x64.tar.gz'
},
'linux': {
'ffmpeg': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n5.1-latest-linux64-gpl-5.1.tar.xz',
// 'rethinkdb': null,
'sharp': 'https://github.com/lovell/sharp/releases/download/v0.21.1/sharp-v0.21.1-node-v64-linux-x64.tar.gz',
'libvips': 'https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-linux-x64.tar.gz'
},
'linux-arm64': {
'ffmpeg': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n5.1-latest-linuxarm64-gpl-5.1.tar.xz',
// 'rethinkdb': null,
'sharp': 'https://github.com/lovell/sharp/releases/download/v0.27.0/sharp-v0.27.0-napi-v3-linux-arm64.tar.gz',
// 'libvips': 'https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-linux-armv7.tar.gz'
'libvips': 'https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-linux-armv7.tar.gz'
},
'darwin': {
'ffmpeg': 'https://evermeet.cx/ffmpeg/ffmpeg-4.1.7z',
// 'rethinkdb': 'https://download.rethinkdb.com/repository/raw/osx/rethinkdb-2.3.6.dmg',
'sharp': 'https://github.com/lovell/sharp/releases/download/v0.21.1/sharp-v0.21.1-node-v64-darwin-x64.tar.gz',
'libvips': 'https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-darwin-x64.tar.gz'
}
};
const extractors = {};
async function fetch ( platform, dependency, force = false ) {
if ( dependency instanceof Array ) {
for ( let dep of dependency ) {
await fetch( platform, dep, force );
}
return;
}
const targetFolder = path.join( 'builds', '.cache', 'compressed', platform, dependency );
if ( !force && await fs.exists( targetFolder ) ) {
return;
}
const url = packageDependencies[ platform ][ dependency ];
if ( !url ) {
return;
}
try {
const targetFile = path.join( targetFolder, path.basename( url ) );
const format = path.extname( targetFile );
await makeDir( targetFolder );
const stream = got.stream( url ).pipe( fs.createWriteStream( targetFile ) );
await new Promise( ( resolve, reject ) => stream.on( 'error', reject ).on( 'finish', () => resolve() ) );
if ( format in extractors ) {
await extractors[ format ]( platform, dependency, targetFile );
await fs.unlink( targetFile );
}
} catch ( error ) {
console.log( error.response.body );
}
}
function getPackageHost ( platform ) {
if ( platform == 'win32' ) {
return 'win';
} else {
return platform;
}
}
async function package ( platform ) {
// sharp: https://github.com/lovell/sharp/releases
// lipvips: https://github.com/lovell/sharp-libvips/releases/
await fetch( platform, [ 'ffmpeg' /* , 'rethinkdb' */, 'libvips', 'sharp' ] );
const buildFolder = `builds/${ platform }/`;
await reset( buildFolder );
await tsc( __dirname, {
...JSON.parse( ( await fs.readFile( 'tsconfig.json', 'utf8' ) ) ).compilerOptions
} );
await pkg( [ '.', '--target', getPackageHost( platform ), '--out-path', buildFolder ] );
for ( let native of nativeFolders ) {
await makeDir( path.join( buildFolder, native ) );
await src( path.join( native, '**/*' ), { base: "." } )
.dest( buildFolder )
.exec();
}
await copy(
'./lib/Extensions',
path.join( buildFolder, 'Extensions' )
);
await copy(
'./config',
path.join( buildFolder, 'config' ),
[ 'default*.yaml' ]
);
// await copy(
// `./builds/.cache/uncompressed/${ platform }/rethinkdb`,
// path.join( buildFolder, 'storage' )
// );
await copy(
`./builds./cache/uncompressed/${ platform }/ffmpeg`,
path.join( buildFolder, 'storage', 'ffmpeg' )
);
await copy(
`./builds/.cache/uncompressed/${ platform }/sharp`,
path.join( buildFolder, 'node_modules', 'sharp' )
);
await copy(
`./builds/.cache/uncompressed/${ platform }/libvips/lib`,
path.join( buildFolder, 'node_modules', 'sharp', 'build', 'Release' )
);
}
const targets = [ 'win', 'linux', 'linux-arm64', 'darwin' ];
for ( const target of [ 'host', ...targets ] ) {
task( 'package:' + target, context => package( target ) );
}
task( 'package', targets.map( target => 'package:' + target ) );
task( 'check', async context => {
await tsc( __dirname, {
...JSON.parse( ( await fs.readFile( 'tsconfig.json', 'utf8' ) ) ).compilerOptions,
noEmit: true,
watch: true
} );
} );
task( 'default', [ 'build:dev' ] );
async function reset ( folder ) {
const stats = await fs.stat( folder ).catch( err => null );
if ( stats ) {
await del( folder );
} else {
await makeDir( folder );
}
}
async function copy ( source, dest, filters = null ) {
await makeDir( dest );
const globs = !filters || filters.len == 0
? [ path.join( '**', '*' ) ]
: filters;
await src( globs, { base: source } )
.dest( dest )
.exec();
}