forked from emmercm/igir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.ts
137 lines (124 loc) · 4.78 KB
/
package.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 * as child_process from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
// eslint-disable-next-line import/no-extraneous-dependencies
import { path7za } from '7zip-bin';
import caxa from 'caxa';
import fg, { Options as GlobOptions } from 'fast-glob';
import yargs from 'yargs';
import Logger from './src/console/logger.js';
import LogLevel from './src/console/logLevel.js';
import Constants from './src/constants.js';
import FsPoly from './src/polyfill/fsPoly.js';
interface FileFilter extends GlobOptions {
include?: string,
exclude?: string,
}
const fileFilter = (filters: FileFilter[]): string[] => {
let results: string[] = [];
filters.forEach((filter) => {
if (filter.include) {
const include = fg.globSync(filter.include.replace(/\\/g, '/'), filter)
.map((file) => path.resolve(file));
if (include.length === 0) {
throw new Error(`glob pattern '${filter.include}' returned no paths`);
}
results = [...results, ...include];
}
if (filter.exclude) {
const exclude = new Set(fg.globSync(filter.exclude.replace(/\\/g, '/'), filter)
.map((file) => path.resolve(file)));
if (exclude.size === 0) {
throw new Error(`glob pattern '${filter.exclude}' returned no paths`);
}
results = results.filter((result) => !exclude.has(result));
}
});
return results;
};
(async (): Promise<void> => {
const logger = new Logger(LogLevel.TRACE);
const argv = await yargs(process.argv.slice(2))
.locale('en')
.usage('Usage: $0 <input> <output>')
.positional('input', {
description: 'input directory',
type: 'string',
default: '.',
})
.check((_argv) => {
if (!_argv.input || !fs.existsSync(_argv.input)) {
throw new Error(`input directory '${_argv.input}' doesn't exist`);
}
return true;
})
.positional('output', {
description: 'output file',
type: 'string',
default: Constants.COMMAND_NAME + (process.platform === 'win32' ? '.exe' : ''),
}).argv;
const input = path.resolve(argv.input);
logger.info(`Input: '${input}'`);
const include = new Set(fileFilter([
// Start with the files we need
{ include: 'dist{,/**}', onlyFiles: false },
{ include: 'node_modules{,/**}', onlyFiles: false },
{ include: 'package*.json' },
// Exclude unnecessary JavaScript files
{ exclude: 'dist/test/**' },
{ exclude: 'dist/{**/,}*.test.*' },
{ exclude: '**/jest.config.(js|ts|mjs|cjs|json)' },
{ exclude: '**/tsconfig*' },
{ exclude: '**/*.d.ts' },
{ exclude: '**/*.(js|ts).map' },
// Exclude unnecessary docs files
{ exclude: 'node_modules/**/docs/{**/,}*.md' },
{ exclude: 'node_modules/**/(AUTHORS|CHANGELOG|CHANGES|CODE_OF_CONDUCT|CONTRIBUTING|GOVERNANCE|HISTORY|LICENSE|README|RELEASE|RELEASE-NOTES|SECURITY|TROUBLESHOOTING){,*.md,*.markdown,*.txt}', caseSensitiveMatch: false },
// Only include the exact 7zip-bin we need
{ exclude: 'node_modules/{**/,}7zip-bin/**/7z*' },
{ include: path7za },
]));
const includeSize = (await Promise.all([...include].map(async (file) => {
if (await FsPoly.isDirectory(file)) {
return 0;
}
return FsPoly.size(file);
}))).reduce((sum, size) => sum + size, 0);
logger.info(`Include: found ${FsPoly.sizeReadable(includeSize)} of ${include.size.toLocaleString()} file${include.size !== 1 ? 's' : ''} to include`);
const exclude = fileFilter([
{ include: '*{,/**}', onlyFiles: false, dot: true },
]).filter((file) => !include.has(file));
const excludeSize = (await Promise.all(exclude.map(async (file) => {
if (await FsPoly.isDirectory(file)) {
return 0;
}
return FsPoly.size(file);
}))).reduce((sum, size) => sum + size, 0);
logger.info(`Exclude: found ${FsPoly.sizeReadable(excludeSize)} of ${exclude.length.toLocaleString()} file${exclude.length !== 1 ? 's' : ''} to exclude`);
const excludeGlobs = exclude.map((glob) => fg.convertPathToPattern(glob));
const output = path.resolve(argv.output);
logger.info(`Output: '${input}'`);
logger.info('Building ...');
await caxa({
input,
output,
exclude: excludeGlobs,
command: [
'{{caxa}}/node_modules/.bin/node',
'{{caxa}}/dist/index.js',
],
});
if (!await FsPoly.exists(output)) {
throw new Error(`output file '${output}' doesn't exist`);
}
logger.info(`Output: ${FsPoly.sizeReadable(await FsPoly.size(output))}`);
const proc = child_process.spawn(output, ['--help'], { windowsHide: true });
let procOutput = '';
proc.stdout.on('data', (chunk) => { procOutput += chunk.toString(); });
proc.stderr.on('data', (chunk) => { procOutput += chunk.toString(); });
await new Promise((resolve, reject) => {
proc.on('exit', resolve);
proc.on('error', reject);
});
logger.trace(procOutput);
})();