From fba8d89f70bbf42f0ea34eff4173ed0de00ef271 Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 6 Mar 2020 15:59:19 +0800 Subject: [PATCH] feat: make Deno declaration file read-only --- server/src/server.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index 0fd3a33..359fac3 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -88,16 +88,23 @@ connection.onInitialized(async () => { const currentDenoTypesContent = await deno.getTypes(); const denoDtsFile = getDenoDts(); const isExistDtsFile = await pathExists(denoDtsFile); - const fileOptions = { encoding: "utf8" }; // if dst file not exist. then create a new one if (!isExistDtsFile) { - await fs.writeFile(denoDtsFile, currentDenoTypesContent, fileOptions); + await fs.writeFile(denoDtsFile, currentDenoTypesContent, { mode: 0o444 }); } else { - const typesContent = await fs.readFile(denoDtsFile, fileOptions); + // set it to writable + await fs.chmod(denoDtsFile, 0o666); + + const typesContent = await fs.readFile(denoDtsFile, { encoding: "utf8" }); if (typesContent.toString() !== currentDenoTypesContent.toString()) { - await fs.writeFile(denoDtsFile, currentDenoTypesContent, fileOptions); + await fs.writeFile(denoDtsFile, currentDenoTypesContent, { + mode: 0o444 + }); + + // set to readonly + await fs.chmod(denoDtsFile, 0o444); } } } catch (err) {