microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulp_scripts/release.js
125lines · modeblame
c29f0740benjaminbi3 years ago | 1 | const gulp = require("gulp"); |
| 2 | const log = require("fancy-log"); | |
| 3 | const path = require("path"); | |
| 4 | const os = require("os"); | |
| 5 | const fs = require("fs"); | |
| 6 | const GulpExtras = require("../tools/gulp-extras"); | |
| 7 | | |
| 8 | const isNightly = process.argv.includes("--nightly"); | |
e9123b75Ezio Li10 months ago | 9 | const useNpm = process.argv.includes("--no-yarn"); |
3016b3b0Ezio Li9 months ago | 10 | const isPrepublish = process.argv.includes("--no-prepublish"); |
b203fbbfbenjaminbi3 years ago | 11 | const extensionName = isNightly ? "vscode-react-native-preview" : "vscode-react-native"; |
c29f0740benjaminbi3 years ago | 12 | const executeCommand = GulpExtras.executeCommand; |
| 13 | | |
| 14 | function release(cb) { | |
| 15 | prepareLicenses(); | |
| 16 | cb(); | |
| 17 | } | |
| 18 | | |
| 19 | function prepareLicenses() { | |
| 20 | const backupFiles = [ | |
| 21 | "LICENSE.txt", | |
| 22 | "ThirdPartyNotices.txt", | |
| 23 | "package.json", | |
| 24 | "package-lock.json", | |
| 25 | ]; | |
| 26 | const backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native")); | |
| 27 | if (!fs.existsSync(backupFolder)) { | |
| 28 | fs.mkdirSync(backupFolder); | |
| 29 | } | |
| 30 | | |
| 31 | return Promise.resolve() | |
| 32 | .then(() => { | |
| 33 | /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */ | |
| 34 | log("Backing up license files to " + backupFolder + "..."); | |
| 35 | backupFiles.forEach(fileName => { | |
| 36 | fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName)); | |
| 37 | }); | |
| 38 | | |
| 39 | /* copy over the release package license files */ | |
| 40 | log("Preparing license files for release..."); | |
| 41 | fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt")); | |
| 42 | fs.writeFileSync( | |
| 43 | "ThirdPartyNotices.txt", | |
| 44 | fs.readFileSync("release/ThirdPartyNotices.txt"), | |
| 45 | ); | |
| 46 | }) | |
| 47 | .then(() => { | |
| 48 | let packageJson = readJson("/package.json"); | |
| 49 | packageJson.main = "/dist/rn-extension"; | |
3016b3b0Ezio Li9 months ago | 50 | |
| 51 | if (isPrepublish) { | |
| 52 | log("Ignore vscode:prepublish script before vsce package"); | |
| 53 | const scripts = packageJson.scripts; | |
| 54 | delete scripts["vscode:prepublish"]; | |
| 55 | } | |
| 56 | | |
c29f0740benjaminbi3 years ago | 57 | if (isNightly) { |
dd8d7777Ezio Li1 years ago | 58 | log("Performing gulp release..."); |
c29f0740benjaminbi3 years ago | 59 | packageJson.version = getVersionNumber(); |
| 60 | packageJson.name = extensionName; | |
| 61 | packageJson.preview = true; | |
| 62 | packageJson.displayName += " (Preview)"; | |
| 63 | } | |
| 64 | writeJson("/package.json", packageJson); | |
| 65 | log("Creating release package..."); | |
| 66 | return new Promise((resolve, reject) => { | |
| 67 | // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies. | |
e9123b75Ezio Li10 months ago | 68 | let vsceArgs = ["package"]; |
| 69 | if (useNpm) { | |
| 70 | vsceArgs = ["package", "--no-yarn"]; | |
| 71 | log("Using npm for vsce packaging..."); | |
| 72 | } else { | |
| 73 | log("Using yarn for vsce packaging..."); | |
| 74 | } | |
c29f0740benjaminbi3 years ago | 75 | executeCommand( |
| 76 | "vsce", | |
e9123b75Ezio Li10 months ago | 77 | vsceArgs, |
c29f0740benjaminbi3 years ago | 78 | arg => { |
| 79 | if (arg) { | |
| 80 | reject(arg); | |
| 81 | } | |
| 82 | resolve(); | |
| 83 | }, | |
| 84 | { cwd: appRoot }, | |
| 85 | ); | |
| 86 | }); | |
| 87 | }) | |
| 88 | .finally(() => { | |
| 89 | /* restore backed up files */ | |
| 90 | log("Restoring modified files..."); | |
| 91 | backupFiles.forEach(fileName => { | |
| 92 | fs.writeFileSync( | |
| 93 | path.join(appRoot, fileName), | |
| 94 | fs.readFileSync(path.join(backupFolder, fileName)), | |
| 95 | ); | |
| 96 | }); | |
| 97 | }); | |
| 98 | } | |
| 99 | | |
| 100 | function readJson(file) { | |
| 101 | const contents = fs.readFileSync(path.join(appRoot, file), "utf-8").toString(); | |
| 102 | return JSON.parse(contents); | |
| 103 | } | |
| 104 | | |
| 105 | function writeJson(file, jsonObj) { | |
| 106 | const content = JSON.stringify(jsonObj, null, 2); | |
| 107 | fs.writeFileSync(path.join(appRoot, file), content); | |
| 108 | } | |
| 109 | | |
| 110 | const getVersionNumber = () => { | |
| 111 | const date = new Date(new Date().toLocaleString("en-US", { timeZone: "America/Los_Angeles" })); | |
| 112 | | |
| 113 | return [ | |
| 114 | // YY | |
| 115 | date.getFullYear(), | |
| 116 | // MM, | |
| 117 | date.getMonth() + 1, | |
| 118 | //DDHH | |
| 119 | `${date.getDate()}${String(date.getHours()).padStart(2, "0")}`, | |
| 120 | ].join("."); | |
| 121 | }; | |
| 122 | | |
| 123 | module.exports = { | |
| 124 | release, | |
e4001e74benjaminbi3 years ago | 125 | }; |