1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const ssri = require("ssri");
|
7 | const fs = require("fs");
|
8 | const { version } = require("./package.json");
|
9 |
|
10 | const readmeMdFilePath = "./README.md";
|
11 | const cdnUsageFilePath = "./docs/cdn-usage.md";
|
12 | const sriTableMarker = "<!-- SRI_TABLE_START -->";
|
13 | const sriTableMarkerOffset = 3;
|
14 |
|
15 | const latestVersionMarker = "<!-- CDN_LATEST -->";
|
16 | const latestVersionMarkerOffset = 2;
|
17 |
|
18 | const latestVersionString = `<script type="text/javascript" src="https://alcdn.msauth.net/lib/${version}/js/msal.min.js"></script>`;
|
19 |
|
20 | async function generateSris() {
|
21 |
|
22 | const readmeMd = await fs.promises.readFile(readmeMdFilePath, "utf8");
|
23 | const readMeMdLines = readmeMd.toString().split(/\r?\n/);
|
24 |
|
25 |
|
26 | const readmeInsertLineIndex = readMeMdLines.indexOf(latestVersionMarker);
|
27 | const readMeInsertIndex = readmeInsertLineIndex + latestVersionMarkerOffset;
|
28 | readMeMdLines.splice(readMeInsertIndex, 1, latestVersionString);
|
29 |
|
30 |
|
31 | const newReadMd = readMeMdLines.join("\n");
|
32 | await fs.promises.writeFile(readmeMdFilePath, newReadMd);
|
33 |
|
34 |
|
35 | const cdnMd = await fs.promises.readFile(cdnUsageFilePath, "utf8");
|
36 | const cdnMdLines = cdnMd.toString().split(/\r?\n/);
|
37 |
|
38 |
|
39 | const latestVersionInsertLineIndex = cdnMdLines.indexOf(latestVersionMarker);
|
40 | const latestVersionInsertIndex = latestVersionInsertLineIndex + latestVersionMarkerOffset;
|
41 | cdnMdLines.splice(latestVersionInsertIndex, 1, latestVersionString);
|
42 |
|
43 |
|
44 | const unminifiedVersionSri = await generateSriTableEntry("msal.js", " ");
|
45 | const minifiedVersionSri = await generateSriTableEntry("msal.min.js", "");
|
46 |
|
47 |
|
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 |
|
54 | const newCdnMd = cdnMdLines.join("\n");
|
55 | await fs.promises.writeFile(cdnUsageFilePath, newCdnMd);
|
56 | }
|
57 |
|
58 | async 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 |
|
68 | generateSris()
|
69 | .then(() => {
|
70 | process.exit(0);
|
71 | })
|
72 | .catch(error => {
|
73 | console.error(error);
|
74 | process.exit(1);
|
75 | });
|