-
Notifications
You must be signed in to change notification settings - Fork 771
/
gulpfile.js
163 lines (142 loc) · 3.92 KB
/
gulpfile.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
const { src, dest, series, task } = require("gulp");
const fs = require("fs"),
path = require("path"),
PluginError = require("plugin-error"),
ZSchema = require("z-schema"),
log = require("fancy-log"),
axios = require("axios"),
zip = require("gulp-zip"),
argv = require("yargs").argv,
replace = require("gulp-token-replace");
// Config
const config = require("./gulp.config");
const package = require("./package.json");
const env = argv["env"];
if (env === undefined) {
require("dotenv").config();
} else {
log(`Using custom .env`);
require("dotenv").config({ path: path.resolve(process.cwd(), env) });
}
process.env.VERSION = package.version;
/**
* Replace parameters in the appManifest
*/
task("generate-manifest", (cb) => {
return (
src("src/appManifest/manifest.json")
.pipe(
replace({
tokens: {
...process.env,
},
})
)
.pipe(dest("./temp"))
);
});
/**
* Schema validation
*/
task("schema-validation", (callback) => {
let filePath = path.join(__dirname, "temp/manifest.json");
if (fs.existsSync(filePath)) {
let manifest = fs.readFileSync(filePath, {
encoding: "UTF-8",
}),
manifestJson;
try {
manifestJson = JSON.parse(manifest);
} catch (error) {
callback(new PluginError(error.message));
return;
}
log("Using manifest schema " + manifestJson.manifestVersion);
let definition = config.SCHEMAS.find(
(s) => s.version == manifestJson.manifestVersion
);
if (definition === undefined) {
callback(new PluginError("validate-manifest", "Unable to locate schema"));
return;
}
if (manifestJson["$schema"] !== definition.schema) {
log(
"Note: the defined schema in your manifest does not correspond to the manifestVersion"
);
}
let requiredUrl = definition.schema;
let validator = new ZSchema();
let schema = {
$ref: requiredUrl,
};
axios
.get(requiredUrl, {
decompress: true,
responseType: "json",
})
.then((response) => {
validator.setRemoteReference(requiredUrl, response.data);
var valid = validator.validate(manifestJson, schema);
var errors = validator.getLastErrors();
if (!valid) {
callback(
new PluginError(
"validate-manifest",
errors
.map((e) => {
return e.message;
})
.join("\n")
)
);
} else {
callback();
}
})
.catch((err) => {
log.warn("WARNING: unable to download and validate schema: " + err);
callback();
});
} else {
console.log("Manifest doesn't exist");
}
});
/**
* Creates the tab manifest
*/
task("zip", () => {
return src("./src/appManifest/**/*.*")
.pipe(src("./temp/manifest.json"))
.pipe(zip(config.manifestFileName))
.pipe(dest("package"));
});
task("manifest", series("generate-manifest", "schema-validation", "zip"));
task("start-ngrok", (cb) => {
log("[NGROK] starting ngrok...");
const ngrok = require("ngrok");
const conf = {
subdomain: process.env.NGROK_SUBDOMAIN,
region: process.env.NGROK_REGION,
addr: process.env.PORT,
authtoken: process.env.NGROK_AUTH,
};
ngrok
.connect(conf)
.then((url) => {
log("[NGROK] Url: " + url);
if (!conf.authtoken) {
log(
"[NGROK] You have been assigned a random ngrok URL that will only be available for this session. You wil need to re-upload the Teams manifest next time you run this command."
);
}
let hostName = url.replace("http://", "");
hostName = hostName.replace("https://", "");
log("[NGROK] HOSTNAME: " + hostName);
process.env.HOSTNAME = hostName;
cb();
})
.catch((err) => {
log.error(`[NGROK] Error: ${JSON.stringify(err)}`);
cb(err.msg);
});
});