-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
252 lines (229 loc) · 9.11 KB
/
gulpfile.js
File metadata and controls
252 lines (229 loc) · 9.11 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
"use strict";
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require('gulp-mocha');
const moment = require('moment');
// const gulpWebpack = require('webpack-stream');
// const webpack = require('webpack');
const vsce = require('vsce');
const argv = require('yargs').argv;
const fetch = require('node-fetch');
const fs = require('fs-extra');
const os = require('os');
const log = require('fancy-log');
const path = require('path');
const pslist = require('ps-list');
const unzip = require('unzip-stream');
//const webPackConfig = require('./webpack.config');
const distdir = path.resolve('./dist');
const outdir = path.resolve('./out');
const packagedir = path.resolve('./package');
const feedPAT = argv.feedPAT || process.env['AZ_DevOps_Read_PAT'];
async function clean() {
(await pslist())
.filter((info) => info.name.startsWith('pacTelemetryUpload'))
.forEach(info => {
log.info(`Terminating: ${info.name} - ${info.pid}...`);
process.kill(info.pid);
});
fs.emptyDirSync(outdir);
return fs.emptyDir(distdir);
}
function compile() {
return gulp
.src('src/**/*.ts')
//.pipe(gulpWebpack(webPackConfig, webpack))
.pipe(gulp.dest(distdir));
}
async function nugetInstall(nugetSource, packageName, version, targetDir) {
// https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource
const feeds = {
'nuget.org': {
authenticated: false,
baseUrl: 'https://api.nuget.org/v3-flatcontainer/'
}
};
const selectedFeed = feeds[nugetSource];
const baseUrl = selectedFeed.baseUrl;
packageName = packageName.toLowerCase();
version = version.toLowerCase();
const packagePath = `${packageName}/${version}/${packageName}.${version}.nupkg`;
const nupkgUrl = new URL(packagePath, baseUrl);
const reqInit = {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'User-Agent': 'gulpfile-DPX-team/0.1',
// eslint-disable-next-line @typescript-eslint/naming-convention
'Accept': '*/*'
},
redirect: 'manual'
};
if (selectedFeed.authenticated) {
if (!feedPAT) {
throw new Error(`nuget feed ${nugetSource} requires authN but neither '--feedToken' argument nor env var 'AZ_DevOps_Read_PAT' was defined!`);
}
reqInit.headers['Authorization'] = `Basic ${Buffer.from('PAT:' + feedPAT).toString('base64')}`;
}
log.info(`Downloading package: ${nupkgUrl}...`);
let res = await fetch(nupkgUrl, reqInit);
if (res.status === 303) {
const location = res.headers.get('location');
const url = new URL(location);
log.info(` ... redirecting to: ${url.origin}${url.pathname}}...`);
// AzDevOps feeds will redirect to Azure storage with location url w/ SAS token: on 2nd request drop authZ header
delete reqInit.headers['Authorization'];
res = await fetch(location, reqInit);
}
if (!res.ok) {
const body = res.body.read();
throw new Error(`Cannot download ${res.url}, status: ${res.statusText} (${res.status}), body: ${body ? body.toString('ascii') : '<empty>'}`);
}
const localNupkg = path.join(targetDir, `${packageName}.${version}.nupkg`);
fs.ensureDirSync(targetDir);
return new Promise((resolve, reject) => {
res.body.pipe(fs.createWriteStream(localNupkg))
.on('close', () => {
resolve();
}).on('error', err => {
reject(err);
});
});
}
function extractNupkg(sourceDir, packageName, version, internalPath, targetDir) {
const localNupkg = path.join(sourceDir, `${packageName}.${version}.nupkg`);
const tmpFolder = path.join(distdir, "tmp");
fs.ensureDirSync(tmpFolder);
fs.ensureDirSync(targetDir);
fs.createReadStream(localNupkg)
.pipe(unzip.Extract({ path: tmpFolder }))
.on('finish', () => {
fs.copySync(path.join(tmpFolder, internalPath), targetDir, { overwrite: true, recursive: true});
fs.removeSync(tmpFolder);
fs.removeSync(sourceDir);
});
}
function lint() {
return gulp
.src(['src/**/*.ts', __filename])
.pipe(eslint({
formatter: 'verbose',
configuration: '.eslintrc.js'
}))
.pipe(eslint.format())
.pipe(eslint.results(results => {
if (results.warningCount > 0){
throw new Error(`Found ${results.warningCount} eslint errors.`);
}
}))
.pipe(eslint.failAfterError());
}
function test() {
return gulp
.src('src/client/test/unit/**/*.ts', { read: false })
.pipe(mocha({
require: [ "ts-node/register" ],
ui: 'bdd'
}));
}
async function packageVsix() {
fs.emptyDirSync(packagedir);
return vsce.createVSIX({
packagePath: packagedir,
});
}
async function git(args) {
args.unshift('git');
const {stdout, stderr } = await exec(args.join(' '));
return {stdout: stdout, stderr: stderr};
}
async function setGitAuthN() {
const repoUrl = 'https://github.com';
const repoToken = argv.repoToken;
if (!repoToken) {
throw new Error(`Must specify parameter --repoToken with read and push rights to ${repoUrl}!`);
}
const bearer = `AUTHORIZATION: basic ${Buffer.from(`PAT:${repoToken}`).toString('base64')}`;
await git(['config', '--local', `http.${repoUrl}/.extraheader`, `"${bearer}"`]);
await git(['config', '--local', 'user.email', 'capisvaatdev@microsoft.com' ]);
await git(['config', '--local', 'user.name', '"DPT Tools Dev Team"' ]);
}
async function snapshot() {
const targetBranch = argv.targetBranch || 'release/daily';
const sourceSpecParam = argv.sourceSpec;
const tmpRepo = path.resolve('./out/tmpRepo');
fs.emptyDirSync(tmpRepo);
const repoUrl = (await git(['remote', 'get-url', '--all', 'origin'])).stdout.trim();
log.info(`snapshot: remote repoUrl: ${repoUrl}`);
const orgDir = process.cwd();
process.chdir(tmpRepo);
try
{
await git(['init']);
await git(['remote', 'add', 'origin', repoUrl]);
await setGitAuthN();
await git(['fetch', 'origin']);
const remotes = (await git(['remote', 'show', 'origin'])).stdout;
const head = remotes
.split('\n')
.map(line => {
const branch = line.match(/HEAD branch:\s*(\S+)/);
if (branch && branch.length >= 2) {
return branch[1];
}
})
.filter(b => !!b);
if (!head || head.length < 1 || head.length > 1 || !head[0]) {
throw new Error(`Cannot determine HEAD from remote: ${repoUrl}`);
}
const headBranch = head[0];
if (headBranch === targetBranch) {
throw new Error(`Cannot snapshot into default HEAD branch: ${headBranch}`);
}
const sourceSpec = sourceSpecParam || `origin/${headBranch}`;
log.info(` > snap shotting '${sourceSpec}' into branch: ${targetBranch}...`);
await git(['checkout', headBranch]);
const snapshotTag = `snapshot-${targetBranch.replace('/', '_').replace(' ', '_')}-${moment.utc().format('YYMMDD[Z]HHmmss')}`;
// TODO: setting this tag can interfere with the versioning tool, release-it; for now, don't set this tag
// await git(['tag', snapshotTag, sourceSpec]);
await git(['checkout', '--force', '-B', targetBranch]);
const resetMsg = (await git(['reset', '--hard', `"${sourceSpec}"`])).stdout.trim();
log.info(` > snapshot (${snapshotTag}): ${resetMsg}`);
log.info(` > pushing snapshot branch '${targetBranch} to origin...`);
const pushMsg = (await git(['push', '--force', '--tags', 'origin', targetBranch])).stderr.trim();
log.info(` > ${pushMsg}`);
}
finally {
process.chdir(orgDir);
}
}
const recompile = gulp.series(
clean,
// async () => nugetInstall('nuget.org', 'Microsoft.CrmSdk.CoreTools', '9.1.0.92', path.resolve(distdir, 'CoreTools')),
// async () => extractNupkg(path.resolve(distdir, 'CoreTools'), 'Microsoft.CrmSdk.CoreTools', '9.1.0.92', 'content/bin/coretools', path.resolve('.', `bin/windows/CoreTools`)),
//async () => nugetInstall('nuget.org', 'Microsoft.PowerApps.CLI', '1.9.4', path.resolve(distdir, 'pac')),
//async () => nugetInstall('nuget.org', 'Microsoft.PowerApps.CLI.Core.osx-x64', '1.9.4', path.resolve(distdir, 'pac')),
compile,
);
const dist = gulp.series(
recompile,
packageVsix,
lint,
test
);
exports.clean = clean;
exports.compile = compile;
exports.recompile = recompile;
exports.snapshot = snapshot;
exports.lint = lint;
exports.test = test;
exports.package = packageVsix;
exports.ci = dist;
exports.dist = dist;
exports.setGitAuthN = setGitAuthN;
exports.default = compile;