forked from Azure/azure-functions-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
144 lines (118 loc) · 3.8 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
#!/usr/bin/env node
import * as program from "commander";
import * as path from "path";
import { PackhostGenerator, WebpackRunner, FunctionDeploy } from "./";
import { ConfigLoader, IFuncpackConfig, createLogger, FileHelper } from "./utils";
import { DEFAULT_OUTPUT } from "./CONSTANTS";
const logger = createLogger('azure.functions.webpack.main');
async function runCli() {
const p = program
.version("0.0.1")
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")
.action(pack);
p.command("publish <unique-app-name>")
.option("-d --dir <path>", "bundle directory")
.option("-l --location <location>", "host loction")
.description("Will pack the specified path or the current directory if none is specified")
.action(publish);
p.command("deploy <name> [path]")
.option("-o, --output <path>", "Path for output directory")
.option("-l --location <location>", "host loction")
.description("Will pack the specified path or the current directory, and publish it to azure with given Function app name")
.action(deploy);
p.command("*", null, { noHelp: true, isDefault: true })
.action(() => {
p.help();
});
p.parse(process.argv);
if (!process.argv.slice(2).length) {
p.help();
}
}
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: [],
};
// 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) {
logger.error(error);
throw new Error("Could not determine route");
}
let uglify = false;
try {
if (options.uglify) {
uglify = true;
}
} catch (e) {
logger.error(e);
throw new Error("Could not parse the uglify option");
}
let outputPath = DEFAULT_OUTPUT;
try {
if (options.output) {
outputPath = options.output;
}
} catch (e) {
logger.error(e);
throw new Error("Could not parse the output option");
}
// Create new generator object with settings
const generator = new PackhostGenerator({
projectRootPath,
outputPath,
});
// Attempt to generate the project
try {
logger.info("Generating project files/metadata");
await generator.updateProject();
} catch (error) {
logger.error(error);
throw new Error("Could not generate project");
}
// Webpack
try {
logger.info("Webpacking project");
await WebpackRunner.run({
projectRootPath,
uglify,
outputPath,
ignoredModules: config.ignoredModules,
});
} catch (error) {
logger.error(error);
throw new Error("Could not webpack project");
}
logger.info("Pack Completed!");
}
async function publish(name: string, options: any) {
try {
logger.info("Publishing project");
const bundlePath = options.dir || DEFAULT_OUTPUT;
if (!await FileHelper.exists(bundlePath)) {
logger.info("The bundle folder is not existing");
process.exit(1);
}
const deploy = new FunctionDeploy({ functionAppName: name, bundlePath, location: options.location });
await deploy.deploy();
logger.info("Publishing complete");
} catch (error) {
logger.error(error);
throw new Error("Error happenned while deploy the function app");
}
}
async function deploy(name: string, projectPath: string, options: any) {
logger.info("Deploying project");
await pack(projectPath, options);
await publish(name, options);
logger.info("Project deployed");
}
runCli();