create-pkgbld 1.8.0 → 1.8.2

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.mjs DELETED
@@ -1,681 +0,0 @@
1
- import prompts from 'prompts';
2
- import path from 'path';
3
- import fs from 'fs/promises';
4
- import userName from 'git-user-name';
5
- import gitConfig from 'parse-git-config';
6
- import { cli } from 'cleye';
7
- import kleur from 'kleur';
8
- import { parseArgsStringToArgv } from 'string-argv';
9
- import childProcess from 'node:child_process';
10
- import util from 'node:util';
11
- import isEqual from 'lodash/isEqual.js';
12
-
13
- async function getGitRoot() {
14
- const exec = util.promisify(childProcess.exec);
15
- const { stdout } = await exec('git rev-parse --show-toplevel');
16
- return stdout.trim();
17
- }
18
-
19
- function isPackageJson(value) {
20
- return (input => {
21
- 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.repository || "object" === typeof input.repository && null !== input.repository && false === Array.isArray(input.repository) && $io3(input.repository)) && (undefined === input.files || Array.isArray(input.files) && input.files.every(elem => "string" === typeof elem)) && (undefined === input.bugs || "string" === typeof input.bugs) && (undefined === input.homepage || "string" === typeof input.homepage) && (undefined === input.dependencies || "object" === typeof input.dependencies && null !== input.dependencies && false === Array.isArray(input.dependencies) && $io4(input.dependencies)) && (undefined === input.devDependencies || "object" === typeof input.devDependencies && null !== input.devDependencies && false === Array.isArray(input.devDependencies) && $io4(input.devDependencies)) && (undefined === input.peerDependencies || "object" === typeof input.peerDependencies && null !== input.peerDependencies && false === Array.isArray(input.peerDependencies) && $io4(input.peerDependencies)) && (undefined === input.exports || "object" === typeof input.exports && null !== input.exports && false === Array.isArray(input.exports) && $io5(input.exports));
22
- const $io1 = input => (undefined === input.name || "string" === typeof input.name) && (undefined === input.email || "string" === typeof input.email) && (undefined === input.url || "string" === typeof input.url);
23
- const $io2 = input => Object.keys(input).every(key => {
24
- const value = input[key];
25
- if (undefined === value)
26
- return true;
27
- return "string" === typeof value;
28
- });
29
- const $io3 = input => (undefined === input.type || "string" === typeof input.type) && (undefined === input.url || "string" === typeof input.url);
30
- const $io4 = input => Object.keys(input).every(key => {
31
- const value = input[key];
32
- if (undefined === value)
33
- return true;
34
- return "string" === typeof value;
35
- });
36
- const $io5 = input => Object.keys(input).every(key => {
37
- const value = input[key];
38
- if (undefined === value)
39
- return true;
40
- return null !== value && undefined !== value && ("string" === typeof value || "object" === typeof value && null !== value && false === Array.isArray(value) && $io4(value));
41
- });
42
- return "object" === typeof input && null !== input && false === Array.isArray(input) && $io0(input);
43
- })(value);
44
- }
45
- function CommaSeparatedString(value) {
46
- return value.split(',').map((arg) => arg.trim());
47
- }
48
- function CommaSeparatedStringOrBoolean(value) {
49
- if (typeof value === 'boolean') {
50
- return value;
51
- }
52
- // TODO check how we get array here
53
- if (Array.isArray(value) && value.length === 0) {
54
- return true;
55
- }
56
- return CommaSeparatedString(value);
57
- }
58
- const cliFlagsDefaults = {
59
- formats: ['es', 'cjs'],
60
- umd: [],
61
- compress: ['umd'],
62
- sourcemaps: ['umd'],
63
- preprocess: [],
64
- dest: 'dist',
65
- src: 'src',
66
- bin: undefined,
67
- includeExternals: false,
68
- eject: false,
69
- noTsConfig: false,
70
- noUpdatePackageJson: false,
71
- commonjsPattern: '[name].cjs',
72
- esmPattern: '[name].mjs',
73
- umdPattern: '[name].umd.js',
74
- formatPackageJson: false
75
- };
76
- const cliFlags = {
77
- umd: {
78
- type: CommaSeparatedString,
79
- description: 'Package subpath exports in UMD format',
80
- default: cliFlagsDefaults.umd
81
- },
82
- compress: {
83
- type: CommaSeparatedString,
84
- description: 'Compress formats using terser',
85
- default: cliFlagsDefaults.compress
86
- },
87
- sourcemaps: {
88
- type: CommaSeparatedString,
89
- description: 'Emit sourcemaps for the specified formats',
90
- default: cliFlagsDefaults.sourcemaps
91
- },
92
- formats: {
93
- type: CommaSeparatedString,
94
- description: 'Formats to emit',
95
- default: cliFlagsDefaults.formats
96
- },
97
- preprocess: {
98
- type: CommaSeparatedString,
99
- description: 'Preprocess entry points / subpath exports',
100
- default: cliFlagsDefaults.preprocess
101
- },
102
- dest: {
103
- type: String,
104
- description: 'Output directory',
105
- default: cliFlagsDefaults.dest
106
- },
107
- src: {
108
- type: String,
109
- description: 'Source directory',
110
- default: cliFlagsDefaults.src
111
- },
112
- bin: {
113
- type: CommaSeparatedString,
114
- description: 'Executable files',
115
- default: cliFlagsDefaults.bin
116
- },
117
- includeExternals: {
118
- type: CommaSeparatedStringOrBoolean,
119
- description: 'Include all/specified externals into result bundle(s)',
120
- default: cliFlagsDefaults.includeExternals
121
- },
122
- eject: {
123
- type: Boolean,
124
- description: 'Eject config',
125
- default: cliFlagsDefaults.eject
126
- },
127
- noTsConfig: {
128
- type: Boolean,
129
- description: 'Do not create / update tsconfig.json',
130
- default: cliFlagsDefaults.noTsConfig
131
- },
132
- noUpdatePackageJson: {
133
- type: Boolean,
134
- description: 'Do not create / update package.json',
135
- default: cliFlagsDefaults.noUpdatePackageJson
136
- },
137
- commonjsPattern: {
138
- type: String,
139
- description: 'CommonJS output file name pattern',
140
- default: cliFlagsDefaults.commonjsPattern
141
- },
142
- esmPattern: {
143
- type: String,
144
- description: 'ES output file name pattern',
145
- default: cliFlagsDefaults.esmPattern
146
- },
147
- umdPattern: {
148
- type: String,
149
- description: 'UMD output file name pattern',
150
- default: cliFlagsDefaults.umdPattern
151
- },
152
- formatPackageJson: {
153
- type: Boolean,
154
- description: 'Format package.json',
155
- default: cliFlagsDefaults.formatPackageJson
156
- },
157
- noPack: {
158
- type: Boolean,
159
- description: 'Do not pack',
160
- default: false
161
- },
162
- noExports: {
163
- type: Boolean,
164
- description: 'Do not add exports field in package.json',
165
- default: false
166
- },
167
- };
168
- const packageJsonFieldsOrder = new Set([
169
- 'private',
170
- 'type',
171
- 'version',
172
- 'name',
173
- 'scope', // custom
174
- 'description',
175
- 'license',
176
- 'author',
177
- 'contributors',
178
- 'funding',
179
- 'bin',
180
- 'main',
181
- 'browser',
182
- 'unpkg',
183
- 'module',
184
- 'svelte',
185
- 'exports',
186
- 'imports',
187
- 'types',
188
- 'typings',
189
- 'typesVersions', // non standard but required for typescript with resolution other than nodenext
190
- 'files',
191
- 'packageManager',
192
- 'sideEffects',
193
- 'engines',
194
- 'os',
195
- 'cpu',
196
- 'man',
197
- 'directories',
198
- 'repository',
199
- 'bugs',
200
- 'homepage',
201
- 'readme',
202
- 'keywords',
203
- 'scripts',
204
- 'config',
205
- 'dependencies',
206
- 'devDependencies',
207
- 'peerDependencies',
208
- 'peerDependenciesMeta',
209
- 'bundleDependencies',
210
- 'bundledDependencies',
211
- 'optionalDependencies',
212
- 'overrides',
213
- 'publishConfig',
214
- 'workspaces'
215
- ]);
216
- function processPackageJson(pkg, needTreatment, treatKey) {
217
- const newPkg = {};
218
- for (const key of packageJsonFieldsOrder) {
219
- if (needTreatment(key)) {
220
- newPkg[key] = treatKey(key);
221
- }
222
- }
223
- for (const key in pkg) {
224
- if (!packageJsonFieldsOrder.has(key)) {
225
- newPkg[key] = pkg[key];
226
- }
227
- }
228
- return newPkg;
229
- }
230
- function toFormattedJson(json) {
231
- return JSON.stringify(json, null, 2) + '\n';
232
- }
233
-
234
- const done = Symbol('done');
235
- const formats = ['amd', 'cjs', 'es', 'iife', 'system', 'umd'];
236
- const pkgbldBinaries = ['pkgbld', 'pkgbld-internal', 'node ../pkgbld/dist/index.js'];
237
- async function execute() {
238
- const version = await reportVersion();
239
- const args = cli({
240
- name: 'create-pkgbld',
241
- version,
242
- parameters: [
243
- '[package name]'
244
- ],
245
- flags: {
246
- quiet: {
247
- type: Boolean,
248
- description: 'Quiet mode',
249
- default: false
250
- }
251
- }
252
- });
253
- !args.flags.quiet && args.showVersion();
254
- const targetDir = path.join(process.cwd(), args._.packageName ?? '.');
255
- !args.flags.quiet && console.log(kleur.grey(pad16plus('Target Directory', 0)) + kleur.white(targetDir));
256
- const pkg = await readPackage(targetDir);
257
- !args.flags.quiet && console.log(kleur.grey(pad16plus('Mode', 0)) + kleur.white(pkg.mode));
258
- const packageName = path.basename(targetDir);
259
- let cancelled = false;
260
- const options = getBasicOptions(packageName, pkg);
261
- options.push(...await getGitOptions(targetDir, pkg.pkg));
262
- options.push(...getPkgbldOptions(pkg.pkg));
263
- const state = getOptionsValue(options);
264
- if (!args.flags.quiet) {
265
- for (;;) {
266
- const topLevelAction = await prompts({
267
- type: 'select',
268
- name: 'value',
269
- message: 'Select an option to change, Done to execute, Escape to cancel',
270
- choices: [
271
- { title: kleur.green('Done'), description: `${pkg.mode === 'update' ? 'Update' : 'Create'} package`, value: done },
272
- ...options.map(mapOption(state))
273
- ],
274
- initial: 0
275
- }, { onCancel });
276
- if (cancelled) {
277
- process.exit(-1);
278
- }
279
- if (topLevelAction.value === done) {
280
- break;
281
- }
282
- let option = options.find(item => item.field === topLevelAction.value);
283
- let mutateObject = state;
284
- while ('items' in option) {
285
- const nextLevelAction = await prompts([{
286
- type: 'select',
287
- name: 'value',
288
- message: option.title,
289
- choices: option.items.map(mapOption(option.mutateInnerObject ? state[option.field] : state))
290
- }], { onCancel });
291
- if (cancelled) {
292
- process.exit(-1);
293
- }
294
- if (option.mutateInnerObject) {
295
- mutateObject = state[option.field];
296
- }
297
- option = option.items.find(item => item.field === nextLevelAction.value);
298
- }
299
- const action = await prompts(getPromptOption(option, mutateObject), { onCancel });
300
- if (cancelled) {
301
- process.exit(-1);
302
- }
303
- mutateObject[option.field] = action[option.field];
304
- }
305
- if (cancelled) {
306
- process.exit(-1);
307
- }
308
- }
309
- updatePackage(pkg, state);
310
- pkg.readme ??= `# ${state.name}`;
311
- await writePackage(targetDir, pkg);
312
- function onCancel() {
313
- cancelled = true;
314
- }
315
- }
316
- execute();
317
- function updatePackage(pkg, options) {
318
- pkg.pkg = processPackageJson(pkg.pkg, (key) => (key in options || key in pkg.pkg), treatKey);
319
- function treatKey(key) {
320
- if (key === 'scripts') {
321
- ({ ...pkg.pkg.scripts });
322
- getScriptValue(options.pkgbld);
323
- }
324
- return options[key] ?? pkg.pkg[key];
325
- }
326
- }
327
- function getScriptValue(pkgbld) {
328
- const binary = pkgbld.pkgbldBinary;
329
- const extraArgs = pkgbld.extraParameters;
330
- const pkgBldCopy = { ...pkgbld };
331
- delete pkgBldCopy['pkgbldBinary'];
332
- delete pkgBldCopy['extraParameters'];
333
- return `${binary} ${asCommandLineArgs(pkgBldCopy, cliFlagsDefaults)} ${extraArgs}`.trimEnd();
334
- }
335
- function getPromptOption(option, mutateObject) {
336
- const value = mutateObject[option.field];
337
- const type = option.type ?? 'text';
338
- const promptOption = {
339
- type,
340
- name: option.field,
341
- message: option.title,
342
- initial: (Array.isArray(value) ? value.join(',') : value) ?? ''
343
- };
344
- if (type === 'multiselect') {
345
- promptOption.choices = 'list' in option ? option.list.map(item => ({
346
- title: item,
347
- value: item,
348
- selected: value.includes(item)
349
- })) : [];
350
- }
351
- if (type === 'select') {
352
- promptOption.choices = 'list' in option ? option.list.map(item => ({
353
- title: item,
354
- value: item
355
- })) : [];
356
- promptOption.initial = promptOption.choices.findIndex(item => item.value === promptOption.initial);
357
- }
358
- return promptOption;
359
- }
360
- function mapOption(state) {
361
- return (option) => {
362
- const fieldValue = state[option.field];
363
- return {
364
- title: pad16plus(option.title) + kleur.grey('items' in option ? getPrintString(option, (option.mutateInnerObject ? fieldValue : state)) : fieldValue ?? ''),
365
- value: option.field
366
- };
367
- };
368
- }
369
- function getPrintString(option, json) {
370
- if (option.render) {
371
- return option.render(option, json);
372
- }
373
- return option.items
374
- .filter(item => item.field in json && json[item.field] && (Array.isArray(json[item.field]) ? json[item.field].length > 0 : true))
375
- .map(item => kleur.grey(item.title) + ' ' + (kleur.white('items' in item ?
376
- `[${getPrintString(item, (item.mutateInnerObject ? json[item.field] :
377
- json))}]` : json[item.field])))
378
- .join(', ');
379
- }
380
- function getOptionsValue(options) {
381
- const result = {};
382
- for (const item of options) {
383
- if ('items' in item) {
384
- const value = getOptionsValue(item.items);
385
- if (item.mutateInnerObject) {
386
- result[item.field] = value;
387
- }
388
- else {
389
- Object.assign(result, value);
390
- }
391
- }
392
- else {
393
- result[item.field] = item.initialValue;
394
- }
395
- }
396
- return result;
397
- }
398
- async function reportVersion() {
399
- const createPkgBldPackage = await readPackage(path.resolve(__dirname, '..'));
400
- const version = createPkgBldPackage.pkg.version ?? '<unknown>';
401
- return version;
402
- }
403
- async function getGitOptions(targetDir, packageJson) {
404
- try {
405
- const gitCfg = await gitConfig();
406
- if (gitCfg) {
407
- const url = gitCfg['remote "origin"'].url;
408
- const root = await getGitRoot();
409
- const directory = path.relative(root, targetDir);
410
- return [{
411
- title: 'Git',
412
- field: 'git',
413
- mutateInnerObject: false,
414
- render: (_option, value) => isEqual(removeEmpty({
415
- repository: value.repository,
416
- bugs: value.bugs,
417
- homepage: value.homepage
418
- }), removeEmpty({
419
- repository: packageJson.repository,
420
- bugs: packageJson.bugs,
421
- homepage: packageJson.homepage
422
- })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
423
- items: [{
424
- title: 'Homepage',
425
- field: 'homepage',
426
- initialValue: url.replace('.git', `/blob/main${directory ? '/' + directory : ''}/README.md`)
427
- }, {
428
- title: 'Repository',
429
- field: 'repository',
430
- items: [{
431
- title: 'Type',
432
- field: 'type',
433
- initialValue: 'git'
434
- }, {
435
- title: 'Url',
436
- field: 'url',
437
- initialValue: `git+${url}`
438
- }, {
439
- title: 'Directory',
440
- field: 'directory',
441
- initialValue: directory || undefined
442
- }],
443
- mutateInnerObject: true
444
- }, {
445
- title: 'Bugs',
446
- field: 'bugs',
447
- initialValue: url.replace('.git', '/issues')
448
- }]
449
- }];
450
- }
451
- }
452
- catch (e) {
453
- /* ignore */
454
- }
455
- return [];
456
- }
457
- function getPkgbldOptions(pkg) {
458
- let args = cliFlagsDefaults;
459
- const cmd = pkg.scripts?.build ?? '';
460
- let binary = 'pkgbld';
461
- if (cmd) {
462
- if (cmd.startsWith('pkgbld-internal')) {
463
- binary = 'pkgbld-internal';
464
- }
465
- else if (cmd.startsWith('node ../pkgbld/dist/index.js')) {
466
- binary = 'node ../pkgbld/dist/index.js';
467
- }
468
- else if (!cmd.startsWith('pkgbld')) {
469
- const naiveArgs = cmd.split(' ');
470
- if (naiveArgs.length > 1 && naiveArgs[0] === 'node') {
471
- binary = naiveArgs[0] + ' ' + naiveArgs[1];
472
- }
473
- else if (naiveArgs.length > 0) {
474
- binary = naiveArgs[0]; // ?????
475
- }
476
- }
477
- const parsedArgs = cli({
478
- help: false,
479
- flags: cliFlags
480
- }, undefined, parseArgsStringToArgv(cmd));
481
- args = parsedArgs.flags;
482
- args.extraParameters = asCommandLineArgs(parsedArgs.unknownFlags);
483
- }
484
- return [{
485
- title: 'pkgbld',
486
- field: 'pkgbld',
487
- mutateInnerObject: true,
488
- render: (_option, value) => cmd === getScriptValue(value) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
489
- items: [{
490
- title: 'Destination folder',
491
- field: 'dest',
492
- initialValue: args.dest
493
- }, {
494
- title: 'Source folder',
495
- field: 'src',
496
- initialValue: args.src
497
- }, {
498
- title: 'UMD exports',
499
- field: 'umd',
500
- initialValue: args.umd,
501
- type: 'list'
502
- }, {
503
- title: 'Compress formats',
504
- field: 'compress',
505
- initialValue: args.compress,
506
- type: 'multiselect',
507
- list: formats
508
- }, {
509
- title: 'Sorcemaps formats',
510
- field: 'sourcemaps',
511
- initialValue: args.sourcemaps,
512
- type: 'multiselect',
513
- list: formats
514
- }, {
515
- title: 'Formats',
516
- field: 'formats',
517
- initialValue: args.formats,
518
- type: 'multiselect',
519
- list: formats
520
- }, {
521
- title: 'Preprocess formats',
522
- field: 'preprocess',
523
- initialValue: args.preprocess,
524
- type: 'multiselect',
525
- list: formats
526
- }, {
527
- title: 'Binaries',
528
- field: 'bin',
529
- initialValue: args.bin,
530
- type: 'list'
531
- }, {
532
- title: 'Include externals',
533
- field: 'includeExternals',
534
- type: 'toggle',
535
- initialValue: args.includeExternals
536
- }, {
537
- title: 'Eject config',
538
- field: 'eject',
539
- type: 'toggle',
540
- initialValue: args.eject
541
- }, {
542
- title: 'Do not create tsconfig',
543
- field: 'noTsConfig',
544
- type: 'toggle',
545
- initialValue: args.noTsConfig
546
- }, {
547
- title: 'Do not update package.json',
548
- field: 'noUpdatePackageJson',
549
- type: 'toggle',
550
- initialValue: args.noUpdatePackageJson
551
- }, {
552
- title: 'Extra parameters',
553
- field: 'extraParameters',
554
- type: 'text',
555
- initialValue: args.extraParameters
556
- }, {
557
- title: 'Pkgbld Binary',
558
- field: 'pkgbldBinary',
559
- type: 'select',
560
- list: pkgbldBinaries.includes(binary) ? pkgbldBinaries : [...pkgbldBinaries, binary],
561
- initialValue: binary
562
- }]
563
- }];
564
- }
565
- function asCommandLineArgs(parsedArgs, defaultArgs = {}) {
566
- return Object.entries(parsedArgs)
567
- .filter(([key, value]) => !isEqual(value, defaultArgs[key]))
568
- .map(([key, value]) => `--${kebabize(key)}${typeof value === 'string' || Array.isArray(value) ? `=${asArray(value).join(',')}` : ''}`)
569
- .filter(Boolean)
570
- .join(' ');
571
- }
572
- function asArray(value) {
573
- return Array.isArray(value) ? value : [value];
574
- }
575
- function getBasicOptions(packageName, pkg) {
576
- return [{
577
- title: 'General',
578
- field: 'general',
579
- render: (_option, value) => isEqual(removeEmpty({
580
- name: value.name,
581
- version: value.version,
582
- description: value.description,
583
- license: value.license,
584
- author: value.author,
585
- readme: value.readme
586
- }), removeEmpty({
587
- name: pkg.pkg.name,
588
- version: pkg.pkg.version,
589
- description: pkg.pkg.description,
590
- license: pkg.pkg.license,
591
- author: pkg.pkg.author,
592
- readme: pkg.pkg.readme
593
- })) ? `[${kleur.green('Ok')}]` : `[${kleur.blue('Updated')}]`,
594
- items: [{
595
- title: 'Package Name',
596
- field: 'name',
597
- initialValue: chooseValue(pkg.pkg.name, packageName)
598
- }, {
599
- title: 'Version',
600
- field: 'version',
601
- initialValue: chooseValue(pkg.pkg.version, '0.0.1')
602
- }, {
603
- title: 'Description',
604
- field: 'description',
605
- initialValue: chooseValue(pkg.pkg.description, '')
606
- }, {
607
- title: 'License',
608
- field: 'license',
609
- initialValue: chooseValue(pkg.pkg.license, 'MIT')
610
- }, {
611
- title: 'Author',
612
- field: 'author',
613
- initialValue: chooseValue(toAuthorString(pkg.pkg.author), userName() ?? '')
614
- }, {
615
- title: 'Readme',
616
- field: 'readme',
617
- initialValue: chooseValue(pkg.pkg.readme, 'README.md')
618
- }],
619
- mutateInnerObject: false
620
- }];
621
- function chooseValue(pkgValue, defaultValue) {
622
- return pkg.mode === 'update' ? pkgValue : defaultValue;
623
- }
624
- function toAuthorString(author) {
625
- if (typeof author === 'string' || !author) {
626
- return author;
627
- }
628
- const name = author.name ?? '';
629
- const email = author.email ?? '';
630
- const url = author.url ?? '';
631
- return `${name}${email ? ` <${email}>` : ''}${url ? ` (${url})` : ''}`;
632
- }
633
- }
634
- function pad16plus(value, indent = 4, offset = 3) {
635
- return value + ''.padEnd(offset - Math.floor((value.length + indent) / 8), '\t');
636
- }
637
- async function readPackage(dir) {
638
- const packageFileName = path.resolve(dir, 'package.json');
639
- const readmeFileName = path.resolve(dir, 'README.md');
640
- const defaultPkg = {};
641
- try {
642
- const pkgFile = await fs.readFile(packageFileName);
643
- const readmeFile = await fs.readFile(readmeFileName);
644
- const pkg = JSON.parse(pkgFile.toString());
645
- const isValidPackageJson = isPackageJson(pkg);
646
- if (!isValidPackageJson) {
647
- console.error('Invalid package.json');
648
- throw new Error('Invalid package.json');
649
- }
650
- return {
651
- pkg,
652
- readme: readmeFile.toString(),
653
- mode: 'update'
654
- };
655
- }
656
- catch (e) { /**/ }
657
- return {
658
- pkg: defaultPkg,
659
- readme: '',
660
- mode: 'create'
661
- };
662
- }
663
- async function writePackage(dir, pkg) {
664
- const packageFileName = path.resolve(dir, 'package.json');
665
- const readmeFileName = path.resolve(dir, 'README.md');
666
- try {
667
- await fs.mkdir(dir, { recursive: true });
668
- await fs.writeFile(packageFileName, toFormattedJson(pkg.pkg));
669
- await fs.writeFile(readmeFileName, pkg.readme);
670
- }
671
- catch (e) {
672
- console.error(e);
673
- process.exit(-1);
674
- }
675
- }
676
- function removeEmpty(data) {
677
- return JSON.parse(JSON.stringify(data));
678
- }
679
- function kebabize(value) {
680
- return value.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
681
- }