Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --overwrite flag to staticrypt #200

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ staticrypt dir_to_encrypt/* -r
staticrypt dir_to_encrypt/* -r -d dir_to_encrypt
```

#### Overwrite files in-place

```bash
# Use the --overwrite flag to overwrite the original files in-place
staticrypt dir_to_encrypt/* -r --overwrite
```

#### Get a shareable auto-decrypt link

The link contains the hashed password, that will auto-decrypt the file - you can include your file URL or leave blank. (⚠️ you should keep your `.staticrypt.json` so the salt is the same each time you encrypt, or re-encrypting will [invalidate the link](#why-does-staticrypt-create-a-config-file)):
Expand Down Expand Up @@ -153,6 +160,8 @@ The password argument is optional if `STATICRYPT_PASSWORD` is set in the environ
[string] [default: "encrypted"]
--decrypt Include this flag to decrypt files instead of
encrypt. [boolean] [default: false]
--overwrite Overwrite the original files in-place.
[boolean] [default: false]
-p, --password The password to encrypt your file with. Leave
empty to be prompted for it. If
STATICRYPT_PASSWORD is set in the env, we'll
Expand Down
18 changes: 14 additions & 4 deletions cli/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ exports.genFile = genFile;
* @param {string} path
* @param {string} fullRootDirectory
* @param {string} outputDirectory
* @param {boolean} overwrite
* @returns {string}
*/
function getFullOutputPath(path, fullRootDirectory, outputDirectory) {
function getFullOutputPath(path, fullRootDirectory, outputDirectory, overwrite) {
if (overwrite) {
return path;
}
const relativePath = pathModule.relative(fullRootDirectory, path);
return outputDirectory + "/" + relativePath;
}
Expand Down Expand Up @@ -339,17 +343,18 @@ exports.isCustomPasswordTemplateDefault = isCustomPasswordTemplateDefault;
* @param {string} path
* @param {string} outputDirectory
* @param {string} rootDirectory
* @param {boolean} overwrite
* @param {(fullPath: string, rootDirectoryFromArgument: string) => void} callback
*/
function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, rootDirectory = "") {
function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, rootDirectory = "", overwrite = false) {
const fullPath = pathModule.resolve(path);
const fullRootDirectory = rootDirectory || pathModule.dirname(fullPath);

if (fs.statSync(fullPath).isDirectory()) {
fs.readdirSync(fullPath).forEach((filePath) => {
const fullFilePath = `${fullPath}/${filePath}`;

recursivelyApplyCallbackToHtmlFiles(callback, fullFilePath, outputDirectory, fullRootDirectory);
recursivelyApplyCallbackToHtmlFiles(callback, fullFilePath, outputDirectory, fullRootDirectory, overwrite);
});
return;
}
Expand All @@ -360,7 +365,7 @@ function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, ro
}
// else just copy the file as is
else {
const fullOutputPath = getFullOutputPath(fullPath, fullRootDirectory, outputDirectory);
const fullOutputPath = getFullOutputPath(fullPath, fullRootDirectory, outputDirectory, overwrite);
copyFile(fullPath, fullOutputPath);
}
}
Expand Down Expand Up @@ -388,6 +393,11 @@ function parseCommandLineArguments() {
describe: "Include this flag to decrypt files instead of encrypt.",
default: false,
})
.option("overwrite", {
type: "boolean",
describe: "Overwrite the original files in-place.",
default: false,
})
.option("p", {
alias: "password",
type: "string",
Expand Down
9 changes: 5 additions & 4 deletions cli/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ async function runStatiCrypt() {
);
},
path,
namedArgs.directory
namedArgs.directory,
"",
namedArgs.overwrite
);
});
}
Expand Down Expand Up @@ -223,10 +225,9 @@ async function encodeAndGenerateFile(
};

// remove the base path so that the actual output path is relative to the base path
const relativePath = pathModule.relative(rootDirectoryFromArguments, path);
const outputFilepath = namedArgs.directory + "/" + relativePath;
const outputFilepath = getFullOutputPath(path, rootDirectoryFromArguments, namedArgs.directory, namedArgs.overwrite);

genFile(templateData, outputFilepath, namedArgs.template);
}

runStatiCrypt();
runStatiCrypt();