create-pkgbld 1.6.0 → 1.7.1

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/dist/index.cjs CHANGED
@@ -11,6 +11,8 @@ var kleur = require('kleur');
11
11
  var stringArgv = require('string-argv');
12
12
  var childProcess = require('node:child_process');
13
13
  var util = require('node:util');
14
+ var options = require('options');
15
+ var lodash = require('lodash');
14
16
 
15
17
  async function getGitRoot() {
16
18
  const exec = util.promisify(childProcess.exec);
@@ -18,196 +20,6 @@ async function getGitRoot() {
18
20
  return stdout.trim();
19
21
  }
20
22
 
21
- function isPackageJson(value) {
22
- return (input => {
23
- const $io0 = input => (undefined === input.private || "boolean" === typeof input.private) && (undefined === input.version || "string" === typeof input.version) && (undefined === input.name || "string" === typeof input.name) && (undefined === input.license || "string" === typeof input.license) && (undefined === input.readme || "string" === typeof input.readme) && (null !== input.author && (undefined === input.author || "string" === typeof input.author || "object" === typeof input.author && null !== input.author && false === Array.isArray(input.author) && $io1(input.author))) && (undefined === input.description || "string" === typeof input.description) && (undefined === input.scripts || "object" === typeof input.scripts && null !== input.scripts && false === Array.isArray(input.scripts) && $io2(input.scripts)) && (undefined === input.dependencies || "object" === typeof input.dependencies && null !== input.dependencies && false === Array.isArray(input.dependencies) && $io3(input.dependencies)) && (undefined === input.devDependencies || "object" === typeof input.devDependencies && null !== input.devDependencies && false === Array.isArray(input.devDependencies) && $io3(input.devDependencies)) && (undefined === input.peerDependencies || "object" === typeof input.peerDependencies && null !== input.peerDependencies && false === Array.isArray(input.peerDependencies) && $io3(input.peerDependencies));
24
- const $io1 = input => (undefined === input.name || "string" === typeof input.name) && (undefined === input.email || "string" === typeof input.email) && (undefined === input.url || "string" === typeof input.url);
25
- const $io2 = input => Object.keys(input).every(key => {
26
- const value = input[key];
27
- if (undefined === value)
28
- return true;
29
- if (RegExp(/(.*)/).test(key))
30
- return "string" === typeof value;
31
- return true;
32
- });
33
- const $io3 = input => Object.keys(input).every(key => {
34
- const value = input[key];
35
- if (undefined === value)
36
- return true;
37
- if (RegExp(/(.*)/).test(key))
38
- return "string" === typeof value;
39
- return true;
40
- });
41
- return "object" === typeof input && null !== input && false === Array.isArray(input) && $io0(input);
42
- })(value);
43
- }
44
- function CommaSeparatedString(value) {
45
- return value.split(',').map((arg) => arg.trim());
46
- }
47
- function CommaSeparatedStringOrBoolean(value) {
48
- if (typeof value === 'boolean') {
49
- return value;
50
- }
51
- if (Array.isArray(value) && value.length === 0) {
52
- return true;
53
- }
54
- return CommaSeparatedString(value);
55
- }
56
- const cliFlagsDefaults = {
57
- formats: ['es', 'cjs'],
58
- umdInputs: [],
59
- compressFormats: ['umd'],
60
- sourcemapFormats: ['umd'],
61
- preprocess: [],
62
- dir: 'dist',
63
- sourceDir: 'src',
64
- includeExternals: false,
65
- eject: false,
66
- noTsConfig: false,
67
- noUpdatePackageJson: false,
68
- commonjsPattern: '[name].cjs',
69
- esPattern: '[name].mjs',
70
- umdPattern: '[name].umd.js'
71
- };
72
- const cliFlags = {
73
- umd: {
74
- type: CommaSeparatedString,
75
- description: 'Package subpath exports in UMD format',
76
- default: cliFlagsDefaults.umdInputs
77
- },
78
- compress: {
79
- type: CommaSeparatedString,
80
- description: 'Compress formats using terser',
81
- default: cliFlagsDefaults.compressFormats
82
- },
83
- sourcemaps: {
84
- type: CommaSeparatedString,
85
- description: 'Emit sourcemaps for the specified formats',
86
- default: cliFlagsDefaults.sourcemapFormats
87
- },
88
- formats: {
89
- type: CommaSeparatedString,
90
- description: 'Formats to emit',
91
- default: cliFlagsDefaults.formats
92
- },
93
- preprocess: {
94
- type: CommaSeparatedString,
95
- description: 'Preprocess entry points / subpath exports',
96
- default: cliFlagsDefaults.preprocess
97
- },
98
- dir: {
99
- type: String,
100
- description: 'Output directory',
101
- default: cliFlagsDefaults.dir
102
- },
103
- sourceDir: {
104
- type: String,
105
- description: 'Source directory',
106
- default: cliFlagsDefaults.sourceDir
107
- },
108
- bin: {
109
- type: CommaSeparatedString,
110
- description: 'Executable files'
111
- },
112
- includeExternals: {
113
- type: CommaSeparatedStringOrBoolean,
114
- description: 'Include all/specified externals into result bundle(s)',
115
- default: cliFlagsDefaults.includeExternals
116
- },
117
- eject: {
118
- type: Boolean,
119
- description: 'Eject config',
120
- default: cliFlagsDefaults.eject
121
- },
122
- noTsConfig: {
123
- type: Boolean,
124
- description: 'Do not create / update tsconfig.json',
125
- default: cliFlagsDefaults.noTsConfig
126
- },
127
- noUpdatePackageJson: {
128
- type: Boolean,
129
- description: 'Do not create / update package.json',
130
- default: cliFlagsDefaults.noUpdatePackageJson
131
- },
132
- commonjsPattern: {
133
- type: String,
134
- description: 'CommonJS output file name pattern',
135
- default: cliFlagsDefaults.commonjsPattern
136
- },
137
- esmPattern: {
138
- type: String,
139
- description: 'ES output file name pattern',
140
- default: cliFlagsDefaults.esPattern
141
- },
142
- umdPattern: {
143
- type: String,
144
- description: 'UMD output file name pattern',
145
- default: cliFlagsDefaults.umdPattern
146
- }
147
- };
148
- const packageJsonFieldsOrder = new Set([
149
- 'private',
150
- 'type',
151
- 'version',
152
- 'name',
153
- 'description',
154
- 'license',
155
- 'author',
156
- 'contributors',
157
- 'funding',
158
- 'bin',
159
- 'main',
160
- 'browser',
161
- 'unpkg',
162
- 'module',
163
- 'exports',
164
- 'imports',
165
- 'types',
166
- 'typings',
167
- 'files',
168
- 'packageManager',
169
- 'sideEffects',
170
- 'engines',
171
- 'os',
172
- 'cpu',
173
- 'man',
174
- 'directories',
175
- 'repository',
176
- 'bugs',
177
- 'homepage',
178
- 'readme',
179
- 'keywords',
180
- 'scripts',
181
- 'config',
182
- 'dependencies',
183
- 'devDependencies',
184
- 'peerDependencies',
185
- 'peerDependenciesMeta',
186
- 'bundleDependencies',
187
- 'bundledDependencies',
188
- 'optionalDependencies',
189
- 'overrides',
190
- 'publishConfig',
191
- 'workspaces'
192
- ]);
193
- function processPackageJson(pkg, needTreatment, treatKey) {
194
- const newPkg = {};
195
- for (const key of packageJsonFieldsOrder) {
196
- if (needTreatment(key)) {
197
- newPkg[key] = treatKey(key);
198
- }
199
- }
200
- for (const key in pkg) {
201
- if (!packageJsonFieldsOrder.has(key)) {
202
- newPkg[key] = pkg[key];
203
- }
204
- }
205
- return newPkg;
206
- }
207
- function toFormattedJson(json) {
208
- return JSON.stringify(json, null, 2) + '\n';
209
- }
210
-
211
23
  const done = Symbol('done');
212
24
  const formats = ['amd', 'cjs', 'es', 'iife', 'system', 'umd'];
213
25
  const pkgbldBinaries = ['pkgbld', 'pkgbld-internal', 'node ../pkgbld/dist/index.js'];
@@ -235,7 +47,7 @@ async function execute() {
235
47
  const packageName = path.basename(targetDir);
236
48
  let cancelled = false;
237
49
  const options = getBasicOptions(packageName, pkg);
238
- options.push(...await getGitOptions(targetDir));
50
+ options.push(...await getGitOptions(targetDir, pkg.pkg));
239
51
  options.push(...getPkgbldOptions(pkg.pkg));
240
52
  const state = getOptionsValue(options);
241
53
  if (!args.flags.quiet) {
@@ -291,14 +103,14 @@ async function execute() {
291
103
  }
292
104
  }
293
105
  execute();
294
- function updatePackage(pkg, options) {
295
- pkg.pkg = processPackageJson(pkg.pkg, (key) => (key in options || key in pkg.pkg), treatKey);
106
+ function updatePackage(pkg, options$1) {
107
+ pkg.pkg = options.processPackageJson(pkg.pkg, (key) => (key in options$1 || key in pkg.pkg), treatKey);
296
108
  function treatKey(key) {
297
109
  if (key === 'scripts') {
298
110
  ({ ...pkg.pkg.scripts });
299
- getScriptValue(options.pkgbld);
111
+ getScriptValue(options$1.pkgbld);
300
112
  }
301
- return options[key] ?? pkg.pkg[key];
113
+ return options$1[key] ?? pkg.pkg[key];
302
114
  }
303
115
  }
304
116
  function getScriptValue(pkgbld) {
@@ -307,7 +119,7 @@ function getScriptValue(pkgbld) {
307
119
  const pkgBldCopy = { ...pkgbld };
308
120
  delete pkgBldCopy['pkgbldBinary'];
309
121
  delete pkgBldCopy['extraParameters'];
310
- return `${binary} ${asCommandLineArgs(pkgBldCopy)} ${extraArgs}`;
122
+ return `${binary} ${asCommandLineArgs(pkgBldCopy, options.cliFlagsDefaults)} ${extraArgs}`.trimEnd();
311
123
  }
312
124
  function getPromptOption(option, mutateObject) {
313
125
  const value = mutateObject[option.field];
@@ -338,16 +150,19 @@ function mapOption(state) {
338
150
  return (option) => {
339
151
  const fieldValue = state[option.field];
340
152
  return {
341
- title: pad16plus(option.title) + kleur.grey('items' in option ? getPrintString(option.items, (option.mutateInnerObject ? fieldValue : state)) : fieldValue ?? ''),
153
+ title: pad16plus(option.title) + kleur.grey('items' in option ? getPrintString(option, (option.mutateInnerObject ? fieldValue : state)) : fieldValue ?? ''),
342
154
  value: option.field
343
155
  };
344
156
  };
345
157
  }
346
- function getPrintString(items, json) {
347
- return items
158
+ function getPrintString(option, json) {
159
+ if (option.render) {
160
+ return option.render(option, json);
161
+ }
162
+ return option.items
348
163
  .filter(item => item.field in json && json[item.field] && (Array.isArray(json[item.field]) ? json[item.field].length > 0 : true))
349
164
  .map(item => kleur.grey(item.title) + ' ' + (kleur.white('items' in item ?
350
- `[${getPrintString(item.items, (item.mutateInnerObject ? json[item.field] :
165
+ `[${getPrintString(item, (item.mutateInnerObject ? json[item.field] :
351
166
  json))}]` : json[item.field])))
352
167
  .join(', ');
353
168
  }
@@ -374,7 +189,7 @@ async function reportVersion() {
374
189
  const version = createPkgBldPackage.pkg.version ?? '<unknown>';
375
190
  return version;
376
191
  }
377
- async function getGitOptions(targetDir) {
192
+ async function getGitOptions(targetDir, packageJson) {
378
193
  try {
379
194
  const gitCfg = await gitConfig();
380
195
  if (gitCfg) {
@@ -385,6 +200,15 @@ async function getGitOptions(targetDir) {
385
200
  title: 'Git',
386
201
  field: 'git',
387
202
  mutateInnerObject: false,
203
+ render: (_option, value) => lodash.isEqual(removeEmpty({
204
+ repository: value.repository,
205
+ bugs: value.bugs,
206
+ homepage: value.homepage
207
+ }), removeEmpty({
208
+ repository: packageJson.repository,
209
+ bugs: packageJson.bugs,
210
+ homepage: packageJson.homepage
211
+ })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
388
212
  items: [{
389
213
  title: 'Homepage',
390
214
  field: 'homepage',
@@ -420,20 +244,7 @@ async function getGitOptions(targetDir) {
420
244
  return [];
421
245
  }
422
246
  function getPkgbldOptions(pkg) {
423
- let args = {
424
- formats: ['es', 'cjs'],
425
- umd: [],
426
- compress: ['umd'],
427
- sourcemaps: ['umd'],
428
- preprocess: [],
429
- dir: 'dist',
430
- sourceDir: 'src',
431
- bin: undefined,
432
- includeExternals: false,
433
- eject: false,
434
- noTsConfig: false,
435
- noUpdatePackageJson: false
436
- };
247
+ let args = options.cliFlagsDefaults;
437
248
  const cmd = pkg.scripts?.build ?? '';
438
249
  let binary = 'pkgbld';
439
250
  if (cmd) {
@@ -454,7 +265,7 @@ function getPkgbldOptions(pkg) {
454
265
  }
455
266
  const parsedArgs = cleye.cli({
456
267
  help: false,
457
- flags: cliFlags
268
+ flags: options.cliFlags
458
269
  }, undefined, stringArgv.parseArgsStringToArgv(cmd));
459
270
  args = parsedArgs.flags;
460
271
  args.extraParameters = asCommandLineArgs(parsedArgs.unknownFlags);
@@ -463,14 +274,15 @@ function getPkgbldOptions(pkg) {
463
274
  title: 'pkgbld',
464
275
  field: 'pkgbld',
465
276
  mutateInnerObject: true,
277
+ render: (_option, value) => cmd === getScriptValue(value) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
466
278
  items: [{
467
279
  title: 'Destination folder',
468
280
  field: 'dest',
469
- initialValue: args.dir
281
+ initialValue: args.dest
470
282
  }, {
471
283
  title: 'Source folder',
472
284
  field: 'src',
473
- initialValue: args.sourceDir
285
+ initialValue: args.src
474
286
  }, {
475
287
  title: 'UMD exports',
476
288
  field: 'umd',
@@ -516,15 +328,15 @@ function getPkgbldOptions(pkg) {
516
328
  type: 'toggle',
517
329
  initialValue: args.eject
518
330
  }, {
519
- title: 'Create tsconfig',
520
- field: 'tsConfig',
331
+ title: 'Do not create tsconfig',
332
+ field: 'noTsConfig',
521
333
  type: 'toggle',
522
- initialValue: !args.noTsConfig
334
+ initialValue: args.noTsConfig
523
335
  }, {
524
- title: 'Update package.json',
525
- field: 'updatePackageJson',
336
+ title: 'Do not update package.json',
337
+ field: 'noUpdatePackageJson',
526
338
  type: 'toggle',
527
- initialValue: !args.noUpdatePackageJson
339
+ initialValue: args.noUpdatePackageJson
528
340
  }, {
529
341
  title: 'Extra parameters',
530
342
  field: 'extraParameters',
@@ -539,11 +351,12 @@ function getPkgbldOptions(pkg) {
539
351
  }]
540
352
  }];
541
353
  }
542
- function asCommandLineArgs(parsedArgs) {
354
+ function asCommandLineArgs(parsedArgs, defaultArgs = {}) {
543
355
  return Object.entries(parsedArgs)
544
- .flatMap(([key, value]) => asArray(value)
356
+ .filter(([key, value]) => !lodash.isEqual(value, defaultArgs[key]))
357
+ .map(([key, value]) => `--${kebabize(key)}${typeof value === 'string' || Array.isArray(value) ? `=${asArray(value).join(',')}` : ''}`)
545
358
  .filter(Boolean)
546
- .map(value => `--${key}${typeof value === 'string' ? `=${value}` : ''}`)).join(' ');
359
+ .join(' ');
547
360
  }
548
361
  function asArray(value) {
549
362
  return Array.isArray(value) ? value : [value];
@@ -552,6 +365,21 @@ function getBasicOptions(packageName, pkg) {
552
365
  return [{
553
366
  title: 'General',
554
367
  field: 'general',
368
+ render: (_option, value) => lodash.isEqual(removeEmpty({
369
+ name: value.name,
370
+ version: value.version,
371
+ description: value.description,
372
+ license: value.license,
373
+ author: value.author,
374
+ readme: value.readme
375
+ }), removeEmpty({
376
+ name: pkg.pkg.name,
377
+ version: pkg.pkg.version,
378
+ description: pkg.pkg.description,
379
+ license: pkg.pkg.license,
380
+ author: pkg.pkg.author,
381
+ readme: pkg.pkg.readme
382
+ })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
555
383
  items: [{
556
384
  title: 'Package Name',
557
385
  field: 'name',
@@ -603,7 +431,7 @@ async function readPackage(dir) {
603
431
  const pkgFile = await fs.readFile(packageFileName);
604
432
  const readmeFile = await fs.readFile(readmeFileName);
605
433
  const pkg = JSON.parse(pkgFile.toString());
606
- const isValidPackageJson = isPackageJson(pkg);
434
+ const isValidPackageJson = options.isPackageJson(pkg);
607
435
  if (!isValidPackageJson) {
608
436
  console.error('Invalid package.json');
609
437
  throw new Error('Invalid package.json');
@@ -626,7 +454,7 @@ async function writePackage(dir, pkg) {
626
454
  const readmeFileName = path.resolve(dir, 'README.md');
627
455
  try {
628
456
  await fs.mkdir(dir, { recursive: true });
629
- await fs.writeFile(packageFileName, toFormattedJson(pkg.pkg));
457
+ await fs.writeFile(packageFileName, options.toFormattedJson(pkg.pkg));
630
458
  await fs.writeFile(readmeFileName, pkg.readme);
631
459
  }
632
460
  catch (e) {
@@ -634,3 +462,9 @@ async function writePackage(dir, pkg) {
634
462
  process.exit(-1);
635
463
  }
636
464
  }
465
+ function removeEmpty(data) {
466
+ return JSON.parse(JSON.stringify(data));
467
+ }
468
+ function kebabize(value) {
469
+ return value.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
470
+ }
package/dist/index.mjs CHANGED
@@ -8,6 +8,8 @@ import kleur from 'kleur';
8
8
  import { parseArgsStringToArgv } from 'string-argv';
9
9
  import childProcess from 'node:child_process';
10
10
  import util from 'node:util';
11
+ import { processPackageJson, cliFlags, isPackageJson, toFormattedJson, cliFlagsDefaults } from 'options';
12
+ import { isEqual } from 'lodash';
11
13
 
12
14
  async function getGitRoot() {
13
15
  const exec = util.promisify(childProcess.exec);
@@ -15,196 +17,6 @@ async function getGitRoot() {
15
17
  return stdout.trim();
16
18
  }
17
19
 
18
- function isPackageJson(value) {
19
- return (input => {
20
- const $io0 = input => (undefined === input.private || "boolean" === typeof input.private) && (undefined === input.version || "string" === typeof input.version) && (undefined === input.name || "string" === typeof input.name) && (undefined === input.license || "string" === typeof input.license) && (undefined === input.readme || "string" === typeof input.readme) && (null !== input.author && (undefined === input.author || "string" === typeof input.author || "object" === typeof input.author && null !== input.author && false === Array.isArray(input.author) && $io1(input.author))) && (undefined === input.description || "string" === typeof input.description) && (undefined === input.scripts || "object" === typeof input.scripts && null !== input.scripts && false === Array.isArray(input.scripts) && $io2(input.scripts)) && (undefined === input.dependencies || "object" === typeof input.dependencies && null !== input.dependencies && false === Array.isArray(input.dependencies) && $io3(input.dependencies)) && (undefined === input.devDependencies || "object" === typeof input.devDependencies && null !== input.devDependencies && false === Array.isArray(input.devDependencies) && $io3(input.devDependencies)) && (undefined === input.peerDependencies || "object" === typeof input.peerDependencies && null !== input.peerDependencies && false === Array.isArray(input.peerDependencies) && $io3(input.peerDependencies));
21
- const $io1 = input => (undefined === input.name || "string" === typeof input.name) && (undefined === input.email || "string" === typeof input.email) && (undefined === input.url || "string" === typeof input.url);
22
- const $io2 = input => Object.keys(input).every(key => {
23
- const value = input[key];
24
- if (undefined === value)
25
- return true;
26
- if (RegExp(/(.*)/).test(key))
27
- return "string" === typeof value;
28
- return true;
29
- });
30
- const $io3 = input => Object.keys(input).every(key => {
31
- const value = input[key];
32
- if (undefined === value)
33
- return true;
34
- if (RegExp(/(.*)/).test(key))
35
- return "string" === typeof value;
36
- return true;
37
- });
38
- return "object" === typeof input && null !== input && false === Array.isArray(input) && $io0(input);
39
- })(value);
40
- }
41
- function CommaSeparatedString(value) {
42
- return value.split(',').map((arg) => arg.trim());
43
- }
44
- function CommaSeparatedStringOrBoolean(value) {
45
- if (typeof value === 'boolean') {
46
- return value;
47
- }
48
- if (Array.isArray(value) && value.length === 0) {
49
- return true;
50
- }
51
- return CommaSeparatedString(value);
52
- }
53
- const cliFlagsDefaults = {
54
- formats: ['es', 'cjs'],
55
- umdInputs: [],
56
- compressFormats: ['umd'],
57
- sourcemapFormats: ['umd'],
58
- preprocess: [],
59
- dir: 'dist',
60
- sourceDir: 'src',
61
- includeExternals: false,
62
- eject: false,
63
- noTsConfig: false,
64
- noUpdatePackageJson: false,
65
- commonjsPattern: '[name].cjs',
66
- esPattern: '[name].mjs',
67
- umdPattern: '[name].umd.js'
68
- };
69
- const cliFlags = {
70
- umd: {
71
- type: CommaSeparatedString,
72
- description: 'Package subpath exports in UMD format',
73
- default: cliFlagsDefaults.umdInputs
74
- },
75
- compress: {
76
- type: CommaSeparatedString,
77
- description: 'Compress formats using terser',
78
- default: cliFlagsDefaults.compressFormats
79
- },
80
- sourcemaps: {
81
- type: CommaSeparatedString,
82
- description: 'Emit sourcemaps for the specified formats',
83
- default: cliFlagsDefaults.sourcemapFormats
84
- },
85
- formats: {
86
- type: CommaSeparatedString,
87
- description: 'Formats to emit',
88
- default: cliFlagsDefaults.formats
89
- },
90
- preprocess: {
91
- type: CommaSeparatedString,
92
- description: 'Preprocess entry points / subpath exports',
93
- default: cliFlagsDefaults.preprocess
94
- },
95
- dir: {
96
- type: String,
97
- description: 'Output directory',
98
- default: cliFlagsDefaults.dir
99
- },
100
- sourceDir: {
101
- type: String,
102
- description: 'Source directory',
103
- default: cliFlagsDefaults.sourceDir
104
- },
105
- bin: {
106
- type: CommaSeparatedString,
107
- description: 'Executable files'
108
- },
109
- includeExternals: {
110
- type: CommaSeparatedStringOrBoolean,
111
- description: 'Include all/specified externals into result bundle(s)',
112
- default: cliFlagsDefaults.includeExternals
113
- },
114
- eject: {
115
- type: Boolean,
116
- description: 'Eject config',
117
- default: cliFlagsDefaults.eject
118
- },
119
- noTsConfig: {
120
- type: Boolean,
121
- description: 'Do not create / update tsconfig.json',
122
- default: cliFlagsDefaults.noTsConfig
123
- },
124
- noUpdatePackageJson: {
125
- type: Boolean,
126
- description: 'Do not create / update package.json',
127
- default: cliFlagsDefaults.noUpdatePackageJson
128
- },
129
- commonjsPattern: {
130
- type: String,
131
- description: 'CommonJS output file name pattern',
132
- default: cliFlagsDefaults.commonjsPattern
133
- },
134
- esmPattern: {
135
- type: String,
136
- description: 'ES output file name pattern',
137
- default: cliFlagsDefaults.esPattern
138
- },
139
- umdPattern: {
140
- type: String,
141
- description: 'UMD output file name pattern',
142
- default: cliFlagsDefaults.umdPattern
143
- }
144
- };
145
- const packageJsonFieldsOrder = new Set([
146
- 'private',
147
- 'type',
148
- 'version',
149
- 'name',
150
- 'description',
151
- 'license',
152
- 'author',
153
- 'contributors',
154
- 'funding',
155
- 'bin',
156
- 'main',
157
- 'browser',
158
- 'unpkg',
159
- 'module',
160
- 'exports',
161
- 'imports',
162
- 'types',
163
- 'typings',
164
- 'files',
165
- 'packageManager',
166
- 'sideEffects',
167
- 'engines',
168
- 'os',
169
- 'cpu',
170
- 'man',
171
- 'directories',
172
- 'repository',
173
- 'bugs',
174
- 'homepage',
175
- 'readme',
176
- 'keywords',
177
- 'scripts',
178
- 'config',
179
- 'dependencies',
180
- 'devDependencies',
181
- 'peerDependencies',
182
- 'peerDependenciesMeta',
183
- 'bundleDependencies',
184
- 'bundledDependencies',
185
- 'optionalDependencies',
186
- 'overrides',
187
- 'publishConfig',
188
- 'workspaces'
189
- ]);
190
- function processPackageJson(pkg, needTreatment, treatKey) {
191
- const newPkg = {};
192
- for (const key of packageJsonFieldsOrder) {
193
- if (needTreatment(key)) {
194
- newPkg[key] = treatKey(key);
195
- }
196
- }
197
- for (const key in pkg) {
198
- if (!packageJsonFieldsOrder.has(key)) {
199
- newPkg[key] = pkg[key];
200
- }
201
- }
202
- return newPkg;
203
- }
204
- function toFormattedJson(json) {
205
- return JSON.stringify(json, null, 2) + '\n';
206
- }
207
-
208
20
  const done = Symbol('done');
209
21
  const formats = ['amd', 'cjs', 'es', 'iife', 'system', 'umd'];
210
22
  const pkgbldBinaries = ['pkgbld', 'pkgbld-internal', 'node ../pkgbld/dist/index.js'];
@@ -232,7 +44,7 @@ async function execute() {
232
44
  const packageName = path.basename(targetDir);
233
45
  let cancelled = false;
234
46
  const options = getBasicOptions(packageName, pkg);
235
- options.push(...await getGitOptions(targetDir));
47
+ options.push(...await getGitOptions(targetDir, pkg.pkg));
236
48
  options.push(...getPkgbldOptions(pkg.pkg));
237
49
  const state = getOptionsValue(options);
238
50
  if (!args.flags.quiet) {
@@ -304,7 +116,7 @@ function getScriptValue(pkgbld) {
304
116
  const pkgBldCopy = { ...pkgbld };
305
117
  delete pkgBldCopy['pkgbldBinary'];
306
118
  delete pkgBldCopy['extraParameters'];
307
- return `${binary} ${asCommandLineArgs(pkgBldCopy)} ${extraArgs}`;
119
+ return `${binary} ${asCommandLineArgs(pkgBldCopy, cliFlagsDefaults)} ${extraArgs}`.trimEnd();
308
120
  }
309
121
  function getPromptOption(option, mutateObject) {
310
122
  const value = mutateObject[option.field];
@@ -335,16 +147,19 @@ function mapOption(state) {
335
147
  return (option) => {
336
148
  const fieldValue = state[option.field];
337
149
  return {
338
- title: pad16plus(option.title) + kleur.grey('items' in option ? getPrintString(option.items, (option.mutateInnerObject ? fieldValue : state)) : fieldValue ?? ''),
150
+ title: pad16plus(option.title) + kleur.grey('items' in option ? getPrintString(option, (option.mutateInnerObject ? fieldValue : state)) : fieldValue ?? ''),
339
151
  value: option.field
340
152
  };
341
153
  };
342
154
  }
343
- function getPrintString(items, json) {
344
- return items
155
+ function getPrintString(option, json) {
156
+ if (option.render) {
157
+ return option.render(option, json);
158
+ }
159
+ return option.items
345
160
  .filter(item => item.field in json && json[item.field] && (Array.isArray(json[item.field]) ? json[item.field].length > 0 : true))
346
161
  .map(item => kleur.grey(item.title) + ' ' + (kleur.white('items' in item ?
347
- `[${getPrintString(item.items, (item.mutateInnerObject ? json[item.field] :
162
+ `[${getPrintString(item, (item.mutateInnerObject ? json[item.field] :
348
163
  json))}]` : json[item.field])))
349
164
  .join(', ');
350
165
  }
@@ -371,7 +186,7 @@ async function reportVersion() {
371
186
  const version = createPkgBldPackage.pkg.version ?? '<unknown>';
372
187
  return version;
373
188
  }
374
- async function getGitOptions(targetDir) {
189
+ async function getGitOptions(targetDir, packageJson) {
375
190
  try {
376
191
  const gitCfg = await gitConfig();
377
192
  if (gitCfg) {
@@ -382,6 +197,15 @@ async function getGitOptions(targetDir) {
382
197
  title: 'Git',
383
198
  field: 'git',
384
199
  mutateInnerObject: false,
200
+ render: (_option, value) => isEqual(removeEmpty({
201
+ repository: value.repository,
202
+ bugs: value.bugs,
203
+ homepage: value.homepage
204
+ }), removeEmpty({
205
+ repository: packageJson.repository,
206
+ bugs: packageJson.bugs,
207
+ homepage: packageJson.homepage
208
+ })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
385
209
  items: [{
386
210
  title: 'Homepage',
387
211
  field: 'homepage',
@@ -417,20 +241,7 @@ async function getGitOptions(targetDir) {
417
241
  return [];
418
242
  }
419
243
  function getPkgbldOptions(pkg) {
420
- let args = {
421
- formats: ['es', 'cjs'],
422
- umd: [],
423
- compress: ['umd'],
424
- sourcemaps: ['umd'],
425
- preprocess: [],
426
- dir: 'dist',
427
- sourceDir: 'src',
428
- bin: undefined,
429
- includeExternals: false,
430
- eject: false,
431
- noTsConfig: false,
432
- noUpdatePackageJson: false
433
- };
244
+ let args = cliFlagsDefaults;
434
245
  const cmd = pkg.scripts?.build ?? '';
435
246
  let binary = 'pkgbld';
436
247
  if (cmd) {
@@ -460,14 +271,15 @@ function getPkgbldOptions(pkg) {
460
271
  title: 'pkgbld',
461
272
  field: 'pkgbld',
462
273
  mutateInnerObject: true,
274
+ render: (_option, value) => cmd === getScriptValue(value) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
463
275
  items: [{
464
276
  title: 'Destination folder',
465
277
  field: 'dest',
466
- initialValue: args.dir
278
+ initialValue: args.dest
467
279
  }, {
468
280
  title: 'Source folder',
469
281
  field: 'src',
470
- initialValue: args.sourceDir
282
+ initialValue: args.src
471
283
  }, {
472
284
  title: 'UMD exports',
473
285
  field: 'umd',
@@ -513,15 +325,15 @@ function getPkgbldOptions(pkg) {
513
325
  type: 'toggle',
514
326
  initialValue: args.eject
515
327
  }, {
516
- title: 'Create tsconfig',
517
- field: 'tsConfig',
328
+ title: 'Do not create tsconfig',
329
+ field: 'noTsConfig',
518
330
  type: 'toggle',
519
- initialValue: !args.noTsConfig
331
+ initialValue: args.noTsConfig
520
332
  }, {
521
- title: 'Update package.json',
522
- field: 'updatePackageJson',
333
+ title: 'Do not update package.json',
334
+ field: 'noUpdatePackageJson',
523
335
  type: 'toggle',
524
- initialValue: !args.noUpdatePackageJson
336
+ initialValue: args.noUpdatePackageJson
525
337
  }, {
526
338
  title: 'Extra parameters',
527
339
  field: 'extraParameters',
@@ -536,11 +348,12 @@ function getPkgbldOptions(pkg) {
536
348
  }]
537
349
  }];
538
350
  }
539
- function asCommandLineArgs(parsedArgs) {
351
+ function asCommandLineArgs(parsedArgs, defaultArgs = {}) {
540
352
  return Object.entries(parsedArgs)
541
- .flatMap(([key, value]) => asArray(value)
353
+ .filter(([key, value]) => !isEqual(value, defaultArgs[key]))
354
+ .map(([key, value]) => `--${kebabize(key)}${typeof value === 'string' || Array.isArray(value) ? `=${asArray(value).join(',')}` : ''}`)
542
355
  .filter(Boolean)
543
- .map(value => `--${key}${typeof value === 'string' ? `=${value}` : ''}`)).join(' ');
356
+ .join(' ');
544
357
  }
545
358
  function asArray(value) {
546
359
  return Array.isArray(value) ? value : [value];
@@ -549,6 +362,21 @@ function getBasicOptions(packageName, pkg) {
549
362
  return [{
550
363
  title: 'General',
551
364
  field: 'general',
365
+ render: (_option, value) => isEqual(removeEmpty({
366
+ name: value.name,
367
+ version: value.version,
368
+ description: value.description,
369
+ license: value.license,
370
+ author: value.author,
371
+ readme: value.readme
372
+ }), removeEmpty({
373
+ name: pkg.pkg.name,
374
+ version: pkg.pkg.version,
375
+ description: pkg.pkg.description,
376
+ license: pkg.pkg.license,
377
+ author: pkg.pkg.author,
378
+ readme: pkg.pkg.readme
379
+ })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
552
380
  items: [{
553
381
  title: 'Package Name',
554
382
  field: 'name',
@@ -631,3 +459,9 @@ async function writePackage(dir, pkg) {
631
459
  process.exit(-1);
632
460
  }
633
461
  }
462
+ function removeEmpty(data) {
463
+ return JSON.parse(JSON.stringify(data));
464
+ }
465
+ function kebabize(value) {
466
+ return value.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
467
+ }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { PackageJson } from 'options';
1
2
  export type Option = ({
2
3
  title: string;
3
4
  field: string;
@@ -17,26 +18,8 @@ export type Option = ({
17
18
  } | {
18
19
  items: Option[];
19
20
  mutateInnerObject: boolean;
21
+ render?: (option: Option, value: OptionsValue) => string;
20
22
  });
21
- export type PackageJson = {
22
- private?: boolean;
23
- version?: string;
24
- name?: string;
25
- license?: string;
26
- readme?: string;
27
- author?: string | {
28
- name?: string;
29
- email?: string;
30
- url?: string;
31
- };
32
- description?: string;
33
- scripts?: {
34
- [key: string]: string;
35
- };
36
- dependencies?: Record<string, string>;
37
- devDependencies?: Record<string, string>;
38
- peerDependencies?: Record<string, string>;
39
- };
40
23
  export type PkgInfo = {
41
24
  readme: string;
42
25
  pkg: PackageJson;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.6.0",
2
+ "version": "1.7.1",
3
3
  "name": "create-pkgbld",
4
4
  "license": "MIT",
5
5
  "author": "Konstantin Shutkin",
@@ -8,6 +8,8 @@
8
8
  "module": "./dist/index.mjs",
9
9
  "exports": {
10
10
  ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
11
13
  "require": "./dist/index.cjs",
12
14
  "default": "./dist/index.mjs"
13
15
  },
@@ -34,20 +36,22 @@
34
36
  "init"
35
37
  ],
36
38
  "dependencies": {
37
- "string-argv": "^0.3.2",
39
+ "string-argv": "0.3.2",
38
40
  "git-user-name": "2.0.0",
39
- "cleye": "^1.3.2",
41
+ "cleye": "1.3.2",
40
42
  "parse-git-config": "3.0.0",
41
43
  "prompts": "2.4.2",
42
- "kleur": "4.1.5"
44
+ "kleur": "4.1.5",
45
+ "lodash": "4.17.21"
43
46
  },
44
47
  "devDependencies": {
45
48
  "@types/git-user-name": "2.0.1",
46
49
  "@types/parse-git-config": "3.0.2",
47
- "@types/prompts": "2.4.5",
48
- "@total-typescript/ts-reset": "^0.5.1",
49
- "pkgbld": "1.20.0",
50
- "options": "0.0.0"
50
+ "@types/prompts": "2.4.6",
51
+ "@types/lodash": "4.14.199",
52
+ "@total-typescript/ts-reset": "0.5.1",
53
+ "pkgbld": "1.24.0",
54
+ "options": "0.2.0"
51
55
  },
52
56
  "scripts": {
53
57
  "build": "node ../pkgbld/dist/index.js --include-externals=options",