@ffflorian/jszip-cli 3.6.4 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { TerminalOptions } from './interfaces';
1
+ import { TerminalOptions } from './interfaces.js';
2
2
  export declare class BuildService {
3
3
  compressedFilesCount: number;
4
4
  outputFile: string | null;
@@ -1,62 +1,24 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.BuildService = void 0;
39
- const fs = __importStar(require("fs-extra"));
40
- const jszip_1 = __importDefault(require("jszip"));
41
- const logdown_1 = __importDefault(require("logdown"));
42
- const path_1 = __importDefault(require("path"));
43
- const progress_1 = __importDefault(require("progress"));
44
- const FileService_1 = require("./FileService");
45
- const glob_1 = require("glob");
46
- class BuildService {
1
+ import path from 'node:path';
2
+ import * as fs from 'fs-extra';
3
+ import JSZip from 'jszip';
4
+ import logdown from 'logdown';
5
+ import progress from 'progress';
6
+ import { globSync } from 'glob';
7
+ import { FileService } from './FileService.js';
8
+ export class BuildService {
47
9
  constructor(options) {
48
- this.fileService = new FileService_1.FileService(options);
49
- this.jszip = new jszip_1.default();
10
+ this.fileService = new FileService(options);
11
+ this.jszip = new JSZip();
50
12
  this.options = options;
51
- this.logger = (0, logdown_1.default)('jszip-cli/BuildService', {
13
+ this.logger = logdown('jszip-cli/BuildService', {
52
14
  logger: console,
53
15
  markdown: false,
54
16
  });
55
17
  this.logger.state = { isEnabled: options.verbose };
56
18
  this.entries = [];
57
19
  this.ignoreEntries = this.options.ignoreEntries.map(entry => entry instanceof RegExp ? entry : new RegExp(entry.replace(/\*/g, '.*')));
58
- this.outputFile = this.options.outputEntry ? path_1.default.resolve(this.options.outputEntry) : null;
59
- this.progressBar = new progress_1.default('Compressing [:bar] :percent :elapseds', {
20
+ this.outputFile = this.options.outputEntry ? path.resolve(this.options.outputEntry) : null;
21
+ this.progressBar = new progress('Compressing [:bar] :percent :elapseds', {
60
22
  complete: '=',
61
23
  incomplete: ' ',
62
24
  total: 100,
@@ -67,9 +29,9 @@ class BuildService {
67
29
  add(rawEntries) {
68
30
  this.logger.info(`Adding ${rawEntries.length} entr${rawEntries.length === 1 ? 'y' : 'ies'} to ZIP file.`);
69
31
  const normalizedEntries = this.normalizePaths(rawEntries);
70
- this.entries = (0, glob_1.globSync)(normalizedEntries).map(rawEntry => {
71
- const resolvedPath = path_1.default.resolve(rawEntry);
72
- const baseName = path_1.default.basename(rawEntry);
32
+ this.entries = globSync(normalizedEntries).map(rawEntry => {
33
+ const resolvedPath = path.resolve(rawEntry);
34
+ const baseName = path.basename(rawEntry);
73
35
  return {
74
36
  resolvedPath,
75
37
  zipPath: baseName,
@@ -77,23 +39,21 @@ class BuildService {
77
39
  });
78
40
  return this;
79
41
  }
80
- save() {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- yield this.checkOutput();
83
- yield Promise.all(this.entries.map(entry => this.checkEntry(entry)));
84
- const data = yield this.getBuffer();
85
- if (this.outputFile) {
86
- if (!this.outputFile.match(/\.\w+$/)) {
87
- this.outputFile = path_1.default.join(this.outputFile, 'data.zip');
88
- }
89
- this.logger.info(`Saving finished zip file to "${this.outputFile}" ...`);
90
- yield this.fileService.writeFile(data, this.outputFile);
91
- }
92
- else {
93
- process.stdout.write(data);
94
- }
95
- return this;
96
- });
42
+ async save() {
43
+ await this.checkOutput();
44
+ await Promise.all(this.entries.map(entry => this.checkEntry(entry)));
45
+ const data = await this.getBuffer();
46
+ if (this.outputFile) {
47
+ if (!this.outputFile.match(/\.\w+$/)) {
48
+ this.outputFile = path.join(this.outputFile, 'data.zip');
49
+ }
50
+ this.logger.info(`Saving finished zip file to "${this.outputFile}" ...`);
51
+ await this.fileService.writeFile(data, this.outputFile);
52
+ }
53
+ else {
54
+ process.stdout.write(data);
55
+ }
56
+ return this;
97
57
  }
98
58
  /**
99
59
  * Note: glob patterns should always use / as a path separator, even on Windows systems,
@@ -103,119 +63,111 @@ class BuildService {
103
63
  normalizePaths(rawEntries) {
104
64
  return rawEntries.map(entry => entry.replace(/\\/g, '/'));
105
65
  }
106
- addFile(entry_1) {
107
- return __awaiter(this, arguments, void 0, function* (entry, isLink = false) {
108
- const { resolvedPath, zipPath } = entry;
109
- let fileStat;
110
- let fileData;
66
+ async addFile(entry, isLink = false) {
67
+ const { resolvedPath, zipPath } = entry;
68
+ let fileStat;
69
+ let fileData;
70
+ try {
71
+ fileData = isLink ? await fs.readlink(resolvedPath) : await fs.readFile(resolvedPath);
72
+ fileStat = await fs.lstat(resolvedPath);
73
+ }
74
+ catch (error) {
75
+ if (!this.options.quiet) {
76
+ this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
77
+ }
78
+ this.logger.info(error);
79
+ return;
80
+ }
81
+ this.logger.info(`Adding file "${resolvedPath}" to ZIP file ...`);
82
+ this.jszip.file(zipPath, fileData, {
83
+ createFolders: true,
84
+ date: fileStat.mtime,
85
+ // See https://github.com/Stuk/jszip/issues/550
86
+ // dosPermissions: fileStat.mode,
87
+ unixPermissions: fileStat.mode,
88
+ });
89
+ this.compressedFilesCount++;
90
+ }
91
+ async addLink(entry) {
92
+ const { resolvedPath, zipPath } = entry;
93
+ if (this.options.dereferenceLinks) {
94
+ let realPath;
111
95
  try {
112
- fileData = isLink ? yield fs.readlink(resolvedPath) : yield fs.readFile(resolvedPath);
113
- fileStat = yield fs.lstat(resolvedPath);
96
+ realPath = await fs.realpath(resolvedPath);
114
97
  }
115
98
  catch (error) {
116
99
  if (!this.options.quiet) {
117
- this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
100
+ this.logger.info(`Can't read link "${entry.resolvedPath}". Ignoring.`);
118
101
  }
119
102
  this.logger.info(error);
120
103
  return;
121
104
  }
122
- this.logger.info(`Adding file "${resolvedPath}" to ZIP file ...`);
123
- this.jszip.file(zipPath, fileData, {
124
- createFolders: true,
125
- date: fileStat.mtime,
126
- // See https://github.com/Stuk/jszip/issues/550
127
- // dosPermissions: fileStat.mode,
128
- unixPermissions: fileStat.mode,
105
+ this.logger.info(`Found real path "${realPath} for symbolic link".`);
106
+ await this.checkEntry({
107
+ resolvedPath: realPath,
108
+ zipPath,
129
109
  });
130
- this.compressedFilesCount++;
131
- });
110
+ }
111
+ else {
112
+ await this.addFile(entry, true);
113
+ }
132
114
  }
133
- addLink(entry) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- const { resolvedPath, zipPath } = entry;
136
- if (this.options.dereferenceLinks) {
137
- let realPath;
138
- try {
139
- realPath = yield fs.realpath(resolvedPath);
140
- }
141
- catch (error) {
142
- if (!this.options.quiet) {
143
- this.logger.info(`Can't read link "${entry.resolvedPath}". Ignoring.`);
144
- }
145
- this.logger.info(error);
146
- return;
147
- }
148
- this.logger.info(`Found real path "${realPath} for symbolic link".`);
149
- yield this.checkEntry({
150
- resolvedPath: realPath,
151
- zipPath,
152
- });
153
- }
154
- else {
155
- yield this.addFile(entry, true);
156
- }
157
- });
115
+ async checkEntry(entry) {
116
+ let fileStat;
117
+ try {
118
+ fileStat = await fs.lstat(entry.resolvedPath);
119
+ }
120
+ catch (error) {
121
+ if (!this.options.quiet) {
122
+ this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
123
+ }
124
+ this.logger.info(error);
125
+ return;
126
+ }
127
+ const ignoreEntries = this.ignoreEntries.filter(ignoreEntry => Boolean(entry.resolvedPath.match(ignoreEntry)));
128
+ if (ignoreEntries.length) {
129
+ this.logger.info(`Found ${entry.resolvedPath}. Not adding since it's on the ignore list:`, ignoreEntries.map(entry => String(entry)));
130
+ return;
131
+ }
132
+ if (fileStat.isDirectory()) {
133
+ this.logger.info(`Found directory "${entry.resolvedPath}".`);
134
+ await this.walkDir(entry);
135
+ }
136
+ else if (fileStat.isFile()) {
137
+ this.logger.info(`Found file "${entry.resolvedPath}".`);
138
+ this.logger.info(`Found file "${entry.resolvedPath}".`);
139
+ await this.addFile(entry);
140
+ }
141
+ else if (fileStat.isSymbolicLink()) {
142
+ this.logger.info(`Found symbolic link "${entry.resolvedPath}".`);
143
+ await this.addLink(entry);
144
+ }
145
+ else {
146
+ this.logger.info('Unknown file type.', { fileStat });
147
+ if (!this.options.quiet) {
148
+ this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
149
+ }
150
+ }
158
151
  }
159
- checkEntry(entry) {
160
- return __awaiter(this, void 0, void 0, function* () {
161
- let fileStat;
162
- try {
163
- fileStat = yield fs.lstat(entry.resolvedPath);
164
- }
165
- catch (error) {
166
- if (!this.options.quiet) {
167
- this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
152
+ async checkOutput() {
153
+ if (this.outputFile) {
154
+ if (this.outputFile.match(/\.\w+$/)) {
155
+ const dirExists = await this.fileService.dirExists(path.dirname(this.outputFile));
156
+ if (!dirExists) {
157
+ throw new Error(`Directory "${path.dirname(this.outputFile)}" doesn't exist or is not writable.`);
168
158
  }
169
- this.logger.info(error);
170
- return;
171
- }
172
- const ignoreEntries = this.ignoreEntries.filter(ignoreEntry => Boolean(entry.resolvedPath.match(ignoreEntry)));
173
- if (ignoreEntries.length) {
174
- this.logger.info(`Found ${entry.resolvedPath}. Not adding since it's on the ignore list:`, ignoreEntries.map(entry => String(entry)));
175
- return;
176
- }
177
- if (fileStat.isDirectory()) {
178
- this.logger.info(`Found directory "${entry.resolvedPath}".`);
179
- yield this.walkDir(entry);
180
- }
181
- else if (fileStat.isFile()) {
182
- this.logger.info(`Found file "${entry.resolvedPath}".`);
183
- this.logger.info(`Found file "${entry.resolvedPath}".`);
184
- yield this.addFile(entry);
185
- }
186
- else if (fileStat.isSymbolicLink()) {
187
- this.logger.info(`Found symbolic link "${entry.resolvedPath}".`);
188
- yield this.addLink(entry);
189
- }
190
- else {
191
- this.logger.info('Unknown file type.', { fileStat });
192
- if (!this.options.quiet) {
193
- this.logger.info(`Can't read file "${entry.resolvedPath}". Ignoring.`);
159
+ const fileIsWritable = await this.fileService.fileIsWritable(this.outputFile);
160
+ if (!fileIsWritable) {
161
+ throw new Error(`File "${this.outputFile}" already exists.`);
194
162
  }
195
163
  }
196
- });
197
- }
198
- checkOutput() {
199
- return __awaiter(this, void 0, void 0, function* () {
200
- if (this.outputFile) {
201
- if (this.outputFile.match(/\.\w+$/)) {
202
- const dirExists = yield this.fileService.dirExists(path_1.default.dirname(this.outputFile));
203
- if (!dirExists) {
204
- throw new Error(`Directory "${path_1.default.dirname(this.outputFile)}" doesn't exist or is not writable.`);
205
- }
206
- const fileIsWritable = yield this.fileService.fileIsWritable(this.outputFile);
207
- if (!fileIsWritable) {
208
- throw new Error(`File "${this.outputFile}" already exists.`);
209
- }
210
- }
211
- else {
212
- const dirExists = yield this.fileService.dirExists(this.outputFile);
213
- if (!dirExists) {
214
- throw new Error(`Directory "${this.outputFile}" doesn't exist or is not writable.`);
215
- }
164
+ else {
165
+ const dirExists = await this.fileService.dirExists(this.outputFile);
166
+ if (!dirExists) {
167
+ throw new Error(`Directory "${this.outputFile}" doesn't exist or is not writable.`);
216
168
  }
217
169
  }
218
- });
170
+ }
219
171
  }
220
172
  getBuffer() {
221
173
  const compressionType = this.options.compressionLevel === 0 ? 'STORE' : 'DEFLATE';
@@ -234,19 +186,16 @@ class BuildService {
234
186
  }
235
187
  });
236
188
  }
237
- walkDir(entry) {
238
- return __awaiter(this, void 0, void 0, function* () {
239
- this.logger.info(`Walking directory ${entry.resolvedPath} ...`);
240
- const dirEntries = yield fs.readdir(entry.resolvedPath);
241
- for (const dirEntry of dirEntries) {
242
- const newZipPath = entry.zipPath === '.' ? dirEntry : `${entry.zipPath}/${dirEntry}`;
243
- const newResolvedPath = path_1.default.join(entry.resolvedPath, dirEntry);
244
- yield this.checkEntry({
245
- resolvedPath: newResolvedPath,
246
- zipPath: newZipPath,
247
- });
248
- }
249
- });
189
+ async walkDir(entry) {
190
+ this.logger.info(`Walking directory ${entry.resolvedPath} ...`);
191
+ const dirEntries = await fs.readdir(entry.resolvedPath);
192
+ for (const dirEntry of dirEntries) {
193
+ const newZipPath = entry.zipPath === '.' ? dirEntry : `${entry.zipPath}/${dirEntry}`;
194
+ const newResolvedPath = path.join(entry.resolvedPath, dirEntry);
195
+ await this.checkEntry({
196
+ resolvedPath: newResolvedPath,
197
+ zipPath: newZipPath,
198
+ });
199
+ }
250
200
  }
251
201
  }
252
- exports.BuildService = BuildService;
@@ -1,4 +1,4 @@
1
- import { TerminalOptions } from './interfaces';
1
+ import { TerminalOptions } from './interfaces.js';
2
2
  export declare class ExtractService {
3
3
  extractedFilesCount: number;
4
4
  outputDir: string | null;
@@ -1,34 +1,19 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ExtractService = void 0;
16
- const fs_extra_1 = __importDefault(require("fs-extra"));
17
- const jszip_1 = __importDefault(require("jszip"));
18
- const logdown_1 = __importDefault(require("logdown"));
19
- const os_1 = __importDefault(require("os"));
20
- const path_1 = __importDefault(require("path"));
21
- const progress_1 = __importDefault(require("progress"));
22
- class ExtractService {
1
+ import os from 'node:os';
2
+ import path from 'node:path';
3
+ import fs from 'fs-extra';
4
+ import JSZip from 'jszip';
5
+ import logdown from 'logdown';
6
+ import progress from 'progress';
7
+ export class ExtractService {
23
8
  constructor(options) {
24
9
  this.options = options;
25
- this.logger = (0, logdown_1.default)('jszip-cli/ExtractService', {
10
+ this.logger = logdown('jszip-cli/ExtractService', {
26
11
  logger: console,
27
12
  markdown: false,
28
13
  });
29
14
  this.logger.state.isEnabled = this.options.verbose;
30
- this.outputDir = this.options.outputEntry ? path_1.default.resolve(this.options.outputEntry) : null;
31
- this.progressBar = new progress_1.default('Extracting [:bar] :percent :elapseds', {
15
+ this.outputDir = this.options.outputEntry ? path.resolve(this.options.outputEntry) : null;
16
+ this.progressBar = new progress('Extracting [:bar] :percent :elapseds', {
32
17
  complete: '=',
33
18
  incomplete: ' ',
34
19
  total: 100,
@@ -36,60 +21,58 @@ class ExtractService {
36
21
  });
37
22
  this.extractedFilesCount = 0;
38
23
  }
39
- extract(rawEntries) {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- const isWin32 = os_1.default.platform() === 'win32';
42
- for (const entry of rawEntries) {
43
- const jszip = new jszip_1.default();
44
- if (this.outputDir) {
45
- yield fs_extra_1.default.ensureDir(this.outputDir);
24
+ async extract(rawEntries) {
25
+ const isWin32 = os.platform() === 'win32';
26
+ for (const entry of rawEntries) {
27
+ const jszip = new JSZip();
28
+ if (this.outputDir) {
29
+ await fs.ensureDir(this.outputDir);
30
+ }
31
+ const resolvedPath = path.resolve(entry);
32
+ const data = await fs.readFile(resolvedPath);
33
+ const entries = [];
34
+ await jszip.loadAsync(data, { createFolders: true });
35
+ if (!this.outputDir) {
36
+ await this.printStream(jszip.generateNodeStream());
37
+ return this;
38
+ }
39
+ jszip.forEach((filePath, entry) => {
40
+ if (filePath.includes('..')) {
41
+ this.logger.info(`Skipping bad path "${filePath}"`);
46
42
  }
47
- const resolvedPath = path_1.default.resolve(entry);
48
- const data = yield fs_extra_1.default.readFile(resolvedPath);
49
- const entries = [];
50
- yield jszip.loadAsync(data, { createFolders: true });
51
- if (!this.outputDir) {
52
- yield this.printStream(jszip.generateNodeStream());
53
- return this;
43
+ else {
44
+ entries.push([filePath, entry]);
54
45
  }
55
- jszip.forEach((filePath, entry) => {
56
- if (filePath.includes('..')) {
57
- this.logger.info(`Skipping bad path "${filePath}"`);
58
- }
59
- else {
60
- entries.push([filePath, entry]);
61
- }
62
- });
63
- let lastPercent = 0;
64
- yield Promise.all(entries.map((_a, index_1) => __awaiter(this, [_a, index_1], void 0, function* ([filePath, entry], index) {
65
- const resolvedFilePath = path_1.default.join(this.outputDir, filePath);
66
- if (entry.dir) {
67
- yield fs_extra_1.default.ensureDir(resolvedFilePath);
68
- }
69
- else {
70
- const data = yield entry.async('nodebuffer');
71
- yield fs_extra_1.default.writeFile(resolvedFilePath, data, {
72
- encoding: 'utf-8',
73
- });
74
- this.extractedFilesCount++;
75
- const diff = Math.floor(index / entries.length) - Math.floor(lastPercent);
76
- if (diff && !this.options.quiet) {
77
- this.progressBar.tick(diff);
78
- lastPercent = Math.floor(index / entries.length);
79
- }
80
- }
81
- if (isWin32) {
82
- if (entry.dosPermissions) {
83
- yield fs_extra_1.default.chmod(resolvedFilePath, entry.dosPermissions);
84
- }
46
+ });
47
+ let lastPercent = 0;
48
+ await Promise.all(entries.map(async ([filePath, entry], index) => {
49
+ const resolvedFilePath = path.join(this.outputDir, filePath);
50
+ if (entry.dir) {
51
+ await fs.ensureDir(resolvedFilePath);
52
+ }
53
+ else {
54
+ const data = await entry.async('nodebuffer');
55
+ await fs.writeFile(resolvedFilePath, data, {
56
+ encoding: 'utf-8',
57
+ });
58
+ this.extractedFilesCount++;
59
+ const diff = Math.floor(index / entries.length) - Math.floor(lastPercent);
60
+ if (diff && !this.options.quiet) {
61
+ this.progressBar.tick(diff);
62
+ lastPercent = Math.floor(index / entries.length);
85
63
  }
86
- else if (entry.unixPermissions) {
87
- yield fs_extra_1.default.chmod(resolvedFilePath, entry.unixPermissions);
64
+ }
65
+ if (isWin32) {
66
+ if (entry.dosPermissions) {
67
+ await fs.chmod(resolvedFilePath, entry.dosPermissions);
88
68
  }
89
- })));
90
- }
91
- return this;
92
- });
69
+ }
70
+ else if (entry.unixPermissions) {
71
+ await fs.chmod(resolvedFilePath, entry.unixPermissions);
72
+ }
73
+ }));
74
+ }
75
+ return this;
93
76
  }
94
77
  printStream(fileStream) {
95
78
  return new Promise((resolve, reject) => {
@@ -99,4 +82,3 @@ class ExtractService {
99
82
  });
100
83
  }
101
84
  }
102
- exports.ExtractService = ExtractService;
@@ -1,5 +1,4 @@
1
- /// <reference types="node" />
2
- import type { TerminalOptions } from './interfaces';
1
+ import type { TerminalOptions } from './interfaces.js';
3
2
  export declare class FileService {
4
3
  private readonly logger;
5
4
  private readonly options;
@@ -1,94 +1,70 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.FileService = void 0;
16
- const fs_extra_1 = __importDefault(require("fs-extra"));
17
- const logdown_1 = __importDefault(require("logdown"));
18
- const path_1 = __importDefault(require("path"));
19
- class FileService {
1
+ import path from 'node:path';
2
+ import fs from 'fs-extra';
3
+ import logdown from 'logdown';
4
+ export class FileService {
20
5
  constructor(options) {
21
6
  this.options = options;
22
- this.logger = (0, logdown_1.default)('jszip-cli/FileService', {
7
+ this.logger = logdown('jszip-cli/FileService', {
23
8
  logger: console,
24
9
  markdown: false,
25
10
  });
26
11
  this.logger.state.isEnabled = this.options.verbose;
27
12
  }
28
- dirExists(dirPath) {
29
- return __awaiter(this, void 0, void 0, function* () {
13
+ async dirExists(dirPath) {
14
+ try {
15
+ await fs.access(dirPath, fs.constants.F_OK);
30
16
  try {
31
- yield fs_extra_1.default.access(dirPath, fs_extra_1.default.constants.F_OK);
32
- try {
33
- yield fs_extra_1.default.access(dirPath, fs_extra_1.default.constants.W_OK);
34
- return true;
35
- }
36
- catch (error) {
37
- this.logger.info(`Directory "${dirPath}" exists but is not writable.`);
38
- return false;
39
- }
17
+ await fs.access(dirPath, fs.constants.W_OK);
18
+ return true;
40
19
  }
41
20
  catch (error) {
42
- this.logger.info(`Directory "${dirPath}" doesn't exist.`, this.options.force ? 'Creating.' : 'Not creating.');
43
- if (this.options.force) {
44
- yield fs_extra_1.default.ensureDir(dirPath);
45
- return true;
46
- }
21
+ this.logger.info(`Directory "${dirPath}" exists but is not writable.`);
47
22
  return false;
48
23
  }
49
- });
50
- }
51
- fileIsReadable(filePath) {
52
- return __awaiter(this, void 0, void 0, function* () {
53
- const dirExists = yield this.dirExists(path_1.default.dirname(filePath));
54
- if (dirExists) {
55
- try {
56
- yield fs_extra_1.default.access(filePath, fs_extra_1.default.constants.F_OK | fs_extra_1.default.constants.R_OK);
57
- return true;
58
- }
59
- catch (error) {
60
- return false;
61
- }
24
+ }
25
+ catch (error) {
26
+ this.logger.info(`Directory "${dirPath}" doesn't exist.`, this.options.force ? 'Creating.' : 'Not creating.');
27
+ if (this.options.force) {
28
+ await fs.ensureDir(dirPath);
29
+ return true;
62
30
  }
63
31
  return false;
64
- });
32
+ }
65
33
  }
66
- fileIsWritable(filePath) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const dirName = path_1.default.dirname(filePath);
69
- const dirExists = yield this.dirExists(dirName);
70
- if (dirExists) {
71
- try {
72
- yield fs_extra_1.default.access(filePath, fs_extra_1.default.constants.F_OK | fs_extra_1.default.constants.R_OK);
73
- this.logger.info(`File "${filePath}" already exists.`, this.options.force ? 'Forcing overwrite.' : 'Not overwriting.');
74
- return this.options.force;
75
- }
76
- catch (error) {
77
- return true;
78
- }
34
+ async fileIsReadable(filePath) {
35
+ const dirExists = await this.dirExists(path.dirname(filePath));
36
+ if (dirExists) {
37
+ try {
38
+ await fs.access(filePath, fs.constants.F_OK | fs.constants.R_OK);
39
+ return true;
79
40
  }
80
- return false;
81
- });
41
+ catch (error) {
42
+ return false;
43
+ }
44
+ }
45
+ return false;
82
46
  }
83
- writeFile(data, filePath) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- const fileIsWritable = yield this.fileIsWritable(filePath);
86
- if (fileIsWritable) {
87
- yield fs_extra_1.default.writeFile(filePath, data);
88
- return this;
47
+ async fileIsWritable(filePath) {
48
+ const dirName = path.dirname(filePath);
49
+ const dirExists = await this.dirExists(dirName);
50
+ if (dirExists) {
51
+ try {
52
+ await fs.access(filePath, fs.constants.F_OK | fs.constants.R_OK);
53
+ this.logger.info(`File "${filePath}" already exists.`, this.options.force ? 'Forcing overwrite.' : 'Not overwriting.');
54
+ return this.options.force;
89
55
  }
90
- throw new Error(`File "${this.options.outputEntry}" already exists.`);
91
- });
56
+ catch (error) {
57
+ return true;
58
+ }
59
+ }
60
+ return false;
61
+ }
62
+ async writeFile(data, filePath) {
63
+ const fileIsWritable = await this.fileIsWritable(filePath);
64
+ if (fileIsWritable) {
65
+ await fs.writeFile(filePath, data);
66
+ return this;
67
+ }
68
+ throw new Error(`File "${this.options.outputEntry}" already exists.`);
92
69
  }
93
70
  }
94
- exports.FileService = FileService;
@@ -1,6 +1,6 @@
1
- import { BuildService } from './BuildService';
2
- import { ExtractService } from './ExtractService';
3
- import type { TerminalOptions } from './interfaces';
1
+ import { BuildService } from './BuildService.js';
2
+ import { ExtractService } from './ExtractService.js';
3
+ import type { TerminalOptions } from './interfaces.js';
4
4
  export declare class JSZipCLI {
5
5
  private readonly buildService;
6
6
  private readonly configExplorer;
package/dist/JSZipCLI.js CHANGED
@@ -1,22 +1,7 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.JSZipCLI = void 0;
16
- const cosmiconfig_1 = require("cosmiconfig");
17
- const logdown_1 = __importDefault(require("logdown"));
18
- const BuildService_1 = require("./BuildService");
19
- const ExtractService_1 = require("./ExtractService");
1
+ import { cosmiconfigSync } from 'cosmiconfig';
2
+ import logdown from 'logdown';
3
+ import { BuildService } from './BuildService.js';
4
+ import { ExtractService } from './ExtractService.js';
20
5
  const defaultOptions = {
21
6
  compressionLevel: 5,
22
7
  configFile: true,
@@ -27,21 +12,21 @@ const defaultOptions = {
27
12
  quiet: false,
28
13
  verbose: false,
29
14
  };
30
- class JSZipCLI {
15
+ export class JSZipCLI {
31
16
  constructor(options) {
32
17
  this.terminalOptions = options;
33
- this.logger = (0, logdown_1.default)('jszip-cli/index', {
18
+ this.logger = logdown('jszip-cli/index', {
34
19
  logger: console,
35
20
  markdown: false,
36
21
  });
37
- this.configExplorer = (0, cosmiconfig_1.cosmiconfigSync)('jszip');
38
- this.options = Object.assign(Object.assign({}, defaultOptions), this.terminalOptions);
22
+ this.configExplorer = cosmiconfigSync('jszip');
23
+ this.options = { ...defaultOptions, ...this.terminalOptions };
39
24
  this.logger.state.isEnabled = this.options.verbose;
40
25
  this.logger.info('Merged options', this.options);
41
26
  this.checkConfigFile();
42
27
  this.logger.info('Loaded options', this.options);
43
- this.buildService = new BuildService_1.BuildService(this.options);
44
- this.extractService = new ExtractService_1.ExtractService(this.options);
28
+ this.buildService = new BuildService(this.options);
29
+ this.extractService = new ExtractService(this.options);
45
30
  }
46
31
  /**
47
32
  * Add files and directories to the ZIP file.
@@ -79,27 +64,25 @@ class JSZipCLI {
79
64
  * Run in file mode - reads entries and settings from configuration file.
80
65
  * Options from the constructor still take precedence.
81
66
  */
82
- fileMode() {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- if (!this.options.mode && !this.configFile) {
85
- throw new Error('No configuration file and no mode specified.');
86
- }
87
- if (this.options.mode === 'add') {
88
- const { outputFile, compressedFilesCount } = yield this.add().save();
89
- if (this.options.outputEntry && !this.options.quiet) {
90
- console.info(`Done compressing ${compressedFilesCount} files to "${outputFile}".`);
91
- }
92
- return this;
67
+ async fileMode() {
68
+ if (!this.options.mode && !this.configFile) {
69
+ throw new Error('No configuration file and no mode specified.');
70
+ }
71
+ if (this.options.mode === 'add') {
72
+ const { outputFile, compressedFilesCount } = await this.add().save();
73
+ if (this.options.outputEntry && !this.options.quiet) {
74
+ console.info(`Done compressing ${compressedFilesCount} files to "${outputFile}".`);
93
75
  }
94
- else if (this.options.mode === 'extract') {
95
- const { outputDir, extractedFilesCount } = yield this.extract();
96
- if (this.options.outputEntry && !this.options.quiet) {
97
- console.info(`Done extracting ${extractedFilesCount} files to "${outputDir}".`);
98
- }
99
- return this;
76
+ return this;
77
+ }
78
+ else if (this.options.mode === 'extract') {
79
+ const { outputDir, extractedFilesCount } = await this.extract();
80
+ if (this.options.outputEntry && !this.options.quiet) {
81
+ console.info(`Done extracting ${extractedFilesCount} files to "${outputDir}".`);
100
82
  }
101
- throw new Error('No or invalid mode in configuration file defined.');
102
- });
83
+ return this;
84
+ }
85
+ throw new Error('No or invalid mode in configuration file defined.');
103
86
  }
104
87
  save() {
105
88
  return this.buildService.save();
@@ -132,8 +115,7 @@ class JSZipCLI {
132
115
  }
133
116
  const configFileData = configResult.config;
134
117
  this.logger.info(`Using configuration file ${configResult.filepath}`);
135
- this.options = Object.assign(Object.assign(Object.assign({}, defaultOptions), configFileData), this.terminalOptions);
118
+ this.options = { ...defaultOptions, ...configFileData, ...this.terminalOptions };
136
119
  this.logger.state.isEnabled = this.options.verbose;
137
120
  }
138
121
  }
139
- exports.JSZipCLI = JSZipCLI;
package/dist/cli.js CHANGED
@@ -1,28 +1,14 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
- return new (P || (P = Promise))(function (resolve, reject) {
6
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
- step((generator = generator.apply(thisArg, _arguments || [])).next());
10
- });
11
- };
12
- var __importDefault = (this && this.__importDefault) || function (mod) {
13
- return (mod && mod.__esModule) ? mod : { "default": mod };
14
- };
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- const commander_1 = require("commander");
17
- const fs_extra_1 = __importDefault(require("fs-extra"));
18
- const path_1 = __importDefault(require("path"));
19
- const JSZipCLI_1 = require("./JSZipCLI");
20
- const defaultPackageJsonPath = path_1.default.join(__dirname, '../../package.json');
21
- const packageJsonPath = fs_extra_1.default.existsSync(defaultPackageJsonPath)
22
- ? defaultPackageJsonPath
23
- : path_1.default.join(__dirname, '../package.json');
24
- const { description, name, version } = fs_extra_1.default.readJSONSync(packageJsonPath);
25
- commander_1.program
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { program as commander } from 'commander';
5
+ import fs from 'fs-extra';
6
+ import { JSZipCLI } from './JSZipCLI.js';
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+ const packageJsonPath = path.join(__dirname, '../package.json');
10
+ const { description, name, version } = fs.readJSONSync(packageJsonPath);
11
+ commander
26
12
  .name(name.replace(/^@[^/]+\//, ''))
27
13
  .description(description)
28
14
  .option('--noconfig', "don't look for a configuration file")
@@ -39,7 +25,7 @@ commander_1.program
39
25
  console.error(`\n error: invalid command \`${args[0]}'\n`);
40
26
  process.exit(1);
41
27
  });
42
- commander_1.program
28
+ commander
43
29
  .command('add')
44
30
  .alias('a')
45
31
  .description('add files and directories to a new ZIP archive')
@@ -53,12 +39,21 @@ commander_1.program
53
39
  .option('-q, --quiet', "don't log anything excluding errors", false)
54
40
  .option('-V, --verbose', 'enable verbose logging', false)
55
41
  .arguments('[entries...]')
56
- .action((entries) => __awaiter(void 0, void 0, void 0, function* () {
57
- const options = commander_1.program.opts();
42
+ .action(async (entries) => {
43
+ const options = commander.opts();
58
44
  try {
59
- const jszip = new JSZipCLI_1.JSZipCLI(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (options.level && { compressionLevel: Number(options.level) })), ((options.config && { configFile: options.config }) || (options.noconfig && { configFile: false }))), (options.dereference && { dereferenceLinks: options.dereference })), (options.force && { force: options.force })), (options.ignore && { ignoreEntries: [options.ignore] })), (options.output && { outputEntry: options.output })), (options.quiet && { quiet: options.quiet })), (options.verbose && { verbose: options.verbose })));
45
+ const jszip = new JSZipCLI({
46
+ ...(options.level && { compressionLevel: Number(options.level) }),
47
+ ...((options.config && { configFile: options.config }) || (options.noconfig && { configFile: false })),
48
+ ...(options.dereference && { dereferenceLinks: options.dereference }),
49
+ ...(options.force && { force: options.force }),
50
+ ...(options.ignore && { ignoreEntries: [options.ignore] }),
51
+ ...(options.output && { outputEntry: options.output }),
52
+ ...(options.quiet && { quiet: options.quiet }),
53
+ ...(options.verbose && { verbose: options.verbose }),
54
+ });
60
55
  jszip.add(entries);
61
- const { outputFile, compressedFilesCount } = yield jszip.save();
56
+ const { outputFile, compressedFilesCount } = await jszip.save();
62
57
  if (options.output && !options.quiet) {
63
58
  console.info(`Done compressing ${compressedFilesCount} files to "${outputFile}".`);
64
59
  }
@@ -67,8 +62,8 @@ commander_1.program
67
62
  console.error('Error:', error.message);
68
63
  process.exit(1);
69
64
  }
70
- }));
71
- commander_1.program
65
+ });
66
+ commander
72
67
  .command('extract')
73
68
  .alias('e')
74
69
  .description('extract files and directories from ZIP archive(s)')
@@ -80,10 +75,17 @@ commander_1.program
80
75
  .option('-V, --verbose', 'enable verbose logging', false)
81
76
  .option('-q, --quiet', "don't log anything excluding errors", false)
82
77
  .arguments('<archives...>')
83
- .action((archives) => __awaiter(void 0, void 0, void 0, function* () {
84
- const options = commander_1.program.opts();
78
+ .action(async (archives) => {
79
+ const options = commander.opts();
85
80
  try {
86
- const { outputDir, extractedFilesCount } = yield new JSZipCLI_1.JSZipCLI(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, ((options.config && { configFile: options.config }) || (options.noconfig && { configFile: false }))), (options.force && { force: options.force })), (options.ignore && { ignoreEntries: [options.ignore] })), (options.output && { outputEntry: options.output })), (options.quiet && { quiet: options.quiet })), (options.verbose && { verbose: options.verbose }))).extract(archives);
81
+ const { outputDir, extractedFilesCount } = await new JSZipCLI({
82
+ ...((options.config && { configFile: options.config }) || (options.noconfig && { configFile: false })),
83
+ ...(options.force && { force: options.force }),
84
+ ...(options.ignore && { ignoreEntries: [options.ignore] }),
85
+ ...(options.output && { outputEntry: options.output }),
86
+ ...(options.quiet && { quiet: options.quiet }),
87
+ ...(options.verbose && { verbose: options.verbose }),
88
+ }).extract(archives);
87
89
  if (options.output && !options.quiet) {
88
90
  console.info(`Done extracting ${extractedFilesCount} files to "${outputDir}".`);
89
91
  }
@@ -92,8 +94,8 @@ commander_1.program
92
94
  console.error('Error:', error.message);
93
95
  process.exit(1);
94
96
  }
95
- }));
96
- commander_1.program
97
+ });
98
+ commander
97
99
  .command('fileMode', { hidden: true, isDefault: true })
98
100
  .option('--noconfig', "don't look for a configuration file", false)
99
101
  .option('-c, --config <path>', 'use a configuration file (default: .jsziprc.json)')
@@ -102,13 +104,20 @@ commander_1.program
102
104
  .option('-f, --force', 'force overwriting files and directories', false)
103
105
  .option('-V, --verbose', 'enable verbose logging', false)
104
106
  .option('-q, --quiet', "don't log anything excluding errors", false)
105
- .action(() => __awaiter(void 0, void 0, void 0, function* () {
106
- const options = commander_1.program.opts();
107
+ .action(async () => {
108
+ const options = commander.opts();
107
109
  try {
108
110
  if (options.noconfig) {
109
- commander_1.program.outputHelp();
111
+ commander.outputHelp();
110
112
  }
111
- yield new JSZipCLI_1.JSZipCLI(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (options.config && { configFile: options.config })), (options.force && { force: options.force })), (options.ignore && { ignoreEntries: [options.ignore] })), (options.output && { outputEntry: options.output })), (options.quiet && { quiet: options.quiet })), (options.verbose && { verbose: options.verbose }))).fileMode();
113
+ await new JSZipCLI({
114
+ ...(options.config && { configFile: options.config }),
115
+ ...(options.force && { force: options.force }),
116
+ ...(options.ignore && { ignoreEntries: [options.ignore] }),
117
+ ...(options.output && { outputEntry: options.output }),
118
+ ...(options.quiet && { quiet: options.quiet }),
119
+ ...(options.verbose && { verbose: options.verbose }),
120
+ }).fileMode();
112
121
  }
113
122
  catch (error) {
114
123
  if (error.message.includes('ENOENT')) {
@@ -119,5 +128,5 @@ commander_1.program
119
128
  }
120
129
  process.exit(1);
121
130
  }
122
- }));
123
- commander_1.program.parse(process.argv);
131
+ });
132
+ commander.parse(process.argv);
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './JSZipCLI';
2
- export * from './interfaces';
1
+ export * from './JSZipCLI.js';
2
+ export * from './interfaces.js';
package/dist/index.js CHANGED
@@ -1,18 +1,2 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./JSZipCLI"), exports);
18
- __exportStar(require("./interfaces"), exports);
1
+ export * from './JSZipCLI.js';
2
+ export * from './interfaces.js';
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "commander": "12.1.0",
6
6
  "cosmiconfig": "9.0.0",
7
7
  "fs-extra": "11.2.0",
8
- "glob": "10.4.1",
8
+ "glob": "11.0.0",
9
9
  "jszip": "3.10.1",
10
10
  "logdown": "3.3.1",
11
11
  "progress": "2.0.3"
@@ -15,13 +15,12 @@
15
15
  "@types/fs-extra": "11.0.4",
16
16
  "@types/progress": "2.0.7",
17
17
  "cross-env": "7.0.3",
18
- "rimraf": "5.0.7",
19
- "ts-node": "10.9.2",
20
- "typescript": "5.4.5",
21
- "vitest": "1.6.0"
18
+ "rimraf": "6.0.1",
19
+ "typescript": "5.5.4",
20
+ "vitest": "2.0.5"
22
21
  },
23
22
  "engines": {
24
- "node": ">= 10.9"
23
+ "node": ">= 18.0"
25
24
  },
26
25
  "files": [
27
26
  "dist"
@@ -33,7 +32,7 @@
33
32
  "zip"
34
33
  ],
35
34
  "license": "GPL-3.0",
36
- "main": "dist/index.js",
35
+ "module": "dist/index.js",
37
36
  "name": "@ffflorian/jszip-cli",
38
37
  "repository": "https://github.com/ffflorian/node-packages/tree/main/packages/jszip-cli",
39
38
  "scripts": {
@@ -43,6 +42,7 @@
43
42
  "start": "cross-env NODE_DEBUG=\"jszip-cli/*\" node --loader ts-node/esm src/cli.ts",
44
43
  "test": "vitest run"
45
44
  },
46
- "version": "3.6.4",
47
- "gitHead": "28c184f53a87d8eb082cc27c923ef7b352bbe875"
45
+ "type": "module",
46
+ "version": "3.7.0",
47
+ "gitHead": "f1a74d8ec9721d5b52a00e41b2ec73278e048290"
48
48
  }