UNPKG

2.84 kBJavaScriptView Raw
1/*
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License.
4 */
5
6const ssri = require("ssri");
7const fs = require("fs");
8const { version } = require("./package.json");
9
10const readmeMdFilePath = "./README.md";
11const cdnUsageFilePath = "./docs/cdn-usage.md";
12const sriTableMarker = "<!-- SRI_TABLE_START -->";
13const sriTableMarkerOffset = 3;
14
15const latestVersionMarker = "<!-- CDN_LATEST -->";
16const latestVersionMarkerOffset = 2;
17
18const latestVersionString = `<script type="text/javascript" src="https://alcdn.msauth.net/lib/${version}/js/msal.min.js"></script>`;
19
20async function generateSris() {
21 // Read contents of README md file
22 const readmeMd = await fs.promises.readFile(readmeMdFilePath, "utf8");
23 const readMeMdLines = readmeMd.toString().split(/\r?\n/);
24
25 // Update REAMDE
26 const readmeInsertLineIndex = readMeMdLines.indexOf(latestVersionMarker);
27 const readMeInsertIndex = readmeInsertLineIndex + latestVersionMarkerOffset;
28 readMeMdLines.splice(readMeInsertIndex, 1, latestVersionString);
29
30 // Write new README to disk
31 const newReadMd = readMeMdLines.join("\n");
32 await fs.promises.writeFile(readmeMdFilePath, newReadMd);
33
34 // Read contents of cdn md file
35 const cdnMd = await fs.promises.readFile(cdnUsageFilePath, "utf8");
36 const cdnMdLines = cdnMd.toString().split(/\r?\n/);
37
38 // Update basic usage
39 const latestVersionInsertLineIndex = cdnMdLines.indexOf(latestVersionMarker);
40 const latestVersionInsertIndex = latestVersionInsertLineIndex + latestVersionMarkerOffset;
41 cdnMdLines.splice(latestVersionInsertIndex, 1, latestVersionString);
42
43 // Generate entries for each file
44 const unminifiedVersionSri = await generateSriTableEntry("msal.js", " ");
45 const minifiedVersionSri = await generateSriTableEntry("msal.min.js", "");
46
47 // Add entries to table
48 const sriInsertLineIndex = cdnMdLines.indexOf(sriTableMarker);
49 const tableInsertIndex = sriInsertLineIndex + sriTableMarkerOffset;
50 cdnMdLines.splice(tableInsertIndex, 0, minifiedVersionSri);
51 cdnMdLines.splice(tableInsertIndex, 0, unminifiedVersionSri);
52
53 // Write new file to disk
54 const newCdnMd = cdnMdLines.join("\n");
55 await fs.promises.writeFile(cdnUsageFilePath, newCdnMd);
56}
57
58async function generateSriTableEntry(file, padding) {
59 const filePath = `./dist/${file}`;
60 const fileStream = fs.createReadStream(filePath);
61 const sri = await ssri.fromStream(fileStream, {
62 algorithms: [ "sha384" ]
63 });
64
65 return `${version} | ${file}${padding} | \`${sri.toString()}\``;
66}
67
68generateSris()
69 .then(() => {
70 process.exit(0);
71 })
72 .catch(error => {
73 console.error(error);
74 process.exit(1);
75 });