-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathpreRelease.js
More file actions
97 lines (86 loc) · 3.84 KB
/
Copy pathpreRelease.js
File metadata and controls
97 lines (86 loc) · 3.84 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
/* eslint-disable */
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const execShellCommand = async (cmd) => {
return new Promise((resolve, reject) => {
exec(cmd, { maxBuffer: 1024 * 500 }, (error, stdout, stderr) => {
if (error) {
reject(error);
} else if (stderr) {
// Most cli programs output logs to stderr
// Breaking errors would be rejected in the prior case
console.log(stderr);
resolve(stderr);
} else {
resolve(stdout);
}
});
});
};
const buildAndGetIntegrityHash = async () => {
const relativePathToManifestJson = '../../packages/teams-js/dist/umd/MicrosoftTeams-manifest.json';
const absolutePathToManifestJson = path.resolve(__dirname, relativePathToManifestJson);
console.log('Building @microsoft/teams-js');
await execShellCommand('pnpm install');
await execShellCommand('pnpm build');
if (!fs.existsSync(absolutePathToManifestJson)) {
throw `ERROR: MicrosoftTeams-manifest.json at ${absolutePathToManifestJson} was not found.`;
}
const manifestFile = fs.readFileSync(absolutePathToManifestJson);
const manifestJson = JSON.parse(manifestFile);
const integrityHash = manifestJson['MicrosoftTeams.min.js']['integrity'];
if (!integrityHash) {
throw new Error('MicrosoftTeams.min.js integrity hash value was not parsed');
}
return integrityHash;
};
const updatePackageJson = (absolutePath, version) => {
console.log(`Updating ${absolutePath} version to ${version}`);
if (!fs.existsSync(absolutePath)) {
throw `ERROR: ${absolutePath} was not found.`;
}
const packageJson = fs.readFileSync(absolutePath, 'utf8');
const newPackageJson = packageJson.replace(/"version": ".*"/, `"version": "${version}"`);
fs.writeFileSync(absolutePath, newPackageJson);
};
const updateVersionAndIntegrity = async (absolutePath, version, integrityHash) => {
console.log(`Updating ${absolutePath} with new version and integrity hash`);
if (!fs.existsSync(absolutePath)) {
throw `ERROR: README.md at ${absolutePath} was not found.`;
}
const readme = fs.readFileSync(absolutePath, 'utf8');
const result = readme
.replace(/integrity=".*?"/g, `integrity="${integrityHash}"`)
.replace(
/res\.cdn\.office.net\/teams-js\/.*\/js\/MicrosoftTeams\.min\.js/g,
`res.cdn.office.net/teams-js/${version}/js/MicrosoftTeams.min.js`,
)
.replace(
/node_modules\/@microsoft\/teams-js@.*\/dist\/MicrosoftTeams\.min\.js/g,
`node_modules/@microsoft/teams-js@${version}/dist/MicrosoftTeams.min.js`,
);
fs.writeFileSync(absolutePath, result);
};
(async () => {
try {
const relativePathToTeamsJsPackageJson = '../../packages/teams-js/package.json';
const relativePathToTeamsJsReadme = '../../packages/teams-js/README.md';
const relativePathToTestAppPackageJson = '../../apps/teams-test-app/package.json';
const relativePathToTestAppHtml = '../../apps/teams-test-app/index_cdn.html';
const absolutePathToTeamsJsPackageJson = path.resolve(__dirname, relativePathToTeamsJsPackageJson);
const absolutePathTestAppPackageJson = path.resolve(__dirname, relativePathToTestAppPackageJson);
const absolutePathToTeamsJsReadme = path.resolve(__dirname, relativePathToTeamsJsReadme);
const absolutePathToTestAppHtml = path.resolve(__dirname, relativePathToTestAppHtml);
await execShellCommand('pnpm beachball bump');
let version = require(relativePathToTeamsJsPackageJson).version;
updatePackageJson(absolutePathTestAppPackageJson, version);
const integrityHash = await buildAndGetIntegrityHash();
updateVersionAndIntegrity(absolutePathToTeamsJsReadme, version, integrityHash);
updateVersionAndIntegrity(absolutePathToTestAppHtml, version, integrityHash);
} catch (e) {
console.log('Something went wrong!');
console.error(e);
process.exit(1);
}
})();