Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 1.61 KB
/
Copy pathindex.js
File metadata and controls
62 lines (52 loc) · 1.61 KB
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
import {Tokenizer} from './tokenizer.js';
import {Minimizer} from './minimizer.js';
import fs from 'fs'
import commandLineArgs from 'command-line-args';
const optionDefinitions =
[
{ name: 'input', alias: 'i', type: String, defaultValue: null, defaultOption: true },
{ name: 'output', alias: 'o', type: String, defaultValue: null },
{ name: 'stringstoconst', alias: 's', type: Boolean, defaultValue: false },
{ name: 'keepnewlines', alias: 'k', type: Boolean, defaultValue: false }
];
const options = commandLineArgs( optionDefinitions );
// Load the file to be minimized
if( options.input == null )
{
console.error( "No Input file specified." );
process.exit( -1 );
}
let inputFile = fs.readFileSync( options.input ).toString();
let tokenizer = new Tokenizer();
let minimizer = new Minimizer();
// Tokenize the file
console.log( "Tokenizing source code" );
let tokens = tokenizer.parse( inputFile );
tokens = minimizer.estimateScope( tokens );
// Combine duplicate strings
if( options.stringstoconst == true )
{
console.log( "Combining Duplicate Strings." );
tokens = minimizer.combineDuplicateStrings( tokens );
}
// Combine duplicate strings
let output;
if( options.keepnewlines == true )
{
console.log( "Rebuilding the source without new lines removed." );
output = minimizer.rebuild( tokens, false );
}
else
{
console.log( "Rebuilding the source with new lines removed." );
output = minimizer.rebuild( tokens );
}
// Output the minimized file
if( options.output == null )
{
fs.writeFileSync( options.input + ".m.nut", output );
}
else
{
fs.writeFileSync( options.output, output );
}