@sanity/pkg-utils 3.3.7 → 3.3.9-canary.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.
Files changed (44) hide show
  1. package/dist/_chunks/{build-7mn5THX6.cjs → build-xtvcN4rn.cjs} +50 -61
  2. package/dist/_chunks/build-xtvcN4rn.cjs.map +1 -0
  3. package/dist/_chunks/{buildAction-fRHTIDmx.cjs → buildAction-lOl6hAY8.cjs} +5 -3
  4. package/dist/_chunks/{buildAction-fRHTIDmx.cjs.map → buildAction-lOl6hAY8.cjs.map} +1 -1
  5. package/dist/_chunks/check-5On74q2h.cjs +222 -0
  6. package/dist/_chunks/check-5On74q2h.cjs.map +1 -0
  7. package/dist/_chunks/{checkAction-0xA1q64g.cjs → checkAction-RYG9-Wqo.cjs} +5 -3
  8. package/dist/_chunks/checkAction-RYG9-Wqo.cjs.map +1 -0
  9. package/dist/_chunks/{consoleSpy-g4ZadSyB.cjs → consoleSpy-bV4ZI2h8.cjs} +181 -179
  10. package/dist/_chunks/{consoleSpy-g4ZadSyB.cjs.map → consoleSpy-bV4ZI2h8.cjs.map} +1 -1
  11. package/dist/_chunks/{handleError-esTdSrHu.cjs → handleError-fVK262CU.cjs} +6 -6
  12. package/dist/_chunks/handleError-fVK262CU.cjs.map +1 -0
  13. package/dist/_chunks/index-YSy1xob6.cjs +36038 -0
  14. package/dist/_chunks/{index-1Mfz_QDA.cjs.map → index-YSy1xob6.cjs.map} +1 -1
  15. package/dist/_chunks/init-tT7f7xkO.cjs +646 -0
  16. package/dist/_chunks/{init-U-HiL7BD.cjs.map → init-tT7f7xkO.cjs.map} +1 -1
  17. package/dist/_chunks/{initAction-QVQEhQh6.cjs → initAction-zkJJWu4U.cjs} +5 -3
  18. package/dist/_chunks/initAction-zkJJWu4U.cjs.map +1 -0
  19. package/dist/_chunks/{logger-iA0nC-kL.cjs → logger-c-JGqYo_.cjs} +45 -43
  20. package/dist/_chunks/logger-c-JGqYo_.cjs.map +1 -0
  21. package/dist/_chunks/watch-Mo2KRBJf.cjs +226 -0
  22. package/dist/_chunks/watch-Mo2KRBJf.cjs.map +1 -0
  23. package/dist/_chunks/{watchAction-JfAFOHP0.cjs → watchAction-7JsrPTEX.cjs} +5 -3
  24. package/dist/_chunks/watchAction-7JsrPTEX.cjs.map +1 -0
  25. package/dist/cli.cjs +14 -30
  26. package/dist/cli.cjs.map +1 -1
  27. package/dist/index.cjs +11 -10
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.js +37229 -1530
  30. package/dist/index.js.map +1 -1
  31. package/package.json +33 -28
  32. package/src/node/tasks/rollup/resolveRollupConfig.ts +6 -11
  33. package/dist/_chunks/build-7mn5THX6.cjs.map +0 -1
  34. package/dist/_chunks/check-pxYRC97N.cjs +0 -250
  35. package/dist/_chunks/check-pxYRC97N.cjs.map +0 -1
  36. package/dist/_chunks/checkAction-0xA1q64g.cjs.map +0 -1
  37. package/dist/_chunks/handleError-esTdSrHu.cjs.map +0 -1
  38. package/dist/_chunks/index-1Mfz_QDA.cjs +0 -2159
  39. package/dist/_chunks/init-U-HiL7BD.cjs +0 -500
  40. package/dist/_chunks/initAction-QVQEhQh6.cjs.map +0 -1
  41. package/dist/_chunks/logger-iA0nC-kL.cjs.map +0 -1
  42. package/dist/_chunks/watch-6xc8QXJF.cjs +0 -233
  43. package/dist/_chunks/watch-6xc8QXJF.cjs.map +0 -1
  44. package/dist/_chunks/watchAction-JfAFOHP0.cjs.map +0 -1
@@ -0,0 +1,646 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs/promises');
4
+ var mkdirp = require('mkdirp');
5
+ var path = require('path');
6
+ var prompts = require('prompts');
7
+ var logger = require('./logger-c-JGqYo_.cjs');
8
+ var getLatestVersion = require('get-latest-version');
9
+ var gitUrlParse = require('git-url-parse');
10
+ var outdent = require('outdent');
11
+ var parseGitConfig = require('parse-git-config');
12
+ var prettier = require('prettier');
13
+ require('esbuild-register/dist/node');
14
+ require('pkg-up');
15
+ require('find-config');
16
+ require('fs');
17
+ require('chalk');
18
+ require('zod');
19
+ require('typescript');
20
+
21
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
22
+
23
+ var prompts__default = /*#__PURE__*/_interopDefaultCompat(prompts);
24
+ var getLatestVersion__default = /*#__PURE__*/_interopDefaultCompat(getLatestVersion);
25
+ var gitUrlParse__default = /*#__PURE__*/_interopDefaultCompat(gitUrlParse);
26
+ var parseGitConfig__default = /*#__PURE__*/_interopDefaultCompat(parseGitConfig);
27
+ var prettier__default = /*#__PURE__*/_interopDefaultCompat(prettier);
28
+
29
+ const promptsTypes = {
30
+ string: "text"
31
+ };
32
+ async function createFromTemplate(options) {
33
+ const { cwd, logger, packagePath, template: templateOrResolver } = options;
34
+ const template = typeof templateOrResolver === "function" ? await templateOrResolver({ cwd, logger, packagePath }) : templateOrResolver;
35
+ logger.log("create new package at", path.relative(cwd, packagePath));
36
+ const templateOptions = {};
37
+ for (const templateOption of template.options) {
38
+ const templateValidate = templateOption.validate;
39
+ const res = await prompts__default.default(
40
+ {
41
+ type: promptsTypes[templateOption.type],
42
+ name: templateOption.name,
43
+ message: templateOption.description,
44
+ validate: templateValidate ? (prev) => templateValidate(prev) : void 0,
45
+ initial: typeof templateOption.initial === "function" ? templateOption.initial(templateOptions) : templateOption.initial
46
+ },
47
+ { onCancel: () => process.exit(0) }
48
+ );
49
+ templateOptions[templateOption.name] = templateOption.parse ? templateOption.parse(res[templateOption.name]) : res[templateOption.name];
50
+ }
51
+ const features = {};
52
+ for (const templateFeature of template.features) {
53
+ const res = templateFeature.optional ? await prompts__default.default(
54
+ {
55
+ type: "confirm",
56
+ name: "confirm",
57
+ message: `use ${templateFeature.name}?`,
58
+ initial: templateFeature.initial
59
+ },
60
+ { onCancel: () => process.exit(0) }
61
+ ) : void 0;
62
+ features[templateFeature.name] = res?.confirm || !templateFeature.optional;
63
+ }
64
+ const files = await template.getFiles(templateOptions, features);
65
+ files.sort((a, b) => {
66
+ return a.name.localeCompare(b.name);
67
+ });
68
+ for (const file of files) {
69
+ const filePath = path.resolve(packagePath, file.name);
70
+ await mkdirp.mkdirp(path.dirname(filePath));
71
+ await fs.writeFile(filePath, file.contents.trim() + "\n");
72
+ logger.success(`wrote ${path.relative(cwd, filePath)}`);
73
+ }
74
+ }
75
+
76
+ function defineTemplateOption(option) {
77
+ return option;
78
+ }
79
+
80
+ async function isEmptyDirectory(dirPath) {
81
+ return (await fs.readdir(dirPath)).length === 0;
82
+ }
83
+
84
+ const RE_NAME = /^(?:@(?:[a-z0-9-*~][a-z0-9-*._~]*)\/)?[a-z0-9-~][a-z0-9-._~]*$/i;
85
+ const defaultTemplate = async ({ cwd, logger: logger$1, packagePath }) => {
86
+ const gitConfig = await parseGitConfig__default.default({ cwd, type: "global" });
87
+ return {
88
+ options: [
89
+ defineTemplateOption({
90
+ name: "repo",
91
+ type: "string",
92
+ description: "git url",
93
+ validate: (v) => {
94
+ if (!v)
95
+ return true;
96
+ try {
97
+ gitUrlParse__default.default(v);
98
+ return true;
99
+ } catch (err) {
100
+ return "invalid git url";
101
+ }
102
+ },
103
+ parse: (v) => {
104
+ if (!v)
105
+ return null;
106
+ const result = gitUrlParse__default.default(v);
107
+ return { source: result.source, owner: result.owner, name: result.name };
108
+ }
109
+ }),
110
+ defineTemplateOption({
111
+ name: "pkgName",
112
+ type: "string",
113
+ description: "package name",
114
+ initial: (options) => options.repo?.name || void 0,
115
+ validate: (v) => {
116
+ if (!v)
117
+ return "package name is required";
118
+ const match = RE_NAME.exec(v);
119
+ if (!match) {
120
+ return "invalid package name";
121
+ }
122
+ return true;
123
+ },
124
+ parse: (v) => {
125
+ if (!v) {
126
+ throw new Error("package name is required");
127
+ }
128
+ const match = RE_NAME.exec(v);
129
+ if (!match) {
130
+ throw new Error("invalid package name");
131
+ }
132
+ const [scope, name] = v.split("/");
133
+ return { scope, name, fullName: v };
134
+ }
135
+ }),
136
+ defineTemplateOption({
137
+ name: "description",
138
+ type: "string",
139
+ description: "package description"
140
+ }),
141
+ defineTemplateOption({
142
+ name: "authorName",
143
+ type: "string",
144
+ description: "package author name",
145
+ initial: gitConfig?.user?.name
146
+ }),
147
+ defineTemplateOption({
148
+ name: "authorEmail",
149
+ type: "string",
150
+ description: "package author email",
151
+ initial: gitConfig?.user?.email
152
+ }),
153
+ defineTemplateOption({
154
+ name: "license",
155
+ type: "string",
156
+ description: "package license",
157
+ initial: "MIT",
158
+ validate: (v) => {
159
+ if (!v)
160
+ return "license is required";
161
+ return true;
162
+ }
163
+ })
164
+ ],
165
+ features: [
166
+ {
167
+ name: "eslint",
168
+ optional: true,
169
+ initial: true
170
+ },
171
+ {
172
+ name: "prettier",
173
+ optional: true,
174
+ initial: true
175
+ },
176
+ {
177
+ name: "typescript",
178
+ optional: true,
179
+ initial: true
180
+ }
181
+ ],
182
+ async getFiles(options, features) {
183
+ const { pkgName, repo } = options;
184
+ const { fullName: name } = pkgName;
185
+ const author = [options.authorName, options.authorEmail && `<${options.authorEmail}>`].filter(Boolean).join(" ") ?? void 0;
186
+ const prettierConfig = features.prettier ? {
187
+ bracketSpacing: false,
188
+ plugins: ["prettier-plugin-packagejson"],
189
+ printWidth: 100,
190
+ quoteProps: "consistent",
191
+ semi: false,
192
+ singleQuote: true,
193
+ tabWidth: 2,
194
+ overrides: [
195
+ {
196
+ files: ["*.yml"],
197
+ options: {
198
+ singleQuote: false
199
+ }
200
+ }
201
+ ]
202
+ } : void 0;
203
+ const pkgJson = {
204
+ name,
205
+ "version": "0.0.0",
206
+ "description": options.description ?? void 0,
207
+ "keywords": [],
208
+ "homepage": void 0,
209
+ "bugs": void 0,
210
+ "repository": void 0,
211
+ "license": options.license,
212
+ author,
213
+ "type": "module",
214
+ "exports": {
215
+ ".": {
216
+ types: void 0,
217
+ source: features.typescript ? "./src/index.ts" : "./src/index.js",
218
+ import: "./dist/index.js",
219
+ require: "./dist/index.cjs",
220
+ default: "./dist/index.js"
221
+ },
222
+ "./package.json": "./package.json"
223
+ },
224
+ "main": "./dist/index.cjs",
225
+ "module": "./dist/index.js",
226
+ "source": features.typescript ? "./src/index.ts" : "./src/index.js",
227
+ "types": void 0,
228
+ "files": ["dist", "src"],
229
+ "scripts": {
230
+ "build": "run-s clean pkg:build pkg:check",
231
+ "clean": "rimraf dist",
232
+ "format": features.prettier ? "prettier --write --cache --ignore-unknown ." : void 0,
233
+ "pkg:build": "pkg build --strict",
234
+ "pkg:check": "pkg check --strict"
235
+ },
236
+ "lint-staged": features.prettier ? {
237
+ "*": ["prettier --write --cache --ignore-unknown"]
238
+ } : void 0,
239
+ // prettier: prettierConfig,
240
+ "dependencies": {},
241
+ "devDependencies": {
242
+ "@sanity/pkg-utils": "*",
243
+ "@typescript-eslint/eslint-plugin": void 0,
244
+ "@typescript-eslint/parser": void 0,
245
+ "eslint": void 0,
246
+ "eslint-config-prettier": void 0,
247
+ "eslint-plugin-import": void 0,
248
+ "eslint-plugin-prettier": void 0,
249
+ "eslint-plugin-simple-import-sort": void 0,
250
+ "lint-staged": "*",
251
+ "npm-run-all": "*",
252
+ "prettier": features.prettier ? "*" : void 0,
253
+ "prettier-plugin-packagejson": features.prettier ? "*" : void 0,
254
+ "rimraf": "*",
255
+ "typescript": void 0
256
+ },
257
+ "engines": {
258
+ node: ">=18.0.0"
259
+ }
260
+ };
261
+ const files = [];
262
+ files.push({
263
+ name: ".editorconfig",
264
+ contents: outdent.outdent`
265
+ root = true
266
+
267
+ [*]
268
+ charset = utf-8
269
+ indent_style = space
270
+ indent_size = 2
271
+ end_of_line = lf
272
+ insert_final_newline = true
273
+ trim_trailing_whitespace = true
274
+ `
275
+ });
276
+ files.push({
277
+ name: ".gitignore",
278
+ contents: outdent.outdent`
279
+ *.local
280
+ *.log
281
+ *.tgz
282
+
283
+ .DS_Store
284
+ dist
285
+ etc
286
+ node_modules
287
+ `
288
+ });
289
+ if (features.prettier) {
290
+ files.push({
291
+ name: ".prettierignore",
292
+ contents: outdent.outdent`
293
+ dist
294
+ pnpm-lock.yaml
295
+ `
296
+ });
297
+ files.push({
298
+ name: ".prettierrc",
299
+ contents: await format(
300
+ path.resolve(packagePath, ".prettierrc.json"),
301
+ JSON.stringify(prettierConfig, null, 2) + "\n",
302
+ prettierConfig
303
+ )
304
+ });
305
+ }
306
+ if (repo) {
307
+ pkgJson.repository = {
308
+ type: "git",
309
+ url: `git+ssh://git@${repo.source}/${repo.owner}/${repo.name}.git`
310
+ };
311
+ pkgJson.bugs = {
312
+ url: `https://${repo.source}/${repo.owner}/${repo.name}/issues`
313
+ };
314
+ pkgJson.homepage = `https://${repo.source}/${repo.owner}/${repo.name}#readme`;
315
+ }
316
+ if (features.typescript) {
317
+ pkgJson.types = "./dist/index.d.ts";
318
+ const mainExport = pkgJson.exports?.["."];
319
+ if (logger.isRecord(mainExport)) {
320
+ mainExport.types = "./dist/index.d.ts";
321
+ }
322
+ pkgJson.scripts = {
323
+ ...pkgJson.scripts,
324
+ ["ts:check"]: "tsc --build"
325
+ };
326
+ const devDependencies = pkgJson.devDependencies;
327
+ if (logger.isRecord(devDependencies)) {
328
+ devDependencies["typescript"] = "*";
329
+ }
330
+ }
331
+ if (features.eslint) {
332
+ const eslintConfig = {
333
+ root: true,
334
+ env: {
335
+ browser: true,
336
+ es6: true,
337
+ node: true
338
+ },
339
+ extends: [
340
+ "eslint:recommended",
341
+ features.prettier ? "plugin:prettier/recommended" : void 0
342
+ ].filter(Boolean),
343
+ parserOptions: {
344
+ ecmaVersion: 2020,
345
+ sourceType: "module"
346
+ },
347
+ plugins: [
348
+ "import",
349
+ "simple-import-sort",
350
+ features.prettier ? "prettier" : void 0
351
+ ].filter(Boolean),
352
+ rules: {
353
+ "no-console": "error",
354
+ "no-shadow": "error",
355
+ "no-warning-comments": ["warn", { location: "start", terms: ["todo", "fixme"] }],
356
+ "quote-props": ["warn", "consistent-as-needed"],
357
+ "simple-import-sort/exports": "warn",
358
+ "simple-import-sort/imports": "warn",
359
+ "strict": ["warn", "global"]
360
+ }
361
+ };
362
+ files.push({
363
+ name: ".eslintignore",
364
+ contents: outdent.outdent`
365
+ dist
366
+ `
367
+ });
368
+ pkgJson.scripts = {
369
+ ...pkgJson.scripts,
370
+ lint: features.typescript ? "eslint . --ext .cjs,.js,.ts,.tsx" : "eslint . --ext .cjs,.js"
371
+ };
372
+ pkgJson.devDependencies = {
373
+ ...pkgJson.devDependencies,
374
+ "eslint": "*",
375
+ "eslint-config-prettier": features.prettier ? "*" : void 0,
376
+ "eslint-plugin-simple-import-sort": "*",
377
+ "eslint-plugin-import": "*",
378
+ "eslint-plugin-prettier": features.prettier ? "*" : void 0
379
+ };
380
+ if (features.typescript) {
381
+ pkgJson.devDependencies = {
382
+ ...pkgJson.devDependencies,
383
+ "@typescript-eslint/eslint-plugin": "*",
384
+ "@typescript-eslint/parser": "*"
385
+ };
386
+ const eslintConfigOverride = {
387
+ files: ["**/*.ts", "**/*.tsx"],
388
+ parser: "@typescript-eslint/parser",
389
+ parserOptions: {
390
+ project: ["./tsconfig.json"]
391
+ },
392
+ extends: [
393
+ "eslint:recommended",
394
+ features.prettier ? "plugin:prettier/recommended" : void 0,
395
+ "plugin:@typescript-eslint/eslint-recommended",
396
+ "plugin:@typescript-eslint/recommended"
397
+ ].filter(Boolean),
398
+ plugins: [
399
+ "import",
400
+ "@typescript-eslint",
401
+ "simple-import-sort",
402
+ features.prettier ? "prettier" : void 0
403
+ ].filter(Boolean),
404
+ rules: {
405
+ "@typescript-eslint/explicit-module-boundary-types": "error",
406
+ "@typescript-eslint/interface-name-prefix": "off",
407
+ "@typescript-eslint/member-delimiter-style": "off",
408
+ "@typescript-eslint/no-empty-interface": "off"
409
+ }
410
+ };
411
+ eslintConfig.overrides = [eslintConfigOverride];
412
+ }
413
+ files.push({
414
+ name: ".eslintrc.cjs",
415
+ contents: await format(
416
+ path.resolve(packagePath, ".eslintrc.cjs"),
417
+ outdent.outdent`
418
+ 'use strict'
419
+
420
+ /** @type import('eslint').Linter.Config */
421
+ module.exports = ${JSON.stringify(eslintConfig, null, 2)}
422
+ `,
423
+ prettierConfig
424
+ )
425
+ });
426
+ }
427
+ if (features.typescript) {
428
+ files.push({
429
+ name: "tsconfig.settings.json",
430
+ contents: await format(
431
+ path.resolve(packagePath, "tsconfig.settings.json"),
432
+ outdent.outdent`
433
+ {
434
+ "compilerOptions": {
435
+ // Completeness
436
+ "skipLibCheck": true,
437
+
438
+ // Interop constraints
439
+ "allowSyntheticDefaultImports": true,
440
+ "esModuleInterop": true,
441
+
442
+ // Language and environment
443
+ "target": "ES2020",
444
+
445
+ // Modules
446
+ "module": "ES2020",
447
+ "moduleResolution": "Node",
448
+
449
+ // Type checking
450
+ "alwaysStrict": true,
451
+ "noFallthroughCasesInSwitch": true,
452
+ "noImplicitAny": true,
453
+ "noImplicitReturns": true,
454
+ "noImplicitThis": true,
455
+ "noUnusedLocals": true,
456
+ "noUnusedParameters": true,
457
+ "strict": true,
458
+ "strictFunctionTypes": true,
459
+ "strictNullChecks": true,
460
+ "strictPropertyInitialization": true
461
+ }
462
+ }
463
+ `,
464
+ prettierConfig
465
+ )
466
+ });
467
+ files.push({
468
+ name: "tsconfig.dist.json",
469
+ contents: await format(
470
+ path.resolve(packagePath, "tsconfig.dist.json"),
471
+ outdent.outdent`
472
+ {
473
+ "extends": "./tsconfig.settings",
474
+ "include": ["./src"],
475
+ "exclude": ["./src/**/*.test.ts"],
476
+ "compilerOptions": {
477
+ "rootDir": ".",
478
+ "outDir": "./dist",
479
+ "resolveJsonModule": true
480
+ }
481
+ }
482
+ `,
483
+ prettierConfig
484
+ )
485
+ });
486
+ files.push({
487
+ name: "tsconfig.json",
488
+ contents: await format(
489
+ path.resolve(packagePath, "tsconfig.json"),
490
+ outdent.outdent`
491
+ {
492
+ "extends": "./tsconfig.settings",
493
+ "include": ["./**/*.cjs", "./**/*.ts", "./**/*.tsx"],
494
+ "exclude": ["./node_modules"],
495
+ "compilerOptions": {
496
+ "rootDir": ".",
497
+ "outDir": "./dist",
498
+ "noEmit": true,
499
+ "allowJs": true,
500
+ "resolveJsonModule": true,
501
+ }
502
+ }
503
+ `,
504
+ prettierConfig
505
+ )
506
+ });
507
+ }
508
+ if (features.typescript) {
509
+ files.push({
510
+ name: "package.config.ts",
511
+ contents: await format(
512
+ path.resolve(packagePath, "package.config.ts"),
513
+ outdent.outdent`
514
+ import {defineConfig} from '@sanity/pkg-utils'
515
+
516
+ // https://github.com/sanity-io/pkg-utils#configuration
517
+ export default defineConfig({
518
+ // the path to the tsconfig file for distributed builds
519
+ tsconfig: 'tsconfig.dist.json',
520
+ })
521
+ `,
522
+ prettierConfig
523
+ )
524
+ });
525
+ files.push({
526
+ name: "src/index.ts",
527
+ contents: await format(
528
+ path.resolve(packagePath, "src/index.ts"),
529
+ outdent.outdent`
530
+ /** @public */
531
+ export function main(): void {
532
+ //
533
+ }
534
+ `,
535
+ prettierConfig
536
+ )
537
+ });
538
+ } else {
539
+ files.push({
540
+ name: "package.config.js",
541
+ contents: await format(
542
+ path.resolve(packagePath, "package.config.js"),
543
+ outdent.outdent`
544
+ import {defineConfig} from '@sanity/pkg-utils'
545
+
546
+ export default defineConfig({
547
+ extract: {
548
+ rules: {
549
+ // do not require internal members to be prefixed with \`_\`
550
+ 'ae-internal-missing-underscore': 'off',
551
+ },
552
+ },
553
+ })
554
+ `,
555
+ prettierConfig
556
+ )
557
+ });
558
+ files.push({
559
+ name: "src/index.js",
560
+ contents: await format(
561
+ path.resolve(packagePath, "src/index.js"),
562
+ outdent.outdent`
563
+ /** @public */
564
+ export function main() {
565
+ //
566
+ }
567
+ `,
568
+ prettierConfig
569
+ )
570
+ });
571
+ }
572
+ try {
573
+ pkgJson.dependencies = await resolveLatestDeps(pkgJson.dependencies ?? {});
574
+ } catch (error) {
575
+ logger$1.warn(error instanceof Error ? error.message : error);
576
+ }
577
+ try {
578
+ pkgJson.devDependencies = await resolveLatestDeps(pkgJson.devDependencies ?? {});
579
+ } catch (error) {
580
+ logger$1.warn(error instanceof Error ? error.message : error);
581
+ }
582
+ files.push({
583
+ name: "package.json",
584
+ contents: await format(
585
+ path.resolve(packagePath, "package.json"),
586
+ JSON.stringify(pkgJson, null, 2),
587
+ prettierConfig
588
+ )
589
+ });
590
+ return files;
591
+ }
592
+ };
593
+ };
594
+ function format(filepath, input, prettierOptions) {
595
+ return prettier__default.default.format(input, { ...prettierOptions, plugins: [], filepath });
596
+ }
597
+ async function resolveLatestDeps(deps) {
598
+ const depsEntries = Object.entries(deps);
599
+ const latestDeps = {};
600
+ for (const entry of depsEntries) {
601
+ const [name, version] = entry;
602
+ if (version) {
603
+ const latestVersion = await getLatestVersion__default.default(name, version);
604
+ latestDeps[name] = latestVersion ? `^${latestVersion}` : version;
605
+ }
606
+ }
607
+ return latestDeps;
608
+ }
609
+
610
+ async function init(options) {
611
+ if (!options.cwd) {
612
+ throw new Error("Missing required option: cwd");
613
+ }
614
+ if (!options.path) {
615
+ throw new Error("Missing required option: path");
616
+ }
617
+ const logger$1 = logger.createLogger();
618
+ const packagePath = path.resolve(options.cwd, options.path);
619
+ await ensurePackagePath(packagePath);
620
+ await createFromTemplate({
621
+ cwd: options.cwd,
622
+ logger: logger$1,
623
+ template: defaultTemplate,
624
+ packagePath
625
+ });
626
+ }
627
+ async function ensurePackagePath(packagePath) {
628
+ const exists = logger.fileExists(packagePath);
629
+ if (!exists) {
630
+ await mkdirp.mkdirp(packagePath);
631
+ return;
632
+ }
633
+ const dir = (await fs.lstat(packagePath)).isDirectory();
634
+ if (!dir) {
635
+ throw new Error("the package path is a file, not a directory");
636
+ }
637
+ const empty = await isEmptyDirectory(packagePath);
638
+ if (!empty) {
639
+ throw new Error("the package directory is not empty");
640
+ }
641
+ }
642
+
643
+ exports.createFromTemplate = createFromTemplate;
644
+ exports.defineTemplateOption = defineTemplateOption;
645
+ exports.init = init;
646
+ //# sourceMappingURL=init-tT7f7xkO.cjs.map