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