UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

1 lines 4.32 kB
{"version":3,"file":"chmod-x.mjs","names":[],"sources":["../src/chmod-x.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { accessSync, chmodSync, constants, statSync } from \"node:fs\";\nimport { access, chmod } from \"node:fs/promises\";\n\n/**\n * Adds execute permissions to a file\n *\n * @param file - The file to add execute permissions to\n */\nexport function chmodXSync(file: string) {\n // Note: skip for windows as chmod does on exist there\n // and will error with `EACCES: permission denied`\n if (process.platform === \"win32\") {\n return;\n }\n\n const s = statSync(file);\n const newMode = s.mode | 64 | 8 | 1;\n\n if (s.mode === newMode) {\n return;\n }\n const base8 = newMode.toString(8).slice(-3);\n\n chmodSync(file, base8);\n}\n\n/**\n * Adds execute permissions to a file\n *\n * @param file - The file to add execute permissions to\n */\nexport async function chmodX(file: string) {\n // Note: skip for windows as chmod does on exist there\n // and will error with `EACCES: permission denied`\n if (process.platform === \"win32\") {\n return;\n }\n\n const s = statSync(file);\n const newMode = s.mode | 64 | 8 | 1;\n\n if (s.mode === newMode) {\n return;\n }\n const base8 = newMode.toString(8).slice(-3);\n\n return chmod(file, base8);\n}\n\n/**\n * Checks the write permission of a file\n *\n * @param filename - The file to check the permission of\n * @returns A promise that resolves to true if the file is writable, false otherwise\n */\nexport async function isWritable(filename: string): Promise<boolean> {\n try {\n await access(filename, constants.W_OK);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Checks the write permission of a file\n *\n * @param filename - The file to check the permission of\n * @returns True if the file is writable, false otherwise\n */\nexport function isWritableSync(filename: string): boolean {\n try {\n accessSync(filename, constants.W_OK);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Checks the execute permission of a file\n *\n * @param filename - The file to check the permission of\n * @returns A promise that resolves to true if the file is executable, false otherwise\n */\nexport async function isExecutable(filename: string): Promise<boolean> {\n try {\n await access(filename, constants.X_OK);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Checks the execute permission of a file\n *\n * @param filename - The file to check the permission of\n * @returns True if the file is executable, false otherwise\n */\nexport function isExecutableSync(filename: string): boolean {\n try {\n accessSync(filename, constants.X_OK);\n return true;\n } catch {\n return false;\n }\n}\n"],"mappings":";;;;;;;;;AA0BA,SAAgB,WAAW,MAAc;AAGvC,KAAI,QAAQ,aAAa,QACvB;CAGF,MAAM,IAAI,SAAS,KAAK;CACxB,MAAM,UAAU,EAAE,OAAO;AAEzB,KAAI,EAAE,SAAS,QACb;AAIF,WAAU,MAFI,QAAQ,SAAS,EAAE,CAAC,MAAM,GAEnB,CAAC;;;;;;;AAQxB,eAAsB,OAAO,MAAc;AAGzC,KAAI,QAAQ,aAAa,QACvB;CAGF,MAAM,IAAI,SAAS,KAAK;CACxB,MAAM,UAAU,EAAE,OAAO;AAEzB,KAAI,EAAE,SAAS,QACb;AAIF,QAAO,MAAM,MAFC,QAAQ,SAAS,EAAE,CAAC,MAAM,GAEhB,CAAC;;;;;;;;AAS3B,eAAsB,WAAW,UAAoC;AACnE,KAAI;AACF,QAAM,OAAO,UAAU,UAAU,KAAK;AACtC,SAAO;SACD;AACN,SAAO;;;;;;;;;AAUX,SAAgB,eAAe,UAA2B;AACxD,KAAI;AACF,aAAW,UAAU,UAAU,KAAK;AACpC,SAAO;SACD;AACN,SAAO;;;;;;;;;AAUX,eAAsB,aAAa,UAAoC;AACrE,KAAI;AACF,QAAM,OAAO,UAAU,UAAU,KAAK;AACtC,SAAO;SACD;AACN,SAAO;;;;;;;;;AAUX,SAAgB,iBAAiB,UAA2B;AAC1D,KAAI;AACF,aAAW,UAAU,UAAU,KAAK;AACpC,SAAO;SACD;AACN,SAAO"}