Skip to content

Commit

Permalink
0.4.1 (#36)
Browse files Browse the repository at this point in the history
* fix: avoid crash on CTRL-C on inquirer prompt

* chore: bump version to 0.4.1

* feat: add readAccess function

* fix: remove eslint annotations

* feat: remove parse-gitignore package and add check on read permissions

* fix: style issue

* fix: use new GH alerts in md
  • Loading branch information
matteosacchetto authored Dec 19, 2023
1 parent 0f59ce0 commit a985eeb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 59 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ Simple NodeJS CLI which allows you zip and unzip files with support for .gitigno
Install it locally with

```bash
npm i https://github.com/matteosacchetto/node-zip-cli/releases/download/v0.4.0/node-zip-cli-0.4.0.tgz
npm i https://github.com/matteosacchetto/node-zip-cli/releases/download/v0.4.1/node-zip-cli-0.4.1.tgz
```

Or install it globally with

```bash
npm i --location=global https://github.com/matteosacchetto/node-zip-cli/releases/download/v0.4.0/node-zip-cli-0.4.0.tgz
npm i --location=global https://github.com/matteosacchetto/node-zip-cli/releases/download/v0.4.1/node-zip-cli-0.4.1.tgz
```

### Other version
Expand Down Expand Up @@ -166,7 +166,8 @@ test/
└── three
```

> **NOTE**: Up to the current version (0.4.0) the list of paths to ignore which are specified with this options are applied after default ignore paths (like `.git`) BUT before any .gitignore or .zipignore file. This means that paths you specify here could be overridden by the aforementioned files.
> [!NOTE]
> Up to the current version (0.4.1) the list of paths to ignore which are specified with this options are applied after default ignore paths (like `.git`) BUT before any .gitignore or .zipignore file. This means that paths you specify here could be overridden by the aforementioned files.
###### `--allow-git`

Expand Down Expand Up @@ -215,4 +216,5 @@ Simply run this CLI providing to each command all the necessary options.

This file is meant to be placed in a folder which you plan to zip. It is meant to be used instead of the .gitignore, if the content of the folder is not related to git, or as an extension of the .gitignore, where you can specify additional rules related only to the zip file creation. The .zipignore file follow the same syntax and rules of the traditional .gitignore

> **NOTE**: Up to the current version (0.4.0) the .zipignore builds on top of already existing .gitignore rules, so if you only want to ignore some additional files you **do not need** to copy paste the content of the .gitignore. Keep in mind that this behavior may change in the future
> [!NOTE]
> Up to the current version (0.4.1) the .zipignore builds on top of already existing .gitignore rules, so if you only want to ignore some additional files you **do not need** to copy paste the content of the .gitignore. Keep in mind that this behavior may change in the future
18 changes: 2 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-zip-cli",
"version": "0.4.0",
"version": "0.4.1",
"description": "Simple NodeJS CLI which allows you to create a ZIP file with support for .gitignore files",
"main": "dist/app.mjs",
"bin": "dist/app.mjs",
Expand Down Expand Up @@ -69,7 +69,6 @@
"ignore": "^5.3.0",
"jszip": "^3.10.1",
"ora": "^7.0.1",
"parse-gitignore": "^2.0.0",
"valid-filename": "^4.0.0"
},
"tap": {
Expand Down
12 changes: 8 additions & 4 deletions src/commands/unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import chalk from 'chalk';
import figureSet from 'figures';
import { printfileListAsFileTree } from '@/utils/path-utils';
import confirm from '@inquirer/confirm';
import { exit_success_on_error_ignore } from '@/utils/process';

const name = 'unzip';
const description = 'Unzip the content of a zip file';
Expand Down Expand Up @@ -67,10 +68,13 @@ unzipCommand.action(async (options) => {
// Check if empty
const dir = await readdir(output);
if (dir.length !== 0) {
const proceed = await confirm({
message: `Directory ${output} is not empty. Proceed anyway?`,
default: false,
});
const proceed = await exit_success_on_error_ignore(
async () =>
await confirm({
message: `Directory ${output} is not empty. Proceed anyway?`,
default: false,
})
);
if (!proceed) {
return;
}
Expand Down
12 changes: 8 additions & 4 deletions src/commands/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
printfileListAsFileTree,
} from '@/utils/path-utils';
import { InvalidArgumentError } from 'commander';
import { exit_success_on_error_ignore } from '@/utils/process';

const name = 'zip';
const description =
Expand Down Expand Up @@ -70,10 +71,13 @@ zipCommand.action(async (options) => {

if (!options.yes && !options.dryRun) {
if (await exists(output)) {
const overwrite = await confirm({
default: false,
message: `The file ${output} already exists, overwrite it?`,
});
const overwrite = await exit_success_on_error_ignore(
async () =>
await confirm({
default: false,
message: `The file ${output} already exists, overwrite it?`,
})
);

if (!overwrite) {
return;
Expand Down
40 changes: 14 additions & 26 deletions src/lib/scan-fs.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
import { opendir, readFile } from 'fs/promises';
import { join } from 'path';
import parseGitignore from 'parse-gitignore';
import ignore, { Ignore } from 'ignore';
import { readAccess } from '@/utils/fs-utils';

const listDirContent = async (dir: string, parentRules: string[] = []) => {
const gitingoreRules = [...parentRules];
const gitignoreFilter: Ignore = ignore();

try {
// Read a .gitignore
const gitignorePath = join(dir, '.gitignore');
const gitignoreContent = `${await readFile(gitignorePath, {
const loadIgnoreRules = async (path: string) => {
if (await readAccess(path)) {
const gitignoreContent = `${await readFile(path, {
encoding: 'utf-8',
})}\n`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const patterns = (parseGitignore(gitignoreContent) as any).patterns;
gitingoreRules.push(...patterns);
} catch (e) {
// console.log(e);
return gitignoreContent.split('\n');
}

try {
// Read a .zipignore
const zipignorePath = join(dir, '.zipignore');
const zipignoreContent = `${await readFile(zipignorePath, {
encoding: 'utf-8',
})}\n`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const patterns = (parseGitignore(zipignoreContent) as any).patterns;
gitingoreRules.push(...patterns);
} catch (e) {
// console.log(e);
return [] as string[];
};

const listDirContent = async (dir: string, parentRules: string[] = []) => {
const gitingoreRules = [...parentRules];
const gitignoreFilter: Ignore = ignore();

for (const ingoreFile of ['.gitignore', '.zipignore']) {
gitingoreRules.push(...(await loadIgnoreRules(join(dir, ingoreFile))));
}

// Add all rules
Expand All @@ -42,7 +31,6 @@ const listDirContent = async (dir: string, parentRules: string[] = []) => {
const entryPath = join(dir, entry.name);
if (entry.isDirectory()) {
if (!gitignoreFilter.ignores(entryPath + '/')) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const subwalk = await listDirContent(entryPath, gitingoreRules);
walk.push(...subwalk);
}
Expand Down
11 changes: 10 additions & 1 deletion src/utils/fs-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stat } from 'fs/promises';
import { access, constants, stat } from 'fs/promises';

export const exists = async (p: string) => {
try {
Expand All @@ -23,3 +23,12 @@ export const isFile = async (p: string) => {
return false;
}
};

export const readAccess = async (path: string) => {
try {
await access(path, constants.R_OK);
return true;
} catch {
return false;
}
};
2 changes: 0 additions & 2 deletions src/utils/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const getFilename = (p: string) => {
};

const printObjAsFileTree = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
obj: Record<string, any>,
level = 0,
parentPrefix = ''
Expand Down Expand Up @@ -51,7 +50,6 @@ const printObjAsFileTree = (
};

export const printfileListAsFileTree = (files: string[]) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const root: Record<string, any> = {};

// Convert to object
Expand Down
10 changes: 10 additions & 0 deletions src/utils/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const exit_success_on_error_ignore = async <T extends unknown[], R>(
fn: (...params: T) => R,
...params: T
): Promise<R> => {
try {
return await fn(...params);
} catch (e) {
process.exit(0);
}
};

0 comments on commit a985eeb

Please sign in to comment.