|
| 1 | +import * as fs from "original-fs"; |
| 2 | +import { ConfigurationTarget, env, window } from "vscode"; |
| 3 | +import { access, exists, writeFile } from "./fs"; |
| 4 | +import { configuration } from "./helpers/configuration"; |
| 5 | +import { hasSupportToDecorationProvider } from "./util"; |
| 6 | + |
| 7 | +enum ProposedType { |
| 8 | + PRODUCT = "product", |
| 9 | + ARGUMENT = "argument", |
| 10 | + NONE = "none", |
| 11 | +} |
| 12 | + |
| 13 | +export async function checkProposedApi() { |
| 14 | + |
| 15 | + if (hasSupportToDecorationProvider()) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + let status: ProposedType | null | undefined = null; |
| 20 | + status = configuration.get<ProposedType | null>("enableProposedApi", null); |
| 21 | + |
| 22 | + if (!status) { |
| 23 | + status = await promptProposedApi(); |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + setProposedApi(status); |
| 28 | + } catch (error) { |
| 29 | + console.error(error); |
| 30 | + await window.showErrorMessage("Failed to configure proposed features for SVN"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function promptProposedApi() { |
| 35 | + const product = "Yes, edit product.json"; |
| 36 | + const argument = "Yes, with start argument"; |
| 37 | + const none = "No"; |
| 38 | + const choice = await window.showWarningMessage( |
| 39 | + `Would you like to enable proposed features for SVN? |
| 40 | + More info [here](https://github.com/JohnstonCode/svn-scm#experimental)`, |
| 41 | + product, |
| 42 | + argument, |
| 43 | + none |
| 44 | + ); |
| 45 | + |
| 46 | + switch (choice) { |
| 47 | + case product: |
| 48 | + return ProposedType.PRODUCT; |
| 49 | + case argument: |
| 50 | + return ProposedType.ARGUMENT; |
| 51 | + case none: |
| 52 | + return ProposedType.NONE; |
| 53 | + } |
| 54 | + |
| 55 | + return undefined; |
| 56 | +} |
| 57 | + |
| 58 | +export async function setProposedApi(status?: ProposedType) { |
| 59 | + switch (status) { |
| 60 | + case ProposedType.PRODUCT: |
| 61 | + enableProposedProduct(); |
| 62 | + break; |
| 63 | + case ProposedType.ARGUMENT: |
| 64 | + enableProposedArgument(); |
| 65 | + break; |
| 66 | + case ProposedType.NONE: |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + if (status) { |
| 71 | + configuration.update("enableProposedApi", status, ConfigurationTarget.Global); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +async function enableProposedProduct() { |
| 76 | + const productPath = env.appRoot + "/product.json"; |
| 77 | + |
| 78 | + if (!await exists(productPath)) { |
| 79 | + window.showErrorMessage(`Can't find the "product.json" of VSCode.`); |
| 80 | + return; |
| 81 | + } |
| 82 | + if (!await access(productPath, fs.constants.W_OK)) { |
| 83 | + window.showErrorMessage(`The "product.json" of VSCode is not writable. |
| 84 | + Please, append "johnstoncode.svn-scm" on "extensionAllowedProposedApi" array`); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const productJson = require(productPath) as { |
| 89 | + extensionAllowedProposedApi: string[], |
| 90 | + [key: string]: any; |
| 91 | + }; |
| 92 | + |
| 93 | + productJson.extensionAllowedProposedApi = productJson.extensionAllowedProposedApi || []; |
| 94 | + |
| 95 | + if (productJson.extensionAllowedProposedApi.includes("johnstoncode.svn-scm")) { |
| 96 | + return; |
| 97 | + } |
| 98 | + productJson.extensionAllowedProposedApi.push("johnstoncode.svn-scm"); |
| 99 | + |
| 100 | + await writeFile(productPath, JSON.stringify(productJson, null, 2)); |
| 101 | + |
| 102 | + const message = "SVN proposed features enabled, please restart VSCode"; |
| 103 | + |
| 104 | + window.showInformationMessage(message); |
| 105 | +} |
| 106 | + |
| 107 | +async function enableProposedArgument() { |
| 108 | + const packagePath = __dirname + "/../package.json"; |
| 109 | + |
| 110 | + const packageJson = require(packagePath); |
| 111 | + |
| 112 | + if (!packageJson || packageJson.enableProposedApi !== false) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + packageJson.enableProposedApi = true; |
| 117 | + await writeFile(packagePath, JSON.stringify(packageJson, null, 2)); |
| 118 | + |
| 119 | + const message = `SVN proposed features enabled, |
| 120 | + please close the VSCode and run with: --enable-proposed-api johnstoncode.svn-scm`; |
| 121 | + |
| 122 | + window.showInformationMessage(message); |
| 123 | +} |
0 commit comments