pkgprn 0.7.0 → 0.8.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.
package/index.d.ts CHANGED
@@ -23,9 +23,9 @@ declare module 'pkgprn' {
23
23
  };
24
24
  export type PruneOptions = {
25
25
  profile: string;
26
- flatten: string[] | boolean;
26
+ flatten: true | string[] | false;
27
27
  removeSourcemaps: boolean;
28
- stripComments: string | boolean;
28
+ stripComments: true | string[] | false;
29
29
  optimizeFiles: boolean;
30
30
  cleanupFiles: boolean;
31
31
  };
package/index.js CHANGED
@@ -1,33 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
  import { readFile, writeFile } from 'node:fs/promises';
3
- import { dirname, resolve } from 'node:path';
4
- import { fileURLToPath } from 'node:url';
3
+ import { resolve } from 'node:path';
5
4
 
6
5
  import { createLogger } from '@niceties/logger';
7
6
  import { parseArgsPlus } from '@niceties/node-parseargs-plus';
8
7
  import { camelCase } from '@niceties/node-parseargs-plus/camel-case';
8
+ import { customValue } from '@niceties/node-parseargs-plus/custom-value';
9
9
  import { help } from '@niceties/node-parseargs-plus/help';
10
10
  import { optionalValue } from '@niceties/node-parseargs-plus/optional-value';
11
+ import { readPackageJson } from '@niceties/node-parseargs-plus/package-info';
11
12
 
12
13
  import { prunePkg } from './prune.js';
13
14
 
14
- const __dirname = dirname(fileURLToPath(import.meta.url));
15
+ function parseMultiString(values) {
16
+ const items = values
17
+ .flatMap(v => v.split(','))
18
+ .map(s => s.trim())
19
+ .filter(Boolean);
20
+ return items.length ? items : true;
21
+ }
15
22
 
16
23
  const logger = createLogger();
17
24
  logger.update('preparing..');
18
25
 
19
26
  try {
20
- const version = await getMyVersion();
27
+ const myPkgData = await readPackageJson(import.meta.url);
21
28
 
22
29
  logger.update('');
23
- process.stdout.moveCursor?.(0, -1);
24
30
 
25
31
  const { values } = parseArgsPlus(
26
32
  {
27
- name: 'pkgprn',
28
- version: version ?? '<unknown>',
29
- description: 'prune devDependencies and redundant scripts from package.json',
30
- allowPositionals: true,
33
+ ...myPkgData,
31
34
  allowNegative: true,
32
35
  options: {
33
36
  profile: {
@@ -36,10 +39,10 @@ try {
36
39
  default: 'library',
37
40
  },
38
41
  flatten: {
39
- type: 'string',
42
+ type: (parseMultiString),
40
43
  multiple: true,
41
44
  optionalValue: true,
42
- description: 'flatten package files (use "auto" for auto-detect, or specify directories)',
45
+ description: 'flatten package files (omit value to auto-detect, or specify directories)',
43
46
  },
44
47
  removeSourcemaps: {
45
48
  type: 'boolean',
@@ -47,10 +50,10 @@ try {
47
50
  default: false,
48
51
  },
49
52
  stripComments: {
50
- type: 'string',
53
+ type: (parseMultiString),
51
54
  multiple: true,
52
55
  optionalValue: true,
53
- description: 'strip comments: all (default: jsdoc,regular), jsdoc, license, regular, annotation',
56
+ description: 'strip comments (omit value for defaults, or specify types: jsdoc, license, regular, annotation)',
54
57
  },
55
58
  optimizeFiles: {
56
59
  type: 'boolean',
@@ -64,18 +67,13 @@ try {
64
67
  },
65
68
  },
66
69
  },
67
- [camelCase, optionalValue, help]
70
+ [camelCase, customValue, optionalValue, help]
68
71
  );
69
72
 
70
- const flattenDirs = values.flatten
71
- ?.flatMap(v => v.split(','))
72
- .map(s => s.trim())
73
- .filter(Boolean);
74
-
75
73
  const flags = {
76
74
  ...values,
77
- flatten: flattenDirs?.includes('auto') || values.flatten?.includes('') ? true : flattenDirs?.length ? flattenDirs : false,
78
- stripComments: values.stripComments?.length ? values.stripComments.filter(Boolean).join(',') || 'all' : false,
75
+ flatten: values.flatten ?? false,
76
+ stripComments: values.stripComments ?? false,
79
77
  };
80
78
 
81
79
  const pkg = await readPackage('.');
@@ -91,12 +89,6 @@ try {
91
89
  process.exit(255);
92
90
  }
93
91
 
94
- async function getMyVersion() {
95
- const pkg = await readPackage(resolve(__dirname));
96
-
97
- return pkg && 'version' in pkg && typeof pkg.version === 'string' ? pkg.version : '<unknown>';
98
- }
99
-
100
92
  async function readPackage(dir) {
101
93
  const packageFileName = resolve(dir, 'package.json');
102
94
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkgprn",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "author": "Konstantin Shutkin",
6
6
  "bin": "./index.js",
@@ -34,6 +34,6 @@
34
34
  "dependencies": {
35
35
  "@jridgewell/sourcemap-codec": "^1.5.5",
36
36
  "@niceties/logger": "^2.1.0",
37
- "@niceties/node-parseargs-plus": "^0.2.1"
37
+ "@niceties/node-parseargs-plus": "^0.5.0"
38
38
  }
39
39
  }
package/prune.js CHANGED
@@ -88,7 +88,7 @@ export async function prunePkg(pkg, options, logger) {
88
88
  }
89
89
 
90
90
  if (options.stripComments) {
91
- const typesToStrip = parseCommentTypes( (options.stripComments));
91
+ const typesToStrip = parseCommentTypes(options.stripComments);
92
92
  logger.update('stripping comments...');
93
93
  const allFiles = await walkDir('.', ['node_modules']);
94
94
  const jsFiles = allFiles.filter(isStrippableFile);
package/strip-comments.js CHANGED
@@ -292,24 +292,17 @@ function scanTemplateTail(s, i, len, templateStack, comments) {
292
292
  }
293
293
 
294
294
  export function parseCommentTypes(value) {
295
- if (value === true || value === 'all') {
295
+ if (value === true) {
296
296
  return new Set( (['jsdoc', 'regular']));
297
297
  }
298
298
 
299
299
  const valid = (['jsdoc', 'license', 'regular', 'annotation']);
300
- const parts = String(value)
301
- .split(',')
302
- .map(s => s.trim())
303
- .filter(Boolean);
304
300
 
305
301
  const result = new Set();
306
302
 
307
- for (const part of parts) {
308
- if (part === 'all') {
309
- return new Set( (['jsdoc', 'regular']));
310
- }
303
+ for (const part of value) {
311
304
  if (!valid.includes( (part))) {
312
- throw new Error(`unknown comment type "${part}" (expected: ${valid.join(', ')}, all)`);
305
+ throw new Error(`unknown comment type "${part}" (expected: ${valid.join(', ')})`);
313
306
  }
314
307
  result.add( (part));
315
308
  }