-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathinit.ts
More file actions
473 lines (433 loc) · 15.7 KB
/
Copy pathinit.ts
File metadata and controls
473 lines (433 loc) · 15.7 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import { createExtension } from "./create";
import { exec } from "child_process";
import * as jsonInPlace from "json-in-place";
import { promisify } from "util";
import { TfCommand } from "../../lib/tfcommand";
import * as args from "../../lib/arguments";
import * as colors from "colors";
import * as extBase from "./default";
import * as fs from "fs";
import * as http from "https";
import { mkdirp } from "mkdirp";
import * as path from "path";
import * as trace from "../../lib/trace";
import * as jszip from "jszip";
const basicWebpackConfig = `const path = require("path");
const fs = require("fs");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
filename: "[name]/[name].js"
},
resolve: {
extensions: [".js"],
},
stats: {
warnings: false
},
module: {
rules: [
]
},
plugins: [
new CopyWebpackPlugin([ { from: "**/*.html", context: "src/" }])
]
};
`;
const basicDevDependencies = {
"copy-webpack-plugin": "^4.5.4",
"file-loader": "~2.0.0",
rimraf: "~2.6.2",
"tfx-cli": "^0.6.3",
webpack: "^4.22.0",
"webpack-cli": "^3.1.2",
};
const shorthandMap = {
a: "<all>",
all: "<all>",
f: "feature",
h: "hub",
m: "menu",
p: "panel",
v: "pivot",
w: "workitemopen",
n: "<none>",
none: "<none>",
};
function getSamplesToRemove(selection: string, availableSamples: string[]) {
const split = selection
.split(/,|;|\s+/)
.filter(v => v.trim().length > 0)
.map(v => v.toLowerCase().trim())
.map(v => shorthandMap[v] || v);
if (split.indexOf("<all>") >= 0) {
return [];
}
if (split.indexOf("<none>") >= 0) {
return [...availableSamples];
}
return availableSamples.filter(as => split.indexOf(as.toLowerCase()) === -1);
}
export function getCommand(args: string[]): TfCommand<extBase.ExtensionArguments, InitResult> {
return new ExtensionInit(args);
}
export interface InitResult {
path: string;
}
export class ExtensionInit extends extBase.ExtensionBase<InitResult> {
protected description = "Initialize a directory for development of a new Azure DevOps extension.";
protected serverCommand = false;
constructor(passedArgs: string[]) {
super(passedArgs);
}
protected setCommandArgs(): void {
super.setCommandArgs();
this.registerCommandArgument(
"path",
"Path",
"Path to the folder where the extension will be initialized. Must be an empty folder.",
args.FilePathsArgument,
process.cwd(),
);
this.registerCommandArgument(
"branch",
"Branch",
"Branch name for sample repository (default: master)",
args.StringArgument,
"master",
true,
);
this.registerCommandArgument(
"zipUri",
"Zip URI",
"URI to a zip file containing the sample extension. {{branch}} is replaced with the --branch argument value.",
args.StringArgument,
"https://codeload.github.com/Microsoft/azure-devops-extension-sample/zip/{{branch}}",
true,
);
this.registerCommandArgument(
"noDownload",
"No Download",
"Do not download or extract the sample package. Instead use the given folder assuming package already exists there.",
args.BooleanArgument,
"false",
true,
);
this.registerCommandArgument(
"npmPath",
"NPM path",
"Command line to invoke npm. May need to include the node executable.",
args.StringArgument,
"npm",
);
this.registerCommandArgument(
"publisher",
"Publisher ID",
"Publisher ID for this extension. Create a publisher at https://marketplace.visualstudio.com/manage.",
args.StringArgument,
);
this.registerCommandArgument("extensionId", "Extension ID", "This extension's ID.", args.StringArgument);
this.registerCommandArgument("extensionName", "Extension name", "Friendly name of this extension.", args.StringArgument);
this.registerCommandArgument(
"samples",
colors.white("Which samples do you want to start with?") +
colors.gray(
" You may specifiy multiple (comma-separated).\nFor descriptions, see https://github.com/Microsoft/azure-devops-extension-sample.\n (A)ll, \n (F)eature, \n (H)ub, \n (M)enu, \n (P)anel, \n Pi(v)ot, \n (W)orkItemOpen, \n (N)one - empty project\n",
),
"Specify which samples to include in the new extension.",
args.StringArgument,
undefined,
false,
"All",
);
}
protected getHelpArgs(): string[] {
return ["path", "branch", "zipUri", "noDownload", "npmPath", "publisher", "extensionId", "extensionName"];
}
public async exec(): Promise<InitResult> {
trace.info("");
trace.info(colors.yellow(" -- New Azure DevOps Extension --"));
trace.info("");
trace.info(colors.cyan("For all options and help, run `tfx extension init --help`"));
trace.info("");
const initPath = (await this.commandArgs.path.val())[0];
const noDownload = await this.commandArgs.noDownload.val();
await this.createFolderIfNotExists(initPath);
const isFolder = await this.checkIsFolder(initPath);
if (!isFolder) {
throw new Error("Given path is not a folder: " + initPath);
}
const isAccessible = await this.checkFolderAccessible(initPath);
if (!isAccessible) {
throw new Error("Could not access folder for reading and writing: " + initPath);
}
if (!noDownload) {
const isEmpty = await this.checkFolderIsEmpty(initPath, ["node_modules"]);
if (!isEmpty) {
throw new Error("Folder is not empty: " + initPath);
}
}
const branch = await this.commandArgs.branch.val();
const samplePackageUri = (await this.commandArgs.zipUri.val()).replace("{{branch}}", encodeURIComponent(branch));
const npmPath = await this.commandArgs.npmPath.val();
const extensionPublisher = await this.commandArgs.publisher.val();
const extensionId = await this.commandArgs.extensionId.val();
const extensionName = await this.commandArgs.extensionName.val();
const wantedSamples = await this.commandArgs.samples.val();
// If --no-download is passed, do not download or unpack the zip file. Assume the folder's contents contain the zip file.
if (!noDownload) {
const downloadedZipPath = path.join(initPath, "azure-devops-extension-sample.zip");
const zipFile = fs.createWriteStream(downloadedZipPath);
let bytesReceived = 0;
try {
await new Promise((resolve, reject) => {
trace.info("Downloading sample package from " + samplePackageUri);
http.get(samplePackageUri, response => {
response
.on("data", chunk => {
bytesReceived += chunk.length;
zipFile.write(chunk);
})
.on("end", () => {
zipFile.end(resolve);
})
.on("error", err => {
reject(err);
});
}).on("error", err => {
reject(err);
});
});
} catch (e) {
fs.unlink(downloadedZipPath, err => {});
throw new Error("Failed to download sample package from " + samplePackageUri + ". Error: " + e);
}
trace.info(`Package downloaded to ${downloadedZipPath} (${Math.round(bytesReceived / 1000)} kB)`);
// Crack open the zip file.
try {
await new Promise((resolve, reject) => {
fs.readFile(downloadedZipPath, async (err, data) => {
if (err) {
reject(err);
} else {
await jszip.loadAsync(data).then(async zip => {
// Write each file in the zip to the file system in the same directory as the zip file.
for (const fileName of Object.keys(zip.files)) {
trace.debug("Save file " + fileName);
await zip.files[fileName].async("nodebuffer").then(async buffer => {
trace.debug("Writing buffer for " + fileName);
const noLeadingFolderFileName = fileName.substr(fileName.indexOf("/"));
const fullPath = path.join(initPath, noLeadingFolderFileName);
if (fullPath.endsWith("\\") || fullPath.endsWith("/")) {
// don't need to "write" the folders since they are handled by createFolderIfNotExists().
return;
}
trace.debug("Creating folder if it doesn't exist: " + path.dirname(fullPath));
await this.createFolderIfNotExists(path.dirname(fullPath));
fs.writeFile(fullPath, buffer, err => {
if (err) {
console.log("err: " + err);
reject(err);
}
});
});
}
});
resolve(void 0);
}
});
});
} catch (e) {
await this.deleteFolderContents(initPath);
throw new Error(`Error unzipping ${downloadedZipPath}: ${e}`);
}
trace.debug("Delete zip file " + downloadedZipPath);
await promisify(fs.unlink)(downloadedZipPath);
}
trace.debug("Getting available samples");
const samplesPath = path.join(initPath, "src", "Samples");
const samplesList = await promisify(fs.readdir)(samplesPath);
let samplesToRemove = getSamplesToRemove(wantedSamples, samplesList);
const includesHub = samplesToRemove.indexOf("Hub") >= 0;
const includesBcs = samplesToRemove.indexOf("BreadcrumbService") >= 0;
if (includesHub && !includesBcs) {
samplesToRemove.push("BreadcrumbService");
} else if (!includesHub && includesBcs) {
samplesToRemove = samplesToRemove.filter(s => s !== "BreadcrumbService");
}
const includedSamples = samplesList.filter(s => samplesToRemove.indexOf(s) === -1 && s !== "BreadcrumbService");
if (includedSamples.length > 0) {
trace.info("Including the following samples: ");
trace.info(
includedSamples.map(s => {
const text = s === "Hub" ? s + " (with BreadcrumbService)" : s;
return " - " + text;
}),
);
trace.debug("Deleting the following samples: " + samplesToRemove.join(", "));
for (const sampleToRemove of samplesToRemove) {
await this.deleteNonEmptyFolder(path.join(samplesPath, sampleToRemove));
}
} else {
trace.info("Including no samples (starting with an empty project).");
const webpackConfigPath = path.join(initPath, "webpack.config.js");
// Delete all the samples
await this.deleteNonEmptyFolder(samplesPath);
await promisify(fs.unlink)(path.join(initPath, "src", "Common.scss"));
await promisify(fs.unlink)(path.join(initPath, "src", "Common.tsx"));
// Update the webpack config and create a dummy js file in src.
await promisify(fs.unlink)(webpackConfigPath);
await promisify(fs.writeFile)(webpackConfigPath, basicWebpackConfig, "utf8");
await promisify(fs.writeFile)(path.join(initPath, "src", "index.js"), "", "utf8");
// Delete tsconfig, overview.md, and truncate readme
await promisify(fs.unlink)(path.join(initPath, "tsconfig.json"));
await promisify(fs.unlink)(path.join(initPath, "overview.md"));
await promisify(fs.writeFile)(path.join(initPath, "README.md"), "", "utf8");
}
trace.info("Updating azure-devops-extension.json with publisher ID, extension ID, and extension name.");
const mainManifestPath = path.join(initPath, "azure-devops-extension.json");
const manifestContents = await promisify(fs.readFile)(mainManifestPath, "utf8");
const packageJsonPath = path.join(initPath, "package.json");
const packageJsonContents = await promisify(fs.readFile)(packageJsonPath, "utf8");
const newManifest = jsonInPlace(manifestContents)
.set("publisher", extensionPublisher)
.set("id", extensionId)
.set("name", extensionName)
.set("version", "1.0.0")
.set("description", "Azure DevOps Extension")
.set("categories", ["Azure Repos", "Azure Boards", "Azure Pipelines", "Azure Test Plans", "Azure Artifacts"]);
const newPackageJson = jsonInPlace(packageJsonContents)
.set("repository.url", "")
.set("description", extensionName)
.set("name", extensionId)
.set("version", "1.0.0");
const newManifestObj = JSON.parse(newManifest.toString());
if (includedSamples.length === 0) {
newPackageJson
.set("scripts.package-extension", "tfx extension create --manifests azure-devops-extension.json")
.set("scripts.publish-extension", "tfx extension publish --manifests azure-devops-extension.json")
.set("devDependencies", basicDevDependencies);
delete newManifestObj["icons"];
delete newManifestObj["content"];
}
await promisify(fs.writeFile)(mainManifestPath, JSON.stringify(newManifestObj, null, 4), "utf8");
await promisify(fs.writeFile)(packageJsonPath, newPackageJson.toString(), "utf8");
// Check for existence of node_modules. If it's not there, try installing the package.
const nodeModulesPath = path.join(initPath, "node_modules");
const alreadyInstalled = await this.checkFolderAccessible(nodeModulesPath);
if (!alreadyInstalled) {
trace.debug("No node_modules folder found.");
trace.info("Running `" + npmPath + " install` in " + initPath + "... please wait.");
await new Promise((resolve, reject) => {
const npmCommand = exec(
npmPath + " install",
{
cwd: initPath,
},
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
},
);
});
} else {
trace.info(`The folder "${nodeModulesPath}" already exists. Foregoing npm install.`);
}
// Yes, this is a lie. We're actually going to run tfx extension create manually.
trace.info("Building sample with `npm run build`");
await new Promise((resolve, reject) => {
const npmCommand = exec(
npmPath + " run compile:dev",
{
cwd: initPath,
},
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
},
);
});
trace.debug("Building extension package.");
const manifestGlobs = ["azure-devops-extension.json"];
if (includedSamples.length > 0) {
manifestGlobs.push("src/Samples/**/*.json");
}
const createResult = await createExtension(
{
manifestGlobs: manifestGlobs,
revVersion: false,
bypassValidation: includedSamples.length === 0, // need to bypass validation when there are no contributions
locRoot: null,
manifestJs: null,
env: null,
manifests: null,
overrides: {},
root: initPath,
json5: false,
},
{
locRoot: null,
metadataOnly: false,
outputPath: initPath,
},
);
return { path: initPath } as InitResult;
}
protected friendlyOutput(data: InitResult): void {
trace.info(colors.green("\n=== Completed operation: initialize extension ==="));
trace.info(`Azure DevOps Extension initialized in "${data.path}".`);
trace.info("");
trace.info(colors.red("Don't forget to update the package.json file with relevant details about your project and update LICENSE as necessary."));
}
private async checkFolderIsEmpty(folderPath: string, allowedNames: string[] = []) {
const files = (await promisify(fs.readdir)(folderPath)).filter(n => allowedNames.indexOf(n) === -1);
return files.length === 0;
}
private async checkIsFolder(folderPath: string) {
const lstat = await promisify(fs.lstat)(folderPath);
return lstat.isDirectory();
}
private async checkFolderAccessible(folderPath: string) {
try {
const access = await promisify(fs.access)(folderPath, fs.constants.W_OK | fs.constants.R_OK);
return true;
} catch {
return false;
}
}
private async deleteFolderContents(folderPath: string) {
trace.debug("Deleting contents of " + folderPath);
if (folderPath.length <= 3 || !path.isAbsolute(folderPath)) {
throw new Error("Are you really trying to delete " + folderPath + " ?");
}
const files = await promisify(fs.readdir)(folderPath);
for (const file of files) {
const fullName = path.join(folderPath, file);
const stat = await promisify(fs.lstat)(fullName);
if (stat.isDirectory()) {
await this.deleteFolderContents(fullName);
await promisify(fs.rmdir)(fullName);
} else {
await promisify(fs.unlink)(fullName);
}
}
}
private async deleteNonEmptyFolder(folderPath: string) {
await this.deleteFolderContents(folderPath);
await promisify(fs.rmdir)(folderPath);
}
private async createFolderIfNotExists(folderPath: string) {
try {
await mkdirp(folderPath);
} catch {
// folder already exists, perhaps. Or maybe we can't write to it.
}
}
}