/
main.ts
156 lines (132 loc) · 4.18 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
import * as program from "commander";
import * as path from "path";
import * as winston from "winston";
import { PackhostGenerator, Unpacker, WebpackRunner } from "./";
import { ConfigLoader, IFuncpackConfig } from "./utils";
async function runCli() {
const p = program
.version("0.2.2")
.option("-d, --debug", "Emits debug messages");
p.command("unpack <path>")
.description("Will remove all traces of packing tool at the specified "
+ "path or the current directory if none is specified")
.option("-o, --output <path>", "Path for output directory")
.action(unpack);
p.command("pack <path>")
.description("Will pack the specified path or the current directory if none is specified")
.option("-u, --uglify", "Uglify the project when webpacking")
.option("-o, --output <path>", "Path for output directory")
.option("-c, --copyToOutput", "Copy files to output directory")
.action(pack);
p.command("*", null, { noHelp: true, isDefault: true })
.action(() => {
p.help();
});
p.parse(process.argv);
if (!process.argv.slice(2).length) {
p.help();
}
}
async function unpack(name: string, options: any) {
if (options.debug) {
process.env.DEBUG = "*";
}
// Grab the route either from the option, the argument (if there is only 1)
let projectRootPath = "";
try {
projectRootPath = name ?
path.resolve(process.cwd(), name) : process.cwd();
} catch (error) {
winston.error(error);
throw new Error("Could not determine route");
}
let outputPath = ".funcpack";
try {
if (options.output) {
outputPath = path.join(options.output, outputPath);
}
} catch (e) {
winston.error(e);
throw new Error("Could not parse the output option");
}
winston.info("Unpacking project at: " + projectRootPath);
await Unpacker.unpack({ projectRootPath, outputPath });
winston.info("Complete!");
}
async function pack(name: string, options: any) {
// TBD - allow loadConfig to get a filename from options
let config: IFuncpackConfig = await ConfigLoader.loadConfig();
config = config || {
ignoredModules: [],
};
if (options.debug) {
process.env.DEBUG = "*";
}
// Grab the route either from the option, the argument (if there is only 1)
let projectRootPath = "";
try {
projectRootPath = name ?
path.join(process.cwd(), name) : process.cwd();
} catch (error) {
winston.error(error);
throw new Error("Could not determine route");
}
let uglify = false;
try {
if (options.uglify) {
uglify = true;
}
} catch (e) {
winston.error(e);
throw new Error("Could not parse the uglify option");
}
let outputPath = ".funcpack";
try {
if (options.output) {
outputPath = path.join(options.output, outputPath);
}
} catch (e) {
winston.error(e);
throw new Error("Could not parse the output option");
}
let copyToOutput = false;
try {
if (options.copyToOutput) {
copyToOutput = true;
}
} catch (e) {
winston.error(e);
throw new Error("Could not parse the copyToOutput option");
}
// Create new generator object with settings
const generator = new PackhostGenerator({
copyToOutput,
outputPath,
projectRootPath,
});
// Attempt to generate the project
try {
winston.info("Generating project files/metadata");
await generator.updateProject();
} catch (error) {
winston.error(error);
throw new Error("Could not generate project");
}
// Webpack
try {
winston.info("Webpacking project");
await WebpackRunner.run({
ignoredModules: config.ignoredModules,
outputPath,
projectRootPath,
uglify,
});
} catch (error) {
winston.error(error);
throw new Error("Could not webpack project");
}
winston.info("Complete!");
process.exit(0);
}
runCli();